Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: Print gas and gasUsed in hex #592

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/evmone/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ class InstructionTracer : public Tracer

const auto opcode = ctx.code[pc];
m_out << "{";
m_out << R"("pc":)" << pc;
m_out << R"(,"op":)" << int{opcode};
m_out << R"("pc":)" << std::dec << pc;
m_out << R"(,"op":)" << std::dec << 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;
output_stack(stack_top, stack_height);

// Full memory can be dumped as evmc::hex({state.memory.data(), state.memory.size()}),
// but this should not be done by default. Adding --tracing=+memory option would be nice.
m_out << R"(,"memorySize":)" << state.memory.size();
m_out << R"(,"memorySize":)" << std::dec << state.memory.size();

m_out << "}\n";
}
Expand All @@ -135,8 +135,8 @@ 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);
m_out << R"(,"output":")" << evmc::hex({result.output_data, result.output_size}) << '"';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this field is not an even listed as optional in the EIP, @chfast.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it and check once again if we have it implemented according to spec, but let's first confirm with @chfast

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is useful for debugging. Let's keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chfast Approve please if no other comments.

Copy link
Member

@axic axic Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can one of you open a PR to the EIP adding output as an optional field? Not blocking this PR, just as an addition.

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
2 changes: 1 addition & 1 deletion test/integration/statetest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ add_test(
)
set_tests_properties(
${PREFIX}/trace PROPERTIES
PASS_REGULAR_EXPRESSION [=[\{"pc":4,"op":1,"opName":"ADD","gas":378994,"stack":\["0x1","0x1"\],"memorySize":0\}]=]
PASS_REGULAR_EXPRESSION [=[\{"pc":4,"op":1,"opName":"ADD","gas":0x5c872,"stack":\["0x1","0x1"\],"memorySize":0\}]=]
)
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":""}
)");
}