1use crate::predicate::RuntimePredicate;
4
5use fuel_asm::Word;
6use fuel_types::BlockHeight;
7
8#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum Context {
12 PredicateEstimation {
14 program: RuntimePredicate,
16 },
17 PredicateVerification {
19 program: RuntimePredicate,
21 },
22 Script {
24 block_height: BlockHeight,
26 },
27 Call {
29 block_height: BlockHeight,
31 },
32 #[default]
34 NotInitialized,
35}
36
37impl Context {
38 pub const fn is_predicate(&self) -> bool {
40 matches!(
41 self,
42 Self::PredicateEstimation { .. } | Self::PredicateVerification { .. }
43 )
44 }
45
46 pub const fn is_external(&self) -> bool {
48 matches!(
49 self,
50 Self::PredicateEstimation { .. }
51 | Self::PredicateVerification { .. }
52 | Self::Script { .. }
53 )
54 }
55
56 pub const fn is_internal(&self) -> bool {
58 !self.is_external()
59 }
60
61 pub const fn predicate(&self) -> Option<&RuntimePredicate> {
63 match self {
64 Context::PredicateEstimation { program } => Some(program),
65 Context::PredicateVerification { program } => Some(program),
66 _ => None,
67 }
68 }
69
70 pub const fn block_height(&self) -> Option<BlockHeight> {
72 match self {
73 Context::Script { block_height } | Context::Call { block_height } => {
74 Some(*block_height)
75 }
76 _ => None,
77 }
78 }
79
80 pub fn update_from_frame_pointer(&mut self, fp: Word) {
82 match self {
83 Context::Script { block_height } if fp != 0 => {
84 *self = Self::Call {
85 block_height: *block_height,
86 }
87 }
88
89 Context::Call { block_height } if fp == 0 => {
90 *self = Self::Script {
91 block_height: *block_height,
92 }
93 }
94 _ => (),
95 }
96 }
97}