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
//! VM runtime context definitions

use crate::predicate::RuntimePredicate;

use fuel_asm::Word;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Runtime context description.
pub enum Context {
    /// Current context is a predicate verification.
    Predicate {
        /// Predicate program to be executed
        program: RuntimePredicate,
    },
    /// Current context is a script execution.
    Script {
        /// Block height of the context
        block_height: u32,
    },
    /// Current context is under a `CALL` scop.e
    Call {
        /// Block height of the context
        block_height: u32,
    },
    /// No transaction initialized/invalid context.
    NotInitialized,
}

impl Default for Context {
    fn default() -> Self {
        Self::NotInitialized
    }
}

impl Context {
    /// Check if the context is predicate
    pub const fn is_predicate(&self) -> bool {
        matches!(self, Self::Predicate { .. })
    }

    /// Return `true` if the context is external; `false` otherwise.
    pub const fn is_external(&self) -> bool {
        matches!(self, Self::Predicate { .. } | Self::Script { .. })
    }

    /// Return the program to be executed, if its a predicate
    pub const fn predicate(&self) -> Option<&RuntimePredicate> {
        match self {
            Context::Predicate { program } => Some(program),
            _ => None,
        }
    }

    /// Return the block height from the context, if either script or call
    pub const fn block_height(&self) -> Option<u32> {
        match self {
            Context::Script { block_height } | Context::Call { block_height } => Some(*block_height),
            _ => None,
        }
    }

    /// Update the context according to the provided frame pointer
    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,
                }
            }
            _ => (),
        }
    }
}