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

rpcdaemon: undefined opcode gas cost #2397

Merged
merged 6 commits into from
Oct 5, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/rpc-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ jobs:
run: |
rm -rf ${{runner.workspace}}/rpc-tests
if [ ${{ matrix.backend }} == 'Erigon2' ]; then
git -c advice.detachedHead=false clone --depth 1 --branch v0.52.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
git -c advice.detachedHead=false clone --depth 1 --branch v0.54.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
else
git -c advice.detachedHead=false clone --depth 1 --branch v0.52.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
git -c advice.detachedHead=false clone --depth 1 --branch v0.54.0 https://github.com/erigontech/rpc-tests ${{runner.workspace}}/rpc-tests
fi
cd ${{runner.workspace}}/rpc-tests
pip3 install -r requirements.txt
Expand Down
7 changes: 4 additions & 3 deletions silkworm/rpc/common/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ std::string get_opcode_hex(uint8_t opcode) {
return {'0', 'x', kHexDigits[opcode >> 4], kHexDigits[opcode & 0xf]};
}

std::string get_opcode_name(const char* const* names, std::uint8_t opcode) {
std::optional<std::string> get_opcode_name(const char* const* names, std::uint8_t opcode) {
SILKWORM_ASSERT(names != nullptr);

std::optional<std::string> op_name;
const auto name = names[opcode];
if (name != nullptr) {
return name;
op_name = name;
}
return "opcode " + get_opcode_hex(opcode) + " not defined";
return op_name;
}

} // namespace silkworm
2 changes: 1 addition & 1 deletion silkworm/rpc/common/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ inline evmc::bytes32 bytes32_from_hex(const std::string& s) {
std::ostream& operator<<(std::ostream& out, const Account& account);

std::string get_opcode_hex(uint8_t opcode);
std::string get_opcode_name(const char* const* names, std::uint8_t opcode);
std::optional<std::string> get_opcode_name(const char* const* names, std::uint8_t opcode);
} // namespace silkworm

inline auto hash_of(const silkworm::ByteView& bytes) {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/rpc/common/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ TEST_CASE("get_opcode_name") {
}
SECTION("not existent op_code") {
auto op_code_name = get_opcode_name(names, 0x0d);
CHECK(op_code_name == "opcode 0xd not defined");
CHECK(!op_code_name.has_value());
}
}

Expand Down
35 changes: 23 additions & 12 deletions silkworm/rpc/core/evm_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include <evmc/instructions.h>
#include <evmone/execution_state.hpp>
#include <evmone/instructions.hpp>
#include <intx/intx.hpp>

