multiversx_chain_vm/vm_hooks/vh_impl/
vh_static_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::sync::{Mutex, MutexGuard};

use multiversx_chain_core::types::ReturnCode;

use crate::{
    tx_mock::{BackTransfers, TxFunctionName, TxInput, TxLog, TxManagedTypes, TxResult},
    types::{VMAddress, VMCodeMetadata},
    vm_hooks::{
        VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
        VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
        VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer, VMHooksManagedMap,
        VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead, VMHooksStorageWrite,
    },
    world_mock::{AccountData, BlockInfo},
};

/// A simple wrapper around a managed type container Mutex.
///
/// Implements `VMHooksManagedTypes` and thus can be used as a basis of a minimal static API.
#[derive(Debug, Default)]
pub struct StaticApiVMHooksHandler(Mutex<TxManagedTypes>);

impl StaticApiVMHooksHandler {
    pub const CURRENT_ADDRESS_PLACEHOLDER: VMAddress =
        VMAddress::new(*b"STATIC_API_CURRENT_ADDRESS______");
}

impl VMHooksHandlerSource for StaticApiVMHooksHandler {
    fn m_types_lock(&self) -> MutexGuard<TxManagedTypes> {
        self.0.lock().unwrap()
    }

    fn halt_with_error(&self, status: ReturnCode, message: &str) -> ! {
        panic!("VM error occured, status: {status}, message: {message}")
    }

    fn input_ref(&self) -> &TxInput {
        panic!("cannot access tx inputs in the StaticApi")
    }

    fn current_address(&self) -> &VMAddress {
        &Self::CURRENT_ADDRESS_PLACEHOLDER
    }

    fn random_next_bytes(&self, _length: usize) -> Vec<u8> {
        panic!("cannot access the random bytes generator in the StaticApi")
    }

    fn result_lock(&self) -> MutexGuard<TxResult> {
        panic!("cannot access tx results in the StaticApi")
    }

    fn push_tx_log(&self, _tx_log: TxLog) {
        panic!("cannot log events in the StaticApi")
    }

    fn storage_read_any_address(&self, _address: &VMAddress, _key: &[u8]) -> Vec<u8> {
        panic!("cannot access the storage in the StaticApi")
    }

    fn storage_write(&self, _key: &[u8], _value: &[u8]) {
        panic!("cannot access the storage in the StaticApi")
    }

    fn get_previous_block_info(&self) -> &BlockInfo {
        panic!("cannot access the block info in the StaticApi")
    }

    fn get_current_block_info(&self) -> &BlockInfo {
        panic!("cannot access the block info in the StaticApi")
    }

    fn back_transfers_lock(&self) -> MutexGuard<BackTransfers> {
        panic!("cannot access the back transfers in the StaticApi")
    }

    fn account_data(&self, _address: &VMAddress) -> Option<AccountData> {
        panic!("cannot access account data in the StaticApi")
    }

    fn account_code(&self, _address: &VMAddress) -> Vec<u8> {
        panic!("cannot access account data in the StaticApi")
    }

    fn perform_async_call(
        &self,
        _to: VMAddress,
        _egld_value: num_bigint::BigUint,
        _func_name: TxFunctionName,
        _args: Vec<Vec<u8>>,
    ) -> ! {
        panic!("cannot launch contract calls in the StaticApi")
    }

    fn perform_execute_on_dest_context(
        &self,
        _to: VMAddress,
        _egld_value: num_bigint::BigUint,
        _func_name: TxFunctionName,
        _args: Vec<Vec<u8>>,
    ) -> Vec<Vec<u8>> {
        panic!("cannot launch contract calls in the StaticApi")
    }

    fn perform_deploy(
        &self,
        _egld_value: num_bigint::BigUint,
        _contract_code: Vec<u8>,
        _code_metadata: VMCodeMetadata,
        _args: Vec<Vec<u8>>,
    ) -> (VMAddress, Vec<Vec<u8>>) {
        panic!("cannot launch contract calls in the StaticApi")
    }

    fn perform_transfer_execute(
        &self,
        _to: VMAddress,
        _egld_value: num_bigint::BigUint,
        _func_name: TxFunctionName,
        _arguments: Vec<Vec<u8>>,
    ) {
        panic!("cannot launch contract calls in the StaticApi")
    }
}

impl VMHooksBigInt for StaticApiVMHooksHandler {}
impl VMHooksManagedBuffer for StaticApiVMHooksHandler {}
impl VMHooksManagedMap for StaticApiVMHooksHandler {}
impl VMHooksBigFloat for StaticApiVMHooksHandler {}
impl VMHooksManagedTypes for StaticApiVMHooksHandler {}

impl VMHooksCallValue for StaticApiVMHooksHandler {}
impl VMHooksEndpointArgument for StaticApiVMHooksHandler {}
impl VMHooksEndpointFinish for StaticApiVMHooksHandler {}
impl VMHooksError for StaticApiVMHooksHandler {}
impl VMHooksErrorManaged for StaticApiVMHooksHandler {}
impl VMHooksStorageRead for StaticApiVMHooksHandler {}
impl VMHooksStorageWrite for StaticApiVMHooksHandler {}
impl VMHooksCrypto for StaticApiVMHooksHandler {}
impl VMHooksBlockchain for StaticApiVMHooksHandler {}
impl VMHooksLog for StaticApiVMHooksHandler {}
impl VMHooksSend for StaticApiVMHooksHandler {}

impl VMHooksHandler for StaticApiVMHooksHandler {}