Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state: Define MPT root hash procedure for accounts #478

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/state/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ add_library(evmone::state ALIAS evmone-state)
target_link_libraries(evmone-state PRIVATE evmc::evmc_cpp ethash::keccak)
target_sources(
evmone-state PRIVATE
account.hpp
hash_utils.hpp
mpt.hpp
mpt.cpp
mpt_hash.hpp
mpt_hash.cpp
rlp.hpp
)
38 changes: 38 additions & 0 deletions test/state/account.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2021 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmc/evmc.hpp>
#include <intx/intx.hpp>
#include <unordered_map>

namespace evmone::state
{
using evmc::address;
using evmc::bytes;
using evmc::bytes32;

/// The representation of the account storage value.
struct StorageValue
gumb0 marked this conversation as resolved.
Show resolved Hide resolved
{
/// The current value.
bytes32 current{};
};

/// The state account.
struct Account
{
/// The account nonce.
uint64_t nonce = 0;

/// The account balance.
intx::uint256 balance;

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

/// The account code.
bytes code;
};
} // namespace evmone::state
4 changes: 3 additions & 1 deletion test/state/hash_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

namespace evmone
{
using evmc::address;
using evmc::bytes;
using evmc::bytes32;
using evmc::bytes_view;
using namespace evmc::literals;

/// Default type for 256-bit hash.
///
/// Better than ethash::hash256 because has some additional handy constructors.
using hash256 = evmc::bytes32;
using hash256 = bytes32;

/// Computes Keccak hash out of input bytes (wrapper of ethash::keccak256).
inline hash256 keccak256(bytes_view data) noexcept
Expand Down
36 changes: 36 additions & 0 deletions test/state/mpt_hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "mpt_hash.hpp"
#include "account.hpp"
#include "mpt.hpp"
#include "rlp.hpp"

namespace evmone::state
{
namespace
{
hash256 mpt_hash(const std::unordered_map<hash256, StorageValue>& storage)
{
MPT trie;
for (const auto& [key, value] : storage)
{
if (!is_zero(value.current)) // Skip "deleted" values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is rather logic of how MPT data structure is used in Ethereum... so not logic of hashing itself, may be better kept outside.

But I don't mind either way.

Copy link
Member Author

@chfast chfast Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both "hash functions" are use-case specific because at least you need to decide how to serialize a structure. As long as we will keep original and current value together we cannot erase instead of zeroing.

In more generic fashion in this case std::copy_if could be used if we support std::insterter(). In the other case we would need something like "transform if" so ranges should be more suited. Interesting exercise, but decided is too big detour.

trie.insert(keccak256(key), rlp::encode(rlp::trim(value.current)));
}
return trie.hash();
}
} // namespace

hash256 mpt_hash(const std::unordered_map<address, Account>& accounts)
{
MPT trie;
for (const auto& [addr, acc] : accounts)
{
trie.insert(keccak256(addr),
rlp::encode_tuple(acc.nonce, acc.balance, mpt_hash(acc.storage), keccak256(acc.code)));
}
return trie.hash();
}
} // namespace evmone::state
15 changes: 15 additions & 0 deletions test/state/mpt_hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "hash_utils.hpp"
#include <unordered_map>

namespace evmone::state
{
struct Account;

/// Computes Merkle Patricia Trie root hash for the given collection of state accounts.
hash256 mpt_hash(const std::unordered_map<address, Account>& accounts);
} // namespace evmone::state
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_executable(evmone-unittests
evmone_test.cpp
execution_state_test.cpp
instructions_test.cpp
state_mpt_hash_test.cpp
state_mpt_test.cpp
state_rlp_test.cpp
tracing_test.cpp
Expand Down
60 changes: 60 additions & 0 deletions test/unittests/state_mpt_hash_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <test/state/account.hpp>
#include <test/state/mpt.hpp>
#include <test/state/mpt_hash.hpp>

using namespace evmone;
using namespace evmone::state;
using namespace intx;

TEST(state_mpt_hash, empty)
{
EXPECT_EQ(mpt_hash({}), emptyMPTHash);
}

TEST(state_mpt_hash, single_account_v1)
{
// Expected value computed in go-ethereum.
constexpr auto expected =
0x084f337237951e425716a04fb0aaa74111eda9d9c61767f2497697d0a201c92e_bytes32;

Account acc;
acc.balance = 1_u256;
const std::unordered_map<address, Account> accounts{{0x02_address, acc}};
EXPECT_EQ(mpt_hash(accounts), expected);
}

TEST(state_mpt_hash, two_accounts)
{
std::unordered_map<address, Account> accounts;
EXPECT_EQ(mpt_hash(accounts), emptyMPTHash);

accounts[0x00_address] = Account{};
EXPECT_EQ(mpt_hash(accounts),
0x0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785_bytes32);

Account acc2;
acc2.nonce = 1;
acc2.balance = -2_u256;
acc2.code = {0x00};
acc2.storage[0x01_bytes32] = {0xfe_bytes32};
acc2.storage[0x02_bytes32] = {0xfd_bytes32};
accounts[0x01_address] = acc2;
EXPECT_EQ(mpt_hash(accounts),
0xd3e845156fca75de99712281581304fbde104c0fc5a102b09288c07cdde0b666_bytes32);
}

TEST(state_mpt_hash, deleted_storage)
{
Account acc;
acc.storage[0x01_bytes32] = {};
acc.storage[0x02_bytes32] = {0xfd_bytes32};
acc.storage[0x03_bytes32] = {};
const std::unordered_map<address, Account> accounts{{0x07_address, acc}};
EXPECT_EQ(mpt_hash(accounts),
0x4e7338c16731491e0fb5d1623f5265c17699c970c816bab71d4d717f6071414d_bytes32);
}