1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// This file is part of Gear.

// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Gear events additional data.
//!
//! This module contains components for depositing proper
//! and extensive data about actions happen.

use gear_core::{ids::MessageId, message::MessageWaitedType};
use sp_runtime::{
    codec::{self, Decode, Encode},
    scale_info::{self, TypeInfo},
};

/// Programs entry for messages.
///
/// Same as `gear_core::message::DispatchKind`,
/// but with additional info about reply.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum MessageEntry {
    /// Init entry point.
    Init,
    /// Handle entry point.
    Handle,
    /// Handle reply entry point.
    Reply(MessageId),
    /// System signal entry point.
    Signal,
}

/// Status of dispatch dequeue and execution.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum DispatchStatus {
    /// Dispatch was dequeued and succeed with execution.
    Success,
    /// Dispatch was dequeued and failed its execution.
    Failed,
    /// Dispatch was dequeued and wasn't executed.
    /// Occurs if actor no longer exists.
    NotExecuted,
}

/// Behavior of types, which represent runtime reasons for some chain actions.
pub trait RuntimeReason: Sized {
    /// Converter into composite reason type: not only runtime, but system also.
    fn into_reason<S: SystemReason>(self) -> Reason<Self, S> {
        Reason::Runtime(self)
    }
}

// Empty implementation for `()` to skip requirements.
impl RuntimeReason for () {}

/// Behavior of types, which represent system reasons for some chain actions.
pub trait SystemReason: Sized {
    /// Converter into composite reason type: not only system, but runtime also.
    fn into_reason<R: RuntimeReason>(self) -> Reason<R, Self> {
        Reason::System(self)
    }
}

// Empty implementation for `()` to skip requirements.
impl SystemReason for () {}

/// Composite reason type for any action happened on chain.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum Reason<R: RuntimeReason, S: SystemReason> {
    /// Runtime reason variant.
    ///
    /// This means that actor explicitly forced some action,
    /// which this reason explains.
    Runtime(R),
    /// System reason variant.
    ///
    /// This means that system automatically forced some action,
    /// which this reason explains.
    System(S),
}

/// Runtime reason for messages waiting.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum MessageWaitedRuntimeReason {
    /// Program called `gr_wait` while executing message.
    WaitCalled,
    /// Program called `gr_wait_for` while executing message.
    WaitForCalled,
    /// Program called `gr_wait_up_to` with insufficient gas for full
    /// duration while executing message.
    WaitUpToCalled,
    /// Program called `gr_wait_up_to` with enough gas for full duration
    /// storing while executing message.
    WaitUpToCalledFull,
}

impl From<MessageWaitedType> for MessageWaitedRuntimeReason {
    fn from(src: MessageWaitedType) -> Self {
        match src {
            MessageWaitedType::Wait => MessageWaitedRuntimeReason::WaitCalled,
            MessageWaitedType::WaitFor => MessageWaitedRuntimeReason::WaitForCalled,
            MessageWaitedType::WaitUpTo => MessageWaitedRuntimeReason::WaitUpToCalled,
            MessageWaitedType::WaitUpToFull => MessageWaitedRuntimeReason::WaitUpToCalledFull,
        }
    }
}

/// System reason for messages waiting.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum MessageWaitedSystemReason {}

/// Composite reason for messages waiting.
pub type MessageWaitedReason = Reason<MessageWaitedRuntimeReason, MessageWaitedSystemReason>;

/// Runtime reason for messages waking.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum MessageWokenRuntimeReason {
    /// Program called `gr_wake` with corresponding message id.
    WakeCalled,
}

/// System reason for messages waking.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum MessageWokenSystemReason {
    /// Program had finished initialization.
    ///
    /// Note that this variant doesn't contain info
    /// about initialization success or failure.
    ProgramGotInitialized,
    /// Specified by program timeout for waking has come (see #349).
    TimeoutHasCome,
    /// Message can no longer pay rent for holding in storage (see #646).
    OutOfRent,
}

/// Composite reason for messages waking.
pub type MessageWokenReason = Reason<MessageWokenRuntimeReason, MessageWokenSystemReason>;

/// Type of changes applied to code in storage.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum CodeChangeKind<BlockNumber> {
    /// Code become active and ready for use.
    ///
    /// Appear when new code created or expiration block number updated.
    ///
    /// Expiration block number presents block number when this code become
    /// inactive due to losing ability to pay rent for holding.
    /// Equals `None` if stores free (some program relays on it, see #646).
    Active { expiration: Option<BlockNumber> },

    /// Code become inactive and can no longer be used.
    Inactive,

    /// Code was reinstrumented.
    Reinstrumented,
}

/// Runtime reason for messages reading from `Mailbox`.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum UserMessageReadRuntimeReason {
    /// Message was replied by user.
    MessageReplied,
    /// Message was claimed by user.
    MessageClaimed,
}

/// System reason for messages reading from `Mailbox`.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, SystemReason)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum UserMessageReadSystemReason {
    /// Message can no longer pay rent for holding in storage (see #646).
    OutOfRent,
}

/// Composite reason for messages reading from `Mailbox`.
pub type UserMessageReadReason = Reason<UserMessageReadRuntimeReason, UserMessageReadSystemReason>;

/// Type of changes applied to program in storage.
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub enum ProgramChangeKind<BlockNumber> {
    /// Active status achieved.
    ///
    /// Occurs when new program created or paused program was resumed.
    ///
    /// Expiration block number presents block number when this program become
    /// paused due to losing ability to pay rent for holding.
    Active { expiration: BlockNumber },

    /// Program become inactive forever due to `gr_exit` call.
    Inactive,

    /// Paused status.
    ///
    /// Program is no longer available for interaction, but can be
    /// resumed by paying rent and giving whole data related to it.
    Paused,

    /// Program become inactive forever due to init failure.
    Terminated,

    /// Occurs when expiration block number of a program changed.
    ///
    /// Expiration block number presents block number when this program become
    /// paused due to losing ability to pay rent for holding.
    ExpirationChanged { expiration: BlockNumber },

    /// Occurs when new program set in the storage.
    ///
    /// Expiration block number presents block number when this program become
    /// paused due to losing ability to pay rent for holding or terminated in
    /// case of didn't get initialised.
    ProgramSet { expiration: BlockNumber },
}