1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// This file is part of Gear.

// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{manager::ExtManager, CoreLog, Log, RunResult};
use codec::Encode;
use core_processor::common::JournalHandler;
use gear_core::{
    ids::{MessageId, ProgramId},
    message::{Dispatch, DispatchKind, Message, ReplyDetails, StoredMessage},
};
use gear_core_errors::{ReplyCode, SuccessReplyReason};
use std::{cell::RefCell, convert::TryInto};

pub struct Mailbox<'a> {
    manager: &'a RefCell<ExtManager>,
    user_id: ProgramId,
}

impl<'a> Mailbox<'a> {
    pub(crate) fn new(user_id: ProgramId, manager: &'a RefCell<ExtManager>) -> Mailbox<'a> {
        Mailbox { user_id, manager }
    }

    pub fn contains<T: Into<Log> + Clone>(&self, log: &T) -> bool {
        let log: Log = log.clone().into();
        if let Some(mailbox) = self.manager.borrow().mailbox.get(&self.user_id) {
            return mailbox.iter().any(|message| log.eq(message));
        }
        self.manager
            .borrow_mut()
            .mailbox
            .insert(self.user_id, Vec::default());
        false
    }

    pub fn take_message<T: Into<Log>>(&self, log: T) -> MessageReplier {
        MessageReplier::new(self.remove_message(log), self.manager)
    }

    pub fn reply(&self, log: Log, payload: impl Encode, value: u128) -> RunResult {
        self.reply_bytes(log, payload.encode(), value)
    }

    pub fn reply_bytes(&self, log: Log, raw_payload: impl AsRef<[u8]>, value: u128) -> RunResult {
        self.take_message(log).reply_bytes(raw_payload, value)
    }

    pub fn claim_value<T: Into<Log>>(&self, log: T) {
        let message = self.remove_message(log);
        self.manager.borrow_mut().send_value(
            message.source(),
            Some(message.destination()),
            message.value(),
        );
    }

    #[track_caller]
    fn remove_message<T: Into<Log>>(&self, log: T) -> StoredMessage {
        let log = log.into();
        let mut manager = self.manager.borrow_mut();
        let messages = manager
            .mailbox
            .get_mut(&self.user_id)
            .expect("Infallible. No mailbox associated with this user id");
        let index = messages
            .iter()
            .position(|message| log.eq(message))
            .expect("No message that satisfies log");
        messages.remove(index)
    }
}

pub struct MessageReplier<'a> {
    log: CoreLog,
    manager: &'a RefCell<ExtManager>,
}

