multiversx_chain_vm/host/vm_hooks/
vh_context.rs

1use std::{fmt::Debug, sync::MutexGuard};
2
3use multiversx_chain_vm_executor::{MemLength, MemPtr, VMHooksEarlyExit};
4
5use crate::{
6    blockchain::state::{AccountData, BlockConfig},
7    host::context::{
8        BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxLog, TxResult,
9    },
10    schedule::GasSchedule,
11    types::{VMAddress, VMCodeMetadata, H256},
12};
13
14/// Abstracts away the borrowing of a managed types structure.
15pub trait VMHooksContext: Debug {
16    /// Loads a slice of memory from the instance.
17    ///
18    /// ## Safety
19    ///
20    /// The offset and the length must point to valid instance memory.
21    unsafe fn memory_load(&self, offset: MemPtr, length: MemLength) -> Vec<u8>;
22
23    /// Writes to instance memory.
24    ///
25    /// ## Safety
26    ///
27    /// The offset and the length must point to valid instance memory.
28    unsafe fn memory_store(&self, mem_ptr: MemPtr, data: &[u8]);
29
30    fn m_types_lock(&self) -> MutexGuard<'_, ManagedTypeContainer>;
31
32    fn gas_schedule(&self) -> &GasSchedule;
33
34    fn use_gas(&mut self, gas: u64) -> Result<(), VMHooksEarlyExit>;
35
36    fn input_ref(&self) -> &TxInput;
37
38    fn current_address(&self) -> &VMAddress {
39        &self.input_ref().to
40    }
41
42    fn tx_hash(&self) -> H256 {
43        self.input_ref().tx_hash.clone()
44    }
45
46    /// Random number generator, based on the blockchain randomness source.
47    fn random_next_bytes(&self, length: usize) -> Vec<u8>;
48
49    fn result_lock(&self) -> MutexGuard<'_, TxResult>;
50
51    fn push_tx_log(&self, tx_log: TxLog) {
52        self.result_lock().result_logs.push(tx_log);
53    }
54
55    fn storage_read(&self, key: &[u8]) -> Vec<u8> {
56        self.storage_read_any_address(self.current_address(), key)
57    }
58
59    fn storage_read_any_address(&self, address: &VMAddress, key: &[u8]) -> Vec<u8>;
60
61    fn storage_write(&mut self, key: &[u8], value: &[u8]) -> Result<(), VMHooksEarlyExit>;
62
63    fn get_block_config(&self) -> &BlockConfig;
64
65    fn back_transfers_lock(&self) -> MutexGuard<'_, BackTransfers>;
66
67    /// For ownership reasons, needs to return a clone.
68    ///
69    /// Can be optimized, but is not a priority right now.
70    fn account_data(&self, address: &VMAddress) -> Option<AccountData>;
71
72    fn account_code(&self, address: &VMAddress) -> Vec<u8>;
73
74    fn perform_async_call(
75        &mut self,
76        to: VMAddress,
77        egld_value: num_bigint::BigUint,
78        func_name: TxFunctionName,
79        args: Vec<Vec<u8>>,
80    ) -> Result<(), VMHooksEarlyExit>;
81
82    fn perform_execute_on_dest_context(
83        &mut self,
84        to: VMAddress,
85        egld_value: num_bigint::BigUint,
86        func_name: TxFunctionName,
87        args: Vec<Vec<u8>>,
88    ) -> Result<TxResult, VMHooksEarlyExit>;
89
90    fn perform_execute_on_dest_context_readonly(
91        &mut self,
92        to: VMAddress,
93        func_name: TxFunctionName,
94        arguments: Vec<Vec<u8>>,
95    ) -> Result<Vec<Vec<u8>>, VMHooksEarlyExit>;
96
97    fn perform_deploy(
98        &mut self,
99        egld_value: num_bigint::BigUint,
100        contract_code: Vec<u8>,
101        code_metadata: VMCodeMetadata,
102        args: Vec<Vec<u8>>,
103    ) -> Result<(VMAddress, Vec<Vec<u8>>), VMHooksEarlyExit>;
104
105    fn perform_transfer_execute(
106        &mut self,
107        to: VMAddress,
108        egld_value: num_bigint::BigUint,
109        func_name: TxFunctionName,
110        arguments: Vec<Vec<u8>>,
111    ) -> Result<(), VMHooksEarlyExit>;
112}