Skip to content

Commit 021f51c

Browse files
committed
trace: Print gas and gasUsed in hex
1 parent 60977de commit 021f51c

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

lib/evmone/tracing.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class InstructionTracer : public Tracer
115115
m_out << R"("pc":)" << pc;
116116
m_out << R"(,"op":)" << int{opcode};
117117
m_out << R"(,"opName":")" << get_name(opcode) << '"';
118-
m_out << R"(,"gas":)" << state.gas_left;
118+
m_out << R"(,"gas":)" << std::hex << "0x" << state.gas_left << std::dec;
119119
output_stack(stack_top, stack_height);
120120

121121
// Full memory can be dumped as evmc::hex({state.memory.data(), state.memory.size()}),
@@ -135,8 +135,9 @@ class InstructionTracer : public Tracer
135135
m_out << "null";
136136
else
137137
m_out << '"' << result.status_code << '"';
138-
m_out << R"(,"gas":)" << result.gas_left;
139-
m_out << R"(,"gasUsed":)" << (ctx.start_gas - result.gas_left);
138+
m_out << R"(,"gas":)" << std::hex << "0x" << result.gas_left;
139+
m_out << R"(,"gasUsed":)" << std::hex << "0x" << (ctx.start_gas - result.gas_left)
140+
<< std::dec;
140141
m_out << R"(,"output":")" << evmc::hex({result.output_data, result.output_size}) << '"';
141142
m_out << "}\n";
142143

test/unittests/tracing_test.cpp

+38-38
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ TEST_F(tracing, trace)
168168
trace_stream << '\n';
169169
EXPECT_EQ(trace(add(2, 3)), R"(
170170
{"depth":0,"rev":"Berlin","static":false}
171-
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
172-
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x3"],"memorySize":0}
173-
{"pc":4,"op":1,"opName":"ADD","gas":999994,"stack":["0x3","0x2"],"memorySize":0}
174-
{"error":null,"gas":999991,"gasUsed":9,"output":""}
171+
{"pc":0,"op":96,"opName":"PUSH1","gas":0xf4240,"stack":[],"memorySize":0}
172+
{"pc":2,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0x3"],"memorySize":0}
173+
{"pc":4,"op":1,"opName":"ADD","gas":0xf423a,"stack":["0x3","0x2"],"memorySize":0}
174+
{"error":null,"gas":0xf4237,"gasUsed":0x9,"output":""}
175175
)");
176176
}
177177

@@ -183,14 +183,14 @@ TEST_F(tracing, trace_stack)
183183
trace_stream << '\n';
184184
EXPECT_EQ(trace(code), R"(
185185
{"depth":0,"rev":"Berlin","static":false}
186-
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
187-
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x1"],"memorySize":0}
188-
{"pc":4,"op":96,"opName":"PUSH1","gas":999994,"stack":["0x1","0x2"],"memorySize":0}
189-
{"pc":6,"op":96,"opName":"PUSH1","gas":999991,"stack":["0x1","0x2","0x3"],"memorySize":0}
190-
{"pc":8,"op":1,"opName":"ADD","gas":999988,"stack":["0x1","0x2","0x3","0x4"],"memorySize":0}
191-
{"pc":9,"op":1,"opName":"ADD","gas":999985,"stack":["0x1","0x2","0x7"],"memorySize":0}
192-
{"pc":10,"op":1,"opName":"ADD","gas":999982,"stack":["0x1","0x9"],"memorySize":0}
193-
{"error":null,"gas":999979,"gasUsed":21,"output":""}
186+
{"pc":0,"op":96,"opName":"PUSH1","gas":0xf4240,"stack":[],"memorySize":0}
187+
{"pc":2,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0x1"],"memorySize":0}
188+
{"pc":4,"op":96,"opName":"PUSH1","gas":0xf423a,"stack":["0x1","0x2"],"memorySize":0}
189+
{"pc":6,"op":96,"opName":"PUSH1","gas":0xf4237,"stack":["0x1","0x2","0x3"],"memorySize":0}
190+
{"pc":8,"op":1,"opName":"ADD","gas":0xf4234,"stack":["0x1","0x2","0x3","0x4"],"memorySize":0}
191+
{"pc":9,"op":1,"opName":"ADD","gas":0xf4231,"stack":["0x1","0x2","0x7"],"memorySize":0}
192+
{"pc":10,"op":1,"opName":"ADD","gas":0xf422e,"stack":["0x1","0x9"],"memorySize":0}
193+
{"error":null,"gas":0xf422b,"gasUsed":0x15,"output":""}
194194
)");
195195
}
196196

