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
// 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, weights::WeightInfo, Config, DispatchStashOf, Event, Pallet, QueueOf,
};
use alloc::{format, string::ToString};
use common::{
    event::{
        MessageWokenRuntimeReason, MessageWokenSystemReason, RuntimeReason, SystemReason,
        UserMessageReadSystemReason,
    },
    scheduler::*,
    storage::*,
    Gas, Origin,
};
use core::cmp;
use gear_core::{
    ids::{CodeId, MessageId, ProgramId, ReservationId},
    message::{DispatchKind, Payload, ReplyMessage},
    tasks::{ScheduledTask, TaskHandler, VaraScheduledTask},
};
use gear_core_errors::{ErrorReplyReason, SignalCode};

pub fn get_maximum_task_gas<T: Config>(task: &VaraScheduledTask<T::AccountId>) -> Gas {
    use ScheduledTask::*;

    match task {
        PauseProgram(_) => 0,
        RemoveCode(_) => todo!("#646"),
        RemoveFromMailbox(_, _) => {
            <T as Config>::WeightInfo::tasks_remove_from_mailbox().ref_time()
        }
        RemoveFromWaitlist(_, _) => {
            <T as Config>::WeightInfo::tasks_remove_from_waitlist().ref_time()
        }
        RemovePausedProgram(_) => todo!("#646"),
        WakeMessage(_, _) => cmp::max(
            <T as Config>::WeightInfo::tasks_wake_message().ref_time(),
            <T as Config>::WeightInfo::tasks_wake_message_no_wake().ref_time(),
        ),
        SendDispatch(_) => <T as Config>::WeightInfo::tasks_send_dispatch().ref_time(),
        SendUserMessage { .. } => cmp::max(
            <T as Config>::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(),
            <T as Config>::WeightInfo::tasks_send_user_message().ref_time(),
        ),
        RemoveGasReservation(_, _) => {
            <T as Config>::WeightInfo::tasks_remove_gas_reservation().ref_time()
        }
        #[allow(deprecated)]
        RemoveResumeSession(_) => 0,
    }
}

