Skip to content

Commit

Permalink
state view: add has_storage flag
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 30, 2024
1 parent fc528ba commit 0028573
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
6 changes: 4 additions & 2 deletions test/state/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ struct Account

bytes32 code_hash = EMPTY_CODE_HASH;

bool has_cold_storage = false;

/// The account storage map.
std::unordered_map<bytes32, StorageValue> _storage;

Expand All @@ -51,11 +53,11 @@ struct Account
/// The account code.
bytes _code;

/// The account has been destructed and should be erased at the end of of a transaction.
/// The account has been destructed and should be erased at the end of a transaction.
bool destructed = false;

/// The account should be erased if it is empty at the end of a transaction.
/// This flag means the account has been "touched" as defined in EIP-161
/// This flag means the account has been "touched" as defined in EIP-161,
/// or it is a newly created temporary account.
///
/// Yellow Paper uses term "delete" but it is a keyword in C++ while
Expand Down
17 changes: 6 additions & 11 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,12 @@ bytes_view extcode(bytes_view code) noexcept
/// as defined in the [EIP-7610](https://eips.ethereum.org/EIPS/eip-7610).
[[nodiscard]] bool is_create_collision(const Account& acc) noexcept
{
if (acc.nonce != 0 || acc.code_hash != Account::EMPTY_CODE_HASH)
return true;

// acc.storage may have entries from access list, even if account storage is empty.
// Check for non-zero current values.
// FIXME:
// if (std::ranges::any_of(
// acc.storage, [](auto& e) noexcept { return !is_zero(e.second.current); }))
// return true;

return false;
// TODO: This requires much more testing:
// - what if an account had storage but is destructed?
// - what if an account had cold storage but it was emptied?
// - what if an account without cold storage gain one?
return acc.nonce != 0 || acc.code_hash != Account::EMPTY_CODE_HASH || acc.has_cold_storage ||
!acc._storage.empty();
}
} // namespace

Expand Down
8 changes: 5 additions & 3 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// SPDX-License-Identifier: Apache-2.0

#include "state.hpp"
#include "state_view.hpp"
#include "../utils/stdx/utility.hpp"
#include "errors.hpp"
#include "host.hpp"
#include "rlp.hpp"
#include "state_view.hpp"
#include <evmone/constants.hpp>
#include <evmone/eof.hpp>
#include <evmone/execution_state.hpp>
Expand Down Expand Up @@ -104,8 +104,10 @@ Account* State::find(const address& addr) noexcept
if (const auto it = m_accounts.find(addr); it != m_accounts.end())
return &it->second;
if (const auto cacc = m_cold->get_account(addr); cacc)
return &insert(
addr, {.nonce = cacc->nonce, .balance = cacc->balance, .code_hash = cacc->code_hash});
return &insert(addr, {.nonce = cacc->nonce,
.balance = cacc->balance,
.code_hash = cacc->code_hash,
.has_cold_storage = cacc->has_storage});
return nullptr;
}

Expand Down
1 change: 1 addition & 0 deletions test/state/state_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class StateView
uint64_t nonce = 0;
uint256 balance;
bytes32 code_hash;
bool has_storage = false;
};

virtual ~StateView() = default;
Expand Down
2 changes: 1 addition & 1 deletion test/state/test_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std::optional<state::StateView::Account> TestState::get_account(address addr) co

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

bytes TestState::get_account_code(evmc::address addr) const noexcept
Expand Down

0 comments on commit 0028573

Please sign in to comment.