Skip to main content

gear_common/
event.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Gear events additional data.
5//!
6//! This module contains components for depositing proper
7//! and extensive data about actions happen.
8
9use gear_core::{env::MessageWaitedType, ids::MessageId};
10use sp_runtime::{
11    codec::{self, Decode, Encode},
12    scale_info::{self, TypeInfo},
13};
14
15/// Programs entry for messages.
16///
17/// Same as `gear_core::message::DispatchKind`,
18/// but with additional info about reply.
19#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
20#[codec(crate = codec)]
21#[scale_info(crate = scale_info)]
22pub enum MessageEntry {
23    /// Init entry point.
24    Init,
25    /// Handle entry point.
26    Handle,
27    /// Handle reply entry point.
28    Reply(MessageId),
29    /// System signal entry point.
30    Signal,
31}
32
33/// Status of dispatch dequeue and execution.
34#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
35#[codec(crate = codec)]
36#[scale_info(crate = scale_info)]
37pub enum DispatchStatus {
38    /// Dispatch was dequeued and succeed with execution.
39    Success,
40    /// Dispatch was dequeued and failed its execution.
41    Failed,
42    /// Dispatch was dequeued and wasn't executed.
43    /// Occurs if actor no longer exists.
44    NotExecuted,
45}
46
47/// Behavior of types, which represent runtime reasons for some chain actions.
48pub trait RuntimeReason: Sized {
49    /// Converter into composite reason type: not only runtime, but system also.
50    fn into_reason<S: SystemReason>(self) -> Reason<Self, S> {
51        Reason::Runtime(self)
52    }
53}
54
55// Empty implementation for `()` to skip requirements.
56impl RuntimeReason for () {}
57
58/// Behavior of types, which represent system reasons for some chain actions.
59pub trait SystemReason: Sized {
60    /// Converter into composite reason type: not only system, but runtime also.
61    fn into_reason<R: RuntimeReason>(self) -> Reason<R, Self> {
62        Reason::System(self)
63    }
64}
65
66// Empty implementation for `()` to skip requirements.
67impl SystemReason for () {}
68
69/// Composite reason type for any action happened on chain.
70#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
71#[codec(crate = codec)]
72#[scale_info(crate = scale_info)]
73pub enum Reason<R: RuntimeReason, S: SystemReason> {
74    /// Runtime reason variant.
75    ///
76    /// This means that actor explicitly forced some action,
77    /// which this reason explains.
78    Runtime(R),
79    /// System reason variant.
80    ///
81    /// This means that system automatically forced some action,
82    /// which this reason explains.
83    System(S),
84}
85
86/// Runtime reason for messages waiting.
87#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
88#[codec(crate = codec)]
89#[scale_info(crate = scale_info)]
90pub enum MessageWaitedRuntimeReason {
91    /// Program called `gr_wait` while executing message.
92    WaitCalled,
93    /// Program called `gr_wait_for` while executing message.
94    WaitForCalled,
95    /// Program called `gr_wait_up_to` with insufficient gas for full
96    /// duration while executing message.
97    WaitUpToCalled,
98    /// Program called `gr_wait_up_to` with enough gas for full duration
99    /// storing while executing message.
100    WaitUpToCalledFull,
101}
102
103impl From<MessageWaitedType> for MessageWaitedRuntimeReason {
104    fn from(src: MessageWaitedType) -> Self {
105        match src {
106            MessageWaitedType::Wait => MessageWaitedRuntimeReason::WaitCalled,
107            MessageWaitedType::WaitFor => MessageWaitedRuntimeReason::WaitForCalled,
108            MessageWaitedType::WaitUpTo => MessageWaitedRuntimeReason::WaitUpToCalled,
109            MessageWaitedType::WaitUpToFull => MessageWaitedRuntimeReason::WaitUpToCalledFull,
110        }
111    }
112}
113
114/// System reason for messages waiting.
115#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
116#[codec(crate = codec)]
117#[scale_info(crate = scale_info)]
118pub enum MessageWaitedSystemReason {}
119
120/// Composite reason for messages waiting.
121pub type MessageWaitedReason = Reason<MessageWaitedRuntimeReason, MessageWaitedSystemReason>;
122
123/// Runtime reason for messages waking.
124#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
125#[codec(crate = codec)]
126#[scale_info(crate = scale_info)]
127pub enum MessageWokenRuntimeReason {
128    /// Program called `gr_wake` with corresponding message id.
129    WakeCalled,
130}
131
132/// System reason for messages waking.
133#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
134#[codec(crate = codec)]
135#[scale_info(crate = scale_info)]
136pub enum MessageWokenSystemReason {
137    /// Program had finished initialization.
138    ///
139    /// Note that this variant doesn't contain info
140    /// about initialization success or failure.
141    ProgramGotInitialized,
142    /// Specified by program timeout for waking has come (see #349).
143    TimeoutHasCome,
144    /// Message can no longer pay rent for holding in storage (see #646).
145    OutOfRent,
146}
147
148/// Composite reason for messages waking.
149pub type MessageWokenReason = Reason<MessageWokenRuntimeReason, MessageWokenSystemReason>;
150
151/// Type of changes applied to code in storage.
152#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
153#[codec(crate = codec)]
154#[scale_info(crate = scale_info)]
155pub enum CodeChangeKind<BlockNumber> {
156    /// Code become active and ready for use.
157    ///
158    /// Appear when new code created or expiration block number updated.
159    ///
160    /// Expiration block number presents block number when this code become
161    /// inactive due to losing ability to pay rent for holding.
162    /// Equals `None` if stores free (some program relays on it, see #646).
163    Active { expiration: Option<BlockNumber> },
164
165    /// Code become inactive and can no longer be used.
166    Inactive,
167
168    /// Code was reinstrumented.
169    Reinstrumented,
170}
171
172/// Runtime reason for messages reading from `Mailbox`.
173#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
174#[codec(crate = codec)]
175#[scale_info(crate = scale_info)]
176pub enum UserMessageReadRuntimeReason {
177    /// Message was replied by user.
178    MessageReplied,
179    /// Message was claimed by user.
180    MessageClaimed,
181}
182
183/// System reason for messages reading from `Mailbox`.
184#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
185#[codec(crate = codec)]
186#[scale_info(crate = scale_info)]
187pub enum UserMessageReadSystemReason {
188    /// Message can no longer pay rent for holding in storage (see #646).
189    OutOfRent,
190}
191
192/// Composite reason for messages reading from `Mailbox`.
193pub type UserMessageReadReason = Reason<UserMessageReadRuntimeReason, UserMessageReadSystemReason>;
194
195/// Type of changes applied to program in storage.
196#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
197#[codec(crate = codec)]
198#[scale_info(crate = scale_info)]
199pub enum ProgramChangeKind<BlockNumber> {
200    /// Active status achieved.
201    ///
202    /// Occurs when new program created or paused program was resumed.
203    ///
204    /// Expiration block number presents block number when this program become
205    /// paused due to losing ability to pay rent for holding.
206    Active { expiration: BlockNumber },
207
208    /// Program become inactive forever due to `gr_exit` call.
209    Inactive,
210
211    /// Paused status.
212    ///
213    /// Program is no longer available for interaction, but can be
214    /// resumed by paying rent and giving whole data related to it.
215    Paused,
216
217    /// Program become inactive forever due to init failure.
218    Terminated,
219
220    /// Occurs when expiration block number of a program changed.
221    ///
222    /// Expiration block number presents block number when this program become
223    /// paused due to losing ability to pay rent for holding.
224    ExpirationChanged { expiration: BlockNumber },
225
226    /// Occurs when new program set in the storage.
227    ///
228    /// Expiration block number presents block number when this program become
229    /// paused due to losing ability to pay rent for holding or terminated in
230    /// case of didn't get initialised.
231    ProgramSet { expiration: BlockNumber },
232}