Skip to main content

gear_core/message/
mod.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Message processing module.
5
6mod common;
7mod context;
8mod handle;
9mod incoming;
10mod init;
11mod reply;
12mod signal;
13mod stored;
14mod user;
15
16pub use common::{Dispatch, Message, MessageDetails, ReplyDetails, SignalDetails};
17pub use context::{
18    ContextOutcome, ContextOutcomeDrain, ContextSettings, ContextStore, MessageContext,
19};
20pub use gear_core_errors::{ErrorReplyReason, ReplyCode, SuccessReplyReason};
21pub use handle::{HandleMessage, HandlePacket};
22pub use incoming::{IncomingDispatch, IncomingMessage};
23pub use init::{InitMessage, InitPacket};
24pub use reply::{ReplyMessage, ReplyPacket};
25use scale_decode::DecodeAsType;
26use scale_encode::EncodeAsType;
27pub use signal::SignalMessage;
28pub use stored::{StoredDelayedDispatch, StoredDispatch, StoredMessage};
29pub use user::{UserMessage, UserStoredMessage};
30
31use core::fmt::Debug;
32use gear_wasm_instrument::syscalls::SyscallName;
33use parity_scale_codec::{Decode, Encode};
34use scale_info::TypeInfo;
35
36/// Gas limit type for message.
37pub type GasLimit = u64;
38
39/// Value type for message.
40pub type Value = u128;
41
42/// Salt type for init message.
43pub type Salt = crate::buffer::Payload;
44
45/// Entry point for dispatch processing.
46#[derive(
47    Clone,
48    Copy,
49    Debug,
50    Default,
51    PartialEq,
52    Eq,
53    PartialOrd,
54    Ord,
55    Hash,
56    Decode,
57    DecodeAsType,
58    Encode,
59    EncodeAsType,
60    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}