multiversx_sc_scenario/api/impl_vh/
single_tx_api.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
use std::sync::Mutex;

use multiversx_chain_vm::{
    executor::VMHooks,
    types::VMAddress,
    vm_hooks::{SingleTxApiData, SingleTxApiVMHooksHandler, VMHooksDispatcher},
    world_mock::AccountData,
};
use multiversx_sc::api::RawHandle;

use crate::debug_executor::StaticVarData;

use super::{VMHooksApi, VMHooksApiBackend};

thread_local! {
    static SINGLE_TX_API_VH_CELL: Mutex<SingleTxApiVMHooksHandler> = Mutex::default();

    static SINGLE_TX_API_STATIC_CELL: StaticVarData = StaticVarData::default();
}

#[derive(Clone)]
pub struct SingleTxApiBackend;

impl VMHooksApiBackend for SingleTxApiBackend {
    type HandleType = RawHandle;

    fn with_vm_hooks<R, F>(f: F) -> R
    where
        F: FnOnce(&dyn VMHooks) -> R,
    {
        SINGLE_TX_API_VH_CELL.with(|cell| {
            let handler = cell.lock().unwrap().clone();
            let dispatcher = VMHooksDispatcher::new(Box::new(handler));
            f(&dispatcher)
        })
    }

    fn with_static_data<R, F>(f: F) -> R
    where
        F: FnOnce(&StaticVarData) -> R,
    {
        SINGLE_TX_API_STATIC_CELL.with(|data| f(data))
    }
}

/// Similar to the `StaticApi`, but offers allows calls to storage, input, and even creating results.
pub type SingleTxApi = VMHooksApi<SingleTxApiBackend>;

impl SingleTxApi {
    pub fn clear_global() {
        SINGLE_TX_API_VH_CELL.with(|cell| {
            let _ = std::mem::take(&mut *cell.lock().unwrap());
        })
    }

    pub fn with_global<F, R>(f: F) -> R
    where
        F: FnOnce(&mut SingleTxApiData) -> R,
    {
        SINGLE_TX_API_VH_CELL.with(|cell| {
            let mut handler = cell.lock().unwrap();
            handler.with_mut_data(f)
        })
    }

    pub fn with_global_default_account<F, R>(f: F) -> R
    where
        F: FnOnce(&mut AccountData) -> R,
    {
        Self::with_global(|data| data.with_account_mut(&VMAddress::zero(), f))
    }
}

impl std::fmt::Debug for SingleTxApi {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SingleTxApi").finish()
    }
}