drt_chain_vm/tx_execution/builtin_function_mocks/
builtin_func_trait.rs

1use crate::{
2    tx_execution::BlockchainVMRef,
3    tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult, TxTokenTransfer},
4    types::VMAddress,
5};
6
7pub trait BuiltinFunction {
8    /// Function name corresponding the builtin function implementation.
9    ///
10    /// Currently not used.
11    fn name(&self) -> &str;
12
13    /// Extracts data relating DCDT transfers handled by the builtin function, if applicable.
14    fn extract_dcdt_transfers(&self, tx_input: &TxInput) -> BuiltinFunctionDcdtTransferInfo {
15        BuiltinFunctionDcdtTransferInfo::empty(tx_input)
16    }
17
18    /// Executes builtin function for the givn `TxInput` and with access to the underlying contracts states via the `TxCache`.
19    ///
20    /// A few builtin functions (the ones transferring DCDT) can also call the VM after they finish,
21    /// so they are given the extra reference to the VM and a lambda closure to execute on the VM
22    fn execute<F>(
23        &self,
24        tx_input: TxInput,
25        tx_cache: TxCache,
26        vm: &BlockchainVMRef,
27        lambda: F,
28    ) -> (TxResult, BlockchainUpdate)
29    where
30        F: FnOnce();
31}
32
33/// Contains a builtin function call DCDT transfers (if any) and the real recipient of the transfer
34/// (can be different from the "to" field.)
35pub struct BuiltinFunctionDcdtTransferInfo {
36    pub real_recipient: VMAddress,
37    pub transfers: Vec<TxTokenTransfer>,
38}
39
40impl BuiltinFunctionDcdtTransferInfo {
41    pub fn empty(tx_input: &TxInput) -> Self {
42        BuiltinFunctionDcdtTransferInfo {
43            real_recipient: tx_input.to.clone(),
44            transfers: Vec::new(),
45        }
46    }
47
48    pub fn is_empty(&self) -> bool {
49        self.transfers.is_empty()
50    }
51}