use crate::{
ids::{ActorId, MessageId},
reservation::GasReservationMap,
};
use gprimitives::CodeId;
use scale_decode::DecodeAsType;
use scale_encode::EncodeAsType;
use scale_info::{
TypeInfo,
scale::{Decode, Encode},
};
#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)]
pub enum Program<BlockNumber: Copy> {
Active(ActiveProgram<BlockNumber>),
Exited(ActorId),
Terminated(ActorId),
}
impl<BlockNumber: Copy> Program<BlockNumber> {
pub fn is_active(&self) -> bool {
matches!(self, Program::Active(_))
}
pub fn is_exited(&self) -> bool {
matches!(self, Program::Exited(_))
}
pub fn is_terminated(&self) -> bool {
matches!(self, Program::Terminated(_))
}
pub fn is_initialized(&self) -> bool {
matches!(
self,
Program::Active(ActiveProgram {
state: ProgramState::Initialized,
..
})
)
}
}
#[derive(Clone, Debug, derive_more::Display)]
#[display("Program is not an active one")]
pub struct InactiveProgramError;
impl<BlockNumber: Copy> core::convert::TryFrom<Program<BlockNumber>>
for ActiveProgram<BlockNumber>
{
type Error = InactiveProgramError;
fn try_from(prog_with_status: Program<BlockNumber>) -> Result<Self, Self::Error> {
match prog_with_status {
Program::Active(p) => Ok(p),
_ => Err(InactiveProgramError),
}
}
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)]
pub struct ActiveProgram<BlockNumber: Copy> {
pub allocations_tree_len: u32,
pub memory_infix: MemoryInfix,
pub gas_reservation_map: GasReservationMap,
pub code_id: CodeId,
pub state: ProgramState,
pub expiration_block: BlockNumber,
}
#[derive(Clone, Debug, Decode, DecodeAsType, Encode, EncodeAsType, PartialEq, Eq, TypeInfo)]
pub enum ProgramState {
Uninitialized {
message_id: MessageId,
},
Initialized,
}
#[derive(
Clone,
Copy,
Debug,
Default,
Decode,
DecodeAsType,
Encode,
EncodeAsType,
PartialEq,
Eq,
Hash,
TypeInfo,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct MemoryInfix(u32);
impl MemoryInfix {
pub const fn new(value: u32) -> Self {
Self(value)
}
pub fn inner(&self) -> u32 {
self.0
}
}