multiversx_sc_scenario/scenario/run_trace/
scenario_trace_file.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
use std::path::{Path, PathBuf};

use crate::{scenario::ScenarioRunner, scenario_model::*};

use super::ScenarioTrace;

pub struct ScenarioTraceFile {
    full_path: PathBuf,
    // TODO: some caching/flushing might be a good idea, at least for some situations
    // this involves adding some cache/flush methods to ScenarioRunner
}

impl ScenarioTraceFile {
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        ScenarioTraceFile {
            full_path: path.as_ref().into(),
        }
    }

    fn with_tracer(&self, f: impl FnOnce(&mut ScenarioTrace)) {
        let mut tracer = ScenarioTrace::default();
        if self.full_path.is_file() {
            tracer.load_scenario_trace(&self.full_path);
        }

        f(&mut tracer);
        tracer.write_scenario_trace(&self.full_path);
    }
}

impl ScenarioRunner for ScenarioTraceFile {
    fn run_external_steps(&mut self, step: &ExternalStepsStep) {
        self.with_tracer(|tracer| tracer.run_external_steps(step));
    }

    fn run_set_state_step(&mut self, step: &SetStateStep) {
        self.with_tracer(|tracer| tracer.run_set_state_step(step));
    }

    fn run_sc_call_step(&mut self, step: &mut ScCallStep) {
        self.with_tracer(|tracer| tracer.run_sc_call_step(step));
    }

    fn run_multi_sc_call_step(&mut self, steps: &mut [ScCallStep]) {
        self.with_tracer(|tracer| tracer.run_multi_sc_call_step(steps));
    }

    fn run_multi_sc_deploy_step(&mut self, steps: &mut [ScDeployStep]) {
        self.with_tracer(|tracer| tracer.run_multi_sc_deploy_step(steps));
    }

    fn run_sc_query_step(&mut self, step: &mut ScQueryStep) {
        self.with_tracer(|tracer| tracer.run_sc_query_step(step));
    }

    fn run_sc_deploy_step(&mut self, step: &mut ScDeployStep) {
        self.with_tracer(|tracer| tracer.run_sc_deploy_step(step));
    }

    fn run_transfer_step(&mut self, step: &TransferStep) {
        self.with_tracer(|tracer| tracer.run_transfer_step(step));
    }

    fn run_validator_reward_step(&mut self, step: &ValidatorRewardStep) {
        self.with_tracer(|tracer| tracer.run_validator_reward_step(step));
    }

    fn run_check_state_step(&mut self, step: &CheckStateStep) {
        self.with_tracer(|tracer| tracer.run_check_state_step(step));
    }

    fn run_dump_state_step(&mut self) {
        self.with_tracer(|tracer| tracer.run_dump_state_step());
    }
}