Skip to content

Commit

Permalink
Add get_amount usage to the unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Apr 20, 2023
1 parent fe96d0e commit 0150610
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionReceipt, std::error_code> transition(
Expand Down
13 changes: 7 additions & 6 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Withdrawal>;

struct BlockInfo
{
int64_t number = 0;
Expand All @@ -85,7 +86,7 @@ struct BlockInfo
address coinbase;
bytes32 prev_randao;
uint64_t base_fee = 0;
Withdrawals withdrawals;
std::vector<Withdrawal> withdrawals;
};

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

state::Withdrawals withdrawals;
std::vector<state::Withdrawal> withdrawals;
if (const auto withdrawals_it = j.find("withdrawals"); withdrawals_it != j.end())
{
for (const auto& withdrawal : *withdrawals_it)
Expand Down
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/unittests/state_transition_block_test.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 0 additions & 8 deletions test/unittests/state_transition_create_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 0150610

Please sign in to comment.