-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state: Define MPT root hash procedure for accounts
- Loading branch information
Showing
6 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,7 @@ target_sources( | |
hash_utils.hpp | ||
mpt.hpp | ||
mpt.cpp | ||
mpt_hash.hpp | ||
mpt_hash.cpp | ||
rlp.hpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |