gear_core_processor/
handler.rs

1// This file is part of Gear.
2
3// Copyright (C) 2021-2023 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::common::{JournalHandler, JournalNote};
20use alloc::{collections::BTreeMap, vec};
21
22/// Handle some journal records passing them to the journal handler.
23pub fn handle_journal(
24    journal: impl IntoIterator<Item = JournalNote>,
25    handler: &mut impl JournalHandler,
26) {
27    let mut page_updates = BTreeMap::new();
28    let mut exit_list = vec![];
29    let mut allocations_update = BTreeMap::new();
30
31    for note in journal {
32        match note {
33            JournalNote::MessageDispatched {
34                message_id,
35                source,
36                outcome,
37            } => handler.message_dispatched(message_id, source, outcome),
38            JournalNote::GasBurned { message_id, amount } => handler.gas_burned(message_id, amount),
39            JournalNote::ExitDispatch {
40                id_exited,
41                value_destination,
42            } => exit_list.push((id_exited, value_destination)),
43            JournalNote::MessageConsumed(message_id) => handler.message_consumed(message_id),
44            JournalNote::SendDispatch {
45                message_id,
46                dispatch,
47                delay,
48                reservation,
49            } => handler.send_dispatch(message_id, dispatch, delay, reservation),
50            JournalNote::WaitDispatch {
51                dispatch,
52                duration,
53                waited_type,
54            } => handler.wait_dispatch(dispatch, duration, waited_type),
55            JournalNote::WakeMessage {
56                message_id,
57                program_id,
58                awakening_id,
59                delay,
60            } => handler.wake_message(message_id, program_id, awakening_id, delay),
61            JournalNote::UpdatePage {
62                program_id,
63                page_number,
64                data,
65            } => {
66                let entry = page_updates.entry(program_id).or_insert_with(BTreeMap::new);
67                entry.insert(page_number, data);
68            }
69            JournalNote::UpdateAllocations {
70                program_id,
71                allocations,
72            } => {
73                allocations_update.insert(program_id, allocations);
74            }
75            JournalNote::SendValue { from, to, value } => handler.send_value(from, to, value),
76            JournalNote::StoreNewPrograms {
77                code_id,
78                candidates,
79            } => handler.store_new_programs(code_id, candidates),
80            JournalNote::StopProcessing {
81                dispatch,
82                gas_burned,
83            } => handler.stop_processing(dispatch, gas_burned),
84            JournalNote::ReserveGas {
85                message_id,
86                reservation_id,
87                program_id,
88                amount,
89                duration: bn,
90            } => handler.reserve_gas(message_id, reservation_id, program_id, amount, bn),
91            JournalNote::UnreserveGas {
92                reservation_id,
93                program_id,
94                expiration,
95            } => handler.unreserve_gas(reservation_id, program_id, expiration),
96            JournalNote::UpdateGasReservations {
97                program_id,
98                reserver,
99            } => handler.update_gas_reservation(program_id, reserver),
100            JournalNote::SystemReserveGas { message_id, amount } => {
101                handler.system_reserve_gas(message_id, amount)
102            }
103            JournalNote::SystemUnreserveGas { message_id } => {
104                handler.system_unreserve_gas(message_id)
105            }
106            JournalNote::SendSignal {
107                message_id,
108                destination,
109                code,
110            } => handler.send_signal(message_id, destination, code),
111            JournalNote::PayProgramRent {
112                payer,
113                program_id,
114                block_count,
115            } => handler.pay_program_rent(payer, program_id, block_count),
116            JournalNote::ReplyDeposit {
117                message_id,
118                future_reply_id,
119                amount,
120            } => handler.reply_deposit(message_id, future_reply_id, amount),
121        }
122    }
123
124    for (program_id, pages_data) in page_updates {
125        handler.update_pages_data(program_id, pages_data);
126    }
127
128    for (program_id, allocations) in allocations_update {
129        handler.update_allocations(program_id, allocations);
130    }
131
132    for (id_exited, value_destination) in exit_list {
133        handler.exit_dispatch(id_exited, value_destination);
134    }
135}