Skip to content

Commit

Permalink
t8n: add withdrawalsRoot to result
Browse files Browse the repository at this point in the history
  • Loading branch information
danceratopz committed Aug 22, 2023
1 parent 35f4141 commit 424c094
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions test/state/mpt_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ hash256 mpt_hash(std::span<const TransactionReceipt> receipts)
return trie.hash();
}

hash256 mpt_hash(std::span<const Withdrawal> withdrawals)
{
MPT trie;
for (size_t i = 0; i < withdrawals.size(); ++i)
trie.insert(
rlp::encode(i), rlp::encode_tuple(withdrawals[i].index, withdrawals[i].validatorIndex,
withdrawals[i].recipient, withdrawals[i].amount_in_gwei));

return trie.hash();
}

} // namespace evmone::state
4 changes: 4 additions & 0 deletions test/state/mpt_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace evmone::state
struct Account;
struct Transaction;
struct TransactionReceipt;
struct Withdrawal;

/// Computes Merkle Patricia Trie root hash for the given collection of state accounts.
hash256 mpt_hash(const std::unordered_map<address, Account>& accounts);
Expand All @@ -22,4 +23,7 @@ hash256 mpt_hash(std::span<const Transaction> transactions);
/// Computes Merkle Patricia Trie root hash for the given collection of transactions receipts.
hash256 mpt_hash(std::span<const TransactionReceipt> receipts);

/// Computes Merkle Patricia Trie root hash for the given collection of withdrawals.
hash256 mpt_hash(std::span<const Withdrawal> withdrawals);

} // namespace evmone::state
2 changes: 2 additions & 0 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class State

struct Withdrawal
{
uint64_t index = 0;
uint64_t validatorIndex = 0;
address recipient;
uint64_t amount_in_gwei = 0; ///< The amount is denominated in gwei.

Expand Down
4 changes: 3 additions & 1 deletion test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
{
for (const auto& withdrawal : *withdrawals_it)
{
withdrawals.push_back({from_json<evmc::address>(withdrawal.at("address")),
withdrawals.push_back({from_json<uint64_t>(withdrawal.at("index")),
from_json<uint64_t>(withdrawal.at("validatorIndex")),
from_json<evmc::address>(withdrawal.at("address")),
from_json<uint64_t>(withdrawal.at("amount"))});
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/t8n/t8n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int main(int argc, const char* argv[])

state::BlockInfo block;
state::State state;
bool withdrawals_enabled = false;

if (!alloc_file.empty())
{
Expand All @@ -78,6 +79,8 @@ int main(int argc, const char* argv[])
{
const auto j = json::json::parse(std::ifstream{env_file});
block = test::from_json<state::BlockInfo>(j);
if (j.contains("withdrawals"))
withdrawals_enabled = true;
}

json::json j_result;
Expand Down Expand Up @@ -172,6 +175,8 @@ int main(int argc, const char* argv[])

j_result["logsBloom"] = hex0x(compute_bloom_filter(receipts));
j_result["receiptsRoot"] = hex0x(state::mpt_hash(receipts));
if (withdrawals_enabled)
j_result["withdrawalsRoot"] = hex0x(state::mpt_hash(block.withdrawals));
j_result["txRoot"] = hex0x(state::mpt_hash(transactions));
j_result["gasUsed"] = hex0x(cumulative_gas_used);

Expand Down
2 changes: 1 addition & 1 deletion test/unittests/state_transition_block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_F(state_transition, block_apply_withdrawal)
{
static constexpr auto withdrawal_address = 0x8888_address;

block.withdrawals = {{withdrawal_address, 3}};
block.withdrawals = {{0, 0, withdrawal_address, 3}};
tx.to = To;
expect.post[withdrawal_address].balance = intx::uint256{3} * 1'000'000'000;
}

0 comments on commit 424c094

Please sign in to comment.