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};
40use scale_decode::DecodeAsType;
41use scale_encode::EncodeAsType;
42pub use signal::SignalMessage;
43pub use stored::{StoredDelayedDispatch, StoredDispatch, StoredMessage};
44pub use user::{UserMessage, UserStoredMessage};
45
46use core::fmt::Debug;
47use gear_wasm_instrument::syscalls::SyscallName;
48use parity_scale_codec::{Decode, Encode};
49use scale_info::TypeInfo;
50
51/// Gas limit type for message.
52pub type GasLimit = u64;
53
54/// Value type for message.
55pub type Value = u128;
56
57/// Salt type for init message.
58pub type Salt = crate::buffer::Payload;
59
60/// Entry point for dispatch processing.
61#[derive(
62    Clone,
63    Copy,
64    Debug,
65    Default,
66    PartialEq,
67    Eq,
68    PartialOrd,
69    Ord,
70    Hash,
71    Decode,
72    DecodeAsType,
73    Encode,
74    EncodeAsType,
75    TypeInfo,
76)]
77#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
78pub enum DispatchKind {
79    /// Initialization.
80    Init,
81    /// Common handle.
82    #[default]
83    Handle,
84    /// Handle reply.
85    Reply,
86    /// System signal.
87    Signal,
88}
89
90impl DispatchKind {
91    /// Check if kind is init.
92    pub fn is_init(&self) -> bool {
93        matches!(self, Self::Init)
94    }
95
96    /// Check if kind is handle.
97    pub fn is_handle(&self) -> bool {
98        matches!(self, Self::Handle)
99    }
100
101    /// Check if kind is reply.
102    pub fn is_reply(&self) -> bool {
103        matches!(self, Self::Reply)
104    }
105
106    /// Check if kind is signal.
107    pub fn is_signal(&self) -> bool {
108        matches!(self, Self::Signal)
109    }
110
111    /// Returns is syscall forbidden for the dispatch kind.
112    pub fn forbids(&self, syscall_name: SyscallName) -> bool {
113        match self {
114            DispatchKind::Signal => matches!(
115                syscall_name,
116                SyscallName::Source
117                    | SyscallName::Reply
118                    | SyscallName::ReplyPush
119                    | SyscallName::ReplyCommit
120                    | SyscallName::ReplyCommitWGas
121                    | SyscallName::ReplyInput
122                    | SyscallName::ReplyInputWGas
123                    | SyscallName::ReservationReply
124                    | SyscallName::ReservationReplyCommit
125                    | SyscallName::SystemReserveGas
126            ),
127            _ => false,
128        }
129    }
130}
131
132/// Message packet.
133///
134/// Provides common behavior for any message's packet: accessing to payload, gas limit and value.
135pub trait Packet {
136    /// Packet payload bytes.
137    fn payload_bytes(&self) -> &[u8];
138
139    /// Payload len
140    fn payload_len(&self) -> u32;
141
142    /// Packet optional gas limit.
143    fn gas_limit(&self) -> Option<GasLimit>;
144
145    /// Packet value.
146    fn value(&self) -> Value;
147
148    /// A dispatch kind the will be generated from the packet.
149    fn kind() -> DispatchKind;
150}