multiversx_sc_scenario/scenario/run_trace/
scenario_trace_file.rs

1use std::path::{Path, PathBuf};
2
3use crate::{scenario::ScenarioRunner, scenario_model::*};
4
5use super::ScenarioTrace;
6
7pub struct ScenarioTraceFile {
8    full_path: PathBuf,
9    // TODO: some caching/flushing might be a good idea, at least for some situations
10    // this involves adding some cache/flush methods to ScenarioRunner
11}
12
13impl ScenarioTraceFile {
14    pub fn new<P: AsRef<Path>>(path: P) -> Self {
15        ScenarioTraceFile {
16            full_path: path.as_ref().into(),
17        }
18    }
19
20    fn with_tracer(&self, f: impl FnOnce(&mut ScenarioTrace)) {
21        let mut tracer = ScenarioTrace::default();
22        if self.full_path.is_file() {
23            tracer.load_scenario_trace(&self.full_path);
24        }
25
26        f(&mut tracer);
27        tracer.write_scenario_trace(&self.full_path);
28    }
29}
30
31impl ScenarioRunner for ScenarioTraceFile {
32    fn run_external_steps(&mut self, step: &ExternalStepsStep) {
33        self.with_tracer(|tracer| tracer.run_external_steps(step));
34    }
35
36    fn run_set_state_step(&mut self, step: &SetStateStep) {
37        self.with_tracer(|tracer| tracer.run_set_state_step(step));
38    }
39
40    fn run_sc_call_step(&mut self, step: &mut ScCallStep) {
41        self.with_tracer(|tracer| tracer.run_sc_call_step(step));
42    }
43
44    fn run_multi_sc_call_step(&mut self, steps: &mut [ScCallStep]) {
45        self.with_tracer(|tracer| tracer.run_multi_sc_call_step(steps));
46    }
47
48    fn run_multi_sc_deploy_step(&mut self, steps: &mut [ScDeployStep]) {
49        self.with_tracer(|tracer| tracer.run_multi_sc_deploy_step(steps));
50    }
51
52    fn run_sc_query_step(&mut self, step: &mut ScQueryStep) {
53        self.with_tracer(|tracer| tracer.run_sc_query_step(step));
54    }
55
56    fn run_sc_deploy_step(&mut self, step: &mut ScDeployStep) {
57        self.with_tracer(|tracer| tracer.run_sc_deploy_step(step));
58    }
59
60    fn run_transfer_step(&mut self, step: &TransferStep) {
61        self.with_tracer(|tracer| tracer.run_transfer_step(step));
62    }
63
64    fn run_validator_reward_step(&mut self, step: &ValidatorRewardStep) {
65        self.with_tracer(|tracer| tracer.run_validator_reward_step(step));
66    }
67
68    fn run_check_state_step(&mut self, step: &CheckStateStep) {
69        self.with_tracer(|tracer| tracer.run_check_state_step(step));
70    }
71
72    fn run_dump_state_step(&mut self) {
73        self.with_tracer(|tracer| tracer.run_dump_state_step());
74    }
75}