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