Skip to content

Commit 596df26

Browse files
committed
patch: node_hash reversion
#14486 accidentally reverted; reinstates changes from #13758
1 parent b61d4be commit 596df26

File tree

1 file changed

+21
-21
lines changed
  • public/content/developers/docs/data-structures-and-encoding/patricia-merkle-trie

1 file changed

+21
-21
lines changed

public/content/developers/docs/data-structures-and-encoding/patricia-merkle-trie/index.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ lang: en
55
sidebarDepth: 2
66
---
77

8-
The state of Ethereum (the totality of all accounts, balances, and smart contracts), is encoded into a special version of the data structure known generally in computer science as a Merkle Tree. This structure is useful for many applications in cryptography because it creates a verifiable relationship between all the individual pieces of data entangled in the tree, resulting in a single **root** value that can be used to prove things about the data.
8+
The state of Ethereum (the totality of all accounts, balances, and smart contracts), is encoded into a special version of the data structure known generally in computer science as a Merkle Tree. This structure is useful for many applications in cryptography because it creates a verifiable relationship between all the individual pieces of data entangled in the tree, resulting in a single **root** value that can be used to prove things about the data.
99

10-
Ethereum's data structure is a 'modified Merkle-Patricia Trie', named so because it borrows some features of PATRICIA (the Practical Algorithm To Retrieve Information Coded in Alphanumeric), and because it is designed for efficient data re**trie**val of items that comprise the Ethereum state.
10+
Ethereum's data structure is a 'modified Merkle-Patricia Trie', named so because it borrows some features of PATRICIA (the Practical Algorithm To Retrieve Information Coded in Alphanumeric), and because it is designed for efficient data re**trie**val of items that comprise the Ethereum state.
1111

12-
A Merkle-Patricia trie is deterministic and cryptographically verifiable: The only way to generate a state root is by computing it from each individual piece of the state, and two states that are identical can be easily proven so by comparing the root hash and the hashes that led to it (_a Merkle proof_). Conversely, there is no way to create two different states with the same root hash, and any attempt to modify state with different values will result in a different state root hash. Theoretically, this structure provides the 'holy grail' of `O(log(n))` efficiency for inserts, lookups and deletes.
12+
A Merkle-Patricia trie is deterministic and cryptographically verifiable: The only way to generate a state root is by computing it from each individual piece of the state, and two states that are identical can be easily proven so by comparing the root hash and the hashes that led to it (_a Merkle proof_). Conversely, there is no way to create two different states with the same root hash, and any attempt to modify state with different values will result in a different state root hash. Theoretically, this structure provides the 'holy grail' of `O(log(n))` efficiency for inserts, lookups and deletes.
1313

14-
In the near future, Ethereum plans to migrate to a [Verkle Tree](https://ethereum.org/en/roadmap/verkle-trees) structure, which will open up many new possibilities for future protocol improvements.
14+
In the near future, Ethereum plans to migrate to a [Verkle Tree](https://ethereum.org/en/roadmap/verkle-trees) structure, which will open up many new possibilities for future protocol improvements.
1515

1616
## Prerequisites {#prerequisites}
1717

18-
To better understand this page, it would be helpful to have basic knowledge of [hashes](https://en.wikipedia.org/wiki/Hash_function), [Merkle trees](https://en.wikipedia.org/wiki/Merkle_tree), [tries](https://en.wikipedia.org/wiki/Trie) and [serialization](https://en.wikipedia.org/wiki/Serialization). This article begins with a description of a basic [radix tree](https://en.wikipedia.org/wiki/Radix_tree), then gradually introduces the modifications necessary for Ethereum's more optimized data structure.
18+
To better understand this page, it would be helpful to have basic knowledge of [hashes](https://en.wikipedia.org/wiki/Hash_function), [Merkle trees](https://en.wikipedia.org/wiki/Merkle_tree), [tries](https://en.wikipedia.org/wiki/Trie) and [serialization](https://en.wikipedia.org/wiki/Serialization). This article begins with a description of a basic [radix tree](https://en.wikipedia.org/wiki/Radix_tree), then gradually introduces the modifications necessary for Ethereum's more optimized data structure.
1919

2020
## Basic radix tries {#basic-radix-tries}
2121

@@ -34,8 +34,8 @@ There is a difference between looking something up in the 'trie' and the underly
3434
The update and delete operations for radix tries can be defined as follows:
3535

3636
```python
37-
def update(node, path, value):
38-
curnode = db.get(node) if node else [NULL] * 17
37+
def update(node_hash, path, value):
38+
curnode = db.get(node_hash) if node_hash else [NULL] * 17
3939
newnode = curnode.copy()
4040
if path == "":
4141
newnode[-1] = value
@@ -45,8 +45,8 @@ The update and delete operations for radix tries can be defined as follows:
4545
db.put(hash(newnode), newnode)
4646
return hash(newnode)
4747

48-
def delete(node, path):
49-
if node is NULL:
48+
def delete(node_hash, path):
49+
if node_hash is NULL:
5050
return NULL
5151
else:
5252
curnode = db.get(node_hash)
@@ -95,12 +95,12 @@ When traversing paths in nibbles, we may end up with an odd number of nibbles to
9595

9696
The flagging of both _odd vs. even remaining partial path length_ and _leaf vs. extension node_ as described above reside in the first nibble of the partial path of any 2-item node. They result in the following:
9797

98-
| hex char | bits | node type partial | path length |
99-
| -------- | ---- | ------------------ | ----------- |
100-
| 0 | 0000 | extension | even |
101-
| 1 | 0001 | extension | odd |
102-
| 2 | 0010 | terminating (leaf) | even |
103-
| 3 | 0011 | terminating (leaf) | odd |
98+
| hex char | bits | node type partial | path length |
99+
| -------- | ---- | ------------------ | ----------- |
100+
| 0 | 0000 | extension | even |
101+
| 1 | 0001 | extension | odd |
102+
| 2 | 0010 | terminating (leaf) | even |
103+
| 3 | 0011 | terminating (leaf) | odd |
104104

105105
For even remaining path length (`0` or `2`), another `0` "padding" nibble will always follow.
106106

@@ -138,12 +138,12 @@ Examples:
138138
Here is the extended code for getting a node in the Merkle Patricia trie:
139139

140140
```python
141-
def get_helper(node, path):
141+
def get_helper(node_hash, path):
142142
if path == []:
143-
return node
144-
if node == "":
143+
return node_hash
144+
if node_hash == "":
145145
return ""
146-
curnode = rlp.decode(node if len(node) < 32 else db.get(node))
146+
curnode = rlp.decode(node_hash if len(node_hash) < 32 else db.get(node_hash))
147147
if len(curnode) == 2:
148148
(k2, v2) = curnode
149149
k2 = compact_decode(k2)
@@ -154,13 +154,13 @@ Here is the extended code for getting a node in the Merkle Patricia trie:
154154
elif len(curnode) == 17:
155155
return get_helper(curnode[path[0]], path[1:])
156156

157-
def get(node, path):
157+
def get(node_hash, path):
158158
path2 = []
159159
for i in range(len(path)):
160160
path2.push(int(ord(path[i]) / 16))
161161
path2.push(ord(path[i]) % 16)
162162
path2.push(16)
163-
return get_helper(node, path2)
163+
return get_helper(node_hash, path2)
164164
```
165165

166166
### Example Trie {#example-trie}

0 commit comments

Comments
 (0)