Skip to content

Commit

Permalink
Introduce tracing output to a file for t8n
Browse files Browse the repository at this point in the history
Co-authored-by: Paweł Bylica <pawel@ethereum.org>
  • Loading branch information
rodiazet and chfast committed Aug 8, 2023
1 parent 6d6f8ea commit 6c8fa22
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions test/t8n/t8n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main(int argc, const char* argv[])
fs::path output_body_file;
std::optional<uint64_t> block_reward;
uint64_t chain_id = 0;
bool tracing_enabled = false;

try
{
Expand Down Expand Up @@ -63,6 +64,8 @@ int main(int argc, const char* argv[])
chain_id = intx::from_string<uint64_t>(argv[i]);
else if (arg == "--output.body" && ++i < argc)
output_body_file = argv[i];
else if (arg == "--trace")
tracing_enabled = true;
}

state::BlockInfo block;
Expand Down Expand Up @@ -95,6 +98,9 @@ int main(int argc, const char* argv[])

evmc::VM vm{evmc_create_evmone(), {{"O", "0"}}};

if (tracing_enabled)
vm.set_option("trace", "0");

std::vector<state::Log> txs_logs;

if (j_txs.is_array())
Expand All @@ -107,8 +113,6 @@ int main(int argc, const char* argv[])
auto tx = test::from_json<state::Transaction>(j_txs[i]);
tx.chain_id = chain_id;

auto res = state::transition(state, block, tx, rev, vm);

const auto computed_tx_hash = keccak256(rlp::encode(tx));
const auto computed_tx_hash_str = hex0x(computed_tx_hash);

Expand All @@ -123,11 +127,27 @@ int main(int argc, const char* argv[])
hex0x(loaded_tx_hash_opt.value()));
}

std::ofstream trace_file_output;
const auto tmp_clog_buf = std::clog.rdbuf();
if (tracing_enabled)
{
auto output_filename =
fs::path(output_dir / ("trace-" + std::to_string(i) + "-" +
computed_tx_hash_str + ".jsonl"));

// `trace` flag enables trace logging to std::clog.
// Redirect std::clog to the output file.
trace_file_output.open(output_filename);
std::clog.rdbuf(trace_file_output.rdbuf());
}

auto res = state::transition(state, block, tx, rev, vm);

if (holds_alternative<std::error_code>(res))
{
const auto ec = std::get<std::error_code>(res);
json::json j_rejected_tx;
j_rejected_tx["hash"] = hex0x(computed_tx_hash);
j_rejected_tx["hash"] = computed_tx_hash_str;
j_rejected_tx["index"] = i;
j_rejected_tx["error"] = ec.message();
j_result["rejected"].push_back(j_rejected_tx);
Expand All @@ -141,7 +161,7 @@ int main(int argc, const char* argv[])
txs_logs.insert(txs_logs.end(), tx_logs.begin(), tx_logs.end());
auto& j_receipt = j_result["receipts"][j_result["receipts"].size()];

j_receipt["transactionHash"] = hex0x(computed_tx_hash);
j_receipt["transactionHash"] = computed_tx_hash_str;
j_receipt["gasUsed"] = hex0x(static_cast<uint64_t>(receipt.gas_used));
cumulative_gas_used += receipt.gas_used;
j_receipt["cumulativeGasUsed"] = hex0x(cumulative_gas_used);
Expand All @@ -156,6 +176,9 @@ int main(int argc, const char* argv[])
transactions.emplace_back(std::move(tx));
receipts.emplace_back(std::move(receipt));
}
// Set original std::clob buffer
if (tracing_enabled)
std::clog.rdbuf(tmp_clog_buf);
}
}

Expand Down

0 comments on commit 6c8fa22

Please sign in to comment.