From 8314df3068f8a35394a35d3077e1812cf00f3a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 27 Oct 2022 10:01:06 +0200 Subject: [PATCH] state: Improve account insert/get API --- test/state/account.hpp | 10 +++++----- test/state/state.hpp | 9 +++++---- test/statetest/statetest_loader.cpp | 13 +++++-------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/test/state/account.hpp b/test/state/account.hpp index 452eb1f675..37896b1de9 100644 --- a/test/state/account.hpp +++ b/test/state/account.hpp @@ -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. @@ -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 storage; + std::unordered_map storage = {}; /// The account code. - bytes code; + bytes code = {}; }; } // namespace evmone::state diff --git a/test/state/state.hpp b/test/state/state.hpp index 686b1af011..7c895f8bca 100644 --- a/test/state/state.hpp +++ b/test/state/state.hpp @@ -17,15 +17,16 @@ class State std::unordered_map 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); } }; diff --git a/test/statetest/statetest_loader.cpp b/test/statetest/statetest_loader.cpp index dcab02308c..c5168b9355 100644 --- a/test/statetest/statetest_loader.cpp +++ b/test/statetest/statetest_loader.cpp @@ -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
(j_addr); - auto& acc = o.pre_state.create(addr); - acc.balance = from_json(j_acc.at("balance")); - acc.nonce = from_json(j_acc.at("nonce")); - acc.code = from_json(j_acc.at("code")); + auto& acc = o.pre_state.insert(from_json
(j_addr), + {.nonce = from_json(j_acc.at("nonce")), + .balance = from_json(j_acc.at("balance")), + .code = from_json(j_acc.at("code"))}); for (const auto& [j_key, j_value] : j_acc.at("storage").items()) { - auto& slot = acc.storage[from_json(j_key)]; const auto value = from_json(j_value); - slot.original = value; - slot.current = value; + acc.storage.insert({from_json(j_key), {.current = value, .original = value}}); } }