@@ -81,28 +81,14 @@ impl<Trace: Writer, Out: Writer> Informant<Trace, Out> {
81
81
}
82
82
}
83
83
84
- impl < Trace : Writer , Out : Writer > Informant < Trace , Out > {
85
- fn stack ( & self ) -> String {
86
- let items = self . stack . iter ( ) . map ( |i| format ! ( "\" 0x{:x}\" " , i) ) . collect :: < Vec < _ > > ( ) ;
87
- format ! ( "[{}]" , items. join( "," ) )
88
- }
89
-
90
- fn storage ( & self ) -> String {
91
- let vals = self . storage . iter ( )
92
- . map ( |( k, v) | format ! ( "\" 0x{:?}\" : \" 0x{:?}\" " , k, v) )
93
- . collect :: < Vec < _ > > ( ) ;
94
- format ! ( "{{{}}}" , vals. join( "," ) )
95
- }
96
- }
97
-
98
84
impl < Trace : Writer , Out : Writer > vm:: Informant for Informant < Trace , Out > {
99
85
fn before_test ( & mut self , name : & str , action : & str ) {
100
- writeln ! (
101
- & mut self . out_sink ,
102
- "{{ \" test\" : \" { name} \" , \" action \" : \" {action} \" }}" ,
103
- name = name ,
104
- action = action ,
105
- ) . expect ( "The sink must be writeable." ) ;
86
+ let out_data = json ! ( {
87
+ "action" : action ,
88
+ "test" : name,
89
+ } ) ;
90
+
91
+ writeln ! ( & mut self . out_sink , "{}" , out_data ) . expect ( "The sink must be writeable." ) ;
106
92
}
107
93
108
94
fn set_gas ( & mut self , _gas : U256 ) { }
@@ -113,26 +99,26 @@ impl<Trace: Writer, Out: Writer> vm::Informant for Informant<Trace, Out> {
113
99
114
100
match result {
115
101
Ok ( success) => {
116
- writeln ! (
117
- & mut trace_sink,
118
- "{{ \" stateRoot \" : \" {:?} \" }}" , success . state_root
119
- ) . expect ( "The sink must be writeable." ) ;
120
- writeln ! (
121
- & mut out_sink ,
122
- "{{ \" output \" : \" 0x{output} \" , \" gasUsed\" : \" 0x{gas:x} \" , \" time \" :{time}}}" ,
123
- output = success. output . to_hex ( ) ,
124
- gas = success . gas_used ,
125
- time = display :: as_micros ( & success . time ) ,
126
- ) . expect ( "The sink must be writeable." ) ;
102
+ let trace_data = json ! ( { "stateRoot" : success . state_root } ) ;
103
+ writeln ! ( & mut trace_sink, "{}" , trace_data )
104
+ . expect ( "The sink must be writeable." ) ;
105
+
106
+ let out_data = json ! ( {
107
+ "output" : format! ( "0x{}" , success . output . to_hex ( ) ) ,
108
+ "gasUsed" : format! ( "{:#x}" , success . gas_used ) ,
109
+ "time" : display :: as_micros ( & success. time ) ,
110
+ } ) ;
111
+
112
+ writeln ! ( & mut out_sink , "{}" , out_data ) . expect ( "The sink must be writeable." ) ;
127
113
} ,
128
114
Err ( failure) => {
129
- writeln ! (
130
- & mut out_sink ,
131
- "{{ \" error \" : \" {error} \" , \" gasUsed\" : \" 0x{gas:x} \" , \" time \" :{time}}}" ,
132
- error = display:: escape_newlines ( & failure. error ) ,
133
- gas = failure . gas_used ,
134
- time = display :: as_micros ( & failure . time ) ,
135
- ) . expect ( "The sink must be writeable." ) ;
115
+ let out_data = json ! ( {
116
+ "error" : & failure . error . to_string ( ) ,
117
+ "gasUsed" : format! ( "{:#x}" , failure . gas_used ) ,
118
+ "time" : display:: as_micros ( & failure. time ) ,
119
+ } ) ;
120
+
121
+ writeln ! ( & mut out_sink , "{}" , out_data ) . expect ( "The sink must be writeable." ) ;
136
122
} ,
137
123
}
138
124
}
@@ -144,20 +130,17 @@ impl<Trace: Writer, Out: Writer> trace::VMTracer for Informant<Trace, Out> {
144
130
fn trace_next_instruction ( & mut self , pc : usize , instruction : u8 , current_gas : U256 ) -> bool {
145
131
let info = :: evm:: Instruction :: from_u8 ( instruction) . map ( |i| i. info ( ) ) ;
146
132
self . instruction = instruction;
147
- let storage = self . storage ( ) ;
148
- let stack = self . stack ( ) ;
149
-
150
- writeln ! (
151
- & mut self . trace_sink,
152
- "{{\" pc\" :{pc},\" op\" :{op},\" opName\" :\" {name}\" ,\" gas\" :\" 0x{gas:x}\" ,\" stack\" :{stack},\" storage\" :{storage},\" depth\" :{depth}}}" ,
153
- pc = pc,
154
- op = instruction,
155
- name = info. map( |i| i. name) . unwrap_or( "" ) ,
156
- gas = current_gas,
157
- stack = stack,
158
- storage = storage,
159
- depth = self . depth,
160
- ) . expect ( "The sink must be writeable." ) ;
133
+ let trace_data = json ! ( {
134
+ "pc" : pc,
135
+ "op" : instruction,
136
+ "opName" : info. map( |i| i. name) . unwrap_or( "" ) ,
137
+ "gas" : format!( "{:#x}" , current_gas) ,
138
+ "stack" : self . stack,
139
+ "storage" : self . storage,
140
+ "depth" : self . depth,
141
+ } ) ;
142
+
143
+ writeln ! ( & mut self . trace_sink, "{}" , trace_data) . expect ( "The sink must be writeable." ) ;
161
144
162
145
true
163
146
}
@@ -232,8 +215,8 @@ pub mod tests {
232
215
} ,
233
216
"60F8d6" ,
234
217
0xffff ,
235
- r#"{"pc":0," op":96,"opName":"PUSH1","gas":"0xffff" ,"stack":[],"storage":{},"depth":1 }
236
- {"pc":2," op":214,"opName":"","gas":"0xfffc" ,"stack":["0xf8"],"storage":{},"depth":1 }
218
+ r#"{"depth":1,"gas":"0xffff"," op":96,"opName":"PUSH1","pc":0 ,"stack":[],"storage":{}}
219
+ {"depth":1,"gas":"0xfffc"," op":214,"opName":"","pc":2 ,"stack":["0xf8"],"storage":{}}
237
220
"# ,
238
221
) ;
239
222
@@ -246,7 +229,7 @@ pub mod tests {
246
229
} ,
247
230
"F8d6" ,
248
231
0xffff ,
249
- r#"{"pc":0," op":248,"opName":"","gas":"0xffff" ,"stack":[],"storage":{},"depth":1 }
232
+ r#"{"depth":1,"gas":"0xffff"," op":248,"opName":"","pc":0 ,"stack":[],"storage":{}}
250
233
"# ,
251
234
) ;
252
235
}
@@ -262,30 +245,30 @@ pub mod tests {
262
245
} ,
263
246
"32343434345830f138343438323439f0" ,
264
247
0xffff ,
265
- r#"{"pc":0," op":50,"opName":"ORIGIN","gas":"0xffff" ,"stack":[],"storage":{},"depth":1 }
266
- {"pc ":1,"op":52,"opName":"CALLVALUE","gas":"0xfffd" ,"stack":["0x0"],"storage":{},"depth":1 }
267
- {"pc":2," op":52,"opName":"CALLVALUE","gas":"0xfffb" ,"stack":["0x0","0x0"],"storage":{},"depth":1 }
268
- {"pc":3," op":52,"opName":"CALLVALUE","gas":"0xfff9" ,"stack":["0x0","0x0","0x0"],"storage":{},"depth":1 }
269
- {"pc":4," op":52,"opName":"CALLVALUE","gas":"0xfff7" ,"stack":["0x0","0x0","0x0","0x0"],"storage":{},"depth":1 }
270
- {"pc":5," op":88,"opName":"PC","gas":"0xfff5" ,"stack":["0x0","0x0","0x0","0x0","0x0"],"storage":{},"depth":1 }
271
- {"pc":6," op":48,"opName":"ADDRESS","gas":"0xfff3" ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5"],"storage":{},"depth":1 }
272
- {"pc":7," op":241,"opName":"CALL","gas":"0xfff1" ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5","0x0"],"storage":{},"depth":1 }
273
- {"pc":8," op":56,"opName":"CODESIZE","gas":"0x9e21" ,"stack":["0x1"],"storage":{},"depth":1 }
274
- {"pc":9," op":52,"opName":"CALLVALUE","gas":"0x9e1f" ,"stack":["0x1","0x10"],"storage":{},"depth":1 }
275
- {"pc":10," op":52,"opName":"CALLVALUE","gas":"0x9e1d" ,"stack":["0x1","0x10","0x0"],"storage":{},"depth":1 }
276
- {"pc":11," op":56,"opName":"CODESIZE","gas":"0x9e1b" ,"stack":["0x1","0x10","0x0","0x0"],"storage":{},"depth":1 }
277
- {"pc":12," op":50,"opName":"ORIGIN","gas":"0x9e19" ,"stack":["0x1","0x10","0x0","0x0","0x10"],"storage":{},"depth":1 }
278
- {"pc":13," op":52,"opName":"CALLVALUE","gas":"0x9e17" ,"stack":["0x1","0x10","0x0","0x0","0x10","0x0"],"storage":{},"depth":1 }
279
- {"pc":14," op":57,"opName":"CODECOPY","gas":"0x9e15" ,"stack":["0x1","0x10","0x0","0x0","0x10","0x0","0x0"],"storage":{},"depth":1 }
280
- {"pc":15," op":240,"opName":"CREATE","gas":"0x9e0c" ,"stack":["0x1","0x10","0x0","0x0"],"storage":{},"depth":1 }
281
- {"pc":0," op":50,"opName":"ORIGIN","gas":"0x210c" ,"stack":[],"storage":{},"depth":2 }
282
- {"pc":1," op":52,"opName":"CALLVALUE","gas":"0x210a" ,"stack":["0x0"],"storage":{},"depth":2 }
283
- {"pc ":2,"op":52,"opName":"CALLVALUE","gas":"0x2108" ,"stack":["0x0","0x0"],"storage":{},"depth":2 }
284
- {"pc":3," op":52,"opName":"CALLVALUE","gas":"0x2106" ,"stack":["0x0","0x0","0x0"],"storage":{},"depth":2 }
285
- {"pc":4," op":52,"opName":"CALLVALUE","gas":"0x2104" ,"stack":["0x0","0x0","0x0","0x0"],"storage":{},"depth":2 }
286
- {"pc":5," op":88,"opName":"PC","gas":"0x2102" ,"stack":["0x0","0x0","0x0","0x0","0x0"],"storage":{},"depth":2 }
287
- {"pc":6," op":48,"opName":"ADDRESS","gas":"0x2100" ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5"],"storage":{},"depth":2 }
288
- {"pc":7," op":241,"opName":"CALL","gas":"0x20fe" ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5","0xbd770416a3345f91e4b34576cb804a576fa48eb1"],"storage":{},"depth":2 }
248
+ r#"{"depth":1,"gas":"0xffff"," op":50,"opName":"ORIGIN","pc":0 ,"stack":[],"storage":{}}
249
+ {"depth ":1,"gas":"0xfffd"," op":52,"opName":"CALLVALUE","pc":1 ,"stack":["0x0"],"storage":{}}
250
+ {"depth":1,"gas":"0xfffb"," op":52,"opName":"CALLVALUE","pc":2 ,"stack":["0x0","0x0"],"storage":{}}
251
+ {"depth":1,"gas":"0xfff9"," op":52,"opName":"CALLVALUE","pc":3 ,"stack":["0x0","0x0","0x0"],"storage":{}}
252
+ {"depth":1,"gas":"0xfff7"," op":52,"opName":"CALLVALUE","pc":4 ,"stack":["0x0","0x0","0x0","0x0"],"storage":{}}
253
+ {"depth":1,"gas":"0xfff5"," op":88,"opName":"PC","pc":5 ,"stack":["0x0","0x0","0x0","0x0","0x0"],"storage":{}}
254
+ {"depth":1,"gas":"0xfff3"," op":48,"opName":"ADDRESS","pc":6 ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5"],"storage":{}}
255
+ {"depth":1,"gas":"0xfff1"," op":241,"opName":"CALL","pc":7 ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5","0x0"],"storage":{}}
256
+ {"depth":1,"gas":"0x9e21"," op":56,"opName":"CODESIZE","pc":8 ,"stack":["0x1"],"storage":{}}
257
+ {"depth":1,"gas":"0x9e1f"," op":52,"opName":"CALLVALUE","pc":9 ,"stack":["0x1","0x10"],"storage":{}}
258
+ {"depth":1,"gas":"0x9e1d"," op":52,"opName":"CALLVALUE","pc":10 ,"stack":["0x1","0x10","0x0"],"storage":{}}
259
+ {"depth":1,"gas":"0x9e1b"," op":56,"opName":"CODESIZE","pc":11 ,"stack":["0x1","0x10","0x0","0x0"],"storage":{}}
260
+ {"depth":1,"gas":"0x9e19"," op":50,"opName":"ORIGIN","pc":12 ,"stack":["0x1","0x10","0x0","0x0","0x10"],"storage":{}}
261
+ {"depth":1,"gas":"0x9e17"," op":52,"opName":"CALLVALUE","pc":13 ,"stack":["0x1","0x10","0x0","0x0","0x10","0x0"],"storage":{}}
262
+ {"depth":1,"gas":"0x9e15"," op":57,"opName":"CODECOPY","pc":14 ,"stack":["0x1","0x10","0x0","0x0","0x10","0x0","0x0"],"storage":{}}
263
+ {"depth":1,"gas":"0x9e0c"," op":240,"opName":"CREATE","pc":15 ,"stack":["0x1","0x10","0x0","0x0"],"storage":{}}
264
+ {"depth":2,"gas":"0x210c"," op":50,"opName":"ORIGIN","pc":0 ,"stack":[],"storage":{}}
265
+ {"depth":2,"gas":"0x210a"," op":52,"opName":"CALLVALUE","pc":1 ,"stack":["0x0"],"storage":{}}
266
+ {"depth ":2,"gas":"0x2108"," op":52,"opName":"CALLVALUE","pc":2 ,"stack":["0x0","0x0"],"storage":{}}
267
+ {"depth":2,"gas":"0x2106"," op":52,"opName":"CALLVALUE","pc":3 ,"stack":["0x0","0x0","0x0"],"storage":{}}
268
+ {"depth":2,"gas":"0x2104"," op":52,"opName":"CALLVALUE","pc":4 ,"stack":["0x0","0x0","0x0","0x0"],"storage":{}}
269
+ {"depth":2,"gas":"0x2102"," op":88,"opName":"PC","pc":5 ,"stack":["0x0","0x0","0x0","0x0","0x0"],"storage":{}}
270
+ {"depth":2,"gas":"0x2100"," op":48,"opName":"ADDRESS","pc":6 ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5"],"storage":{}}
271
+ {"depth":2,"gas":"0x20fe"," op":241,"opName":"CALL","pc":7 ,"stack":["0x0","0x0","0x0","0x0","0x0","0x5","0xbd770416a3345f91e4b34576cb804a576fa48eb1"],"storage":{}}
289
272
"# ,
290
273
)
291
274
}
0 commit comments