Skip to content

Commit

Permalink
trace: Print gas and gasUsed in hex
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Mar 22, 2023
1 parent 60977de commit c6996ad
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
7 changes: 4 additions & 3 deletions lib/evmone/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class InstructionTracer : public Tracer
m_out << R"("pc":)" << pc;
m_out << R"(,"op":)" << int{opcode};
m_out << R"(,"opName":")" << get_name(opcode) << '"';
m_out << R"(,"gas":)" << state.gas_left;
m_out << R"(,"gas":)" << std::hex << "0x" << state.gas_left << std::dec;
output_stack(stack_top, stack_height);

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

Expand Down
12 changes: 6 additions & 6 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ DUP1,4
set_tests_properties(
${PREFIX}/trace PROPERTIES PASS_REGULAR_EXPRESSION
"{\"depth\":0,\"rev\":\"Paris\",\"static\":false}
{\"pc\":0,\"op\":96,\"opName\":\"PUSH1\",\"gas\":1000000,\"stack\":\\[\\],\"memorySize\":0}
{\"pc\":2,\"op\":96,\"opName\":\"PUSH1\",\"gas\":999997,\"stack\":\\[\"0x0\"\\],\"memorySize\":0}
{\"pc\":4,\"op\":128,\"opName\":\"DUP1\",\"gas\":999994,\"stack\":\\[\"0x0\",\"0x2\"\\],\"memorySize\":0}
{\"pc\":5,\"op\":1,\"opName\":\"ADD\",\"gas\":999991,\"stack\":\\[\"0x0\",\"0x2\",\"0x2\"\\],\"memorySize\":0}
{\"pc\":6,\"op\":3,\"opName\":\"SUB\",\"gas\":999988,\"stack\":\\[\"0x0\",\"0x4\"\\],\"memorySize\":0}
{\"error\":null,\"gas\":999985,\"gasUsed\":15,\"output\":\"\"}
{\"pc\":0,\"op\":96,\"opName\":\"PUSH1\",\"gas\":0xf4240,\"stack\":\\[\\],\"memorySize\":0}
{\"pc\":2,\"op\":96,\"opName\":\"PUSH1\",\"gas\":0xf423d,\"stack\":\\[\"0x0\"\\],\"memorySize\":0}
{\"pc\":4,\"op\":128,\"opName\":\"DUP1\",\"gas\":0xf423a,\"stack\":\\[\"0x0\",\"0x2\"\\],\"memorySize\":0}
{\"pc\":5,\"op\":1,\"opName\":\"ADD\",\"gas\":0xf4237,\"stack\":\\[\"0x0\",\"0x2\",\"0x2\"\\],\"memorySize\":0}
{\"pc\":6,\"op\":3,\"opName\":\"SUB\",\"gas\":0xf4234,\"stack\":\\[\"0x0\",\"0x4\"\\],\"memorySize\":0}
{\"error\":null,\"gas\":0xf4231,\"gasUsed\":0xf,\"output\":\"\"}
")

endif()
Expand Down
76 changes: 38 additions & 38 deletions test/unittests/tracing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ TEST_F(tracing, trace)
trace_stream << '\n';
EXPECT_EQ(trace(add(2, 3)), R"(
{"depth":0,"rev":"Berlin","static":false}
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x3"],"memorySize":0}
{"pc":4,"op":1,"opName":"ADD","gas":999994,"stack":["0x3","0x2"],"memorySize":0}
{"error":null,"gas":999991,"gasUsed":9,"output":""}
{"pc":0,"op":96,"opName":"PUSH1","gas":0xf4240,"stack":[],"memorySize":0}
{"pc":2,"op":96,"opName":"PUSH1","gas":0xf423d,"stack":["0x3"],"memorySize":0}
{"pc":4,"op":1,"opName":"ADD","gas":0xf423a,"stack":["0x3","0x2"],"memorySize":0}
{"error":null,"gas":0xf4237,"gasUsed":0x9,"output":""}
)");
}

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

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

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

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

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

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

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

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

0 comments on commit c6996ad

Please sign in to comment.