Skip to content

Commit b61d4be

Browse files
authored
Merge pull request #14486 from Privat33r-dev/patch-2
Fix formatting and bug in pseudocode in `patricia-merkle-trie`
2 parents b3972a7 + 24b4896 commit b61d4be

File tree

1 file changed

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

1 file changed

+36
-33
lines changed

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

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ There is a difference between looking something up in the 'trie' and the underly
3333

3434
The update and delete operations for radix tries can be defined as follows:
3535

36-
```
37-
def update(node_hash, path, value):
38-
curnode = db.get(node_hash) if node_hash else [ NULL ] * 17
36+
```python
37+
def update(node, path, value):
38+
curnode = db.get(node) if node else [NULL] * 17
3939
newnode = curnode.copy()
40-
if path == '':
40+
if path == "":
4141
newnode[-1] = value
4242
else:
4343
newindex = update(curnode[path[0]], path[1:], value)
4444
newnode[path[0]] = newindex
4545
db.put(hash(newnode), newnode)
4646
return hash(newnode)
4747

48-
def delete(node_hash, path):
49-
if node_hash is NULL:
48+
def delete(node, path):
49+
if node is NULL:
5050
return NULL
5151
else:
5252
curnode = db.get(node_hash)
5353
newnode = curnode.copy()
54-
if path == '':
54+
if path == "":
5555
newnode[-1] = NULL
5656
else:
5757
newindex = delete(curnode[path[0]], path[1:])
@@ -104,60 +104,63 @@ The flagging of both _odd vs. even remaining partial path length_ and _leaf vs.
104104

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

107-
```
107+
```python
108108
def compact_encode(hexarray):
109109
term = 1 if hexarray[-1] == 16 else 0
110-
if term: hexarray = hexarray[:-1]
110+
if term:
111+
hexarray = hexarray[:-1]
111112
oddlen = len(hexarray) % 2
112113
flags = 2 * term + oddlen
113114
if oddlen:
114115
hexarray = [flags] + hexarray
115116
else:
116117
hexarray = [flags] + [0] + hexarray
117-
// hexarray now has an even length whose first nibble is the flags.
118-
o = ''
119-
for i in range(0,len(hexarray),2):
120-
o += chr(16 * hexarray[i] + hexarray[i+1])
118+
# hexarray now has an even length whose first nibble is the flags.
119+
o = ""
120+
for i in range(0, len(hexarray), 2):
121+
o += chr(16 * hexarray[i] + hexarray[i + 1])
121122
return o
122123
```
123124

124125
Examples:
125126

126-
```
127-
> [ 1, 2, 3, 4, 5, ...]
127+
```python
128+
> [1, 2, 3, 4, 5, ...]
128129
'11 23 45'
129-
> [ 0, 1, 2, 3, 4, 5, ...]
130+
> [0, 1, 2, 3, 4, 5, ...]
130131
'00 01 23 45'
131-
> [ 0, f, 1, c, b, 8, 10]
132+
> [0, f, 1, c, b, 8, 10]
132133
'20 0f 1c b8'
133-
> [ f, 1, c, b, 8, 10]
134+
> [f, 1, c, b, 8, 10]
134135
'3f 1c b8'
135136
```
136137

137138
Here is the extended code for getting a node in the Merkle Patricia trie:
138139

139-
```
140-
def get_helper(node_hash,path):
141-
if path == []: return node_hash
142-
if node_hash == '': return ''
143-
curnode = rlp.decode(node_hash if len(node_hash) < 32 else db.get(node_hash))
140+
```python
141+
def get_helper(node, path):
142+
if path == []:
143+
return node
144+
if node == "":
145+
return ""
146+
curnode = rlp.decode(node if len(node) < 32 else db.get(node))
144147
if len(curnode) == 2:
145148
(k2, v2) = curnode
146149
k2 = compact_decode(k2)
147-
if k2 == path[:len(k2)]:
148-
return get(v2, path[len(k2):])
150+
if k2 == path[: len(k2)]:
151+
return get(v2, path[len(k2) :])
149152
else:
150-
return ''
153+
return ""
151154
elif len(curnode) == 17:
152-
return get_helper(curnode[path[0]],path[1:])
155+
return get_helper(curnode[path[0]], path[1:])
153156

154-
def get(node_hash,path):
157+
def get(node, path):
155158
path2 = []
156159
for i in range(len(path)):
157160
path2.push(int(ord(path[i]) / 16))
158161
path2.push(ord(path[i]) % 16)
159162
path2.push(16)
160-
return get_helper(node_hash,path2)
163+
return get_helper(node, path2)
161164
```
162165

163166
### Example Trie {#example-trie}
@@ -205,7 +208,7 @@ There is one global state trie, and it is updated every time a client processes
205208

206209
Storage trie is where _all_ contract data lives. There is a separate storage trie for each account. To retrieve values at specific storage positions at a given address the storage address, integer position of the stored data in the storage, and the block ID are required. These can then be passed as arguments to the `eth_getStorageAt` defined in the JSON-RPC API, e.g. to retrieve the data in storage slot 0 for address `0x295a70b2de5e3953354a6a8344e616ed314d7251`:
207210

208-
```
211+
```bash
209212
curl -X POST --data '{"jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"], "id": 1}' localhost:8545
210213

211214
{"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000000000000000000000000000000000000000004d2"}
@@ -214,7 +217,7 @@ curl -X POST --data '{"jsonrpc":"2.0", "method": "eth_getStorageAt", "params": [
214217

215218
Retrieving other elements in storage is slightly more involved because the position in the storage trie must first be calculated. The position is calculated as the `keccak256` hash of the address and the storage position, both left-padded with zeros to a length of 32 bytes. For example, the position for the data in storage slot 1 for address `0x391694e7e0b0cce554cb130d723a9d27458f9298` is:
216219

217-
```
220+
```python
218221
keccak256(decodeHex("000000000000000000000000391694e7e0b0cce554cb130d723a9d27458f9298" + "0000000000000000000000000000000000000000000000000000000000000001"))
219222
```
220223

@@ -229,7 +232,7 @@ undefined
229232

230233
The `path` is therefore `keccak256(<6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9>)`. This can now be used to retrieve the data from the storage trie as before:
231234

232-
```
235+
```bash
233236
curl -X POST --data '{"jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9", "latest"], "id": 1}' localhost:8545
234237

235238
{"jsonrpc":"2.0","id":1,"result":"0x000000000000000000000000000000000000000000000000000000000000162e"}
@@ -241,7 +244,7 @@ Note: The `storageRoot` for an Ethereum account is empty by default if it's not
241244

242245
There is a separate transactions trie for every block, again storing `(key, value)` pairs. A path here is: `rlp(transactionIndex)` which represents the key that corresponds to a value determined by:
243246

244-
```
247+
```python
245248
if legacyTx:
246249
value = rlp(tx)
247250
else:

0 commit comments

Comments
 (0)