impl<T: Config> TaskHandler<T::AccountId, MessageId, bool> for ExtManager<T>
where
    T::AccountId: Origin,
{
    fn pause_program(&mut self, _program_id: ProgramId) -> Gas {
        log::debug!("Program rent logic is disabled.");

        0
    }

    fn remove_code(&mut self, _code_id: CodeId) -> Gas {
        todo!("#646")
    }

    fn remove_from_mailbox(&mut self, user_id: T::AccountId, message_id: MessageId) -> Gas {
        // Read reason.
        let reason = UserMessageReadSystemReason::OutOfRent.into_reason();

        let message = ReplyMessage::auto(message_id);

        Pallet::<T>::create(user_id.clone(), message.id(), 0, true);

        // Reading message from mailbox.
        let mailboxed = Pallet::<T>::read_message(user_id.clone(), message_id, reason.clone())
            .unwrap_or_else(|e| {
                let err_msg = format!(
                    "TaskHandler::remove_from_mailbox: failed reading message from mailbox. \
                User - {user_id:?}, message - {message_id}, reason - {reason:?}. Got error: {e:?}."
                );

                log::error!("{err_msg}");
                unreachable!("{err_msg}");
            });

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

        // Queueing dispatch.
        QueueOf::<T>::queue(dispatch).unwrap_or_else(|e| {
            let err_msg = format!(
                "TaskHandler::remove_from_mailbox: failed queuing message. \
                Got error: {e:?}"
            );

            log::error!("{err_msg}");
            unreachable!("{err_msg}");
        });

        let gas = <T as Config>::WeightInfo::tasks_remove_from_mailbox().ref_time();
        log::trace!("Task gas: tasks_remove_from_mailbox = {gas}");

        gas
    }

    fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas {
        // Wake reason.
        let reason = MessageWokenSystemReason::OutOfRent.into_reason();

        // Waking dispatch.
        let waitlisted = Pallet::<T>::wake_dispatch(program_id, message_id, reason.clone())
            .unwrap_or_else(|e| {
                let err_msg = format!(
                    "TaskHandler::remove_from_waitlist: failed waking dispatch. \
                Program id - {program_id}, waking message - {message_id}, reason - {reason:?} \
                Got error - {e:?}."
                );

                log::error!("{err_msg}");
                unreachable!("{err_msg}");
            });

        self.send_signal(
            message_id,
            waitlisted.destination(),
            SignalCode::RemovedFromWaitlist,
        );

        if !waitlisted.is_reply() && waitlisted.kind() != DispatchKind::Signal {
            // Trap explanation.
            let err = ErrorReplyReason::RemovedFromWaitlist;

            // Trap reply payload.
            let err_payload = err
                .to_string()
                .into_bytes()
                .try_into()
                .unwrap_or_else(|_| {
                    let error_reply = err.to_string().into_bytes();
                    let err_msg = format!(
                        "TaskHandler::remove_from_waitlist: failed conversion of error reply into `Payload`. \
                        Error reply bytes len - {len}, max payload len - {max_len}",
                        len = error_reply.len(),
                        max_len = Payload::max_len(),
                    );

                    log::error!("{err_msg}");
                    unreachable!("{err_msg}");
                });

            let trap_reply = ReplyMessage::system(message_id, err_payload, err);

            // Generate trap reply.
            if self.check_program_id(&waitlisted.source()) {
                let trap_dispatch =
                    trap_reply.into_stored_dispatch(program_id, waitlisted.source(), message_id);

                // Creating `GasNode` for the reply.
                Pallet::<T>::split(
                    waitlisted.id(),
                    trap_dispatch.id(),
                    trap_dispatch.is_reply(),
                );

                // Queueing dispatch.
                QueueOf::<T>::queue(trap_dispatch).unwrap_or_else(|e| {
                    let err_msg = format!(
                        "TaskHandler::remove_from_waitlist: failed queuing message. \
                        Got error - {e:?}"
                    );

                    log::error!("{err_msg}");
                    unreachable!("{err_msg}");
                });
            } else {
                // Sending trap reply to user, by depositing event.
                //
                // There is no reason to use `Pallet::<T>::send_user_message( .. )`,
                // because there is no need in reply in future, so no reason
                // and funds to pay mailbox rent for it.
                let trap_reply =
                    trap_reply.into_stored(program_id, waitlisted.source(), message_id);
                let trap_reply = trap_reply
                    .try_into()
                    .unwrap_or_else(|_| {
                        // Signal message sent to user
                        let err_msg = format!(
                            "TaskHandler::remove_from_waitlist: failed conversion from stored into user message. \
                            Message id - {message_id}, program id - {program_id}, destination - {dest}",
                            dest = waitlisted.source()
                        );

                        log::error!("{err_msg}");
                        unreachable!("{err_msg}")
                    });

                // Depositing appropriate event.
                Pallet::<T>::deposit_event(Event::UserMessageSent {
                    message: trap_reply,
                    expiration: None,
                });
            }
        }

        // Consuming gas handler for waitlisted message.
        Pallet::<T>::consume_and_retrieve(waitlisted.id());

        if waitlisted.kind() == DispatchKind::Init {
            let origin = waitlisted.source();
            Self::process_failed_init(program_id, origin);
        }

        let gas = <T as Config>::WeightInfo::tasks_remove_from_waitlist().ref_time();
        log::trace!("Task gas: tasks_remove_from_waitlist = {gas}");

        gas
    }

    fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas {
        todo!("#646")
    }

    fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas {
        match Pallet::<T>::wake_dispatch(
            program_id,
            message_id,
            MessageWokenRuntimeReason::WakeCalled.into_reason(),
        )
        .ok()
        {
            Some(dispatch) => {
                QueueOf::<T>::queue(dispatch).unwrap_or_else(|e| {
                    let err_msg = format!(
                        "TaskHandler::wake_message: failed queuing message. \
                        Got error - {e:?}"
                    );

                    log::error!("{err_msg}");
                    unreachable!("{err_msg}");
                });

                let gas = <T as Config>::WeightInfo::tasks_wake_message().ref_time();
                log::trace!("Task gas: tasks_wake_message = {gas}");

                gas
            }
            None => {
                let gas = <T as Config>::WeightInfo::tasks_wake_message_no_wake().ref_time();
                log::trace!("Task gas: tasks_wake_message_no_wake = {gas}");

                gas
            }
        }
    }

    fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas {
        // No validation required. If program doesn't exist, then NotExecuted appears.

        let (dispatch, hold_interval) = DispatchStashOf::<T>::take(stashed_message_id)
            .unwrap_or_else(|| {
                let err_msg = format!(
                    "TaskHandler::send_dispatch: failed taking message from stash. Message id - {stashed_message_id}."
                );

                log::error!("{err_msg}");
                unreachable!("{err_msg}");
            });

        // Charging locked gas for holding in dispatch stash.
        Pallet::<T>::charge_for_hold(dispatch.id(), hold_interval, StorageType::DispatchStash);

        QueueOf::<T>::queue(dispatch.into()).unwrap_or_else(|e| {
            let err_msg = format!(
                "TaskHandler::send_dispatch: failed queuing message. \
                Got error - {e:?}"
            );

            log::error!("{err_msg}");
            unreachable!("{err_msg}");
        });

        let gas = <T as Config>::WeightInfo::tasks_send_dispatch().ref_time();
        log::trace!("Task gas: tasks_send_dispatch = {gas}");

        gas
    }

    fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas {
        // TODO: validate here destination and send error reply, if required.
        // Atm despite the fact that program may exist, message goes into mailbox / event.
        let (message, hold_interval) = DispatchStashOf::<T>::take(stashed_message_id)
            .map(|(dispatch, interval)| (dispatch.into_parts().1, interval))
            .unwrap_or_else(|| {
                let err_msg = format!(
                    "TaskHandler::send_user_message: failed taking message from stash. Message id - {stashed_message_id}."
                );

                log::error!("{err_msg}");
                unreachable!("{err_msg}");
            });

        // Charge gas for message save.
        Pallet::<T>::charge_for_hold(message.id(), hold_interval, StorageType::DispatchStash);

        // Taking data for error log
        let message_id = message.id();
        let program_id = message.source();

        // Cast message type.
        let message = message.try_into().unwrap_or_else(|_| {
            // Signal message sent to user
            let err_msg = format!(
                "TaskHandler::send_user_message: failed conversion from stored into user message. \
                    Message id - {message_id}, program id - {program_id}.",
            );

            log::error!("{err_msg}");
            unreachable!("{err_msg}");
        });
        Pallet::<T>::send_user_message_after_delay(message, to_mailbox);

        if to_mailbox {
            let gas = <T as Config>::WeightInfo::tasks_send_user_message_to_mailbox().ref_time();
            log::trace!("Task gas: tasks_send_user_message_to_mailbox = {gas}");

            gas
        } else {
            let gas = <T as Config>::WeightInfo::tasks_send_user_message().ref_time();
            log::trace!("Task gas: tasks_send_user_message = {gas}");

            gas
        }
    }

    fn remove_gas_reservation(
        &mut self,
        program_id: ProgramId,
        reservation_id: ReservationId,
    ) -> Gas {
        let _slot = Self::remove_gas_reservation_impl(program_id, reservation_id);

        let gas = <T as Config>::WeightInfo::tasks_remove_gas_reservation().ref_time();
        log::trace!("Task gas: tasks_remove_gas_reservation = {gas}");

        gas
    }

    fn remove_resume_session(&mut self, _session_id: u32) -> Gas {
        log::debug!("Program rent logic is disabled");
        0
    }
}