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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#[cfg(test)]
use alloc::{
    vec,
    vec::Vec,
};

use super::Interpreter;
use crate::prelude::*;
use fuel_asm::RegId;

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
where
    Tx: ExecutableTransaction,
{
    /// Get single-stepping mode
    pub const fn single_stepping(&self) -> bool {
        self.debugger.single_stepping()
    }

    /// Set single-stepping mode
    pub fn set_single_stepping(&mut self, single_stepping: bool) {
        self.debugger.set_single_stepping(single_stepping)
    }

    /// Clear all set breakpoints.
    pub fn clear_breakpoints(&mut self) {
        self.debugger.clear_breakpoints();
    }

    /// Set a new breakpoint for the provided location.
    pub fn set_breakpoint(&mut self, breakpoint: Breakpoint) {
        self.debugger.set_breakpoint(breakpoint)
    }

    /// Overwrite all breakpoints with a new set of breakpoints.
    pub fn overwrite_breakpoints(&mut self, breakpoints: &[Breakpoint]) {
        self.debugger.clear_breakpoints();
        for bp in breakpoints {
            self.debugger.set_breakpoint(*bp);
        }
    }

    /// Remove a previously set breakpoint.
    pub fn remove_breakpoint(&mut self, breakpoint: &Breakpoint) {
        self.debugger.remove_breakpoint(breakpoint)
    }

    pub(crate) fn eval_debugger_state(&mut self) -> DebugEval {
        let debugger = &mut self.debugger;

        let contract = self.frames.last().map(CallFrame::to);
        let pc = self.registers[RegId::PC].saturating_sub(self.registers[RegId::IS]);

        debugger.eval_state(contract, pc)
    }

    pub(crate) fn debugger_set_last_state(&mut self, state: ProgramState) {
        self.debugger.set_last_state(state)
    }

    pub(crate) const fn debugger_last_state(&self) -> &Option<ProgramState> {
        self.debugger.last_state()
    }
}

#[test]
fn breakpoint_script() {
    use fuel_asm::op;
    use fuel_tx::ConsensusParameters;

    let mut vm = Interpreter::<_, _, _>::with_memory_storage();

    let gas_limit = 1_000_000;
    let gas_price = 0;
    let height = Default::default();

    let script = [
        op::addi(0x10, RegId::ZERO, 8),
        op::addi(0x11, RegId::ZERO, 16),
        op::addi(0x12, RegId::ZERO, 32),
        op::addi(0x13, RegId::ZERO, 64),
        op::addi(0x14, RegId::ZERO, 128),
        op::ret(0x10),
    ]
    .into_iter()
    .collect();

    let consensus_params = ConsensusParameters::standard();

    let tx = TransactionBuilder::script(script, vec![])
        .script_gas_limit(gas_limit)
        .add_random_fee_input()
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to generate checked tx")
        .into_ready(
            gas_price,
            consensus_params.gas_costs(),
            consensus_params.fee_params(),
        )
        .unwrap();

    let suite = vec![
        (
            Breakpoint::script(0),
            vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)],
        ),
        (
            Breakpoint::script(2),
            vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)],
        ),
        (
            Breakpoint::script(3),
            vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)],
        ),
        (
            Breakpoint::script(5),
            vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)],
        ),
    ];

    suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b));

    let state = vm
        .transact(tx)
        .map(ProgramState::from)
        .expect("Failed to execute script!");

    suite
        .into_iter()
        .fold(state, |state, (breakpoint, registers)| {
            let debug = state.debug_ref().expect("Expected breakpoint");
            let b = debug
                .breakpoint()
                .expect("State without expected breakpoint");

            assert_eq!(&breakpoint, b);
            registers.into_iter().for_each(|(r, w)| {
                assert_eq!(w, vm.registers()[r]);
            });

            vm.resume().expect("Failed to resume")
        });
}

#[test]
fn single_stepping() {
    use fuel_asm::op;
    use fuel_tx::ConsensusParameters;

    let mut vm = Interpreter::<_, _, _>::with_memory_storage();

    let gas_limit = 1_000_000;
    let height = Default::default();
    let gas_price = 0;

    // Repeats the middle two instructions five times
    let script = [
        op::addi(0x10, RegId::ZERO, 5),
        op::addi(0x11, 0x11, 1),
        op::jnei(0x10, 0x11, 1),
        op::ret(0x10),
    ]
    .into_iter()
    .collect();

    let consensus_params = ConsensusParameters::standard();

    let tx = TransactionBuilder::script(script, vec![])
        .script_gas_limit(gas_limit)
        .add_random_fee_input()
        .finalize()
        .into_checked(height, &consensus_params)
        .expect("failed to generate checked tx")
        .into_ready(
            gas_price,
            consensus_params.gas_costs(),
            consensus_params.fee_params(),
        )
        .unwrap();

    vm.set_single_stepping(true);

    let mut state = vm
        .transact(tx)
        .map(ProgramState::from)
        .expect("Failed to execute script!");

    let mut stops = Vec::new();

    while let Some(debug) = state.debug_ref() {
        let b = debug
            .breakpoint()
            .expect("State without expected breakpoint");

        stops.push(b.pc());

        state = vm.resume().expect("Failed to resume");
    }

    assert_eq!(stops, vec![0, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 12]);
}