Skip to main content

multiversx_sc_scenario/executor/debug/
contract_debug_stack.rs

1use std::sync::Mutex;
2
3use multiversx_chain_vm::host::context::TxContextRef;
4
5use super::ContractDebugInstance;
6
7thread_local!(
8    static API_STACK: Mutex<ContractDebugStack> = Mutex::new(ContractDebugStack::default())
9);
10
11#[derive(Debug, Default)]
12pub struct ContractDebugStack(Vec<ContractDebugInstance>);
13
14impl ContractDebugStack {
15    pub fn static_peek() -> ContractDebugInstance {
16        API_STACK.with(|cell| {
17            let stack = cell.lock().unwrap();
18            stack.0.last().unwrap().clone()
19        })
20    }
21
22    /// Searches the stack based on `TxContext` pointer (no deep equls performed).
23    ///
24    /// Used for resolving `DebugHandle`s.
25    pub fn find_by_tx_context(tx_context_ref: &TxContextRef) -> ContractDebugInstance {
26        API_STACK.with(|cell| {
27            let stack = cell.lock().unwrap();
28            stack
29                .0
30                .iter()
31                .find(|instance| TxContextRef::ptr_eq(tx_context_ref, &instance.tx_context_ref))
32                .expect("invalid TxContext: not found on ContractDebugStack")
33                .clone()
34        })
35    }
36
37    pub fn static_push(tx_context_arc: ContractDebugInstance) {
38        API_STACK.with(|cell| {
39            let mut stack = cell.lock().unwrap();
40            stack.0.push(tx_context_arc);
41        })
42    }
43
44    pub fn static_pop() -> ContractDebugInstance {
45        API_STACK.with(|cell| {
46            let mut stack = cell.lock().unwrap();
47            stack.0.pop().unwrap()
48        })
49    }
50}