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

t8n: Add tx hash calculation for tx receipt #590

Merged
merged 1 commit into from
Mar 22, 2023
Merged
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
24 changes: 20 additions & 4 deletions test/t8n/t8n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,25 @@ int main(int argc, const char* argv[])
tx.chain_id = chain_id;

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

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

if (j_txs[i].contains("hash"))
{
const auto loaded_tx_hash_opt =
evmc::from_hex<bytes32>(j_txs[i]["hash"].get<std::string>());

if (loaded_tx_hash_opt != computed_tx_hash)
throw std::logic_error("transaction hash mismatched: computed " +
hex0x(computed_tx_hash) + ", expected " +
hex0x(loaded_tx_hash_opt.value()));
Copy link
Member

@axic axic Mar 22, 2023

Choose a reason for hiding this comment

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

Could have used this here, no?

Suggested change
hex0x(loaded_tx_hash_opt.value()));
hex0x(*loaded_tx_hash_opt));

Copy link
Collaborator Author

@rodiazet rodiazet Mar 22, 2023

Choose a reason for hiding this comment

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

Merged. I will add new PR if necessary

}

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"] = j_txs[i]["hash"];
j_rejected_tx["hash"] = hex0x(computed_tx_hash);
j_rejected_tx["index"] = i;
j_rejected_tx["error"] = ec.message();
j_result["rejected"].push_back(j_rejected_tx);
Expand All @@ -120,7 +134,8 @@ 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"] = j_txs[i]["hash"];
axic marked this conversation as resolved.
Show resolved Hide resolved

j_receipt["transactionHash"] = hex0x(computed_tx_hash);
j_receipt["gasUsed"] = hex0x(static_cast<uint64_t>(receipt.gas_used));
j_receipt["cumulativeGasUsed"] = j_receipt["gasUsed"];

Expand Down Expand Up @@ -165,9 +180,10 @@ int main(int argc, const char* argv[])

std::ofstream{output_dir / output_alloc_file} << std::setw(2) << j_alloc;
}
catch (...)
catch (const std::exception& e)
{
std::cerr << "Unhandled exception" << std::endl;
std::cerr << e.what() << std::endl;
axic marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

return 0;
Expand Down