-
Notifications
You must be signed in to change notification settings - Fork 456
/
traces.rs
166 lines (148 loc) · 4.58 KB
/
traces.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
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
//! Types for the Parity Ad-Hoc Trace API
use types::{H160, H256, U256, Bytes, Action, Res};
use std::collections::BTreeMap;
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize)]
/// Description of the type of trace to make
pub enum TraceType {
/// Transaction Trace
#[serde(rename = "trace")]
Trace,
/// Virtual Machine Execution Trace
#[serde(rename = "vmTrace")]
VmTrace,
/// State Difference
#[serde(rename = "stateDiff")]
StateDiff,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
/// Ad-Hoc trace API type
pub struct BlockTrace {
/// Output Bytes
pub output: Bytes,
/// Transaction Trace
pub trace: Option<Vec<TransactionTrace>>,
/// Virtual Machine Execution Trace
#[serde(rename = "vmTrace")]
pub vm_trace: Option<VMTrace>,
/// State Difference
#[serde(rename = "stateDiff")]
pub state_diff: Option<StateDiff>,
}
//---------------- State Diff ----------------
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
/// Aux type for Diff::Changed.
pub struct ChangedType<T> {
from: T,
to: T,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
/// Serde-friendly `Diff` shadow.
pub enum Diff<T> {
#[serde(rename="=")]
Same,
#[serde(rename="+")]
Born(T),
#[serde(rename="-")]
Died(T),
#[serde(rename="*")]
Changed(ChangedType<T>),
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
/// Serde-friendly `AccountDiff` shadow.
pub struct AccountDiff {
pub balance: Diff<U256>,
pub nonce: Diff<U256>,
pub code: Diff<Bytes>,
pub storage: BTreeMap<H256, Diff<H256>>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
/// Serde-friendly `StateDiff` shadow.
pub struct StateDiff(BTreeMap<H160, AccountDiff>);
// ------------------ Trace -------------
/// Trace
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct TransactionTrace {
/// Trace address
#[serde(rename = "traceAddress")]
trace_address: Vec<usize>,
/// Subtraces
subtraces: usize,
/// Action
action: Action,
/// Result
result: Option<Res>,
}
// ---------------- VmTrace ------------------------------
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
/// A record of a full VM trace for a CALL/CREATE.
pub struct VMTrace {
/// The code to be executed.
pub code: Bytes,
/// The operations executed.
pub ops: Vec<VMOperation>,
}
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
/// A record of the execution of a single VM operation.
pub struct VMOperation {
/// The program counter.
pub pc: usize,
/// The gas cost for this instruction.
pub cost: u64,
/// Information concerning the execution of the operation.
pub ex: Option<VMExecutedOperation>,
/// Subordinate trace of the CALL/CREATE if applicable.
// #[serde(bound="VMTrace: Deserialize")]
pub sub: Option<VMTrace>,
}
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
/// A record of an executed VM operation.
pub struct VMExecutedOperation {
/// The total gas used.
#[serde(rename="used")]
pub used: u64,
/// The stack item placed, if any.
pub push: Vec<U256>,
/// If altered, the memory delta.
#[serde(rename="mem")]
pub mem: Option<MemoryDiff>,
/// The altered storage value, if any.
#[serde(rename="store")]
pub store: Option<StorageDiff>,
}
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
/// A diff of some chunk of memory.
pub struct MemoryDiff {
/// Offset into memory the change begins.
pub off: usize,
/// The changed data.
pub data: Bytes,
}
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
/// A diff of some storage value.
pub struct StorageDiff {
/// Which key in storage is changed.
pub key: U256,
/// What the value has been changed to.
pub val: U256,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json;
// tx: https://etherscan.io/tx/0x4a91b11dbd2b11c308cfe7775eac2036f20c501691e3f8005d83b2dcce62d6b5
// using the 'trace_replayTransaction' API function
// with 'trace', 'vmTrace', 'stateDiff'
const EXAMPLE_TRACE: &'static str = include!("./example-trace-str.rs");
#[test]
fn test_serialize_trace_type() {
let trace_type_str = r#"["trace","vmTrace","stateDiff"]"#;
let trace_type = vec![TraceType::Trace, TraceType::VmTrace, TraceType::StateDiff];
let se_trace_str: String = serde_json::to_string(&trace_type).unwrap();
assert_eq!(trace_type_str, se_trace_str);
}
#[test]
fn test_deserialize_blocktrace() {
let _trace: BlockTrace = serde_json::from_str(EXAMPLE_TRACE).unwrap();
}
}