use crate::predicate::RuntimePredicate;
use fuel_asm::Word;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Context {
Predicate {
program: RuntimePredicate,
},
Script {
block_height: u32,
},
Call {
block_height: u32,
},
NotInitialized,
}
impl Default for Context {
fn default() -> Self {
Self::NotInitialized
}
}
impl Context {
pub const fn is_predicate(&self) -> bool {
matches!(self, Self::Predicate { .. })
}
pub const fn is_external(&self) -> bool {
matches!(self, Self::Predicate { .. } | Self::Script { .. })
}
pub const fn predicate(&self) -> Option<&RuntimePredicate> {
match self {
Context::Predicate { program } => Some(program),
_ => None,
}
}
pub const fn block_height(&self) -> Option<u32> {
match self {
Context::Script { block_height } | Context::Call { block_height } => Some(*block_height),
_ => None,
}
}
pub fn update_from_frame_pointer(&mut self, fp: Word) {
match self {
Context::Script { block_height } if fp != 0 => {
*self = Self::Call {
block_height: *block_height,
}
}
Context::Call { block_height } if fp == 0 => {
*self = Self::Script {
block_height: *block_height,
}
}
_ => (),
}
}
}