Skip to content

Commit

Permalink
add balance
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 23, 2023
1 parent bfff8ee commit 062693a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
10 changes: 8 additions & 2 deletions test/unittests/state_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ void state_transition::TearDown()
else
{
ASSERT_NE(p, nullptr) << "account " << hex(a) << " should exist";
EXPECT_EQ(p->nonce, e.nonce);

if (e.nonce.has_value())
{
EXPECT_EQ(p->nonce, *e.nonce);
}
if (e.balance.has_value())
{
EXPECT_EQ(p->balance, *e.balance);
}
for (const auto& [k, v] : e.storage)
{
EXPECT_EQ(p->storage.at(k).current, v);
Expand Down
20 changes: 16 additions & 4 deletions test/unittests/state_transition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@
#include <gtest/gtest.h>
#include <test/state/host.hpp>

#pragma GCC diagnostic ignored "-Wmissing-field-initializers"

namespace evmone::test
{
using namespace evmone::state;

/// Fixture to defining test cases in form similar to JSON State Tests.
///
/// It takes the "pre" state and produces "post" state by applying the defined "tx" transaction.
/// Then expectations declared in "except" are checked in the "post" state.
class state_transition : public testing::Test
{
protected:
/// The default sender address of the test transaction.
/// TODO: We need a private key of this address so pick some other one later.
static constexpr auto Sender = 0x8e4d_address;

/// The default destination address of the test transaction.
static constexpr auto To = 0xc0de_address;

static inline evmc::VM vm{evmc_create_evmone()};

struct ExpectedAccount
{
bool exists = true;
uint64_t nonce = 0;
std::optional<uint64_t> nonce;
std::optional<intx::uint256> balance;
std::unordered_map<evmc::bytes32, evmc::bytes32> storage;
};

Expand All @@ -36,17 +47,18 @@ class state_transition : public testing::Test


BlockInfo block{.gas_limit = 1'000'000};
Transaction tx{.gas_limit = block.gas_limit, .sender = Sender, .to = To};

Transaction tx{.gas_limit = block.gas_limit, .max_gas_price = 10, .sender = Sender, .to = To};
State pre;
Expectation expect;

state_transition()
{
pre.insert(tx.sender, {.nonce = 1});
pre.insert(
tx.sender, {.nonce = 1, .balance = tx.gas_limit * tx.max_gas_price + tx.value + 1});
pre.insert(*tx.to, {.nonce = 1});
}

/// The test runner.
void TearDown() override;
};

Expand Down
6 changes: 5 additions & 1 deletion test/unittests/state_transition_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ TEST_F(state_transition, eof_invalid_initcode)
static constexpr auto create_address = 0x864bbda5c698ac34b47a9ea3bd4228802cc5ce3b_address;

pre.get(*tx.to).code = eof1_bytecode(create() + push(1) + OP_SSTORE + OP_STOP, 3);
pre.get(*tx.to).storage[0x01_bytes32] = {.current = 0x01_bytes32, .original = 0x01_bytes32};

expect.gas_used = 987407;
EXPECT_EQ(pre.get(tx.sender).balance, 10000001);

expect.gas_used = 985407;

expect.post[tx.sender].nonce = pre.get(tx.sender).nonce + 1;
expect.post[tx.sender].balance = 10'000'001; // FIXME: WAT?
expect.post[*tx.to].nonce = pre.get(*tx.to).nonce + 1; // CREATE caller's nonce must be bumped
expect.post[*tx.to].storage[0x01_bytes32] = 0x00_bytes32; // CREATE must fail
expect.post[create_address].exists = false;
Expand Down

0 comments on commit 062693a

Please sign in to comment.