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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! # VM State Differences
//! This module provides the ability to generate diffs between two VMs internal states.
//! The diff can then be used to invert a VM to the original state.
//! This module is experimental work in progress and currently only used in testing
//! although it could potentially stabilize to be used in production.

use std::any::Any;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::AddAssign;
use std::sync::Arc;

use fuel_asm::Word;
use fuel_storage::Mappable;
use fuel_storage::MerkleRootStorage;
use fuel_storage::StorageInspect;
use fuel_storage::StorageMutate;
use fuel_tx::Contract;
use fuel_tx::Receipt;
use fuel_types::AssetId;

use crate::call::CallFrame;
use crate::context::Context;
use crate::storage::ContractsAssets;
use crate::storage::ContractsInfo;
use crate::storage::ContractsRawCode;
use crate::storage::ContractsState;

use super::balances::Balance;
use super::ExecutableTransaction;
use super::Interpreter;
use super::PanicContext;
use storage::*;

mod storage;

#[cfg(test)]
mod tests;

#[derive(Debug, Clone)]
/// A diff of VM state.
///
/// By default this does not print out the
/// memory bytes but you can supply the `#` flag
/// to get them like:
/// ```ignore
/// println!("{:#}", diff);
/// ```
pub struct Diff<T: VmStateCapture + Clone> {
    changes: Vec<Change<T>>,
}

#[derive(Debug, Clone)]
enum Change<T: VmStateCapture + Clone> {
    /// Holds a snapshot of register state.
    Register(T::State<VecState<Word>>),
    /// Holds a snapshot of memory state.
    Memory(T::State<Memory>),
    /// Holds a snapshot of storage state.
    Storage(T::State<StorageState>),
    /// Holds a snapshot of the call stack.
    Frame(T::State<VecState<Option<CallFrame>>>),
    /// Holds a snapshot of receipt state.
    Receipt(T::State<VecState<Option<Receipt>>>),
    /// Holds a snapshot of balance state.
    Balance(T::State<MapState<AssetId, Option<Balance>>>),
    /// Holds a snapshot of context state.
    Context(T::State<Context>),
    /// Holds a snapshot of the panic context state.
    PanicContext(T::State<PanicContext>),
    /// Holds a snapshot of the transaction state.
    Txn(T::State<Arc<dyn AnyDebug>>),
}

/// A trait that combines the [`Debug`] and [`Any`] traits.
pub trait AnyDebug: Any + Debug {
    /// Returns a reference to the underlying type as `Any`.
    fn as_any_ref(&self) -> &dyn Any;
}

impl<T> AnyDebug for T
where
    T: Any + Debug,
{
    fn as_any_ref(&self) -> &dyn Any {
        self
    }
}

/// A mapping between the kind of state that is being capture
/// and the concrete data that is collected.
pub trait VmStateCapture {
    /// The actual type is defined by the implementations of
    /// the Capture trait.
    type State<S: std::fmt::Debug + Clone>: std::fmt::Debug + Clone;
}

#[derive(Debug, Clone)]
/// Family of state data that are implemented with the [`Delta`]
/// struct. Captures the difference between the current and previous
/// state of the VM.
pub struct Deltas;

impl VmStateCapture for Deltas {
    type State<S: std::fmt::Debug + Clone> = Delta<S>;
}

#[derive(Debug, Clone)]
/// The Delta struct represents the difference between two states of the VM.
pub struct Delta<S> {
    // Represents the state of the VM before a change.
    from: S,
    // Represents the state of the VM after a change.
    to: S,
}

#[derive(Debug, Clone)]
/// Family of state data that are implemented with the [`Previous`]
/// struct. Captures the initial state of the VM.
pub struct InitialVmState;

impl VmStateCapture for InitialVmState {
    type State<S: std::fmt::Debug + Clone> = Previous<S>;
}
#[derive(Debug, Clone)]
/// The State type when capturing the initial state of the VM.
pub struct Previous<S>(S);

#[derive(Debug, Clone)]
/// The state of a vector at an index.
struct VecState<T> {
    /// Index of the value.
    index: usize,
    /// Value at the index.
    value: T,
}

#[derive(Debug, Clone)]
/// The state of a map at a key.
struct MapState<K, V>
where
    K: Hash,
    V: PartialEq,
{
    /// Key of the value.
    key: K,
    /// Value at the key.
    value: V,
}

#[derive(Clone)]
/// The state of a memory region.
struct Memory {
    /// The start of the memory region.
    start: usize,
    /// The region of bytes.
    bytes: Vec<u8>,
}

impl Debug for Memory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            f.debug_struct("Memory")
                .field("start", &self.start)
                .field("bytes", &self.bytes)
                .finish()
        } else {
            f.debug_struct("Memory")
                .field("start", &self.start)
                .field("bytes", &self.bytes.len())
                .finish()
        }
    }
}

