multiversx_sc_scenario/facade/
scenario_world_whitebox.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
#![allow(deprecated)]

use multiversx_chain_vm::tx_mock::{TxFunctionName, TxResult};
use multiversx_sc::contract_base::{CallableContract, ContractBase};

use crate::{
    debug_executor::contract_instance_wrapped_execution,
    scenario_model::{ScCallStep, ScDeployStep, ScQueryStep},
    DebugApi, ScenarioWorld,
};

use super::whitebox_contract::WhiteboxContract;

impl ScenarioWorld {
    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_query<ContractObj, F>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        f: F,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
    {
        self.whitebox_query_check(whitebox_contract, f, |tx_result| {
            tx_result.assert_ok();
        })
    }

    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_query_check<ContractObj, F, C>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        f: F,
        check_result: C,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
        C: FnOnce(TxResult),
    {
        let sc_query_step = ScQueryStep::new().to(&whitebox_contract.address_expr);
        let contract_obj = (whitebox_contract.contract_obj_builder)();
        let debugger_backend = self.get_mut_debugger_backend();
        let tx_result = debugger_backend
            .vm_runner
            .perform_sc_query_lambda_and_check(&sc_query_step, || {
                catch_whitebox_panic(|| {
                    f(contract_obj);
                });
            });
        check_result(tx_result);

        self
    }

    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_call<ContractObj, F>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        sc_call_step: ScCallStep,
        f: F,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
    {
        self.whitebox_call_check(whitebox_contract, sc_call_step, f, |tx_result| {
            tx_result.assert_ok();
        })
    }

    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_call_check<ContractObj, F, C>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        sc_call_step: ScCallStep,
        f: F,
        check_result: C,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
        C: FnOnce(TxResult),
    {
        // the recipient can be deduced from the contract object, it is redundant to provide it in the step
        let mut sc_call_step = sc_call_step.to(&whitebox_contract.address_expr);

        // no endpoint is called per se, but if it is empty, the VM thinks it is a simple transfer of value
        if sc_call_step.tx.function.is_empty() {
            sc_call_step.tx.function = TxFunctionName::WHITEBOX_CALL.to_string();
        }

        let contract_obj = (whitebox_contract.contract_obj_builder)();
        let debugger_backend = self.get_mut_debugger_backend();
        let tx_result =
            debugger_backend
                .vm_runner
                .perform_sc_call_lambda_and_check(&sc_call_step, || {
                    catch_whitebox_panic(|| {
                        f(contract_obj);
                    });
                });
        check_result(tx_result);
        self
    }

    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_deploy<ContractObj, F>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        sc_deploy_step: ScDeployStep,
        f: F,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
    {
        self.whitebox_deploy_check(whitebox_contract, sc_deploy_step, f, |tx_result| {
            tx_result.assert_ok();
        })
    }

    #[deprecated(
        since = "0.53.0",
        note = "Please use method `whitebox`, as part of the unified transaction syntax."
    )]
    pub fn whitebox_deploy_check<ContractObj, F, C>(
        &mut self,
        whitebox_contract: &WhiteboxContract<ContractObj>,
        sc_deploy_step: ScDeployStep,
        f: F,
        check_result: C,
    ) -> &mut Self
    where
        ContractObj: ContractBase<Api = DebugApi> + CallableContract + 'static,
        F: FnOnce(ContractObj),
        C: FnOnce(TxResult),
    {
        let contract_obj = (whitebox_contract.contract_obj_builder)();
        let debugger_backend = self.get_mut_debugger_backend();
        let (_, tx_result) = debugger_backend
            .vm_runner
            .perform_sc_deploy_lambda_and_check(&sc_deploy_step, || {
                catch_whitebox_panic(|| {
                    f(contract_obj);
                });
            });
        check_result(tx_result);
        self
    }
}

fn catch_whitebox_panic<F>(f: F)
where
    F: FnOnce(),
{
    contract_instance_wrapped_execution(true, || {
        f();
        Ok(())
    });
}