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: DebugTracer code refactoring and cleanup #2341

Merged
merged 1 commit into from
Sep 11, 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
144 changes: 11 additions & 133 deletions silkworm/rpc/core/evm_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ void DebugTracer::on_execution_start(evmc_revision rev, const evmc_message& msg,
opcode_names_ = evmc_get_instruction_names_table(rev);
metrics_ = evmc_get_instruction_metrics_table(rev);
}
start_gas_.push(msg.gas);

const evmc::address recipient(msg.recipient);
const evmc::address sender(msg.sender);

if (!logs_.empty()) {
auto& log = logs_[logs_.size() - 1]; // it should be a CALL* opcode
log.gas_cost_check = msg.gas_cost;
log.gas_cost = msg.gas_cost;
}

SILK_DEBUG << "on_execution_start:"
Expand Down Expand Up @@ -165,37 +164,13 @@ void DebugTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t
}
}

if (!logs_.empty()) {
auto& log = logs_[logs_.size() - 1];
if (fix_call_gas_info_) { // previuos opcode was a CALL*
if (execution_state.msg->depth == fix_call_gas_info_->depth) {
if (fix_call_gas_info_->gas_cost) {
log.gas_cost = fix_call_gas_info_->gas_cost + fix_call_gas_info_->code_cost;
}
} else {
if (fix_call_gas_info_->opcode == OP_CALLCODE) {
log.gas_cost += fix_call_gas_info_->stipend + fix_call_gas_info_->gas_cost + fix_call_gas_info_->call_gas;
} else {
log.gas_cost = gas + fix_call_gas_info_->stipend + fix_call_gas_info_->code_cost;
}
}

fix_call_gas_info_.reset();
} else {
const auto depth = log.depth;
if (depth == execution_state.msg->depth + 1 || depth == execution_state.msg->depth) {
log.gas_cost = log.gas - gas;
}
}
}

if (!logs_.empty()) {
auto& log = logs_[logs_.size() - 1];

if (log.opcode == OP_RETURN || log.opcode == OP_STOP || log.opcode == OP_REVERT) {
log.gas_cost_check = 0;
log.gas_cost = 0;
} else if (log.depth == execution_state.msg->depth + 1) {
log.gas_cost_check = execution_state.last_opcode_gas_cost;
log.gas_cost = execution_state.last_opcode_gas_cost;
}
}

Expand All @@ -205,8 +180,6 @@ void DebugTracer::on_instruction_start(uint32_t pc, const intx::uint256* stack_t
logs_.erase(logs_.begin());
}

fill_call_gas_info(opcode, execution_state, stack_top, stack_height, intra_block_state);

DebugLog log;
log.pc = pc;
log.opcode = opcode;
Expand Down Expand Up @@ -237,20 +210,12 @@ void DebugTracer::on_precompiled_run(const evmc_result& result, int64_t gas, con
<< " status: " << result.status_code
<< ", gas: " << std::dec << gas;

if (fix_call_gas_info_) {
fix_call_gas_info_->gas_cost += gas + fix_call_gas_info_->code_cost;
fix_call_gas_info_->code_cost = 0;
fix_call_gas_info_->precompiled = true;
}
if (logs_.size() > 1) {
flush_logs();
}
}

void DebugTracer::on_execution_end(const evmc_result& result, const silkworm::IntraBlockState& /*intra_block_state*/) noexcept {
auto start_gas = start_gas_.top();
start_gas_.pop();

if (!logs_.empty()) {
auto& log = logs_[logs_.size() - 1];

Expand All @@ -261,41 +226,20 @@ 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;
log.gas_cost_check = result.gas_cost;
log.gas_cost = result.gas_cost;
break;

case evmc_status_code::EVMC_OUT_OF_GAS:
if (fix_call_gas_info_) {
if (fix_call_gas_info_->opcode == OP_CALLCODE) {
log.gas_cost = fix_call_gas_info_->code_cost;
} else {
log.gas_cost += fix_call_gas_info_->gas_cost;
}
}
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) {
log.gas_cost_check = log.gas_cost;
} else {
log.gas_cost_check = result.gas_cost;
if (log.opcode != OP_CALLCODE) {
log.gas_cost = result.gas_cost;
}
break;

default:
if (fix_call_gas_info_) {
if (result.gas_left == 0 && !fix_call_gas_info_->precompiled) {
log.gas_cost = fix_call_gas_info_->stipend + fix_call_gas_info_->gas_cost;
} else if (!fix_call_gas_info_->precompiled) {
log.gas_cost = result.gas_left + fix_call_gas_info_->gas_cost + fix_call_gas_info_->code_cost - fix_call_gas_info_->stipend;
fix_call_gas_info_->gas_cost = 0;
} else if (fix_call_gas_info_->precompiled) {
log.gas_cost = fix_call_gas_info_->gas_cost;
fix_call_gas_info_->gas_cost = 0;
}
}
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) {
log.gas_cost_check += result.gas_cost;
log.gas_cost += result.gas_cost;
} else {
log.gas_cost_check = log.gas_cost;
log.gas_cost = log.gas_cost;
}
break;
}
Expand All @@ -320,8 +264,8 @@ void DebugTracer::on_execution_end(const evmc_result& result, const silkworm::In

SILK_DEBUG << "on_execution_end:"
<< " result.status_code: " << result.status_code
<< " start_gas: " << std::dec << start_gas
<< " gas_left: " << std::dec << result.gas_left;
<< " gas_left: " << std::dec << result.gas_left
<< " gas_cost: " << std::dec << result.gas_cost;
}

