multiversx_sc_scenario/scenario/run_trace/
scenario_trace.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
use multiversx_chain_scenario_format::{
    interpret_trait::{InterpretableFrom, InterpreterContext},
    serde_raw::ScenarioRaw,
};

use crate::{
    multiversx_sc::types::Address,
    scenario::{model::*, ScenarioRunner},
    scenario_format::interpret_trait::IntoRaw,
};
use std::{collections::HashMap, path::Path};

#[derive(Default, Debug)]
pub struct ScenarioTrace {
    pub scenario_trace: Scenario,
    pub addr_to_pretty_string_map: HashMap<Address, String>,
}

impl ScenarioTrace {
    pub fn write_scenario_trace<P: AsRef<Path>>(&mut self, file_path: P) {
        self.scenario_trace_prettify();

        let mandos_trace = core::mem::take(&mut self.scenario_trace);
        let mandos_trace_raw = mandos_trace.into_raw();
        mandos_trace_raw.save_to_file(file_path);
    }

    pub fn load_scenario_trace<P: AsRef<Path>>(&mut self, file_path: P) {
        let mandos_trace_raw = ScenarioRaw::load_from_file(file_path);
        let mandos_trace =
            Scenario::interpret_from(mandos_trace_raw, &InterpreterContext::default());
        self.scenario_trace = mandos_trace;
    }

    fn process_address_key(&mut self, address_key: &AddressKey) {
        if !self
            .addr_to_pretty_string_map
            .contains_key(&address_key.value)
        {
            self.addr_to_pretty_string_map
                .insert(address_key.value.clone(), address_key.original.clone());
        }
    }

    fn process_address_value(&mut self, address_value: &AddressValue) {
        if !self
            .addr_to_pretty_string_map
            .contains_key(&address_value.value)
        {
            self.addr_to_pretty_string_map.insert(
                address_value.value.clone(),
                address_value.original.to_concatenated_string(),
            );
        }
    }
}

impl ScenarioRunner for ScenarioTrace {
    fn run_external_steps(&mut self, step: &ExternalStepsStep) {
        self.scenario_trace
            .steps
            .push(Step::ExternalSteps(step.clone()));
    }

    fn run_set_state_step(&mut self, step: &SetStateStep) {
        for address_key in step.accounts.keys() {
            self.process_address_key(address_key);
        }
        for new_address in &step.new_addresses {
            self.process_address_value(&new_address.new_address);
        }
        self.scenario_trace.steps.push(Step::SetState(step.clone()));
    }

    fn run_sc_call_step(&mut self, step: &mut ScCallStep) {
        self.process_address_value(&step.tx.from);
        self.process_address_value(&step.tx.to);
        self.scenario_trace.steps.push(Step::ScCall(step.clone()));
    }

    fn run_multi_sc_call_step(&mut self, steps: &mut [ScCallStep]) {
        for step in steps {
            self.process_address_value(&step.tx.from);
            self.process_address_value(&step.tx.to);
            self.scenario_trace.steps.push(Step::ScCall(step.clone()));
        }
    }

    fn run_multi_sc_deploy_step(&mut self, steps: &mut [ScDeployStep]) {
        for step in steps {
            self.process_address_value(&step.tx.from);
            self.scenario_trace.steps.push(Step::ScDeploy(step.clone()));
        }
    }

    fn run_sc_query_step(&mut self, step: &mut ScQueryStep) {
        self.process_address_value(&step.tx.to);
        self.scenario_trace.steps.push(Step::ScQuery(step.clone()));
    }

    fn run_sc_deploy_step(&mut self, step: &mut ScDeployStep) {
        self.process_address_value(&step.tx.from);
        self.scenario_trace.steps.push(Step::ScDeploy(step.clone()));
    }

    fn run_transfer_step(&mut self, step: &TransferStep) {
        self.process_address_value(&step.tx.from);
        self.process_address_value(&step.tx.to);
        self.scenario_trace.steps.push(Step::Transfer(step.clone()));
    }

    fn run_validator_reward_step(&mut self, step: &ValidatorRewardStep) {
        self.scenario_trace
            .steps
            .push(Step::ValidatorReward(step.clone()));
    }

    fn run_check_state_step(&mut self, step: &CheckStateStep) {
        for address_key in step.accounts.accounts.keys() {
            self.process_address_key(address_key);
        }
        self.scenario_trace
            .steps
            .push(Step::CheckState(step.clone()));
    }

    fn run_dump_state_step(&mut self) {
        self.scenario_trace
            .steps
            .push(Step::DumpState(DumpStateStep::default()));
    }
}