1use crate::{
7 ids::{ActorId, MessageId},
8 reservation::GasReservationMap,
9};
10use gprimitives::CodeId;
11use scale_decode::DecodeAsType;
12use scale_encode::EncodeAsType;
13use scale_info::{
14 TypeInfo,
15 scale::{Decode, Encode},
16};
17
18#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)]
20pub enum Program<BlockNumber: Copy> {
21 Active(ActiveProgram<BlockNumber>),
23 Exited(ActorId),
25 Terminated(ActorId),
27}
28
29impl<BlockNumber: Copy> Program<BlockNumber> {
30 pub fn is_active(&self) -> bool {
32 matches!(self, Program::Active(_))
33 }
34
35 pub fn is_exited(&self) -> bool {
37 matches!(self, Program::Exited(_))
38 }
39
40 pub fn is_terminated(&self) -> bool {
42 matches!(self, Program::Terminated(_))
43 }
44
45 pub fn is_initialized(&self) -> bool {
47 matches!(
48 self,
49 Program::Active(ActiveProgram {
50 state: ProgramState::Initialized,
51 ..
52 })
53 )
54 }
55}
56
57#[derive(Clone, Debug, derive_more::Display)]
59#[display("Program is not an active one")]
60pub struct InactiveProgramError;
61
62impl<BlockNumber: Copy> core::convert::TryFrom<Program<BlockNumber>>
63 for ActiveProgram<BlockNumber>
64{
65 type Error = InactiveProgramError;
66
67 fn try_from(prog_with_status: Program<BlockNumber>) -> Result<Self, Self::Error> {
68 match prog_with_status {
69 Program::Active(p) => Ok(p),
70 _ => Err(InactiveProgramError),
71 }
72 }
73}
74
75#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)]
77pub struct ActiveProgram<BlockNumber: Copy> {
78 pub allocations_tree_len: u32,
80 pub memory_infix: MemoryInfix,
82 pub gas_reservation_map: GasReservationMap,
84 pub code_id: CodeId,
86 pub state: ProgramState,
88 pub expiration_block: BlockNumber,
90}
91
92#[derive(Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, PartialEq, Eq, TypeInfo)]
94pub enum ProgramState {
95 Uninitialized {
98 message_id: MessageId,
100 },
101 Initialized,
103}
104
105#[derive(
107 Clone,
108 Copy,
109 Debug,
110 Default,
111 Decode,
112 DecodeAsType,
113 Encode,
114 EncodeAsType,
115 PartialEq,
116 Eq,
117 Hash,
118 TypeInfo,
119)]
120#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
121pub struct MemoryInfix(u32);
122
123impl MemoryInfix {
124 pub const fn new(value: u32) -> Self {
126 Self(value)
127 }
128
129 pub fn inner(&self) -> u32 {
131 self.0
132 }
133}