@@ -202,8 +202,8 @@ TEST_F(tracing, trace_error)
202202
trace_stream << '\n';
203203
EXPECT_EQ(trace(code), R"(
204204
{"depth":0,"rev":"Berlin","static":false}
205-
{"pc":0,"op":80,"opName":"POP","gas":1000000,"stack":[],"memorySize":0}
206-
{"error":"stack underflow","gas":0,"gasUsed":1000000,"output":""}
205+
{"pc":0,"op":80,"opName":"POP","gas":0xf4240,"stack":[],"memorySize":0}
206+
{"error":"stack underflow","gas":0x0,"gasUsed":0xf4240,"output":""}
207207
)");
208208
}
209209

@@ -215,13 +215,13 @@ TEST_F(tracing, trace_output)
215215
trace_stream << '\n';
216216
EXPECT_EQ(trace(code), R"(
217217
{"depth":0,"rev":"Berlin","static":false}
218-
{"pc":0,"op":98,"opName":"PUSH3","gas":1000000,"stack":[],"memorySize":0}
219-
{"pc":4,"op":96,"opName":"PUSH1","gas":999997,"stack":["0xabcdef"],"memorySize":0}
220-
{"pc":6,"op":82,"opName":"MSTORE","gas":999994,"stack":["0xabcdef","0x0"],"memorySize":0}
221-
{"pc":7,"op":96,"opName":"PUSH1","gas":999988,"stack":[],"memorySize":32}
222-
{"pc":9,"op":96,"opName":"PUSH1","gas":999985,"stack":["0x20"],"memorySize":32}
223-
{"pc":11,"op":243,"opName":"RETURN","gas":999982,"stack":["0x20","0x0"],"memorySize":32}
224-
{"error":null,"gas":999982,"gasUsed":18,"output":"0000000000000000000000000000000000000000000000000000000000abcdef"}
218+
{"pc":0,"op":98,"opName":"PUSH3","gas":0xf4240,"stack":[],"memorySize":0}
219+
{"pc":4,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0xabcdef"],"memorySize":0}
220+
{"pc":6,"op":82,"opName":"MSTORE","gas":0xf423a,"stack":["0xabcdef","0x0"],"memorySize":0}
221+
{"pc":7,"op":96,"opName":"PUSH1","gas":0xf4234,"stack":[],"memorySize":32}
222+
{"pc":9,"op":96,"opName":"PUSH1","gas":0xf4231,"stack":["0x20"],"memorySize":32}
223+
{"pc":11,"op":243,"opName":"RETURN","gas":0xf422e,"stack":["0x20","0x0"],"memorySize":32}
224+
{"error":null,"gas":0xf422e,"gasUsed":0x12,"output":"0000000000000000000000000000000000000000000000000000000000abcdef"}
225225
)");
226226
}
227227

@@ -233,13 +233,13 @@ TEST_F(tracing, trace_revert)
233233
trace_stream << '\n';
234234
EXPECT_EQ(trace(code), R"(
235235
{"depth":0,"rev":"Berlin","static":false}
236-
{"pc":0,"op":98,"opName":"PUSH3","gas":1000000,"stack":[],"memorySize":0}
237-
{"pc":4,"op":96,"opName":"PUSH1","gas":999997,"stack":["0xe4404"],"memorySize":0}
238-
{"pc":6,"op":82,"opName":"MSTORE","gas":999994,"stack":["0xe4404","0x0"],"memorySize":0}
239-
{"pc":7,"op":96,"opName":"PUSH1","gas":999988,"stack":[],"memorySize":32}
240-
{"pc":9,"op":96,"opName":"PUSH1","gas":999985,"stack":["0x3"],"memorySize":32}
241-
{"pc":11,"op":253,"opName":"REVERT","gas":999982,"stack":["0x3","0x1d"],"memorySize":32}
242-
{"error":"revert","gas":999982,"gasUsed":18,"output":"0e4404"}
236+
{"pc":0,"op":98,"opName":"PUSH3","gas":0xf4240,"stack":[],"memorySize":0}
237+
{"pc":4,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0xe4404"],"memorySize":0}
238+
{"pc":6,"op":82,"opName":"MSTORE","gas":0xf423a,"stack":["0xe4404","0x0"],"memorySize":0}
239+
{"pc":7,"op":96,"opName":"PUSH1","gas":0xf4234,"stack":[],"memorySize":32}
240+
{"pc":9,"op":96,"opName":"PUSH1","gas":0xf4231,"stack":["0x3"],"memorySize":32}
241+
{"pc":11,"op":253,"opName":"REVERT","gas":0xf422e,"stack":["0x3","0x1d"],"memorySize":32}
242+
{"error":"revert","gas":0xf422e,"gasUsed":0x12,"output":"0e4404"}
243243
)");
244244
}
245245

