Skip to content

Commit

Permalink
test: TestState implements StateView
Browse files Browse the repository at this point in the history
Implement `StateView` interface in `TestState`. This will be used later.
  • Loading branch information
chfast committed Sep 30, 2024
1 parent 727d74d commit 928edb2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
32 changes: 31 additions & 1 deletion test/state/test_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@

namespace evmone::test
{
std::optional<state::StateView::Account> TestState::get_account(const address& addr) const noexcept
{
const auto it = find(addr);
if (it == end())
return std::nullopt;

const auto& acc = it->second;
// TODO: Cache code hash for MTP root hash calculation?
return Account{acc.nonce, acc.balance, keccak256(acc.code), !acc.storage.empty()};
}

bytes TestState::get_account_code(const address& addr) const noexcept
{
const auto it = find(addr);
if (it == end())
return {};

return it->second.code;
}

state::State TestState::to_intra_state() const
{
state::State intra_state;
for (const auto& [addr, acc] : *this)
{
auto& intra_acc = intra_state.insert(
addr, {.nonce = acc.nonce, .balance = acc.balance, .code = acc.code});
addr, {.nonce = acc.nonce, .balance = acc.balance, .code = get_account_code(addr)});
auto& storage = intra_acc.storage;
for (const auto& [key, value] : acc.storage)
storage[key] = {.current = value, .original = value};
Expand Down Expand Up @@ -43,6 +63,16 @@ void TestState::apply(const state::StateDiff& diff)
erase(addr);
}

bytes32 TestState::get_storage(const address& addr, const bytes32& key) const noexcept
{
const auto ait = find(addr);
if (ait == end()) // TODO: When?
return bytes32{};
const auto& storage = ait->second.storage;
const auto it = storage.find(key);
return (it != storage.end()) ? it->second : bytes32{};
}

[[nodiscard]] std::variant<state::TransactionReceipt, std::error_code> transition(TestState& state,
const state::BlockInfo& block, const state::Transaction& tx, evmc_revision rev, evmc::VM& vm,
int64_t block_gas_left, int64_t blob_gas_left)
Expand Down
7 changes: 6 additions & 1 deletion test/state/test_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "state_view.hpp"
#include <evmc/evmc.hpp>
#include <intx/intx.hpp>
#include <map>
Expand Down Expand Up @@ -45,11 +46,15 @@ struct TestAccount
/// This is a simplified variant of state::State:
/// it hides some details related to transaction execution (e.g. original storage values)
/// and is also easier to work with in tests.
class TestState : public std::map<address, TestAccount>
class TestState : public state::StateView, public std::map<address, TestAccount>
{
public:
using map::map;

std::optional<Account> get_account(const address& addr) const noexcept override;
bytes get_account_code(const address& addr) const noexcept override;
bytes32 get_storage(const address& addr, const bytes32& key) const noexcept override;

/// Inserts new account to the state.
///
/// This method is for compatibility with state::State::insert().
Expand Down

0 comments on commit 928edb2

Please sign in to comment.