multiversx_chain_vm/tx_mock/
tx_context_stack.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
use crate::with_shared::Shareable;

use super::TxContext;

use std::sync::{Arc, Mutex};

thread_local!(
    static API_STACK: Mutex<TxContextStack> = Mutex::new(TxContextStack::default())
);

#[derive(Debug, Default)]
pub struct TxContextStack(Vec<Arc<TxContext>>);

impl TxContextStack {
    pub fn static_peek() -> Arc<TxContext> {
        API_STACK.with(|cell| {
            let stack = cell.lock().unwrap();
            stack.0.last().unwrap().clone()
        })
    }

    pub fn static_push(tx_context_arc: Arc<TxContext>) {
        API_STACK.with(|cell| {
            let mut stack = cell.lock().unwrap();
            stack.0.push(tx_context_arc);
        })
    }

    pub fn static_pop() -> Arc<TxContext> {
        API_STACK.with(|cell| {
            let mut stack = cell.lock().unwrap();
            stack.0.pop().unwrap()
        })
    }

    /// Manages the stack.
    ///
    /// Pushes the context to the stack, executes closure, pops after.
    pub fn execute_on_vm_stack<F, R>(tx_context_sh: &mut Shareable<TxContext>, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        tx_context_sh.with_shared(|tx_context_arc| {
            TxContextStack::static_push(tx_context_arc);

            let result = f();

            let _ = TxContextStack::static_pop();

            result
        })
    }
}