fn capture_buffer_state<'iter, I, T>(
    a: I,
    b: I,
    change: fn(Delta<VecState<T>>) -> Change<Deltas>,
) -> impl Iterator<Item = Change<Deltas>> + 'iter
where
    T: 'static + std::cmp::PartialEq + Clone,
    I: Iterator<Item = &'iter T> + 'iter,
{
    a.enumerate().zip(b).filter_map(move |(a, b)| {
        (a.1 != b).then(|| {
            change(Delta {
                from: VecState {
                    index: a.0,
                    value: a.1.clone(),
                },
                to: VecState {
                    index: a.0,
                    value: b.clone(),
                },
            })
        })
    })
}

type ChangeDeltaVariant<S> = fn(Delta<S>) -> Change<Deltas>;

fn capture_map_state<'iter, K, V>(
    a: &'iter HashMap<K, V>,
    b: &'iter HashMap<K, V>,
    change: ChangeDeltaVariant<MapState<K, Option<V>>>,
) -> Vec<Change<Deltas>>
where
    K: 'static + std::cmp::PartialEq + Eq + Clone + Hash + Debug,
    V: 'static + std::cmp::PartialEq + Clone + Debug,
{
    let a_keys: HashSet<_> = a.keys().collect();
    let b_keys: HashSet<_> = b.keys().collect();
    capture_map_state_inner(a, &a_keys, b, &b_keys).map(change).collect()
}

fn capture_map_state_inner<'iter, K, V>(
    a: &'iter HashMap<K, V>,
    a_keys: &'iter HashSet<&K>,
    b: &'iter HashMap<K, V>,
    b_keys: &'iter HashSet<&K>,
) -> impl Iterator<Item = Delta<MapState<K, Option<V>>>> + 'iter
where
    K: 'static + std::cmp::PartialEq + Eq + Clone + Hash + Debug,
    V: 'static + std::cmp::PartialEq + Clone + Debug,
{
    let a_diff = a_keys.difference(b_keys).map(|k| Delta {
        from: MapState {
            key: (*k).clone(),
            value: Some(a[k].clone()),
        },
        to: MapState {
            key: (*k).clone(),
            value: None,
        },
    });
    let b_diff = b_keys.difference(a_keys).map(|k| Delta {
        from: MapState {
            key: (*k).clone(),
            value: None,
        },
        to: MapState {
            key: (*k).clone(),
            value: Some(b[k].clone()),
        },
    });
    let intersection = a_keys.intersection(b_keys).filter_map(|k| {
        let value_a = &a[k];
        let value_b = &b[k];
        (value_a != value_b).then(|| Delta {
            from: MapState {
                key: (*k).clone(),
                value: Some(value_a.clone()),
            },
            to: MapState {
                key: (*k).clone(),
                value: Some(value_b.clone()),
            },
        })
    });

    a_diff.chain(b_diff).chain(intersection)
}

fn capture_vec_state<'iter, I, T>(
    a: I,
    b: I,
    change: ChangeDeltaVariant<VecState<Option<T>>>,
) -> impl Iterator<Item = Change<Deltas>> + 'iter
where
    T: 'static + std::cmp::PartialEq + Clone,
    I: Iterator<Item = &'iter T> + 'iter,
{
    capture_vec_state_inner(a, b).map(move |(index, a, b)| {
        change(Delta {
            from: VecState { index, value: a },
            to: VecState { index, value: b },
        })
    })
}
fn capture_vec_state_inner<'iter, I, T>(a: I, b: I) -> impl Iterator<Item = (usize, Option<T>, Option<T>)> + 'iter
where
    T: 'static + std::cmp::PartialEq + Clone,
    I: Iterator<Item = &'iter T> + 'iter,
{
    a.map(Some)
        .chain(std::iter::repeat(None))
        .enumerate()
        .zip(b.map(Some).chain(std::iter::repeat(None)))
        .take_while(|((_, a), b)| a.is_some() || b.is_some())
        .filter_map(|((index, a), b)| {
            b.map_or(true, |b| a.map_or(true, |a| a != b))
                .then(|| (index, a.cloned(), b.cloned()))
        })
}

impl<S, Tx> Interpreter<S, Tx> {
    /// The diff function generates a diff of VM state, represented by the Diff struct, between two VMs internal states.
    pub fn diff(&self, other: &Self) -> Diff<Deltas>
    where
        Tx: PartialEq + Clone + Debug + 'static,
    {
        let mut diff = Diff { changes: Vec::new() };
        let registers = capture_buffer_state(self.registers.iter(), other.registers.iter(), Change::Register);
        diff.changes.extend(registers);
        let frames = capture_vec_state(self.frames.iter(), other.frames.iter(), Change::Frame);
        diff.changes.extend(frames);
        let receipts = capture_vec_state(self.receipts.iter(), other.receipts.iter(), Change::Receipt);
        diff.changes.extend(receipts);
        let balances = capture_map_state(self.balances.as_ref(), other.balances.as_ref(), Change::Balance);
        diff.changes.extend(balances);

        let mut memory = self.memory.iter().enumerate().zip(other.memory.iter());

        while let Some(((start, s_from), s_to)) = memory
            .by_ref()
            .find(|((_, a), b)| a != b)
            .map(|((n, a), b)| ((n, *a), *b))
        {
            let (mut from, mut to): (Vec<_>, Vec<_>) = memory
                .by_ref()
                .take_while(|((_, a), b)| a != b)
                .map(|((_, a), b)| (*a, *b))
                .unzip();
            from.splice(..0, std::iter::once(s_from)).next();
            to.splice(..0, std::iter::once(s_to)).next();
            diff.changes.push(Change::Memory(Delta {
                from: Memory { start, bytes: from },
                to: Memory { start, bytes: to },
            }));
        }

        if self.context != other.context {
            diff.changes.push(Change::Context(Delta {
                from: self.context.clone(),
                to: other.context.clone(),
            }))
        }

        if self.panic_context != other.panic_context {
            diff.changes.push(Change::PanicContext(Delta {
                from: self.panic_context.clone(),
                to: other.panic_context.clone(),
            }))
        }

        if self.tx != other.tx {
            let from: Arc<dyn AnyDebug> = Arc::new(self.tx.clone());
            let to: Arc<dyn AnyDebug> = Arc::new(other.tx.clone());
            diff.changes.push(Change::Txn(Delta { from, to }))
        }

        diff
    }

