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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// This file is part of Gear.

// Copyright (C) 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 super::*;

impl ExtManager {
    /// Insert message into the delayed queue.
    pub(crate) fn send_delayed_dispatch(
        &mut self,
        origin_msg: MessageId,
        dispatch: Dispatch,
        delay: u32,
        to_user: bool,
        reservation: Option<ReservationId>,
    ) {
        if delay.is_zero() {
            let err_msg = "send_delayed_dispatch: delayed sending with zero delay appeared";

            unreachable!("{err_msg}");
        }

        let message_id = dispatch.id();

        if self.dispatches_stash.contains_key(&message_id) {
            let err_msg = format!(
                "send_delayed_dispatch: stash already has the message id - {id}",
                id = dispatch.id()
            );

            unreachable!("{err_msg}");
        }

        // Validating dispatch wasn't sent from system with delay.
        if dispatch.is_error_reply() || matches!(dispatch.kind(), DispatchKind::Signal) {
            let err_msg = format!(
                "send_delayed_dispatch: message of an invalid kind is sent: {kind:?}",
                kind = dispatch.kind()
            );

            unreachable!("{err_msg}");
        }

        let mut to_mailbox = false;

        let sender_node = reservation
            .map(Origin::into_origin)
            .unwrap_or_else(|| origin_msg.into_origin());

        let from = dispatch.source();
        let value = dispatch.value();

        let hold_builder = HoldBoundBuilder::new(StorageType::DispatchStash);

        let delay_hold = hold_builder.duration(self, delay);
        let gas_for_delay = delay_hold.lock_amount(self);

        let interval_finish = if to_user {
            let threshold = RentWeights::default().mailbox_threshold.ref_time;

            let gas_limit = dispatch
                .gas_limit()
                .or_else(|| {
                    let gas_limit = self.gas_tree.get_limit(sender_node).unwrap_or_else(|e| {
                        let err_msg = format!(
                            "send_delayed_dispatch: failed getting message gas limit. \
                                Lock sponsor id - {sender_node:?}. Got error - {e:?}"
                        );

                        unreachable!("{err_msg}");
                    });

                    (gas_limit.saturating_sub(gas_for_delay) >= threshold).then_some(threshold)
                })
                .unwrap_or_default();

            to_mailbox = !dispatch.is_reply() && gas_limit >= threshold;

            let gas_amount = if to_mailbox {
                gas_for_delay.saturating_add(gas_limit)
            } else {
                gas_for_delay
            };

            self.gas_tree
                .cut(sender_node, message_id, gas_amount)
                .unwrap_or_else(|e| {
                    let sender_node = sender_node.cast::<PlainNodeId>();
                    let err_msg = format!(
                        "send_delayed_dispatch: failed creating cut node. \
                        Origin node - {sender_node:?}, cut node id - {id}, amount - {gas_amount}. \
                        Got error - {e:?}",
                        id = dispatch.id()
                    );

                    unreachable!("{err_msg}");
                });

            if !to_mailbox {
                self.gas_tree
                    .split_with_value(
                        true,
                        origin_msg,
                        MessageId::generate_reply(dispatch.id()),
                        0,
                    )
                    .expect("failed to split with value gas node");
            }

            if let Some(reservation_id) = reservation {
                self.remove_gas_reservation_with_task(dispatch.source(), reservation_id)
            }

            // Locking funds for holding.
            let lock_id = delay_hold.lock_id().unwrap_or_else(|| {
                // Dispatch stash storage is guaranteed to have an associated lock id
                let err_msg =
                    "send_delayed_dispatch: No associated lock id for the dispatch stash storage";

                unreachable!("{err_msg}");
            });

            self.gas_tree.lock(dispatch.id(), lock_id, delay_hold.lock_amount(self))
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_delayed_dispatch: failed locking gas for the user message stash hold. \
                        Message id - {message_id}, lock amount - {lock}. Got error - {e:?}",
                        message_id = dispatch.id(),
                        lock = delay_hold.lock_amount(self));
                    unreachable!("{err_msg}");
                });

            if delay_hold.expected_duration(self).is_zero() {
                let err_msg = format!(
                    "send_delayed_dispatch: user message got zero duration hold bound for dispatch stash. \
                    Requested duration - {delay}, block cost - {cost}, source - {from:?}",
                    cost = Self::cost_by_storage_type(StorageType::DispatchStash)
                );

                unreachable!("{err_msg}");
            }

            delay_hold.expected()
        } else {
            match (dispatch.gas_limit(), reservation) {
                (Some(gas_limit), None) => self
                    .gas_tree
                    .split_with_value(
                        dispatch.is_reply(),
                        sender_node,
                        dispatch.id(),
                        gas_limit.saturating_add(gas_for_delay),
                    )
                    .expect("GasTree corrupted"),

                (None, None) => self
                    .gas_tree
                    .split(dispatch.is_reply(), sender_node, dispatch.id())
                    .expect("GasTree corrupted"),
                (Some(gas_limit), Some(reservation_id)) => {
                    let err_msg = format!(
                        "send_delayed_dispatch: sending dispatch with gas from reservation isn't implemented. \
                        Message - {message_id}, sender - {sender}, gas limit - {gas_limit}, reservation - {reservation_id}",
                        message_id = dispatch.id(),
                        sender = dispatch.source(),
                    );

                    unreachable!("{err_msg}");
                }

                (None, Some(reservation_id)) => {
                    self.gas_tree
                        .split(dispatch.is_reply(), reservation_id, dispatch.id())
                        .expect("GasTree corrupted");
                    self.remove_gas_reservation_with_task(dispatch.source(), reservation_id);
                }
            }

            let lock_id = delay_hold.lock_id().unwrap_or_else(|| {
                // Dispatch stash storage is guaranteed to have an associated lock id
                let err_msg =
                    "send_delayed_dispatch: No associated lock id for the dispatch stash storage";

                unreachable!("{err_msg}");
            });

            self.gas_tree
                .lock(dispatch.id(), lock_id, delay_hold.lock_amount(self))
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                    "send_delayed_dispatch: failed locking gas for the program message stash hold. \
                    Message id - {message_id}, lock amount - {lock}. Got error - {e:?}",
                    message_id = dispatch.id(),
                    lock = delay_hold.lock_amount(self)
                );

                    unreachable!("{err_msg}");
                });

            if delay_hold.expected_duration(self).is_zero() {
                let err_msg = format!(
                    "send_delayed_dispatch: program message got zero duration hold bound for dispatch stash. \
                    Requested duration - {delay}, block cost - {cost}, source - {from:?}",
                    cost = Self::cost_by_storage_type(StorageType::DispatchStash)
                );

                unreachable!("{err_msg}");
            }

            delay_hold.expected()
        };

        if !dispatch.value().is_zero() {
            self.bank.deposit_value(from, value, false);
        }

        let message_id = dispatch.id();

        let start_bn = self.block_height();
        let delay_interval = Interval {
            start: start_bn,
            finish: interval_finish,
        };

        self.dispatches_stash
            .insert(message_id, (dispatch.into_stored_delayed(), delay_interval));

        let task = if to_user {
            ScheduledTask::SendUserMessage {
                message_id,
                to_mailbox,
            }
        } else {
            ScheduledTask::SendDispatch(message_id)
        };

        let task_bn = self.block_height().saturating_add(delay);

        self.task_pool.add(task_bn, task).unwrap_or_else(|e| {
            let err_msg = format!(
                "send_delayed_dispatch: failed adding task for delayed message sending. \
                    Message to user - {to_user}, message id - {message_id}. Got error - {e:?}"
            );

            unreachable!("{err_msg}");
        });
        self.on_task_pool_change();
    }

    pub(crate) fn send_user_message(
        &mut self,
        origin_msg: MessageId,
        message: Message,
        reservation: Option<ReservationId>,
    ) {
        let threshold = RentWeights::default().mailbox_threshold.ref_time;

        let msg_id = reservation
            .map(Origin::into_origin)
            .unwrap_or_else(|| origin_msg.into_origin());

        let gas_limit = message
            .gas_limit()
            .or_else(|| {
                let gas_limit = self.gas_tree.get_limit(msg_id).unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message: failed getting message gas limit. \
                            Lock sponsor id - {msg_id}. Got error - {e:?}"
                    );

                    unreachable!("{err_msg}");
                });

                // If available gas is greater then threshold,
                // than threshold can be used.
                (gas_limit >= threshold).then_some(threshold)
            })
            .unwrap_or_default();

        let from = message.source();
        let to = message.destination();
        let value = message.value();

        let stored_message = message.into_stored();
        let message: UserMessage = stored_message
            .clone()
            .try_into()
            .expect("failed to convert stored message to user message");

        if Accounts::balance(from) != 0 {
            self.bank.deposit_value(from, value, false);
        }
        let _ = if message.details().is_none() && gas_limit >= threshold {
            let hold = HoldBoundBuilder::new(StorageType::Mailbox).maximum_for(self, gas_limit);

            if hold.expected_duration(self).is_zero() {
                let err_msg = format!(
                    "send_user_message: mailbox message got zero duration hold bound for storing. \
                    Gas limit - {gas_limit}, block cost - {cost}, source - {from:?}",
                    cost = Self::cost_by_storage_type(StorageType::Mailbox)
                );

                unreachable!("{err_msg}");
            }

            self.gas_tree
                .cut(msg_id, message.id(), gas_limit)
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message: failed creating cut node. \
                        Origin node - {msg_id}, cut node id - {id}, amount - {gas_limit}. \
                        Got error - {e:?}",
                        id = message.id()
                    );

                    unreachable!("{err_msg}");
                });

            self.gas_tree
                .lock(message.id(), LockId::Mailbox, gas_limit)
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message: failed locking gas for the user message mailbox. \
                        Message id - {message_id}, lock amount - {gas_limit}. Got error - {e:?}",
                        message_id = message.id(),
                    );

                    unreachable!("{err_msg}");
                });

            let message_id = message.id();
            let message: UserStoredMessage = message
                .clone()
                .try_into()
                .expect("failed to convert user message to user stored message");

            self.mailbox
                .insert(message, hold.expected())
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message: failed inserting message into mailbox. \
                        Message id - {message_id}, source - {from:?}, destination - {to:?}, \
                        expected bn - {bn:?}. Got error - {e:?}",
                        bn = hold.expected(),
                    );

                    unreachable!("{err_msg}");
                });

            self.task_pool
                .add(
                    hold.expected(),
                    ScheduledTask::RemoveFromMailbox(to, message_id),
                )
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message: failed adding task for removing from mailbox. \
                    Bn - {bn:?}, sent to - {to:?}, message id - {message_id}. \
                    Got error - {e:?}",
                        bn = hold.expected()
                    );

                    unreachable!("{err_msg}");
                });

            Some(hold.expected())
        } else {
            self.bank.transfer_value(from, to, value);

            if message.details().is_none() {
                // Creating auto reply message.
                let reply_message = ReplyMessage::auto(message.id());

                self.gas_tree
                    .split_with_value(true, origin_msg, reply_message.id(), 0)
                    .expect("GasTree corrupted");
                // Converting reply message into appropriate type for queueing.
                let reply_dispatch = reply_message.into_stored_dispatch(
                    message.destination(),
                    message.source(),
                    message.id(),
                );

                self.dispatches.push_back(reply_dispatch);
            }

            None
        };
        self.log.push(stored_message);

        if let Some(reservation_id) = reservation {
            self.remove_gas_reservation_with_task(message.source(), reservation_id);
        }
    }

    pub(crate) fn send_user_message_after_delay(&mut self, message: UserMessage, to_mailbox: bool) {
        let from = message.source();
        let to = message.destination();
        let value = message.value();

        let _ = if to_mailbox {
            let gas_limit = self.gas_tree.get_limit(message.id()).unwrap_or_else(|e| {
                let err_msg = format!(
                    "send_user_message_after_delay: failed getting message gas limit. \
                        Message id - {message_id}. Got error - {e:?}",
                    message_id = message.id()
                );

                unreachable!("{err_msg}");
            });

            let hold = HoldBoundBuilder::new(StorageType::Mailbox).maximum_for(self, gas_limit);

            if hold.expected_duration(self).is_zero() {
                let err_msg = format!(
                    "send_user_message_after_delay: mailbox message (after delay) got zero duration hold bound for storing. \
                    Gas limit - {gas_limit}, block cost - {cost}, source - {from:?}",
                    cost = Self::cost_by_storage_type(StorageType::Mailbox)
                );

                unreachable!("{err_msg}");
            }

            self.gas_tree.lock(message.id(), LockId::Mailbox, gas_limit)
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message_after_delay: failed locking gas for the user message mailbox. \
                        Message id - {message_id}, lock amount - {gas_limit}. Got error - {e:?}",
                        message_id = message.id(),
                    );

                    unreachable!("{err_msg}");
                });

            let message_id = message.id();
            let message: UserStoredMessage = message
                .clone()
                .try_into()
                .expect("failed to convert user message to user stored message");
            self.mailbox
                .insert(message, hold.expected())
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "send_user_message_after_delay: failed inserting message into mailbox. \
                        Message id - {message_id}, source - {from:?}, destination - {to:?}, \
                        expected bn - {bn:?}. Got error - {e:?}",
                        bn = hold.expected(),
                    );

                    unreachable!("{err_msg}");
                });

            // Adding removal request in task pool

            self.task_pool
                .add(
                    hold.expected(),
                    ScheduledTask::RemoveFromMailbox(to, message_id),
                )
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                    "send_user_message_after_delay: failed adding task for removing from mailbox. \
                    Bn - {bn:?}, sent to - {to:?}, message id - {message_id}. \
                    Got error - {e:?}",
                    bn = hold.expected()
                );

                    unreachable!("{err_msg}");
                });

            Some(hold.expected())
        } else {
            self.bank.transfer_value(from, to, value);

            // Message is never reply here, because delayed reply sending forbidden.
            if message.details().is_none() {
                // Creating reply message.
                let reply_message = ReplyMessage::auto(message.id());

                // `GasNode` was created on send already.

                // Converting reply message into appropriate type for queueing.
                let reply_dispatch = reply_message.into_stored_dispatch(
                    message.destination(),
                    message.source(),
                    message.id(),
                );

                // Queueing dispatch.
                self.dispatches.push_back(reply_dispatch);
            }

            self.consume_and_retrieve(message.id());
            None
        };

        self.log.push(message.into());
    }
}