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 10, 2023
1 parent e6ee583 commit e3a6c70
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 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,6 +127,22 @@ int main(int argc, const char* argv[])
hex0x(loaded_tx_hash_opt.value()));
}

std::ofstream trace_file_output;
const auto orig_clog_buf = std::clog.rdbuf();
if (tracing_enabled)
{
const auto output_filename =
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);
Expand Down Expand Up @@ -156,6 +176,10 @@ int main(int argc, const char* argv[])
transactions.emplace_back(std::move(tx));
receipts.emplace_back(std::move(receipt));
}
// Set original std::clog buffer
// Have to do it. Otherwise, it's crashing
if (tracing_enabled)
std::clog.rdbuf(orig_clog_buf);
}
}

Expand Down

0 comments on commit e3a6c70

Please sign in to comment.