github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/trie/trie.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package trie implements Merkle Patricia Tries. 18 package trie 19 20 import ( 21 "bytes" 22 "errors" 23 "fmt" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/log" 28 ) 29 30 var ( 31 // emptyRoot is the known root hash of an empty trie. 32 emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 33 34 // emptyState is the known hash of an empty state trie entry. 35 emptyState = crypto.Keccak256Hash(nil) 36 ) 37 38 // LeafCallback is a callback type invoked when a trie operation reaches a leaf 39 // node. 40 // 41 // The keys is a path tuple identifying a particular trie node either in a single 42 // trie (account) or a layered trie (account -> storage). Each key in the tuple 43 // is in the raw format(32 bytes). 44 // 45 // The path is a composite hexary path identifying the trie node. All the key 46 // bytes are converted to the hexary nibbles and composited with the parent path 47 // if the trie node is in a layered trie. 48 // 49 // It's used by state sync and commit to allow handling external references 50 // between account and storage tries. And also it's used in the state healing 51 // for extracting the raw states(leaf nodes) with corresponding paths. 52 type LeafCallback func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error 53 54 // Trie is a Merkle Patricia Trie. Use New to create a trie that sits on 55 // top of a database. Whenever trie performs a commit operation, the generated 56 // nodes will be gathered and returned in a set. Once the trie is committed, 57 // it's not usable anymore. Callers have to re-create the trie with new root 58 // based on the updated trie database. 59 // 60 // Trie is not safe for concurrent use. 61 type Trie struct { 62 root node 63 owner common.Hash 64 65 // Keep track of the number leaves which have been inserted since the last 66 // hashing operation. This number will not directly map to the number of 67 // actually unhashed nodes. 68 unhashed int 69 70 // reader is the handler trie can retrieve nodes from. 71 reader *trieReader 72 73 // tracer is the tool to track the trie changes. 74 // It will be reset after each commit operation. 75 tracer *tracer 76 } 77 78 // newFlag returns the cache flag value for a newly created node. 79 func (t *Trie) newFlag() nodeFlag { 80 return nodeFlag{dirty: true} 81 } 82 83 // Copy returns a copy of Trie. 84 func (t *Trie) Copy() *Trie { 85 return &Trie{ 86 root: t.root, 87 owner: t.owner, 88 unhashed: t.unhashed, 89 reader: t.reader, 90 tracer: t.tracer.copy(), 91 } 92 } 93 94 // New creates the trie instance with provided trie id and the read-only 95 // database. The state specified by trie id must be available, otherwise 96 // an error will be returned. The trie root specified by trie id can be 97 // zero hash or the sha3 hash of an empty string, then trie is initially 98 // empty, otherwise, the root node must be present in database or returns 99 // a MissingNodeError if not. 100 func New(id *ID, db NodeReader) (*Trie, error) { 101 reader, err := newTrieReader(id.StateRoot, id.Owner, db) 102 if err != nil { 103 return nil, err 104 } 105 trie := &Trie{ 106 owner: id.Owner, 107 reader: reader, 108 //tracer: newTracer(), 109 } 110 if id.Root != (common.Hash{}) && id.Root != emptyRoot { 111 rootnode, err := trie.resolveAndTrack(id.Root[:], nil) 112 if err != nil { 113 return nil, err 114 } 115 trie.root = rootnode 116 } 117 return trie, nil 118 } 119 120 // NewEmpty is a shortcut to create empty tree. It's mostly used in tests. 121 func NewEmpty(db *Database) *Trie { 122 tr, _ := New(TrieID(common.Hash{}), db) 123 return tr 124 } 125 126 // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at 127 // the key after the given start key. 128 func (t *Trie) NodeIterator(start []byte) NodeIterator { 129 return newNodeIterator(t, start) 130 } 131 132 // Get returns the value for key stored in the trie. 133 // The value bytes must not be modified by the caller. 134 func (t *Trie) Get(key []byte) []byte { 135 res, err := t.TryGet(key) 136 if err != nil { 137 log.Error("Unhandled trie error in Trie.Get", "err", err) 138 } 139 return res 140 } 141 142 // TryGet returns the value for key stored in the trie. 143 // The value bytes must not be modified by the caller. 144 // If a node was not found in the database, a MissingNodeError is returned. 145 func (t *Trie) TryGet(key []byte) ([]byte, error) { 146 value, newroot, didResolve, err := t.tryGet(t.root, keybytesToHex(key), 0) 147 if err == nil && didResolve { 148 t.root = newroot 149 } 150 return value, err 151 } 152 153 func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) { 154 switch n := (origNode).(type) { 155 case nil: 156 return nil, nil, false, nil 157 case valueNode: 158 return n, n, false, nil 159 case *shortNode: 160 if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) { 161 // key not found in trie 162 return nil, n, false, nil 163 } 164 value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key)) 165 if err == nil && didResolve { 166 n = n.copy() 167 n.Val = newnode 168 } 169 return value, n, didResolve, err 170 case *fullNode: 171 value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1) 172 if err == nil && didResolve { 173 n = n.copy() 174 n.Children[key[pos]] = newnode 175 } 176 return value, n, didResolve, err 177 case hashNode: 178 child, err := t.resolveAndTrack(n, key[:pos]) 179 if err != nil { 180 return nil, n, true, err 181 } 182 value, newnode, _, err := t.tryGet(child, key, pos) 183 return value, newnode, true, err 184 default: 185 panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode)) 186 } 187 } 188 189 // TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not 190 // possible to use keybyte-encoding as the path might contain odd nibbles. 191 func (t *Trie) TryGetNode(path []byte) ([]byte, int, error) { 192 item, newroot, resolved, err := t.tryGetNode(t.root, compactToHex(path), 0) 193 if err != nil { 194 return nil, resolved, err 195 } 196 if resolved > 0 { 197 t.root = newroot 198 } 199 if item == nil { 200 return nil, resolved, nil 201 } 202 return item, resolved, err 203 } 204 205 func (t *Trie) tryGetNode(origNode node, path []byte, pos int) (item []byte, newnode node, resolved int, err error) { 206 // If non-existent path requested, abort 207 if origNode == nil { 208 return nil, nil, 0, nil 209 } 210 // If we reached the requested path, return the current node 211 if pos >= len(path) { 212 // Although we most probably have the original node expanded, encoding 213 // that into consensus form can be nasty (needs to cascade down) and 214 // time consuming. Instead, just pull the hash up from disk directly. 215 var hash hashNode 216 if node, ok := origNode.(hashNode); ok { 217 hash = node 218 } else { 219 hash, _ = origNode.cache() 220 } 221 if hash == nil { 222 return nil, origNode, 0, errors.New("non-consensus node") 223 } 224 blob, err := t.reader.nodeBlob(path, common.BytesToHash(hash)) 225 return blob, origNode, 1, err 226 } 227 // Path still needs to be traversed, descend into children 228 switch n := (origNode).(type) { 229 case valueNode: 230 // Path prematurely ended, abort 231 return nil, nil, 0, nil 232 233 case *shortNode: 234 if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) { 235 // Path branches off from short node 236 return nil, n, 0, nil 237 } 238 item, newnode, resolved, err = t.tryGetNode(n.Val, path, pos+len(n.Key)) 239 if err == nil && resolved > 0 { 240 n = n.copy() 241 n.Val = newnode 242 } 243 return item, n, resolved, err 244 245 case *fullNode: 246 item, newnode, resolved, err = t.tryGetNode(n.Children[path[pos]], path, pos+1) 247 if err == nil && resolved > 0 { 248 n = n.copy() 249 n.Children[path[pos]] = newnode 250 } 251 return item, n, resolved, err 252 253 case hashNode: 254 child, err := t.resolveAndTrack(n, path[:pos]) 255 if err != nil { 256 return nil, n, 1, err 257 } 258 item, newnode, resolved, err := t.tryGetNode(child, path, pos) 259 return item, newnode, resolved + 1, err 260 261 default: 262 panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode)) 263 } 264 } 265 266 // Update associates key with value in the trie. Subsequent calls to 267 // Get will return value. If value has length zero, any existing value 268 // is deleted from the trie and calls to Get will return nil. 269 // 270 // The value bytes must not be modified by the caller while they are 271 // stored in the trie. 272 func (t *Trie) Update(key, value []byte) { 273 if err := t.TryUpdate(key, value); err != nil { 274 log.Error("Unhandled trie error in Trie.Update", "err", err) 275 } 276 } 277 278 // TryUpdate associates key with value in the trie. Subsequent calls to 279 // Get will return value. If value has length zero, any existing value 280 // is deleted from the trie and calls to Get will return nil. 281 // 282 // The value bytes must not be modified by the caller while they are 283 // stored in the trie. 284 // 285 // If a node was not found in the database, a MissingNodeError is returned. 286 func (t *Trie) TryUpdate(key, value []byte) error { 287 return t.tryUpdate(key, value) 288 } 289 290 // tryUpdate expects an RLP-encoded value and performs the core function 291 // for TryUpdate and TryUpdateAccount. 292 func (t *Trie) tryUpdate(key, value []byte) error { 293 t.unhashed++ 294 k := keybytesToHex(key) 295 if len(value) != 0 { 296 _, n, err := t.insert(t.root, nil, k, valueNode(value)) 297 if err != nil { 298 return err 299 } 300 t.root = n 301 } else { 302 _, n, err := t.delete(t.root, nil, k) 303 if err != nil { 304 return err 305 } 306 t.root = n 307 } 308 return nil 309 } 310 311 func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { 312 if len(key) == 0 { 313 if v, ok := n.(valueNode); ok { 314 return !bytes.Equal(v, value.(valueNode)), value, nil 315 } 316 return true, value, nil 317 } 318 switch n := n.(type) { 319 case *shortNode: 320 matchlen := prefixLen(key, n.Key) 321 // If the whole key matches, keep this short node as is 322 // and only update the value. 323 if matchlen == len(n.Key) { 324 dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value) 325 if !dirty || err != nil { 326 return false, n, err 327 } 328 return true, &shortNode{n.Key, nn, t.newFlag()}, nil 329 } 330 // Otherwise branch out at the index where they differ. 331 branch := &fullNode{flags: t.newFlag()} 332 var err error 333 _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) 334 if err != nil { 335 return false, nil, err 336 } 337 _, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value) 338 if err != nil { 339 return false, nil, err 340 } 341 // Replace this shortNode with the branch if it occurs at index 0. 342 if matchlen == 0 { 343 return true, branch, nil 344 } 345 // New branch node is created as a child of the original short node. 346 // Track the newly inserted node in the tracer. The node identifier 347 // passed is the path from the root node. 348 t.tracer.onInsert(append(prefix, key[:matchlen]...)) 349 350 // Replace it with a short node leading up to the branch. 351 return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil 352 353 case *fullNode: 354 dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) 355 if !dirty || err != nil { 356 return false, n, err 357 } 358 n = n.copy() 359 n.flags = t.newFlag() 360 n.Children[key[0]] = nn 361 return true, n, nil 362 363 case nil: 364 // New short node is created and track it in the tracer. The node identifier 365 // passed is the path from the root node. Note the valueNode won't be tracked 366 // since it's always embedded in its parent. 367 t.tracer.onInsert(prefix) 368 369 return true, &shortNode{key, value, t.newFlag()}, nil 370 371 case hashNode: 372 // We've hit a part of the trie that isn't loaded yet. Load 373 // the node and insert into it. This leaves all child nodes on 374 // the path to the value in the trie. 375 rn, err := t.resolveAndTrack(n, prefix) 376 if err != nil { 377 return false, nil, err 378 } 379 dirty, nn, err := t.insert(rn, prefix, key, value) 380 if !dirty || err != nil { 381 return false, rn, err 382 } 383 return true, nn, nil 384 385 default: 386 panic(fmt.Sprintf("%T: invalid node: %v", n, n)) 387 } 388 } 389 390 // Delete removes any existing value for key from the trie. 391 func (t *Trie) Delete(key []byte) { 392 if err := t.TryDelete(key); err != nil { 393 log.Error("Unhandled trie error in Trie.Delete", "err", err) 394 } 395 } 396 397 // TryDelete removes any existing value for key from the trie. 398 // If a node was not found in the database, a MissingNodeError is returned. 399 func (t *Trie) TryDelete(key []byte) error { 400 t.unhashed++ 401 k := keybytesToHex(key) 402 _, n, err := t.delete(t.root, nil, k) 403 if err != nil { 404 return err 405 } 406 t.root = n 407 return nil 408 } 409 410 // delete returns the new root of the trie with key deleted. 411 // It reduces the trie to minimal form by simplifying 412 // nodes on the way up after deleting recursively. 413 func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { 414 switch n := n.(type) { 415 case *shortNode: 416 matchlen := prefixLen(key, n.Key) 417 if matchlen < len(n.Key) { 418 return false, n, nil // don't replace n on mismatch 419 } 420 if matchlen == len(key) { 421 // The matched short node is deleted entirely and track 422 // it in the deletion set. The same the valueNode doesn't 423 // need to be tracked at all since it's always embedded. 424 t.tracer.onDelete(prefix) 425 426 return true, nil, nil // remove n entirely for whole matches 427 } 428 // The key is longer than n.Key. Remove the remaining suffix 429 // from the subtrie. Child can never be nil here since the 430 // subtrie must contain at least two other values with keys 431 // longer than n.Key. 432 dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):]) 433 if !dirty || err != nil { 434 return false, n, err 435 } 436 switch child := child.(type) { 437 case *shortNode: 438 // The child shortNode is merged into its parent, track 439 // is deleted as well. 440 t.tracer.onDelete(append(prefix, n.Key...)) 441 442 // Deleting from the subtrie reduced it to another 443 // short node. Merge the nodes to avoid creating a 444 // shortNode{..., shortNode{...}}. Use concat (which 445 // always creates a new slice) instead of append to 446 // avoid modifying n.Key since it might be shared with 447 // other nodes. 448 return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil 449 default: 450 return true, &shortNode{n.Key, child, t.newFlag()}, nil 451 } 452 453 case *fullNode: 454 dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) 455 if !dirty || err != nil { 456 return false, n, err 457 } 458 n = n.copy() 459 n.flags = t.newFlag() 460 n.Children[key[0]] = nn 461 462 // Because n is a full node, it must've contained at least two children 463 // before the delete operation. If the new child value is non-nil, n still 464 // has at least two children after the deletion, and cannot be reduced to 465 // a short node. 466 if nn != nil { 467 return true, n, nil 468 } 469 // Reduction: 470 // Check how many non-nil entries are left after deleting and 471 // reduce the full node to a short node if only one entry is 472 // left. Since n must've contained at least two children 473 // before deletion (otherwise it would not be a full node) n 474 // can never be reduced to nil. 475 // 476 // When the loop is done, pos contains the index of the single 477 // value that is left in n or -2 if n contains at least two 478 // values. 479 pos := -1 480 for i, cld := range &n.Children { 481 if cld != nil { 482 if pos == -1 { 483 pos = i 484 } else { 485 pos = -2 486 break 487 } 488 } 489 } 490 if pos >= 0 { 491 if pos != 16 { 492 // If the remaining entry is a short node, it replaces 493 // n and its key gets the missing nibble tacked to the 494 // front. This avoids creating an invalid 495 // shortNode{..., shortNode{...}}. Since the entry 496 // might not be loaded yet, resolve it just for this 497 // check. 498 cnode, err := t.resolve(n.Children[pos], append(prefix, byte(pos))) 499 if err != nil { 500 return false, nil, err 501 } 502 if cnode, ok := cnode.(*shortNode); ok { 503 // Replace the entire full node with the short node. 504 // Mark the original short node as deleted since the 505 // value is embedded into the parent now. 506 t.tracer.onDelete(append(prefix, byte(pos))) 507 508 k := append([]byte{byte(pos)}, cnode.Key...) 509 return true, &shortNode{k, cnode.Val, t.newFlag()}, nil 510 } 511 } 512 // Otherwise, n is replaced by a one-nibble short node 513 // containing the child. 514 return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil 515 } 516 // n still contains at least two values and cannot be reduced. 517 return true, n, nil 518 519 case valueNode: 520 return true, nil, nil 521 522 case nil: 523 return false, nil, nil 524 525 case hashNode: 526 // We've hit a part of the trie that isn't loaded yet. Load 527 // the node and delete from it. This leaves all child nodes on 528 // the path to the value in the trie. 529 rn, err := t.resolveAndTrack(n, prefix) 530 if err != nil { 531 return false, nil, err 532 } 533 dirty, nn, err := t.delete(rn, prefix, key) 534 if !dirty || err != nil { 535 return false, rn, err 536 } 537 return true, nn, nil 538 539 default: 540 panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) 541 } 542 } 543 544 func concat(s1 []byte, s2 ...byte) []byte { 545 r := make([]byte, len(s1)+len(s2)) 546 copy(r, s1) 547 copy(r[len(s1):], s2) 548 return r 549 } 550 551 func (t *Trie) resolve(n node, prefix []byte) (node, error) { 552 if n, ok := n.(hashNode); ok { 553 return t.resolveAndTrack(n, prefix) 554 } 555 return n, nil 556 } 557 558 // resolveAndTrack loads node from the underlying store with the given node hash 559 // and path prefix and also tracks the loaded node blob in tracer treated as the 560 // node's original value. The rlp-encoded blob is preferred to be loaded from 561 // database because it's easy to decode node while complex to encode node to blob. 562 func (t *Trie) resolveAndTrack(n hashNode, prefix []byte) (node, error) { 563 blob, err := t.reader.nodeBlob(prefix, common.BytesToHash(n)) 564 if err != nil { 565 return nil, err 566 } 567 t.tracer.onRead(prefix, blob) 568 return mustDecodeNode(n, blob), nil 569 } 570 571 // Hash returns the root hash of the trie. It does not write to the 572 // database and can be used even if the trie doesn't have one. 573 func (t *Trie) Hash() common.Hash { 574 hash, cached, _ := t.hashRoot() 575 t.root = cached 576 return common.BytesToHash(hash.(hashNode)) 577 } 578 579 // Commit collects all dirty nodes in the trie and replaces them with the 580 // corresponding node hash. All collected nodes (including dirty leaves if 581 // collectLeaf is true) will be encapsulated into a nodeset for return. 582 // The returned nodeset can be nil if the trie is clean (nothing to commit). 583 // Once the trie is committed, it's not usable anymore. A new trie must 584 // be created with new root and updated trie database for following usage 585 func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) { 586 defer t.tracer.reset() 587 588 if t.root == nil { 589 return emptyRoot, nil, nil 590 } 591 // Derive the hash for all dirty nodes first. We hold the assumption 592 // in the following procedure that all nodes are hashed. 593 rootHash := t.Hash() 594 595 // Do a quick check if we really need to commit. This can happen e.g. 596 // if we load a trie for reading storage values, but don't write to it. 597 if hashedNode, dirty := t.root.cache(); !dirty { 598 // Replace the root node with the origin hash in order to 599 // ensure all resolved nodes are dropped after the commit. 600 t.root = hashedNode 601 return rootHash, nil, nil 602 } 603 h := newCommitter(t.owner, t.tracer, collectLeaf) 604 newRoot, nodes, err := h.Commit(t.root) 605 if err != nil { 606 return common.Hash{}, nil, err 607 } 608 t.root = newRoot 609 return rootHash, nodes, nil 610 } 611 612 // hashRoot calculates the root hash of the given trie 613 func (t *Trie) hashRoot() (node, node, error) { 614 if t.root == nil { 615 return hashNode(emptyRoot.Bytes()), nil, nil 616 } 617 // If the number of changes is below 100, we let one thread handle it 618 h := newHasher(t.unhashed >= 100) 619 defer returnHasherToPool(h) 620 hashed, cached := h.hash(t.root, true) 621 t.unhashed = 0 622 return hashed, cached, nil 623 } 624 625 // Reset drops the referenced root node and cleans all internal state. 626 func (t *Trie) Reset() { 627 t.root = nil 628 t.owner = common.Hash{} 629 t.unhashed = 0 630 t.tracer.reset() 631 }