gear_core/message/
mod.rs

1// This file is part of Gear.
2
3// Copyright (C) 2022-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
19//! Message processing module.
20
21mod common;
22mod context;
23mod handle;
24mod incoming;
25mod init;
26mod reply;
27mod signal;
28mod stored;
29mod user;
30
31pub use common::{Dispatch, Message, MessageDetails, ReplyDetails, SignalDetails};
32pub use context::{
33    ContextOutcome, ContextOutcomeDrain, ContextSettings, ContextStore, MessageContext,
34};
35pub use gear_core_errors::{ErrorReplyReason, ReplyCode, SuccessReplyReason};
36pub use handle::{HandleMessage, HandlePacket};
37pub use incoming::{IncomingDispatch, IncomingMessage};
38pub use init::{InitMessage, InitPacket};
39pub use reply::{ReplyMessage, ReplyPacket};
40pub use signal::SignalMessage;
41pub use stored::{StoredDelayedDispatch, StoredDispatch, StoredMessage};
42pub use user::{UserMessage, UserStoredMessage};
43
44use core::fmt::Debug;
45use gear_wasm_instrument::syscalls::SyscallName;
46use parity_scale_codec::{Decode, Encode};
47use scale_info::TypeInfo;
48
49/// Gas limit type for message.
50pub type GasLimit = u64;
51
52/// Value type for message.
53pub type Value = u128;
54
55/// Salt type for init message.
56pub type Salt = crate::buffer::Payload;
57
58/// Entry point for dispatch processing.
59#[derive(
60    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Decode, Encode, TypeInfo,
61)]
62#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
63pub enum DispatchKind {
64    /// Initialization.
65    Init,
66    /// Common handle.
67    #[default]
68    Handle,
69    /// Handle reply.
70    Reply,
71    /// System signal.
72    Signal,
73}
74
75impl DispatchKind {
76    /// Check if kind is init.
77    pub fn is_init(&self) -> bool {
78        matches!(self, Self::Init)
79    }
80
81    /// Check if kind is handle.
82    pub fn is_handle(&self) -> bool {
83        matches!(self, Self::Handle)
84    }
85
86    /// Check if kind is reply.
87    pub fn is_reply(&self) -> bool {
88        matches!(self, Self::Reply)
89    }
90
91    /// Check if kind is signal.
92    pub fn is_signal(&self) -> bool {
93        matches!(self, Self::Signal)
94    }
95
96    /// Returns is syscall forbidden for the dispatch kind.
97    pub fn forbids(&self, syscall_name: SyscallName) -> bool {
98        match self {
99            DispatchKind::Signal => matches!(
100                syscall_name,
101                SyscallName::Source
102                    | SyscallName::Reply
103                    | SyscallName::ReplyPush
104                    | SyscallName::ReplyCommit
105                    | SyscallName::ReplyCommitWGas
106                    | SyscallName::ReplyInput
107                    | SyscallName::ReplyInputWGas
108                    | SyscallName::ReservationReply
109                    | SyscallName::ReservationReplyCommit
110                    | SyscallName::SystemReserveGas
111            ),
112            _ => false,
113        }
114    }
115}
116
117/// Message packet.
118///
119/// Provides common behavior for any message's packet: accessing to payload, gas limit and value.
120pub trait Packet {
121    /// Packet payload bytes.
122    fn payload_bytes(&self) -> &[u8];
123
124    /// Payload len
125    fn payload_len(&self) -> u32;
126
127    /// Packet optional gas limit.
128    fn gas_limit(&self) -> Option<GasLimit>;
129
130    /// Packet value.
131    fn value(&self) -> Value;
132
133    /// A dispatch kind the will be generated from the packet.
134    fn kind() -> DispatchKind;
135}