#include <silkworm/core/common/util.hpp>
Expand Down Expand Up @@ -105,6 +104,7 @@ void insert_error(DebugLog& log, evmc_status_code status_code) {
void DebugTracer::on_execution_start(evmc_revision rev, const evmc_message& msg, evmone::bytes_view code) noexcept {
last_opcode_ = std::nullopt;
if (opcode_names_ == nullptr) {
latest_opcode_names_ = evmc_get_instruction_names_table(EVMC_LATEST_STABLE_REVISION);
opcode_names_ = evmc_get_instruction_names_table(rev);
metrics_ = evmc_get_instruction_metrics_table(rev);
}
Expand Down Expand Up @@ -133,13 +133,16 @@ void DebugTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t
const evmc::address sender(execution_state.msg->sender);

const auto opcode = execution_state.original_code[pc];
const auto opcode_name = get_opcode_name(opcode_names_, opcode);
auto opcode_name = get_opcode_name(opcode_names_, opcode);
if (!opcode_name) {
opcode_name = get_opcode_name(latest_opcode_names_, opcode);
}
last_opcode_ = opcode;

SILK_DEBUG << "on_instruction_start:"
<< " pc: " << std::dec << pc
<< " opcode: 0x" << std::hex << evmc::hex(opcode)
<< " opcode_name: " << opcode_name
<< " opcode_name: " << opcode_name.value_or("UNDEFINED")
<< " recipient: " << recipient
<< " sender: " << sender
<< " execution_state: {"
Expand Down Expand Up @@ -167,7 +170,7 @@ void DebugTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t
if (!logs_.empty()) {
auto& log = logs_[logs_.size() - 1];

if (log.opcode == OP_RETURN || log.opcode == OP_STOP || log.opcode == OP_REVERT) {
if (log.op_code == OP_RETURN || log.op_code == OP_STOP || log.op_code == OP_REVERT) {
log.gas_cost = 0;
} else if (log.depth == execution_state.msg->depth + 1) {
log.gas_cost = execution_state.last_opcode_gas_cost;
Expand All @@ -182,8 +185,8 @@ void DebugTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t

DebugLog log;
log.pc = pc;
log.opcode = opcode;
log.op = opcode_name;
log.op_code = opcode;
log.op_name = opcode_name;
log.gas = gas;
log.gas_cost = metrics_[opcode].gas_cost;
log.depth = execution_state.msg->depth + 1;
Expand Down Expand Up @@ -226,17 +229,21 @@ void DebugTracer::on_execution_end(const evmc_result& result, const silkworm::In
case evmc_status_code::EVMC_INVALID_INSTRUCTION:
case evmc_status_code::EVMC_STACK_OVERFLOW:
case evmc_status_code::EVMC_STACK_UNDERFLOW:
log.gas_cost = 0;
if (log.op_name) {
log.gas_cost = result.gas_cost;
} else {
log.gas_cost = 0;
}
break;

case evmc_status_code::EVMC_OUT_OF_GAS:
if (log.opcode != OP_CALLCODE) {
if (log.op_code != OP_CALLCODE) {
log.gas_cost = result.gas_cost;
}
break;

default:
if (log.opcode == OP_CALL || log.opcode == OP_CALLCODE || log.opcode == OP_STATICCALL || log.opcode == OP_DELEGATECALL || log.opcode == OP_CREATE || log.opcode == OP_CREATE2) {
if (log.op_code == OP_CALL || log.op_code == OP_CALLCODE || log.op_code == OP_STATICCALL || log.op_code == OP_DELEGATECALL || log.op_code == OP_CREATE || log.op_code == OP_CREATE2) {
log.gas_cost += result.gas_cost;
} else {
log.gas_cost = log.gas_cost;
Expand All @@ -248,8 +255,8 @@ void DebugTracer::on_execution_end(const evmc_result& result, const silkworm::In
if (result.status_code == EVMC_SUCCESS && last_opcode_ && last_opcode_ != OP_SELFDESTRUCT && last_opcode_ != OP_RETURN && last_opcode_ != OP_STOP) {
DebugLog newlog;
newlog.pc = log.pc + 1;
newlog.op = get_opcode_name(opcode_names_, OP_STOP);
newlog.opcode = OP_STOP;
newlog.op_name = get_opcode_name(opcode_names_, OP_STOP);
newlog.op_code = OP_STOP;
newlog.gas = log.gas - log.gas_cost;
newlog.gas_cost = 0;
newlog.depth = log.depth;
Expand Down Expand Up @@ -287,7 +294,11 @@ void DebugTracer::write_log(const DebugLog& log) {
stream_.write_field("depth", log.depth);
stream_.write_field("gas", log.gas);
stream_.write_field("gasCost", log.gas_cost);
stream_.write_field("op", log.op);
if (log.op_name) {
stream_.write_field("op", log.op_name.value());
} else {
stream_.write_field("op", "opcode " + get_opcode_hex(log.op_code) + " not defined");
}
stream_.write_field("pc", log.pc);

if (!config_.disable_stack) {
Expand Down
5 changes: 3 additions & 2 deletions silkworm/rpc/core/evm_debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ using Storage = std::map<std::string, std::string>;

struct DebugLog {
std::uint32_t pc{0};
unsigned char opcode{0};
std::string op;
unsigned char op_code{0};
std::optional<std::string> op_name;
std::int64_t gas{0};
std::int64_t gas_cost{0};
std::int32_t depth{0};
Expand Down Expand Up @@ -98,6 +98,7 @@ class DebugTracer : public EvmTracer {
std::vector<DebugLog> logs_;
std::map<evmc::address, Storage> storage_;
const char* const* opcode_names_ = nullptr;
const char* const* latest_opcode_names_ = nullptr;
const evmc_instruction_metrics* metrics_ = nullptr;
std::optional<uint8_t> last_opcode_;
};
Expand Down
8 changes: 0 additions & 8 deletions silkworm/rpc/core/evm_debug_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,6 @@ TEST_CASE_METHOD(DebugExecutorTest, "DebugExecutor::execute call 1") {
.WillRepeatedly(InvokeWithoutArgs([]() -> Task<KeyValue> {
co_return KeyValue{kAccountHistoryKey1, kAccountHistoryValue1};
}));
EXPECT_CALL(transaction, get(db::table::kAccountHistoryName, silkworm::ByteView{kAccountHistoryKey3}))
.WillOnce(InvokeWithoutArgs([]() -> Task<KeyValue> {
co_return KeyValue{kAccountHistoryKey3, kAccountHistoryValue3};
}));
EXPECT_CALL(transaction, get(db::table::kAccountHistoryName, silkworm::ByteView{kAccountHistoryKey2}))
.WillRepeatedly(InvokeWithoutArgs([]() -> Task<KeyValue> {
co_return KeyValue{kAccountHistoryKey2, kAccountHistoryValue2};
Expand All @@ -282,10 +278,6 @@ TEST_CASE_METHOD(DebugExecutorTest, "DebugExecutor::execute call 1") {
.WillRepeatedly(InvokeWithoutArgs([]() -> Task<Bytes> {
co_return Bytes{};
}));
EXPECT_CALL(transaction, get_both_range(db::table::kAccountChangeSetName, silkworm::ByteView{kAccountChangeSetKey1}, silkworm::ByteView{kAccountChangeSetSubkey1}))
.WillOnce(InvokeWithoutArgs([]() -> Task<std::optional<Bytes>> {
co_return kAccountChangeSetValue1;
}));

const auto block_number = 5'405'095; // 0x5279A7
Call call;
Expand Down
13 changes: 8 additions & 5 deletions silkworm/rpc/core/evm_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "evm_trace.hpp"

#include <algorithm>
#include <memory>
#include <set>
#include <stack>
#include <string>
Expand Down Expand Up @@ -125,7 +124,11 @@ void to_json(nlohmann::json& json, const TraceOp& trace_op) {
json["ex"] = *(trace_op.trace_ex);
}
json["idx"] = trace_op.idx;
json["op"] = trace_op.op_name;
if (trace_op.op_name) {
json["op"] = trace_op.op_name.value();
} else {
json["op"] = "opcode " + get_opcode_hex(trace_op.op_code) + " not defined";
}
json["pc"] = trace_op.pc;
if (trace_op.sub) {
json["sub"] = *trace_op.sub;
Expand Down Expand Up @@ -670,7 +673,7 @@ void VmTraceTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack
SILK_DEBUG << "VmTraceTracer::on_instruction_start:"
<< " pc: " << std::dec << pc
<< ", opcode: 0x" << std::hex << evmc::hex(op_code)
<< ", opcode_name: " << op_name
<< ", opcode_name: " << op_name.value_or("UNDEFINED")
<< ", index_prefix: " << index_prefix
<< ", execution_state: {"
<< " gas_left: " << std::dec << gas
Expand Down Expand Up @@ -915,7 +918,7 @@ void TraceTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t
SILK_DEBUG << "TraceTracer::on_instruction_start:"
<< " pc: " << std::dec << pc
<< ", opcode: 0x" << std::hex << evmc::hex(opcode)
<< ", opcode_name: " << opcode_name
<< ", opcode_name: " << opcode_name.value_or("UNDEFINED")
<< ", recipient: " << evmc::address{execution_state.msg->recipient}
<< ", sender: " << evmc::address{execution_state.msg->sender}
<< ", execution_state: {"
Expand Down Expand Up @@ -1138,7 +1141,7 @@ void StateDiffTracer::on_instruction_start(uint32_t pc, const intx::uint256* sta

SILK_DEBUG << "StateDiffTracer::on_instruction_start:"
<< " pc: " << std::dec << pc
<< ", opcode_name: " << opcode_name
<< ", opcode_name: " << opcode_name.value_or("UNDEFINED")
<< ", recipient: " << evmc::address{execution_state.msg->recipient}
<< ", sender: " << evmc::address{execution_state.msg->sender}
<< ", execution_state: {"
Expand Down
2 changes: 1 addition & 1 deletion silkworm/rpc/core/evm_trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct TraceOp {
std::string idx;
int32_t depth{0};
uint8_t op_code{0};
std::string op_name;
std::optional<std::string> op_name;
uint32_t pc{0};
std::shared_ptr<VmTrace> sub;
};
Expand Down
Loading