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
// 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 super::*;

impl ExtManager {
    pub(crate) fn wait_dispatch_impl(
        &mut self,
        dispatch: StoredDispatch,
        duration: Option<BlockNumber>,
        reason: MessageWaitedReason,
    ) {
        use MessageWaitedRuntimeReason::*;

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

        let maximal_hold = hold_builder.maximum_for_message(self, dispatch.id());

        let hold = if let Some(duration) = duration {
            hold_builder.duration(self, duration).min(maximal_hold)
        } else {
            maximal_hold
        };

        let message_id = dispatch.id();
        let destination = dispatch.destination();

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

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

            let err_msg = format!(
                "wait_dispatch: message got zero duration hold bound for waitlist. \
                Requested duration - {duration:?}, gas limit - {gas_limit}, \
                wait reason - {reason:?}, message id - {}.",
                dispatch.id(),
            );

            unreachable!("{err_msg}");
        }

        // Locking funds for holding.
        let lock_id = hold.lock_id().unwrap_or_else(|| {
            // Waitlist storage is guaranteed to have an associated lock id
            let err_msg = "wait_dispatch: No associated lock id for the waitlist storage";

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

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

        match reason {
            MessageWaitedReason::Runtime(WaitForCalled | WaitUpToCalledFull) => {
                let expected = hold.expected();
                let task = ScheduledTask::WakeMessage(destination, message_id);

                if !self.task_pool.contains(&expected, &task) {
                    self.task_pool.add(expected, task).unwrap_or_else(|e| {
                        let err_msg = format!(
                            "wait_dispatch: failed adding task for waking message. \
                            Expected bn - {expected:?}, program id - {destination}, message id - {message_id}. Got error - {e:?}",
                        );

                        unreachable!("{err_msg}");
                    });
                    self.on_task_pool_change();
                }
            }
            MessageWaitedReason::Runtime(WaitCalled | WaitUpToCalled) => {
                self.task_pool.add(
                    hold.expected(),
                    ScheduledTask::RemoveFromWaitlist(dispatch.destination(), dispatch.id()),
                )
                .unwrap_or_else(|e| {
                    let err_msg = format!(
                        "wait_dispatch: failed adding task for removing message from waitlist. \
                        Expected bn - {bn:?}, program id - {destination}, message id - {message_id}. Got error - {e:?}",
                        bn = hold.expected(),
                    );

                    unreachable!("{err_msg}");
                });
                self.on_task_pool_change();
            }
            MessageWaitedReason::System(reason) => match reason {},
        }

        self.waitlist.insert(dispatch, hold.expected())
            .unwrap_or_else(|e| {
                let err_msg = format!(
                    "wait_dispatch: failed inserting message to the wailist. \
                    Expected bn - {bn:?}, program id - {destination}, message id - {message_id}. Got error - {e:?}",
                    bn = hold.expected(),
                );

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

    pub(crate) fn wake_dispatch_impl(
        &mut self,
        program_id: ProgramId,
        message_id: MessageId,
    ) -> Result<StoredDispatch, WaitlistErrorImpl> {
        self.waitlist
            .remove(program_id, message_id)
            .map(|waitlisted_message| self.wake_dispatch_requirements(waitlisted_message))
    }

    pub(crate) fn wake_dispatch_requirements(
        &mut self,
        (waitlisted, hold_interval): (StoredDispatch, Interval<BlockNumber>),
    ) -> StoredDispatch {
        let expected = hold_interval.finish;

        self.charge_for_hold(waitlisted.id(), hold_interval, StorageType::Waitlist);

        let _ = self
            .task_pool
            .delete(
                expected,
                ScheduledTask::RemoveFromWaitlist(waitlisted.destination(), waitlisted.id()),
            )
            .map(|_| {
                self.on_task_pool_change();
            });

        waitlisted
    }
}