multiversx_chain_vm/tx_execution/
exec_general_tx.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use num_traits::Zero;

use crate::{
    tx_execution::execute_system_sc,
    tx_mock::{
        BlockchainUpdate, CallType, TxCache, TxContext, TxContextStack, TxFunctionName, TxInput,
        TxLog, TxResult,
    },
    types::{top_encode_big_uint, VMAddress, VMCodeMetadata},
    with_shared::Shareable,
};

use super::{is_system_sc_address, BlockchainVMRef};

fn should_execute_sc_call(tx_input: &TxInput) -> bool {
    // execute whitebox calls no matter what
    if tx_input.func_name == TxFunctionName::WHITEBOX_CALL {
        return true;
    }

    // don't execute anything for an EOA
    if !tx_input.to.is_smart_contract_address() {
        return false;
    }

    // calls with empty func name are simple transfers
    !tx_input.func_name.is_empty()
}

fn should_add_transfer_value_log(tx_input: &TxInput) -> bool {
    if tx_input.call_type == CallType::AsyncCallback
        && !tx_input.callback_payments.esdt_values.is_empty()
    {
        // edge case in the VM
        return false;
    }

    if tx_input.call_type == CallType::UpgradeFromSource {
        // already handled in upgradeContract builtin function
        return false;
    }

    if tx_input.call_type != CallType::DirectCall {
        return true;
    }

    // skip for transactions coming directly from scenario json, which should all be coming from user wallets
    tx_input.from.is_smart_contract_address() && !tx_input.egld_value.is_zero()
}

pub(crate) fn create_transfer_value_log(tx_input: &TxInput, call_type: CallType) -> TxLog {
    let mut data = vec![call_type.to_log_bytes(), tx_input.func_name.to_bytes()];
    data.append(&mut tx_input.args.clone());

    if tx_input.esdt_values.is_empty()
        && !tx_input.callback_payments.egld_value.is_zero()
        && tx_input.call_type == CallType::AsyncCallback
    {
        return TxLog {
            address: tx_input.from.clone(),
            endpoint: "transferValueOnly".into(),
            topics: vec![b"".to_vec(), tx_input.to.to_vec()],
            data,
        };
    }

    let egld_value = if tx_input.call_type == CallType::AsyncCallback {
        &tx_input.callback_payments.egld_value
    } else {
        &tx_input.egld_value
    };

    TxLog {
        address: tx_input.from.clone(),
        endpoint: "transferValueOnly".into(),
        topics: vec![top_encode_big_uint(egld_value), tx_input.to.to_vec()],
        data,
    }
}

impl BlockchainVMRef {
    /// Executes without builtin functions, directly on the contract or the given lambda closure.
    pub fn default_execution<F>(
        &self,
        tx_input: TxInput,
        tx_cache: TxCache,
        f: F,
    ) -> (TxResult, BlockchainUpdate)
    where
        F: FnOnce(),
    {
        if let Err(err) =
            tx_cache.transfer_egld_balance(&tx_input.from, &tx_input.to, &tx_input.egld_value)
        {
            return (TxResult::from_panic_obj(&err), BlockchainUpdate::empty());
        }

        let transfer_value_log = if should_add_transfer_value_log(&tx_input) {
            Some(create_transfer_value_log(&tx_input, tx_input.call_type))
        } else {
            None
        };

        // TODO: temporary, will convert to explicit builtin function first
        for esdt_transfer in tx_input.esdt_values.iter() {
            let transfer_result = tx_cache.transfer_esdt_balance(
                &tx_input.from,
                &tx_input.to,
                &esdt_transfer.token_identifier,
                esdt_transfer.nonce,
                &esdt_transfer.value,
            );
            if let Err(err) = transfer_result {
                return (TxResult::from_panic_obj(&err), BlockchainUpdate::empty());
            }
        }

        let (mut tx_result, blockchain_updates) = if is_system_sc_address(&tx_input.to) {
            execute_system_sc(tx_input, tx_cache)
        } else if should_execute_sc_call(&tx_input) {
            let tx_context = TxContext::new(self.clone(), tx_input, tx_cache);
            let mut tx_context_sh = Shareable::new(tx_context);

            TxContextStack::execute_on_vm_stack(&mut tx_context_sh, f);

            tx_context_sh.into_inner().into_results()
        } else {
            // no execution
            (TxResult::empty(), tx_cache.into_blockchain_updates())
        };

        if let Some(tv_log) = transfer_value_log {
            tx_result.result_logs.insert(0, tv_log);
        }

        (tx_result, blockchain_updates)
    }

    pub fn deploy_contract<F>(
        &self,
        mut tx_input: TxInput,
        contract_path: Vec<u8>,
        code_metadata: VMCodeMetadata,
        tx_cache: TxCache,
        f: F,
    ) -> (TxResult, VMAddress, BlockchainUpdate)
    where
        F: FnOnce(),
    {
        let new_address = tx_cache.get_new_address(&tx_input.from);
        tx_input.to = new_address.clone();
        tx_input.func_name = TxFunctionName::INIT;
        let tx_context = TxContext::new(self.clone(), tx_input, tx_cache);
        let mut tx_context_sh = Shareable::new(tx_context);
        let tx_input_ref = tx_context_sh.input_ref();

        if let Err(err) = tx_context_sh
            .tx_cache
            .subtract_egld_balance(&tx_input_ref.from, &tx_input_ref.egld_value)
        {
            return (
                TxResult::from_panic_obj(&err),
                VMAddress::zero(),
                BlockchainUpdate::empty(),
            );
        }
        tx_context_sh.create_new_contract(
            &new_address,
            contract_path,
            code_metadata,
            tx_input_ref.from.clone(),
        );
        tx_context_sh
            .tx_cache
            .increase_egld_balance(&new_address, &tx_input_ref.egld_value);

        TxContextStack::execute_on_vm_stack(&mut tx_context_sh, f);

        let (tx_result, blockchain_updates) = tx_context_sh.into_inner().into_results();
        (tx_result, new_address, blockchain_updates)
    }
}