From 0150610a43de4522688d4832a9643f5c494fbe41 Mon Sep 17 00:00:00 2001 From: rodiazet Date: Thu, 20 Apr 2023 17:29:19 +0200 Subject: [PATCH] Add `get_amount` usage to the unit test --- test/state/state.cpp | 2 +- test/state/state.hpp | 13 +++++++------ test/statetest/statetest_loader.cpp | 2 +- test/unittests/CMakeLists.txt | 1 + test/unittests/state_transition_block_test.cpp | 16 ++++++++++++++++ test/unittests/state_transition_create_test.cpp | 8 -------- .../statetest_loader_block_info_test.cpp | 4 ++-- 7 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 test/unittests/state_transition_block_test.cpp diff --git a/test/state/state.cpp b/test/state/state.cpp index a3baa56139..c8d646edf3 100644 --- a/test/state/state.cpp +++ b/test/state/state.cpp @@ -133,7 +133,7 @@ void finalize(State& state, evmc_revision rev, const address& coinbase, } for (const auto& withdrawal : withdrawals) - state.touch(withdrawal.recipient).balance += withdrawal.amount_wei(); + state.touch(withdrawal.recipient).balance += withdrawal.get_amount(); } std::variant transition( diff --git a/test/state/state.hpp b/test/state/state.hpp index 441a19a8f8..91d9b373ab 100644 --- a/test/state/state.hpp +++ b/test/state/state.hpp @@ -69,14 +69,15 @@ class State struct Withdrawal { address recipient; - /// The amount is denominated in gwei. - uint64_t amount = 0; + uint64_t amount_in_gwei = 0; ///< The amount is denominated in gwei. - [[nodiscard]] intx::uint256 amount_wei() const noexcept { return intx::uint256{amount} * 1e9; } + /// Returns withdrawal amount in wei. + [[nodiscard]] intx::uint256 get_amount() const noexcept + { + return intx::uint256{amount_in_gwei} * 1'000'000'000; + } }; -using Withdrawals = std::vector; - struct BlockInfo { int64_t number = 0; @@ -85,7 +86,7 @@ struct BlockInfo address coinbase; bytes32 prev_randao; uint64_t base_fee = 0; - Withdrawals withdrawals; + std::vector withdrawals; }; using AccessList = std::vector>>; diff --git a/test/statetest/statetest_loader.cpp b/test/statetest/statetest_loader.cpp index c590e6cc30..ddb2eaa62d 100644 --- a/test/statetest/statetest_loader.cpp +++ b/test/statetest/statetest_loader.cpp @@ -171,7 +171,7 @@ state::BlockInfo from_json(const json::json& j) from_json(j.at("parentBaseFee"))); } - state::Withdrawals withdrawals; + std::vector withdrawals; if (const auto withdrawals_it = j.find("withdrawals"); withdrawals_it != j.end()) { for (const auto& withdrawal : *withdrawals_it) diff --git a/test/unittests/CMakeLists.txt b/test/unittests/CMakeLists.txt index 5978612f9c..f7486fe996 100644 --- a/test/unittests/CMakeLists.txt +++ b/test/unittests/CMakeLists.txt @@ -38,6 +38,7 @@ target_sources( state_rlp_test.cpp state_transition.hpp state_transition.cpp + state_transition_block_test.cpp state_transition_create_test.cpp state_transition_eof_test.cpp statetest_loader_block_info_test.cpp diff --git a/test/unittests/state_transition_block_test.cpp b/test/unittests/state_transition_block_test.cpp new file mode 100644 index 0000000000..f8bbb19a3f --- /dev/null +++ b/test/unittests/state_transition_block_test.cpp @@ -0,0 +1,16 @@ +// evmone: Fast Ethereum Virtual Machine implementation +// Copyright 2023 The evmone Authors. +// SPDX-License-Identifier: Apache-2.0 + +#include "state_transition.hpp" + +using namespace evmc::literals; +using namespace evmone::test; + +TEST_F(state_transition, block_apply_withdrawal) +{ + static constexpr auto withdrawal_address = 0x8ef300b6a6a0b41e4f5d717074d9fd5c605c7285_address; + + block.withdrawals = {{withdrawal_address, 3}}; + expect.post[withdrawal_address].balance = intx::uint256{3} * 1'000'000'000; +} diff --git a/test/unittests/state_transition_create_test.cpp b/test/unittests/state_transition_create_test.cpp index d046ac99d9..f54b80d4e5 100644 --- a/test/unittests/state_transition_create_test.cpp +++ b/test/unittests/state_transition_create_test.cpp @@ -32,11 +32,3 @@ TEST_F(state_transition, create_tx) expect.post[create_address].code = bytes{0xFE}; } - -TEST_F(state_transition, apply_withdrawal) -{ - static constexpr auto withdrawal_address = 0x8ef300b6a6a0b41e4f5d717074d9fd5c605c7285_address; - - block.withdrawals = {{withdrawal_address, 3}}; - expect.post[withdrawal_address].balance = intx::uint256{3} * 1e9; -} diff --git a/test/unittests/statetest_loader_block_info_test.cpp b/test/unittests/statetest_loader_block_info_test.cpp index a1fcc78356..053539e6bb 100644 --- a/test/unittests/statetest_loader_block_info_test.cpp +++ b/test/unittests/statetest_loader_block_info_test.cpp @@ -206,7 +206,7 @@ TEST(statetest_loader, block_info_withdrawals) EXPECT_EQ(bi.number, 0); EXPECT_EQ(bi.withdrawals.size(), 2); EXPECT_EQ(bi.withdrawals[0].recipient, 0x0000000000000000000000000000000000000100_address); - EXPECT_EQ(bi.withdrawals[0].amount, 0x800000000); + EXPECT_EQ(bi.withdrawals[0].get_amount(), intx::uint256{0x800000000} * 1'000'000'000); EXPECT_EQ(bi.withdrawals[1].recipient, 0x0000000000000000000000000000000000000200_address); - EXPECT_EQ(bi.withdrawals[1].amount, 0xffffffffffffffff); + EXPECT_EQ(bi.withdrawals[1].get_amount(), intx::uint256{0xffffffffffffffff} * 1'000'000'000); }