Skip to content

Commit

Permalink
Use Withdrawal struct an amount_wei helper
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Apr 20, 2023
1 parent 02e6b21 commit 0fa05d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ void finalize(State& state, evmc_revision rev, const address& coinbase,
});
}

// Apply withdrawals. Amount value is in gwei.
for (const auto& withdraw : withdrawals)
state.touch(withdraw.first).balance += intx::uint256{withdraw.second} * 1e9;
for (const auto& withdrawal : withdrawals)
state.touch(withdrawal.recipient).balance += withdrawal.amount_wei();
}

std::variant<TransactionReceipt, std::error_code> transition(
Expand Down
11 changes: 9 additions & 2 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ class State
[[nodiscard]] const auto& get_accounts() const noexcept { return m_accounts; }
};

/// Vector of withdrawals in a block. Contains `to` address and ETH `amount` in gwei.
using Withdrawal = std::pair<address, uint64_t>;
struct Withdrawal
{
address recipient;
/// The amount is denominated in gwei.
uint64_t amount;

intx::uint256 amount_wei() const noexcept { return intx::uint256{amount} * 1e9; }
};

using Withdrawals = std::vector<Withdrawal>;

struct BlockInfo
Expand Down
6 changes: 3 additions & 3 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
state::Withdrawals withdrawals;
if (const auto withdrawals_it = j.find("withdrawals"); withdrawals_it != j.end())
{
for (const auto& withdraw : *withdrawals_it)
for (const auto& withdrawal : *withdrawals_it)
{
withdrawals.push_back({from_json<evmc::address>(withdraw.at("address")),
from_json<uint64_t>(withdraw.at("amount"))});
withdrawals.push_back({from_json<evmc::address>(withdrawal.at("address")),
from_json<uint64_t>(withdrawal.at("amount"))});
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ TEST(statetest_loader, block_info_withdrawals)
EXPECT_EQ(bi.timestamp, 0);
EXPECT_EQ(bi.number, 0);
EXPECT_EQ(bi.withdrawals.size(), 2);
EXPECT_EQ(bi.withdrawals[0].first, 0x0000000000000000000000000000000000000100_address);
EXPECT_EQ(bi.withdrawals[0].second, 0x800000000);
EXPECT_EQ(bi.withdrawals[1].first, 0x0000000000000000000000000000000000000200_address);
EXPECT_EQ(bi.withdrawals[1].second, 0xffffffffffffffff);
EXPECT_EQ(bi.withdrawals[0].recipient, 0x0000000000000000000000000000000000000100_address);
EXPECT_EQ(bi.withdrawals[0].amount, 0x800000000);
EXPECT_EQ(bi.withdrawals[1].recipient, 0x0000000000000000000000000000000000000200_address);
EXPECT_EQ(bi.withdrawals[1].amount, 0xffffffffffffffff);
}

0 comments on commit 0fa05d0

Please sign in to comment.