multiversx_chain_vm/vm_hooks/vh_impl/
vh_static_api.rs

1use std::sync::{Mutex, MutexGuard};
2
3use multiversx_chain_core::types::ReturnCode;
4
5use crate::{
6    tx_mock::{BackTransfers, TxFunctionName, TxInput, TxLog, TxManagedTypes, TxResult},
7    types::{VMAddress, VMCodeMetadata},
8    vm_hooks::{
9        VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
10        VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
11        VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer, VMHooksManagedMap,
12        VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead, VMHooksStorageWrite,
13    },
14    world_mock::{AccountData, BlockInfo},
15};
16
17/// A simple wrapper around a managed type container Mutex.
18///
19/// Implements `VMHooksManagedTypes` and thus can be used as a basis of a minimal static API.
20#[derive(Debug, Default)]
21pub struct StaticApiVMHooksHandler(Mutex<TxManagedTypes>);
22
23impl StaticApiVMHooksHandler {
24    pub const CURRENT_ADDRESS_PLACEHOLDER: VMAddress =
25        VMAddress::new(*b"STATIC_API_CURRENT_ADDRESS______");
26}
27
28impl VMHooksHandlerSource for StaticApiVMHooksHandler {
29    fn m_types_lock(&self) -> MutexGuard<TxManagedTypes> {
30        self.0.lock().unwrap()
31    }
32
33    fn halt_with_error(&self, status: ReturnCode, message: &str) -> ! {
34        panic!("VM error occured, status: {status}, message: {message}")
35    }
36
37    fn input_ref(&self) -> &TxInput {
38        panic!("cannot access tx inputs in the StaticApi")
39    }
40
41    fn current_address(&self) -> &VMAddress {
42        &Self::CURRENT_ADDRESS_PLACEHOLDER
43    }
44
45    fn random_next_bytes(&self, _length: usize) -> Vec<u8> {
46        panic!("cannot access the random bytes generator in the StaticApi")
47    }
48
49    fn result_lock(&self) -> MutexGuard<TxResult> {
50        panic!("cannot access tx results in the StaticApi")
51    }
52
53    fn push_tx_log(&self, _tx_log: TxLog) {
54        panic!("cannot log events in the StaticApi")
55    }
56
57    fn storage_read_any_address(&self, _address: &VMAddress, _key: &[u8]) -> Vec<u8> {
58        panic!("cannot access the storage in the StaticApi")
59    }
60
61    fn storage_write(&self, _key: &[u8], _value: &[u8]) {
62        panic!("cannot access the storage in the StaticApi")
63    }
64
65    fn get_previous_block_info(&self) -> &BlockInfo {
66        panic!("cannot access the block info in the StaticApi")
67    }
68
69    fn get_current_block_info(&self) -> &BlockInfo {
70        panic!("cannot access the block info in the StaticApi")
71    }
72
73    fn back_transfers_lock(&self) -> MutexGuard<BackTransfers> {
74        panic!("cannot access the back transfers in the StaticApi")
75    }
76
77    fn account_data(&self, _address: &VMAddress) -> Option<AccountData> {
78        panic!("cannot access account data in the StaticApi")
79    }
80
81    fn account_code(&self, _address: &VMAddress) -> Vec<u8> {
82        panic!("cannot access account data in the StaticApi")
83    }
84
85    fn perform_async_call(
86        &self,
87        _to: VMAddress,
88        _egld_value: num_bigint::BigUint,
89        _func_name: TxFunctionName,
90        _args: Vec<Vec<u8>>,
91    ) -> ! {
92        panic!("cannot launch contract calls in the StaticApi")
93    }
94
95    fn perform_execute_on_dest_context(
96        &self,
97        _to: VMAddress,
98        _egld_value: num_bigint::BigUint,
99        _func_name: TxFunctionName,
100        _args: Vec<Vec<u8>>,
101    ) -> Vec<Vec<u8>> {
102        panic!("cannot launch contract calls in the StaticApi")
103    }
104
105    fn perform_execute_on_dest_context_readonly(
106        &self,
107        _to: VMAddress,
108        _func_name: TxFunctionName,
109        _arguments: Vec<Vec<u8>>,
110    ) -> Vec<Vec<u8>> {
111        panic!("cannot launch contract calls in the StaticApi")
112    }
113
114    fn perform_deploy(
115        &self,
116        _egld_value: num_bigint::BigUint,
117        _contract_code: Vec<u8>,
118        _code_metadata: VMCodeMetadata,
119        _args: Vec<Vec<u8>>,
120    ) -> (VMAddress, Vec<Vec<u8>>) {
121        panic!("cannot launch contract calls in the StaticApi")
122    }
123
124    fn perform_transfer_execute(
125        &self,
126        _to: VMAddress,
127        _egld_value: num_bigint::BigUint,
128        _func_name: TxFunctionName,
129        _arguments: Vec<Vec<u8>>,
130    ) {
131        panic!("cannot launch contract calls in the StaticApi")
132    }
133}
134
135impl VMHooksBigInt for StaticApiVMHooksHandler {}
136impl VMHooksManagedBuffer for StaticApiVMHooksHandler {}
137impl VMHooksManagedMap for StaticApiVMHooksHandler {}
138impl VMHooksBigFloat for StaticApiVMHooksHandler {}
139impl VMHooksManagedTypes for StaticApiVMHooksHandler {}
140
141impl VMHooksCallValue for StaticApiVMHooksHandler {}
142impl VMHooksEndpointArgument for StaticApiVMHooksHandler {}
143impl VMHooksEndpointFinish for StaticApiVMHooksHandler {}
144impl VMHooksError for StaticApiVMHooksHandler {}
145impl VMHooksErrorManaged for StaticApiVMHooksHandler {}
146impl VMHooksStorageRead for StaticApiVMHooksHandler {}
147impl VMHooksStorageWrite for StaticApiVMHooksHandler {}
148impl VMHooksCrypto for StaticApiVMHooksHandler {}
149impl VMHooksBlockchain for StaticApiVMHooksHandler {}
150impl VMHooksLog for StaticApiVMHooksHandler {}
151impl VMHooksSend for StaticApiVMHooksHandler {}
152
153impl VMHooksHandler for StaticApiVMHooksHandler {}