    fn inverse_inner(&mut self, change: &Change<InitialVmState>)
    where
        Tx: Clone + 'static,
    {
        match change {
            Change::Register(Previous(VecState { index, value })) => self.registers[*index] = *value,
            Change::Frame(Previous(value)) => invert_vec(&mut self.frames, value),
            Change::Receipt(Previous(value)) => invert_vec(&mut self.receipts, value),
            Change::Balance(Previous(value)) => invert_map(self.balances.as_mut(), value),
            Change::Memory(Previous(Memory { start, bytes })) => {
                self.memory[*start..(*start + bytes.len())].copy_from_slice(&bytes[..])
            }
            Change::Context(Previous(value)) => self.context = value.clone(),
            Change::PanicContext(Previous(value)) => self.panic_context = value.clone(),
            Change::Txn(Previous(tx)) => {
                self.tx = AsRef::<dyn AnyDebug>::as_ref(tx)
                    .as_any_ref()
                    .downcast_ref::<Tx>()
                    .unwrap()
                    .clone();
            }
            Change::Storage(_) => (),
        }
    }
}

fn invert_vec<T: Clone>(vector: &mut Vec<T>, value: &VecState<Option<T>>) {
    use std::cmp::Ordering;
    match (&value, value.index.cmp(&vector.len())) {
        (
            VecState {
                index,
                value: Some(value),
            },
            Ordering::Equal | Ordering::Greater,
        ) => {
            vector.resize(*index + 1, value.clone());
            vector[*index] = value.clone();
        }
        (
            VecState {
                index,
                value: Some(value),
            },
            Ordering::Less,
        ) => vector[*index] = value.clone(),
        (VecState { value: None, .. }, Ordering::Equal | Ordering::Greater) => (),
        (VecState { index, value: None }, Ordering::Less) => vector.truncate(*index),
    }
}

fn invert_map<K: Hash + PartialEq + Eq + Clone, V: Clone + PartialEq>(
    map: &mut HashMap<K, V>,
    value: &MapState<K, Option<V>>,
) {
    match value {
        MapState {
            key,
            value: Some(value),
        } => {
            map.insert(key.clone(), value.clone());
        }
        MapState { key, value: None } => {
            map.remove(key);
        }
    }
}

impl<S, Tx> PartialEq for Interpreter<S, Tx>
where
    Tx: PartialEq,
{
    /// Does not compare storage, debugger or profiler
    fn eq(&self, other: &Self) -> bool {
        self.registers == other.registers
            && self.memory == other.memory
            && self.frames == other.frames
            && self.receipts == other.receipts
            && self.tx == other.tx
            && self.initial_balances == other.initial_balances
            && self.context == other.context
            && self.balances == other.balances
            && self.gas_costs == other.gas_costs
            && self.params == other.params
            && self.panic_context == other.panic_context
    }
}

impl From<Diff<Deltas>> for Diff<InitialVmState> {
    fn from(d: Diff<Deltas>) -> Self {
        Self {
            changes: d
                .changes
                .into_iter()
                .map(|c| match c {
                    Change::Register(v) => Change::Register(v.into()),
                    Change::Memory(v) => Change::Memory(v.into()),
                    Change::Storage(v) => Change::Storage(v.into()),
                    Change::Frame(v) => Change::Frame(v.into()),
                    Change::Receipt(v) => Change::Receipt(v.into()),
                    Change::Balance(v) => Change::Balance(v.into()),
                    Change::Context(v) => Change::Context(v.into()),
                    Change::PanicContext(v) => Change::PanicContext(v.into()),
                    Change::Txn(v) => Change::Txn(v.into()),
                })
                .collect(),
        }
    }
}

impl<T> From<Delta<T>> for Previous<T> {
    fn from(d: Delta<T>) -> Self {
        Self(d.from)
    }
}

impl<T: VmStateCapture + Clone> AddAssign for Diff<T> {
    fn add_assign(&mut self, rhs: Self) {
        self.changes.extend(rhs.changes);
    }
}