Skip to content

Commit

Permalink
state: Improve account insert/get API
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Oct 27, 2022
1 parent 6937f5d commit 8314df3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions test/state/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ using evmc::bytes32;
struct StorageValue
{
/// The current value.
bytes32 current{};
bytes32 current = {};

/// The original value.
bytes32 original{};
bytes32 original = {};
};

/// The state account.
Expand All @@ -30,12 +30,12 @@ struct Account
uint64_t nonce = 0;

/// The account balance.
intx::uint256 balance;
intx::uint256 balance = {};

/// The account storage map.
std::unordered_map<bytes32, StorageValue> storage;
std::unordered_map<bytes32, StorageValue> storage = {};

/// The account code.
bytes code;
bytes code = {};
};
} // namespace evmone::state
9 changes: 5 additions & 4 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ class State
std::unordered_map<address, Account> m_accounts;

public:
/// Creates new account under the address.
Account& create(const address& addr)
/// Inserts the new account at the address.
/// There must not exist any account under this address before.
Account& insert(const address& addr, Account account = {})
{
const auto r = m_accounts.insert({addr, {}});
const auto r = m_accounts.insert({addr, std::move(account)});
assert(r.second);
return r.first->second;
}

/// Get an account from the address
/// Gets the account at the address (the account must exist).
Account& get(const address& addr) { return m_accounts.at(addr); }
};

Expand Down
13 changes: 5 additions & 8 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,15 @@ static void from_json(const json::json& j, StateTransitionTest& o)

for (const auto& [j_addr, j_acc] : j_t.at("pre").items())
{
const auto addr = from_json<address>(j_addr);
auto& acc = o.pre_state.create(addr);
acc.balance = from_json<intx::uint256>(j_acc.at("balance"));
acc.nonce = from_json<uint64_t>(j_acc.at("nonce"));
acc.code = from_json<bytes>(j_acc.at("code"));
auto& acc = o.pre_state.insert(from_json<address>(j_addr),
{.nonce = from_json<uint64_t>(j_acc.at("nonce")),
.balance = from_json<intx::uint256>(j_acc.at("balance")),
.code = from_json<bytes>(j_acc.at("code"))});

for (const auto& [j_key, j_value] : j_acc.at("storage").items())
{
auto& slot = acc.storage[from_json<bytes32>(j_key)];
const auto value = from_json<bytes32>(j_value);
slot.original = value;
slot.current = value;
acc.storage.insert({from_json<bytes32>(j_key), {.current = value, .original = value}});
}
}

Expand Down

0 comments on commit 8314df3

Please sign in to comment.