core_processor/
handler.rs

1// This file is part of Gear.
2
3// Copyright (C) 2021-2025 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 {
39                message_id,
40                amount,
41                is_panic: _,
42            } => handler.gas_burned(message_id, amount),
43            JournalNote::ExitDispatch {
44                id_exited,
45                value_destination,
46            } => exit_list.push((id_exited, value_destination)),
47            JournalNote::MessageConsumed(message_id) => handler.message_consumed(message_id),
48            JournalNote::SendDispatch {
49                message_id,
50                dispatch,
51                delay,
52                reservation,
53            } => handler.send_dispatch(message_id, dispatch, delay, reservation),
54            JournalNote::WaitDispatch {
55                dispatch,
56                duration,
57                waited_type,
58            } => handler.wait_dispatch(dispatch, duration, waited_type),
59            JournalNote::WakeMessage {
60                message_id,
61                program_id,
62                awakening_id,
63                delay,
64            } => handler.wake_message(message_id, program_id, awakening_id, delay),
65            JournalNote::UpdatePage {
66                program_id,
67                page_number,
68                data,
69            } => {
70                let entry = page_updates.entry(program_id).or_insert_with(BTreeMap::new);
71                entry.insert(page_number, data);
72            }
73            JournalNote::UpdateAllocations {
74                program_id,
75                allocations,
76            } => {
77                allocations_update.insert(program_id, allocations);
78            }
79            JournalNote::SendValue {
80                from,
81                to,
82                value,
83                locked,
84            } => handler.send_value(from, to, value, locked),
85            JournalNote::StoreNewPrograms {
86                program_id,
87                code_id,
88                candidates,
89            } => handler.store_new_programs(program_id, code_id, candidates),
90            JournalNote::StopProcessing {
91                dispatch,
92                gas_burned,
93            } => handler.stop_processing(dispatch, gas_burned),
94            JournalNote::ReserveGas {
95                message_id,
96                reservation_id,
97                program_id,
98                amount,
99                duration: bn,
100            } => handler.reserve_gas(message_id, reservation_id, program_id, amount, bn),
101            JournalNote::UnreserveGas {
102                reservation_id,
103                program_id,
104                expiration,
105            } => handler.unreserve_gas(reservation_id, program_id, expiration),
106            JournalNote::UpdateGasReservations {
107                program_id,
108                reserver,
109            } => handler.update_gas_reservation(program_id, reserver),
110            JournalNote::SystemReserveGas { message_id, amount } => {
111                handler.system_reserve_gas(message_id, amount)
112            }
113            JournalNote::SystemUnreserveGas { message_id } => {
114                handler.system_unreserve_gas(message_id)
115            }
116            JournalNote::SendSignal {
117                message_id,
118                destination,
119                code,
120            } => handler.send_signal(message_id, destination, code),
121            JournalNote::ReplyDeposit {
122                message_id,
123                future_reply_id,
124                amount,
125            } => handler.reply_deposit(message_id, future_reply_id, amount),
126        }
127    }
128
129    for (program_id, pages_data) in page_updates {
130        handler.update_pages_data(program_id, pages_data);
131    }
132
133    for (program_id, allocations) in allocations_update {
134        handler.update_allocations(program_id, allocations);
135    }
136
137    for (id_exited, value_destination) in exit_list {
138        handler.exit_dispatch(id_exited, value_destination);
139    }
140}