fuel_vm/
context.rs

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! VM runtime context definitions

use crate::predicate::RuntimePredicate;

use fuel_asm::Word;
use fuel_types::BlockHeight;

#[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 estimation.
    PredicateEstimation {
        /// Predicate program to be executed
        program: RuntimePredicate,
    },
    /// Current context is a predicate verification.
    PredicateVerification {
        /// Predicate program to be executed
        program: RuntimePredicate,
    },
    /// Current context is a script execution.
    Script {
        /// Block height of the context
        block_height: BlockHeight,
    },
    /// Current context is under a `CALL` scope
    Call {
        /// Block height of the context
        block_height: BlockHeight,
    },
    /// 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::PredicateEstimation { .. } | Self::PredicateVerification { .. }
        )
    }

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

    /// Return `true` if the context is internal; `false` otherwise.
    pub const fn is_internal(&self) -> bool {
        !self.is_external()
    }

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

    /// Return the block height from the context, if either script or call
    pub const fn block_height(&self) -> Option<BlockHeight> {
        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,
                }
            }
            _ => (),
        }
    }
}