void DebugTracer::flush_logs() {
Expand All @@ -331,71 +275,6 @@ void DebugTracer::flush_logs() {
logs_.clear();
}

int64_t memory_cost(const evmone::Memory& memory, std::uint64_t offset, std::uint64_t size) noexcept {
if (size == 0) {
return 0;
}
const auto new_size = offset + size;
if (new_size <= memory.size()) {
return 0;
}

const auto new_words = evmone::num_words(new_size);
const auto current_words = static_cast<int64_t>(memory.size() / evmone::word_size);
const auto new_cost = 3 * new_words + new_words * new_words / 512;
const auto current_cost = 3 * current_words + current_words * current_words / 512;
const auto cost = new_cost - current_cost;

return cost;
}

void DebugTracer::fill_call_gas_info(unsigned char opcode, const evmone::ExecutionState& execution_state, const intx::uint256* stack_top, const int stack_height, const silkworm::IntraBlockState& intra_block_state) {
if (opcode != OP_CALL && opcode != OP_CALLCODE && opcode != OP_STATICCALL && opcode != OP_DELEGATECALL && opcode != OP_CREATE && opcode != OP_CREATE2) {
return;
}
fix_call_gas_info_.emplace(FixCallGasInfo{opcode, execution_state.msg->depth, 0, metrics_[opcode].gas_cost});

auto idx = 0;
const auto call_gas = stack_top[idx--]; // gas
const auto dst = intx::be::trunc<evmc::address>(stack_top[idx--]);
const auto value = (opcode == OP_STATICCALL || opcode == OP_DELEGATECALL) ? 0 : stack_top[idx--];
const auto input_offset = static_cast<std::uint64_t>(stack_top[idx--]);
const auto input_size = static_cast<std::uint64_t>(stack_top[idx--]);
const auto output_offset = static_cast<std::uint64_t>(stack_top[idx--]);
const auto output_size = static_cast<std::uint64_t>(stack_top[idx--]);

SILK_DEBUG << "DebugTracer::evaluate_call_fixes:"
<< " gas: " << std::dec << call_gas
<< ", input_offset: " << std::dec << input_offset
<< ", input_size: " << std::dec << input_size
<< ", output_offset: " << std::dec << output_offset
<< ", output_size: " << std::dec << output_size;

if (call_gas < std::numeric_limits<int64_t>::max()) {
fix_call_gas_info_->call_gas = static_cast<std::int64_t>(call_gas);
}
fix_call_gas_info_->gas_cost += memory_cost(execution_state.memory, input_offset, input_size);
fix_call_gas_info_->gas_cost += memory_cost(execution_state.memory, output_offset, output_size);

if (value != 0) {
fix_call_gas_info_->gas_cost += 9000;
}
if (opcode == OP_CALL) {
if (opcode == OP_CALL && stack_height >= 7 && value != 0) {
fix_call_gas_info_->stipend = 2300; // for CALLs with value, include stipend
}
if ((value != 0 || execution_state.rev < EVMC_SPURIOUS_DRAGON) && !intra_block_state.exists(dst)) {
fix_call_gas_info_->gas_cost += 25000; // add ACCOUNT_CREATION_COST as in instructions_calls.cpp:105
}
SILK_DEBUG << "DebugTracer::evaluate_call_fixes:"
<< " call_gas: " << call_gas
<< " dst: " << dst
<< " value: " << value
<< " gas_cost: " << fix_call_gas_info_->gas_cost
<< " stipend: " << fix_call_gas_info_->stipend;
}
}

void AccountTracer::on_execution_end(const evmc_result& /*result*/, const silkworm::IntraBlockState& intra_block_state) noexcept {
nonce = intra_block_state.get_nonce(address_);
balance = intra_block_state.get_balance(address_);
Expand All @@ -407,8 +286,7 @@ void DebugTracer::write_log(const DebugLog& log) {
stream_.open_object();
stream_.write_field("depth", log.depth);
stream_.write_field("gas", log.gas);
// stream_.write_field("gasCost", log.gas_cost);
stream_.write_field("gasCost", log.gas_cost_check);
stream_.write_field("gasCost", log.gas_cost);
stream_.write_field("op", log.op);
stream_.write_field("pc", log.pc);

Expand Down
19 changes: 0 additions & 19 deletions silkworm/rpc/core/evm_debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,13 @@ struct DebugLog {
std::string op;
std::int64_t gas{0};
std::int64_t gas_cost{0};
std::int64_t gas_cost_check{0};
std::int32_t depth{0};
bool error{false};
std::vector<std::string> memory;
std::vector<std::string> stack;
Storage storage;
};

struct FixCallGasInfo {
unsigned char opcode;
int32_t depth{0};
int64_t stipend{0};
int16_t code_cost{0};
int64_t call_gas{0};
int64_t gas_cost{0};
bool precompiled{false};
};

class DebugTracer : public EvmTracer {
public:
explicit DebugTracer(json::Stream& stream, const DebugConfig& config = {})
Expand All @@ -103,21 +92,13 @@ class DebugTracer : public EvmTracer {

private:
void write_log(const DebugLog& log);
void fill_call_gas_info(unsigned char opcode,
const evmone::ExecutionState& execution_state,
const intx::uint256* stack_top,
int stack_height,
const silkworm::IntraBlockState& intra_block_state);

json::Stream& stream_;
const DebugConfig& config_;
std::vector<DebugLog> logs_;
std::map<evmc::address, Storage> storage_;
const char* const* opcode_names_ = nullptr;
const evmc_instruction_metrics* metrics_ = nullptr;
std::stack<std::int64_t> start_gas_;
// std::int64_t gas_cost_;
std::optional<FixCallGasInfo> fix_call_gas_info_;
std::optional<uint8_t> last_opcode_;
};

Expand Down
Loading