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
514
515
516
517
518
519
520
521
522
// 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::{
    internal::HoldBoundBuilder,
    manager::{CodeInfo, ExtManager},
    Config, Event, GasAllowanceOf, GasHandlerOf, GasTree, GearBank, Pallet, ProgramStorageOf,
    QueueOf, TaskPoolOf, WaitlistOf,
};
use common::{
    event::*,
    scheduler::{ScheduledTask, StorageType, TaskHandler, TaskPool},
    storage::*,
    CodeStorage, LockableTree, Origin, Program, ProgramState, ProgramStorage, ReservableTree,
};
use core_processor::common::{DispatchOutcome as CoreDispatchOutcome, JournalHandler};
use frame_support::sp_runtime::Saturating;
use frame_system::pallet_prelude::BlockNumberFor;
use gear_core::{
    ids::{CodeId, MessageId, ProgramId, ReservationId},
    memory::PageBuf,
    message::{Dispatch, MessageWaitedType, StoredDispatch},
    pages::{GearPage, WasmPage},
    reservation::GasReserver,
};
use gear_core_errors::SignalCode;
use sp_runtime::traits::{UniqueSaturatedInto, Zero};
use sp_std::{
    collections::{btree_map::BTreeMap, btree_set::BTreeSet},
    prelude::*,
};

impl<T> JournalHandler for ExtManager<T>
where
    T: Config,
    T::AccountId: Origin,
{
    fn message_dispatched(
        &mut self,
        message_id: MessageId,
        source: ProgramId,
        outcome: CoreDispatchOutcome,
    ) {
        use CoreDispatchOutcome::*;

        let status = match outcome {
            Exit { program_id } => {
                log::trace!("Dispatch outcome exit: {:?}", message_id);

                Pallet::<T>::deposit_event(Event::ProgramChanged {
                    id: program_id,
                    change: ProgramChangeKind::Inactive,
                });

                DispatchStatus::Success
            }
            Success => {
                log::trace!("Dispatch outcome success: {:?}", message_id);

                DispatchStatus::Success
            }
            MessageTrap { program_id, trap } => {
                log::trace!("Dispatch outcome trap: {:?}", message_id);
                log::debug!(
                    "🪤 Program {} terminated with a trap: {}",
                    program_id.into_origin(),
                    trap
                );

                DispatchStatus::Failed
            }
            InitSuccess { program_id, .. } => {
                log::trace!(
                    "Dispatch ({:?}) init success for program {:?}",
                    message_id,
                    program_id
                );

                let expiration =
                    ProgramStorageOf::<T>::update_program_if_active(program_id, |p, bn| {
                        match p {
                            Program::Active(active) => active.state = ProgramState::Initialized,
                            _ => unreachable!("Only active programs are able to initialize"),
                        }

                        bn
                    })
                    .unwrap_or_else(|e| {
                        unreachable!(
                            "Program initialized status may only be set to active program {:?}",
                            e
                        );
                    });

                Pallet::<T>::deposit_event(Event::ProgramChanged {
                    id: program_id,
                    change: ProgramChangeKind::Active { expiration },
                });

                DispatchStatus::Success
            }
            InitFailure {
                program_id,
                origin,
                reason,
            } => {
                log::trace!(
                    "Dispatch ({message_id:?}) init failure for program {program_id:?}: {reason}"
                );

                Self::process_failed_init(program_id, origin);

                DispatchStatus::Failed
            }
            NoExecution => {
                log::trace!("Dispatch ({:?}) for program wasn't executed", message_id);

                DispatchStatus::NotExecuted
            }
        };

        if self.check_user_id(&source) {
            self.dispatch_statuses.insert(message_id, status);
        }
    }

    fn gas_burned(&mut self, message_id: MessageId, amount: u64) {
        log::debug!("Burned: {:?} from: {:?}", amount, message_id);

        GasAllowanceOf::<T>::decrease(amount);

        Pallet::<T>::spend_burned(message_id, amount)
    }

    fn exit_dispatch(&mut self, id_exited: ProgramId, value_destination: ProgramId) {
        log::debug!(
            "Exit dispatch: id_exited = {id_exited}, value_destination = {value_destination}"
        );

        Self::clean_waitlist(id_exited);

        ProgramStorageOf::<T>::update_program_if_active(id_exited, |p, bn| {
            let _ = TaskPoolOf::<T>::delete(bn, ScheduledTask::PauseProgram(id_exited));

            match p {
                Program::Active(program) => {
                    Self::remove_gas_reservation_map(
                        id_exited,
                        core::mem::take(&mut program.gas_reservation_map),
                    );

                    Self::clean_inactive_program(
                        id_exited,
                        program.memory_infix,
                        value_destination,
                    );
                }
                _ => unreachable!("Action executed only for active program"),
            }

            *p = Program::Exited(value_destination);
        })
        .unwrap_or_else(|e| {
            unreachable!("`exit` can be called only from active program: {:?}", e);
        });
    }

    fn message_consumed(&mut self, message_id: MessageId) {
        Pallet::<T>::consume_and_retrieve(message_id)
    }

    fn send_dispatch(
        &mut self,
        message_id: MessageId,
        dispatch: Dispatch,
        delay: u32,
        reservation: Option<ReservationId>,
    ) {
        // This method shouldn't reduce gas allowance for enqueueing dispatch,
        // because message already charged for it within the env.

        let to_user = self.check_user_id(&dispatch.destination());

        if !delay.is_zero() {
            log::debug!("Sending delayed for {delay} blocks dispatch");
            Pallet::<T>::send_delayed_dispatch(message_id, dispatch, delay, to_user, reservation)
        } else if !to_user {
            let gas_limit = dispatch.gas_limit();
            let dispatch = dispatch.into_stored();

            log::debug!(
                "Sending message {:?} from {:?} with gas limit {:?}",
                dispatch.message(),
                message_id,
                gas_limit,
            );

            if dispatch.value() != 0 {
                GearBank::<T>::deposit_value(
                    &dispatch.source().cast(),
                    dispatch.value().unique_saturated_into(),
                    false,
                )
                .unwrap_or_else(|e| unreachable!("Gear bank error: {e:?}"));
            }

            match (gas_limit, reservation) {
                (Some(gas_limit), None) => Pallet::<T>::split_with_value(
                    message_id,
                    dispatch.id(),
                    gas_limit,
                    dispatch.is_reply(),
                ),
                (None, None) => Pallet::<T>::split(message_id, dispatch.id(), dispatch.is_reply()),
                (Some(_gas_limit), Some(_reservation_id)) => {
                    // TODO: #1828
                    unreachable!(
                        "Sending dispatch with gas limit from reservation \
                    is currently unimplemented and there is no way to send such dispatch"
                    );
                }
                (None, Some(reservation_id)) => {
                    Pallet::<T>::split(reservation_id, dispatch.id(), dispatch.is_reply());
                    Pallet::<T>::remove_gas_reservation_with_task(
                        dispatch.source(),
                        reservation_id,
                    );
                }
            }

            QueueOf::<T>::queue(dispatch)
                .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e));
        } else {
            log::debug!(
                "Sending user message {:?} from {:?} with gas limit {:?}",
                dispatch.message(),
                message_id,
                dispatch.gas_limit(),
            );
            Pallet::<T>::send_user_message(message_id, dispatch.into_parts().1, reservation);
        }
    }

    fn wait_dispatch(
        &mut self,
        dispatch: StoredDispatch,
        duration: Option<u32>,
        waited_type: MessageWaitedType,
    ) {
        // This method shouldn't reduce gas allowance for waiting dispatch,
        // because message already charged for it within the env.
        Pallet::<T>::wait_dispatch(
            dispatch,
            duration.map(UniqueSaturatedInto::unique_saturated_into),
            MessageWaitedRuntimeReason::from(waited_type).into_reason(),
        )
    }

    fn wake_message(
        &mut self,
        message_id: MessageId,
        program_id: ProgramId,
        awakening_id: MessageId,
        delay: u32,
    ) {
        // This method shouldn't reduce gas allowance for waking dispatch,
        // because message already charged for it within the env.

        if delay.is_zero() {
            if let Some(dispatch) = Pallet::<T>::wake_dispatch(
                program_id,
                awakening_id,
                MessageWokenRuntimeReason::WakeCalled.into_reason(),
            ) {
                QueueOf::<T>::queue(dispatch)
                    .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e));

                return;
            }
        } else if WaitlistOf::<T>::contains(&program_id, &awakening_id) {
            let expected_bn =
                Pallet::<T>::block_number().saturating_add(delay.unique_saturated_into());
            let task = ScheduledTask::WakeMessage(program_id, awakening_id);

            // This validation helps us to avoid returning error on insertion into `TaskPool` in case of duplicate wake.
            if !TaskPoolOf::<T>::contains(&expected_bn, &task) {
                TaskPoolOf::<T>::add(expected_bn, task)
                    .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e));
            }

            return;
        }

        log::debug!(
            "Attempt to wake unknown message {:?} from {:?}",
            awakening_id,
            message_id
        );
    }

    fn update_pages_data(
        &mut self,
        program_id: ProgramId,
        pages_data: BTreeMap<GearPage, PageBuf>,
    ) {
        self.state_changes.insert(program_id);

        ProgramStorageOf::<T>::update_active_program(program_id, |p| {
            for (page, data) in pages_data {
                log::trace!("{:?} has been write accessed, update it in storage", page);

                ProgramStorageOf::<T>::set_program_page_data(
                    program_id,
                    p.memory_infix,
                    page,
                    data,
                );
                p.pages_with_data.insert(page);
            }
        })
        .unwrap_or_else(|e| {
            unreachable!(
                "Page update guaranteed to be called only for existing and active program: {:?}",
                e
            )
        });
    }

    fn update_allocations(&mut self, program_id: ProgramId, allocations: BTreeSet<WasmPage>) {
        ProgramStorageOf::<T>::update_active_program(program_id, |p| {
            let removed_pages = p.allocations.difference(&allocations);
            for page in removed_pages.flat_map(|page| page.to_iter()) {
                if p.pages_with_data.remove(&page) {
                    ProgramStorageOf::<T>::remove_program_page_data(program_id, p.memory_infix, page);
                }
            }

            p.allocations = allocations;
        }).unwrap_or_else(|e| {
            unreachable!("Allocations update guaranteed to be called only for existing and active program: {:?}", e)
        });
    }

    fn send_value(&mut self, from: ProgramId, to: Option<ProgramId>, value: u128) {
        let to = Pallet::<T>::inheritor_for(to.unwrap_or(from)).cast();
        let from = from.cast();
        let value = value.unique_saturated_into();

        GearBank::<T>::transfer_value(&from, &to, value)
            .unwrap_or_else(|e| unreachable!("Gear bank error: {e:?}"));
    }

    fn store_new_programs(&mut self, code_id: CodeId, candidates: Vec<(MessageId, ProgramId)>) {
        if let Some(code) = T::CodeStorage::get_code(code_id) {
            let code_info = CodeInfo::from_code(&code_id, &code);
            for (init_message, candidate_id) in candidates {
                if !Pallet::<T>::program_exists(self.builtins(), candidate_id) {
                    let block_number = Pallet::<T>::block_number();
                    self.set_program(candidate_id, &code_info, init_message, block_number);

                    Pallet::<T>::deposit_event(Event::ProgramChanged {
                        id: candidate_id,
                        change: ProgramChangeKind::ProgramSet {
                            expiration: block_number,
                        },
                    });
                } else {
                    log::debug!("Program with id {:?} already exists", candidate_id);
                }
            }
        } else {
            log::debug!(
                "No referencing code with code hash {:?} for candidate programs",
                code_id
            );
            for (_, candidate) in candidates {
                self.programs.insert(candidate);
            }
        }
    }

    fn stop_processing(&mut self, dispatch: StoredDispatch, gas_burned: u64) {
        log::debug!(
            "Not enough gas for processing msg id {}, allowance equals {}, gas tried to burn at least {}",
            dispatch.id(),
            GasAllowanceOf::<T>::get(),
            gas_burned,
        );

        GasAllowanceOf::<T>::decrease(gas_burned);
        // TODO: #3112. Rework requeueing logic to avoid blocked queue.
        QueueOf::<T>::requeue(dispatch)
            .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e));
    }

    fn reserve_gas(
        &mut self,
        message_id: MessageId,
        reservation_id: ReservationId,
        program_id: ProgramId,
        amount: u64,
        duration: u32,
    ) {
        log::debug!(
            "Reserved: {:?} from {:?} with {:?} for {} blocks",
            amount,
            message_id,
            reservation_id,
            duration
        );

        let hold = HoldBoundBuilder::<T>::new(StorageType::Reservation)
            .duration(BlockNumberFor::<T>::from(duration));

        // Validating holding duration.
        if hold.expected_duration().is_zero() {
            unreachable!("Threshold for reservation invalidated")
        }

        let total_amount = amount.saturating_add(hold.lock_amount());

        GasHandlerOf::<T>::reserve(message_id, reservation_id, total_amount)
            .unwrap_or_else(|e| unreachable!("GasTree corrupted: {:?}", e));

        let lock_id = hold.lock_id().unwrap_or_else(|| {
            unreachable!("Reservation storage is guaranteed to have an associated lock id")
        });
        GasHandlerOf::<T>::lock(reservation_id, lock_id, hold.lock_amount())
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));

        TaskPoolOf::<T>::add(
            hold.expected(),
            ScheduledTask::RemoveGasReservation(program_id, reservation_id),
        )
        .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e));
    }

    fn unreserve_gas(
        &mut self,
        reservation_id: ReservationId,
        program_id: ProgramId,
        expiration: u32,
    ) {
        <Self as TaskHandler<T::AccountId>>::remove_gas_reservation(
            self,
            program_id,
            reservation_id,
        );

        let _ = TaskPoolOf::<T>::delete(
            BlockNumberFor::<T>::from(expiration),
            ScheduledTask::RemoveGasReservation(program_id, reservation_id),
        );
    }

    fn update_gas_reservation(&mut self, program_id: ProgramId, reserver: GasReserver) {
        ProgramStorageOf::<T>::update_active_program(program_id, |p| {
            p.gas_reservation_map = reserver.into_map(
                Pallet::<T>::block_number().unique_saturated_into(),
                |duration| {
                    HoldBoundBuilder::<T>::new(StorageType::Reservation)
                        .duration(BlockNumberFor::<T>::from(duration))
                        .expected()
                        .unique_saturated_into()
                },
            );
        })
        .unwrap_or_else(|e| {
            unreachable!(
                "Gas reservation update guaranteed to be called only on an existing program: {:?}",
                e
            )
        });
    }

    fn system_reserve_gas(&mut self, message_id: MessageId, amount: u64) {
        log::debug!("Reserve {} of gas for system from {}", amount, message_id);

        GasHandlerOf::<T>::system_reserve(message_id, amount)
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));
    }

    fn system_unreserve_gas(&mut self, message_id: MessageId) {
        let amount = GasHandlerOf::<T>::system_unreserve(message_id)
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));

        if amount != 0 {
            log::debug!("Unreserved {} gas for system from {}", amount, message_id);
        } else {
            log::debug!(
                "Gas for system was not unreserved from {} as there is no supply",
                message_id
            );
        }
    }

    fn send_signal(&mut self, message_id: MessageId, destination: ProgramId, code: SignalCode) {
        Self::send_signal(self, message_id, destination, code)
    }

    fn reply_deposit(&mut self, message_id: MessageId, future_reply_id: MessageId, amount: u64) {
        log::debug!("Creating reply deposit {amount} gas for message id {future_reply_id}");

        GasHandlerOf::<T>::create_deposit(message_id, future_reply_id, amount)
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));
    }
}