From 7a0f31103258b48a6ab1d6392d803ba842c2b227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 Nov 2019 12:54:17 +0100 Subject: [PATCH] EVMHost: Use evmc::MockedAccount --- test/EVMHost.cpp | 18 +++++++++--------- test/EVMHost.h | 27 +++++++++------------------ test/ExecutionFramework.cpp | 4 ++-- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/test/EVMHost.cpp b/test/EVMHost.cpp index 863bd2fdf..205351f0f 100644 --- a/test/EVMHost.cpp +++ b/test/EVMHost.cpp @@ -98,7 +98,7 @@ EVMHost::EVMHost(langutil::EVMVersion _evmVersion, evmc::VM& _vm): evmc::address address{}; address.bytes[19] = precompiledAddress; // 1wei - m_state.accounts[address].balance.bytes[31] = 1; + accounts[address].balance.bytes[31] = 1; } // TODO: support short literals in EVMC and use them here @@ -113,8 +113,8 @@ EVMHost::EVMHost(langutil::EVMVersion _evmVersion, evmc::VM& _vm): evmc_storage_status EVMHost::set_storage(const evmc::address& _addr, const evmc::bytes32& _key, const evmc::bytes32& _value) noexcept { - evmc::bytes32 previousValue = m_state.accounts[_addr].storage[_key]; - m_state.accounts[_addr].storage[_key] = _value; + evmc::bytes32 previousValue = m_state.accounts[_addr].storage[_key].value; + m_state.accounts[_addr].storage[_key].value = _value; // TODO EVMC_STORAGE_MODIFIED_AGAIN should be also used if (previousValue == _value) @@ -158,9 +158,9 @@ evmc::result EVMHost::call(evmc_message const& _message) noexcept State stateBackup = m_state; u256 value{convertFromEVMC(_message.value)}; - Account& sender = m_state.accounts[_message.sender]; + auto& sender = m_state.accounts[_message.sender]; - bytes code; + evmc::bytes code; evmc_message message = _message; if (message.depth == 0) @@ -186,7 +186,7 @@ evmc::result EVMHost::call(evmc_message const& _message) noexcept asBytes(to_string(sender.nonce++)) )); message.destination = convertToEVMC(createAddress); - code = bytes(message.input_data, message.input_data + message.input_size); + code = evmc::bytes(message.input_data, message.input_data + message.input_size); } else if (message.kind == EVMC_DELEGATECALL) { @@ -202,7 +202,7 @@ evmc::result EVMHost::call(evmc_message const& _message) noexcept code = m_state.accounts[message.destination].code; //TODO CREATE2 - Account& destination = m_state.accounts[message.destination]; + auto& destination = m_state.accounts[message.destination]; if (value != 0 && message.kind != EVMC_DELEGATECALL && message.kind != EVMC_CALLCODE) { @@ -227,8 +227,8 @@ evmc::result EVMHost::call(evmc_message const& _message) noexcept else { result.create_address = message.destination; - destination.code = bytes(result.output_data, result.output_data + result.output_size); - destination.codeHash = convertToEVMC(keccak256(destination.code)); + destination.code = evmc::bytes(result.output_data, result.output_data + result.output_size); + destination.codehash = convertToEVMC(keccak256({result.output_data, result.output_size})); } } diff --git a/test/EVMHost.h b/test/EVMHost.h index 14d3869f9..a673360dc 100644 --- a/test/EVMHost.h +++ b/test/EVMHost.h @@ -45,27 +45,18 @@ public: explicit EVMHost(langutil::EVMVersion _evmVersion, evmc::VM& _vm = getVM()); - struct Account - { - evmc::uint256be balance = {}; - size_t nonce = 0; - bytes code; - evmc::bytes32 codeHash = {}; - std::map storage; - }; - struct State { - std::map accounts; + std::map accounts; }; - Account const* account(evmc::address const& _address) const + evmc::MockedAccount const* account(evmc::address const& _address) const { auto it = m_state.accounts.find(_address); return it == m_state.accounts.end() ? nullptr : &it->second; } - Account* account(evmc::address const& _address) + evmc::MockedAccount* account(evmc::address const& _address) { auto it = m_state.accounts.find(_address); return it == m_state.accounts.end() ? nullptr : &it->second; @@ -90,7 +81,7 @@ public: { auto it = acc->storage.find(_key); if (it != acc->storage.end()) - return it->second; + return it->second.value; } return {}; } @@ -103,22 +94,22 @@ public: evmc::uint256be get_balance(evmc::address const& _addr) const noexcept final { - if (Account const* acc = account(_addr)) + if (auto const* acc = account(_addr)) return acc->balance; return {}; } size_t get_code_size(evmc::address const& _addr) const noexcept final { - if (Account const* acc = account(_addr)) + if (auto const* acc = account(_addr)) return acc->code.size(); return 0; } evmc::bytes32 get_code_hash(evmc::address const& _addr) const noexcept final { - if (Account const* acc = account(_addr)) - return acc->codeHash; + if (auto const* acc = account(_addr)) + return acc->codehash; return {}; } @@ -130,7 +121,7 @@ public: ) const noexcept final { size_t i = 0; - if (Account const* acc = account(_addr)) + if (auto const* acc = account(_addr)) for (; i < _bufferSize && _codeOffset + i < acc->code.size(); i++) _bufferData[i] = acc->code[_codeOffset + i]; return i; diff --git a/test/ExecutionFramework.cpp b/test/ExecutionFramework.cpp index 3be6418dd..f049da70d 100644 --- a/test/ExecutionFramework.cpp +++ b/test/ExecutionFramework.cpp @@ -234,10 +234,10 @@ u256 ExecutionFramework::balanceAt(Address const& _addr) bool ExecutionFramework::storageEmpty(Address const& _addr) { - if (EVMHost::Account const* acc = m_evmHost->account(EVMHost::convertToEVMC(_addr))) + if (auto const* acc = m_evmHost->account(EVMHost::convertToEVMC(_addr))) { for (auto const& entry: acc->storage) - if (!(entry.second == evmc::bytes32{})) + if (!(entry.second.value == evmc::bytes32{})) return false; } return true;