TLA Line data Source code
1 : // evmone: Fast Ethereum Virtual Machine implementation
2 : // Copyright 2022 The evmone Authors.
3 : // SPDX-License-Identifier: Apache-2.0
4 :
5 : #include "mpt_hash.hpp"
6 : #include "account.hpp"
7 : #include "mpt.hpp"
8 : #include "rlp.hpp"
9 : #include "state.hpp"
10 :
11 : namespace evmone::state
12 : {
13 : namespace
14 : {
15 CBC 212 : hash256 mpt_hash(const std::unordered_map<hash256, StorageValue>& storage)
16 : {
17 212 : MPT trie;
18 499 : for (const auto& [key, value] : storage)
19 : {
20 287 : if (!is_zero(value.current)) // Skip "deleted" values.
21 226 : trie.insert(keccak256(key), rlp::encode(rlp::trim(value.current)));
22 : }
23 424 : return trie.hash();
24 212 : }
25 : } // namespace
26 :
27 53 : hash256 mpt_hash(const std::unordered_map<address, Account>& accounts)
28 : {
29 53 : MPT trie;
30 265 : for (const auto& [addr, acc] : accounts)
31 : {
32 212 : trie.insert(keccak256(addr),
33 424 : rlp::encode_tuple(acc.nonce, acc.balance, mpt_hash(acc.storage), keccak256(acc.code)));
34 : }
35 106 : return trie.hash();
36 53 : }
37 :
38 : template <typename T>
39 159 : hash256 mpt_hash(std::span<const T> list)
40 : {
41 159 : MPT trie;
42 265 : for (size_t i = 0; i < list.size(); ++i)
43 106 : trie.insert(rlp::encode(i), rlp::encode(list[i]));
44 :
45 318 : return trie.hash();
46 159 : }
47 :
48 : template hash256 mpt_hash<Transaction>(std::span<const Transaction>);
49 : template hash256 mpt_hash<TransactionReceipt>(std::span<const TransactionReceipt>);
50 : template hash256 mpt_hash<Withdrawal>(std::span<const Withdrawal>);
51 :
52 : } // namespace evmone::state
|