@@ -250,7 +250,7 @@ TEST_F(tracing, trace_create)
250250
trace_stream << '\n';
251251
EXPECT_EQ(trace({}, 2), R"(
252252
{"depth":2,"rev":"Berlin","static":false}
253-
{"error":null,"gas":1000000,"gasUsed":0,"output":""}
253+
{"error":null,"gas":0xf4240,"gasUsed":0x0,"output":""}
254254
)");
255255
}
256256

@@ -261,7 +261,7 @@ TEST_F(tracing, trace_static)
261261
trace_stream << '\n';
262262
EXPECT_EQ(trace({}, 2, EVMC_STATIC), R"(
263263
{"depth":2,"rev":"Berlin","static":true}
264-
{"error":null,"gas":1000000,"gasUsed":0,"output":""}
264+
{"error":null,"gas":0xf4240,"gasUsed":0x0,"output":""}
265265
)");
266266
}
267267

@@ -273,9 +273,9 @@ TEST_F(tracing, trace_undefined_instruction)
273273
trace_stream << '\n';
274274
EXPECT_EQ(trace(code), R"(
275275
{"depth":0,"rev":"Berlin","static":false}
276-
{"pc":0,"op":91,"opName":"JUMPDEST","gas":1000000,"stack":[],"memorySize":0}
277-
{"pc":1,"op":239,"opName":"0xef","gas":999999,"stack":[],"memorySize":0}
278-
{"error":"undefined instruction","gas":0,"gasUsed":1000000,"output":""}
276+
{"pc":0,"op":91,"opName":"JUMPDEST","gas":0xf4240,"stack":[],"memorySize":0}
277+
{"pc":1,"op":239,"opName":"0xef","gas":0xf423f,"stack":[],"memorySize":0}
278+
{"error":"undefined instruction","gas":0x0,"gasUsed":0xf4240,"output":""}
279279
)");
280280
}
281281

@@ -299,10 +299,10 @@ TEST_F(tracing, trace_eof)
299299
trace_stream << '\n';
300300
EXPECT_EQ(trace(eof1_bytecode(add(2, 3) + OP_STOP, 2), 0, 0, EVMC_CANCUN), R"(
301301
{"depth":0,"rev":"Cancun","static":false}
302-
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
303-
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x3"],"memorySize":0}
304-
{"pc":4,"op":1,"opName":"ADD","gas":999994,"stack":["0x3","0x2"],"memorySize":0}
305-
{"pc":5,"op":0,"opName":"STOP","gas":999991,"stack":["0x5"],"memorySize":0}
306-
{"error":null,"gas":999991,"gasUsed":9,"output":""}
302+
{"pc":0,"op":96,"opName":"PUSH1","gas":0xf4240,"stack":[],"memorySize":0}
303+
{"pc":2,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0x3"],"memorySize":0}
304+
{"pc":4,"op":1,"opName":"ADD","gas":0xf423a,"stack":["0x3","0x2"],"memorySize":0}
305+
{"pc":5,"op":0,"opName":"STOP","gas":0xf4237,"stack":["0x5"],"memorySize":0}
306+
{"error":null,"gas":0xf4237,"gasUsed":0x9,"output":""}
307307
)");
308308
}

0 commit comments

Comments
 (0)