drt_chain_vm/vm_hooks/vh_impl/
vh_static_api.rs

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