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