Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 23, 2023
1 parent 4c5186f commit bfff8ee
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 74 deletions.
2 changes: 2 additions & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ target_sources(
state_mpt_test.cpp
state_new_account_address_test.cpp
state_rlp_test.cpp
state_transition.hpp
state_transition.cpp
state_transition_test.cpp
statetest_loader_test.cpp
statetest_loader_tx_test.cpp
Expand Down
43 changes: 43 additions & 0 deletions test/unittests/state_transition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "state_transition.hpp"

namespace evmone::test
{
void state_transition::TearDown()
{
auto& state = pre;
const auto res = evmone::state::transition(state, block, tx, EVMC_CANCUN, vm);
ASSERT_TRUE(holds_alternative<TransactionReceipt>(res))
<< std::get<std::error_code>(res).message();
const auto& receipt = std::get<TransactionReceipt>(res);

EXPECT_EQ(receipt.status, expect.status);
if (expect.gas_used.has_value())
{
EXPECT_EQ(receipt.gas_used, *expect.gas_used);
}

for (const auto& [a, e] : expect.post)
{
const auto p = state.find(a);
if (!e.exists)
{
// TODO: Add printing for address and bytes32.
EXPECT_EQ(p, nullptr) << "account " << hex(a) << " should not exist";
}
else
{
ASSERT_NE(p, nullptr) << "account " << hex(a) << " should exist";
EXPECT_EQ(p->nonce, e.nonce);

for (const auto& [k, v] : e.storage)
{
EXPECT_EQ(p->storage.at(k).current, v);
}
}
}
}
} // namespace evmone::test
53 changes: 53 additions & 0 deletions test/unittests/state_transition.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmone/evmone.h>
#include <gtest/gtest.h>
#include <test/state/host.hpp>

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

class state_transition : public testing::Test
{
protected:
static constexpr auto Sender = 0x8e4d_address;
static constexpr auto To = 0xc0de_address;

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

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

struct Expectation
{
evmc_status_code status = EVMC_SUCCESS;
std::optional<int64_t> gas_used;

std::unordered_map<evmc::address, ExpectedAccount> post;
};


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

State pre;
Expectation expect;

state_transition()
{
pre.insert(tx.sender, {.nonce = 1});
pre.insert(*tx.to, {.nonce = 1});
}

void TearDown() override;
};

} // namespace evmone::test
76 changes: 2 additions & 74 deletions test/unittests/state_transition_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#include "../utils/bytecode.hpp"
#include <evmone/evmone.h>
#include <gtest/gtest.h>
#include <test/state/host.hpp>
#include "state_transition.hpp"

using namespace evmc;
using namespace evmc::literals;
using namespace evmone::state;

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

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

struct Expectation
{
evmc_status_code status = EVMC_SUCCESS;
std::optional<int64_t> gas_used;

std::unordered_map<evmc::address, ExpectedAccount> post;
};

class state_transition : public testing::Test
{
protected:
const BlockInfo block{.gas_limit = 1'000'000};
const Transaction tx{.gas_limit = block.gas_limit, .sender = 0x66_address, .to = 0xcc_address};

State pre;
Expectation expect;

state_transition()
{
pre.insert(tx.sender, {.nonce = 3});
pre.insert(*tx.to, {.nonce = 1});
}

void TearDown() override
{
auto& state = pre;
evmc::VM vm{evmc_create_evmone()};
const auto res = evmone::state::transition(state, block, tx, EVMC_CANCUN, vm);
ASSERT_TRUE(holds_alternative<TransactionReceipt>(res))
<< std::get<std::error_code>(res).message();
const auto& receipt = std::get<TransactionReceipt>(res);

EXPECT_EQ(receipt.status, expect.status);
if (expect.gas_used.has_value())
{
EXPECT_EQ(receipt.gas_used, *expect.gas_used);
}

for (const auto& [a, e] : expect.post)
{
const auto p = state.find(a);
if (!e.exists)
{
// TODO: Add printing for address and bytes32.
EXPECT_EQ(p, nullptr) << "account " << hex(a) << " should not exist";
}
else
{
ASSERT_NE(p, nullptr);
EXPECT_EQ(p->nonce, e.nonce);

for (const auto& [k, v] : e.storage)
{
EXPECT_EQ(p->storage.at(k).current, v);
}
}
}
}
};
using namespace evmone::test;

TEST_F(state_transition, eof_invalid_initcode)
{
Expand Down

0 comments on commit bfff8ee

Please sign in to comment.