impl<'a> MessageReplier<'a> {
    pub(crate) fn new(
        message: StoredMessage,
        manager: &'a RefCell<ExtManager>,
    ) -> MessageReplier<'a> {
        MessageReplier {
            log: message.into(),
            manager,
        }
    }

    pub fn reply(&self, payload: impl Encode, value: u128) -> RunResult {
        self.reply_bytes(payload.encode(), value)
    }

    pub fn reply_bytes(&self, raw_payload: impl AsRef<[u8]>, value: u128) -> RunResult {
        let message = Message::new(
            MessageId::from(self.manager.borrow_mut().fetch_inc_message_nonce()),
            self.log.destination(),
            self.log.source(),
            raw_payload.as_ref().to_vec().try_into().unwrap(),
            None,
            value,
            Some(
                ReplyDetails::new(
                    self.log.id(),
                    ReplyCode::Success(SuccessReplyReason::Manual),
                )
                .into(),
            ),
        );

        self.manager
            .borrow_mut()
            .validate_and_run_dispatch(Dispatch::new(DispatchKind::Reply, message))
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;

    use crate::{program::ProgramIdWrapper, Log, Program, System};
    use codec::Encode;
    use gear_core::{
        ids::MessageId,
        message::{Dispatch, DispatchKind, Message, Payload},
    };

    #[test]
    fn mailbox_walk_through_test() {
        //Arranging data for future messages
        let system = System::new();
        let message_id: MessageId = Default::default();
        let source_user_id = ProgramIdWrapper::from(100).0;
        let destination_user_id = ProgramIdWrapper::from(200).0;
        let message_payload: Payload = vec![1, 2, 3].try_into().unwrap();
        let encoded_message_payload: Payload = message_payload.encode().try_into().unwrap();
        let reply_payload: Payload = vec![3, 2, 1].try_into().unwrap();
        let encoded_reply_payload: Payload = reply_payload.encode().try_into().unwrap();
        let log = Log::builder().payload(message_payload);

        //Building message based on arranged data
        let message = Message::new(
            message_id,
            source_user_id,
            destination_user_id,
            encoded_message_payload.clone(),
            Default::default(),
            0,
            None,
        );

        //Sending created message and extracting its log
        let message_result =
            system.send_dispatch(Dispatch::new(DispatchKind::Handle, message.clone()));
        let message_log = message_result
            .log
            .last()
            .expect("No message log in run result");

        //Getting mailbox of destination user and extracting message
        let destination_user_mailbox = system.get_mailbox(destination_user_id);
        let message_replier = destination_user_mailbox.take_message(log);

        //Replying on sended message and extracting log
        let reply_log = message_replier.reply(reply_payload, 0).log;
        let last_reply_log = reply_log.last().expect("No message log in run result");

        //Sending one more message to be sure that no critical move semantic didn't
        // occur
        let second_message_result =
            system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));
        let second_message_log = message_result
            .log
            .last()
            .expect("No message log in run result");

        //Asserting results
        assert!(!message_result.main_failed);
        assert!(!message_result.others_failed);
        assert!(!second_message_result.main_failed);
        assert!(!second_message_result.others_failed);
        assert_eq!(reply_log.len(), 1);
        assert_eq!(last_reply_log.payload(), encoded_reply_payload.inner());
        assert_eq!(message_log.payload(), encoded_message_payload.inner());
        assert_eq!(
            second_message_log.payload(),
            encoded_message_payload.inner()
        );
    }

    #[test]
    fn mailbox_deletes_message_after_reply() {
        //Arranging data for future messages
        let system = System::new();
        let message_id: MessageId = Default::default();
        let source_user_id = ProgramIdWrapper::from(100).0;
        let destination_user_id = ProgramIdWrapper::from(200).0;
        let message_payload: Payload = vec![1, 2, 3].try_into().unwrap();
        let reply_payload: Payload = vec![3, 2, 1].try_into().unwrap();
        let message_log = Log::builder().payload(message_payload.clone());

        //Building message based on arranged data
        let message = Message::new(
            message_id,
            source_user_id,
            destination_user_id,
            message_payload.encode().try_into().unwrap(),
            Default::default(),
            0,
            None,
        );

        //Sending created message
        system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));

        //Getting mailbox of destination user and replying on it
        let mut destination_user_mailbox = system.get_mailbox(destination_user_id);
        destination_user_mailbox.reply(message_log.clone(), reply_payload, 0);

        //Making sure that original message deletes after reply
        destination_user_mailbox = system.get_mailbox(destination_user_id);
        assert!(!destination_user_mailbox.contains(&message_log))
    }

    #[test]
    fn mailbox_reply_bytes_test() {
        //Arranging data for future messages
        let system = System::new();
        let message_id: MessageId = Default::default();
        let source_user_id = ProgramIdWrapper::from(100).0;
        let destination_user_id = ProgramIdWrapper::from(200).0;
        let message_payload: Payload = vec![1, 2, 3].try_into().unwrap();
        let reply_payload_array: [u8; 3] = [3, 2, 1];
        let reply_payload: Payload = reply_payload_array.to_vec().try_into().unwrap();
        let log = Log::builder().payload(message_payload.clone());

        //Building message based on arranged data
        let message = Message::new(
            message_id,
            source_user_id,
            destination_user_id,
            message_payload.encode().try_into().unwrap(),
            Default::default(),
            0,
            None,
        );

        //Sending created message
        system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));

        //Getting mailbox of destination user and extracting message
        let destination_user_mailbox = system.get_mailbox(destination_user_id);
        let message_replier = destination_user_mailbox.take_message(log);

        //Replying by bytes and extracting result log
        let result = message_replier.reply_bytes(reply_payload_array, 0);
        let result_log = result.log;
        let last_result_log = result_log.last().expect("No message log in run result");

        assert_eq!(last_result_log.payload(), reply_payload.inner());
    }

    #[test]
    fn mailbox_deletes_message_after_taking() {
        //Arranging data for future messages
        let system = System::new();
        let message_id: MessageId = Default::default();
        let source_user_id = ProgramIdWrapper::from(100).0;
        let destination_user_id = ProgramIdWrapper::from(200).0;
        let message_payload: Payload = vec![1, 2, 3].try_into().unwrap();
        let log = Log::builder().payload(message_payload.clone());

        //Building message based on arranged data
        let message = Message::new(
            message_id,
            source_user_id,
            destination_user_id,
            message_payload.encode().try_into().unwrap(),
            Default::default(),
            0,
            None,
        );

        //Sending created message
        system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));

        //Getting mailbox of destination user and extracting message
        let destination_user_mailbox = system.get_mailbox(destination_user_id);
        destination_user_mailbox.take_message(log.clone());

        //Making sure that taken message is deleted
        assert!(!destination_user_mailbox.contains(&log))
    }

    #[test]
    #[should_panic(expected = "No message that satisfies log")]
    fn take_unknown_log_message() {
        // Arranging data for future messages
        let system = System::new();
        let source_user_id = 100;
        let destination_user_id = 200;
        let log = Log::builder().source(source_user_id);

        // Taking mailbox and message that doesn't exists
        let mailbox = system.get_mailbox(destination_user_id);
        mailbox.take_message(log);
    }

    #[test]
    #[should_panic(expected = "Mailbox available only for users")]
    fn take_programs_mailbox() {
        // Setting up variables for test
        let system = System::new();
        let restricted_user_id = 42;

        Program::from_binary_with_id(
            &system,
            restricted_user_id,
            demo_futures_unordered::WASM_BINARY,
        );

        // Getting user id that is already registered as a program
        system.get_mailbox(restricted_user_id);
    }

    #[test]
    fn claim_value_from_mailbox() {
        let system = System::new();
        let message_id: MessageId = Default::default();
        let sender_id = 1;
        let receiver_id = 42;
        let payload = b"hello".to_vec();

        let log = Log::builder()
            .source(sender_id)
            .dest(receiver_id)
            .payload(payload.clone());

        let message = Message::new(
            message_id,
            sender_id.into(),
            receiver_id.into(),
            payload.encode().try_into().unwrap(),
            Default::default(),
            2 * crate::EXISTENTIAL_DEPOSIT,
            None,
        );

        system.mint_to(sender_id, 2 * crate::EXISTENTIAL_DEPOSIT);
        system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));

        let receiver_mailbox = system.get_mailbox(receiver_id);
        receiver_mailbox.claim_value(log);

        assert_eq!(
            system.balance_of(receiver_id),
            2 * crate::EXISTENTIAL_DEPOSIT
        );
    }

    #[test]
    fn delayed_dispatches_works() {
        // Arranging data for future messages.
        let system = System::new();
        let message_id: MessageId = Default::default();
        let source_user_id = ProgramIdWrapper::from(100).0;
        let destination_user_id = ProgramIdWrapper::from(200).0;
        let message_payload: Payload = vec![1, 2, 3].try_into().unwrap();
        let log = Log::builder().payload(message_payload.clone());

        // Building message based on arranged data.
        let message = Message::new(
            message_id,
            source_user_id,
            destination_user_id,
            message_payload.encode().try_into().unwrap(),
            Default::default(),
            0,
            None,
        );

        let bn_before_schedule = 5;
        let scheduled_delay = 10;
        system.0.borrow_mut().send_delayed_dispatch(
            Dispatch::new(DispatchKind::Handle, message),
            scheduled_delay,
        );

        let mailbox = system.get_mailbox(destination_user_id);
        assert!(!mailbox.contains(&log));

        // Run to block number before scheduled delay
        assert_eq!(system.spend_blocks(bn_before_schedule).len(), 0);
        assert!(!mailbox.contains(&log));

        // Run to block number at scheduled delay
        assert_eq!(
            system
                .spend_blocks(scheduled_delay - bn_before_schedule)
                .len(),
            1
        );
        assert!(mailbox.contains(&log));
    }
}