Skip to content

Commit

Permalink
Support withdrawals in t8n
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Apr 12, 2023
1 parent 39357ec commit f942863
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class State
[[nodiscard]] const auto& get_accounts() const noexcept { return m_accounts; }
};

using Withdrawals = std::vector<std::pair<address, uint64_t>>;

struct BlockInfo
{
int64_t number = 0;
Expand All @@ -74,6 +76,7 @@ struct BlockInfo
address coinbase;
bytes32 prev_randao;
uint64_t base_fee = 0;
Withdrawals withdrawals;
};

using AccessList = std::vector<std::pair<address, std::vector<bytes32>>>;
Expand Down
13 changes: 12 additions & 1 deletion test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,20 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
from_json<uint64_t>(j.at("parentGasLimit")),
from_json<uint64_t>(j.at("parentBaseFee")));
}

state::Withdrawals withdrawals;
if (const auto withdrawals_it = j.find("withdrawals"); withdrawals_it != j.end())
{
for (const auto& withdraw : *withdrawals_it)
{
withdrawals.push_back({from_json<evmc::address>(withdraw.at("address")),
from_json<uint64_t>(withdraw.at("amount"))});
}
}

return {from_json<int64_t>(j.at("currentNumber")), from_json<int64_t>(j.at("currentTimestamp")),
from_json<int64_t>(j.at("currentGasLimit")),
from_json<evmc::address>(j.at("currentCoinbase")), difficulty, base_fee};
from_json<evmc::address>(j.at("currentCoinbase")), difficulty, base_fee, withdrawals};
}

template <>
Expand Down
4 changes: 4 additions & 0 deletions test/t8n/t8n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ int main(int argc, const char* argv[])
if (block_reward.has_value())
state.touch(block.coinbase).balance += *block_reward;

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

j_result["logsHash"] = hex0x(logs_hash(txs_logs));
j_result["stateRoot"] = hex0x(state::mpt_hash(state.get_accounts()));
}
Expand Down
40 changes: 40 additions & 0 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,43 @@ TEST(statetest_loader, block_info_0_random)
EXPECT_EQ(bi.timestamp, 0);
EXPECT_EQ(bi.number, 0);
}

TEST(statetest_loader, block_info_withdrawals)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x1111111111111111111111111111111111111111",
"currentDifficulty": "0x0",
"currentGasLimit": "0x0",
"currentNumber": "0",
"currentTimestamp": "0",
"currentBaseFee": "7",
"currentRandom": "0x00",
"withdrawals": [
{
"index": "0x0",
"validatorIndex": "0x0",
"address": "0x0000000000000000000000000000000000000100",
"amount": "0x800000000"
},
{
"index": "0x1",
"validatorIndex": "0x1",
"address": "0x0000000000000000000000000000000000000200",
"amount": "0xffffffffffffffff"
}
]
})";

const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x1111111111111111111111111111111111111111_address);
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 0x0);
EXPECT_EQ(bi.base_fee, 7);
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);
}

0 comments on commit f942863

Please sign in to comment.