github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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 paths is a path tuple identifying a particular trie node either in a single 46 // trie (account) or a layered trie (account -> storage). Each path in the tuple 47 // is in the raw format(32 bytes). 48 // 49 // The hexpath 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(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) 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 67 // Keep track of the number leaves which have been inserted since the last 68 // hashing operation. This number will not directly map to the number of 69 // actually unhashed nodes 70 unhashed int 71 72 // tracer is the state diff tracer can be used to track newly added/deleted 73 // trie node. It will be reset after each commit operation. 74 tracer *tracer 75 } 76 77 // newFlag returns the cache flag value for a newly created node. 78 func (t *Trie) newFlag() nodeFlag { 79 return nodeFlag{dirty: true} 80 } 81 82 // Copy returns a copy of Trie. 83 func (t *Trie) Copy() *Trie { 84 return &Trie{ 85 db: t.db, 86 root: t.root, 87 unhashed: t.unhashed, 88 tracer: t.tracer.copy(), 89 } 90 } 91 92 // New creates a trie with an existing root node from db. 93 // 94 // If root is the zero hash or the sha3 hash of an empty string, the 95 // trie is initially empty and does not require a database. Otherwise, 96 // New will panic if db is nil and returns a MissingNodeError if root does 97 // not exist in the database. Accessing the trie loads nodes from db on demand. 98 func New(root common.Hash, db *Database) (*Trie, error) { 99 if db == nil { 100 panic("trie.New called without a database") 101 } 102 trie := &Trie{ 103 db: db, 104 //tracer: newTracer(), 105 } 106 if root != (common.Hash{}) && root != emptyRoot { 107 rootnode, err := trie.resolveHash(root[:], nil) 108 if err != nil { 109 return nil, err 110 } 111 trie.root = rootnode 112 } 113 return trie, nil 114 } 115 116 // newWithRootNode initializes the trie with the given root node. 117 // It's only used by range prover. 118 func newWithRootNode(root node) *Trie { 119 return &Trie{ 120 root: root, 121 //tracer: newTracer(), 122 db: NewDatabase(rawdb.NewMemoryDatabase()), 123 } 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(fmt.Sprintf("Unhandled trie error: %v", 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.resolveHash(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.db.Node(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.resolveHash(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(fmt.Sprintf("Unhandled trie error: %v", err)) 275 } 276 } 277 278 func (t *Trie) TryUpdateAccount(key []byte, acc *types.StateAccount) error { 279 data, err := rlp.EncodeToBytes(acc) 280 if err != nil { 281 return fmt.Errorf("can't encode object at %x: %w", key[:], err) 282 } 283 return t.TryUpdate(key, data) 284 } 285 286 // TryUpdate associates key with value in the trie. Subsequent calls to 287 // Get will return value. If value has length zero, any existing value 288 // is deleted from the trie and calls to Get will return nil. 289 // 290 // The value bytes must not be modified by the caller while they are 291 // stored in the trie. 292 // 293 // If a node was not found in the database, a MissingNodeError is returned. 294 func (t *Trie) TryUpdate(key, value []byte) error { 295 t.unhashed++ 296 k := keybytesToHex(key) 297 if len(value) != 0 { 298 _, n, err := t.insert(t.root, nil, k, valueNode(value)) 299 if err != nil { 300 return err 301 } 302 t.root = n 303 } else { 304 _, n, err := t.delete(t.root, nil, k) 305 if err != nil { 306 return err 307 } 308 t.root = n 309 } 310 return nil 311 } 312 313 func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { 314 if len(key) == 0 { 315 if v, ok := n.(valueNode); ok { 316 return !bytes.Equal(v, value.(valueNode)), value, nil 317 } 318 return true, value, nil 319 } 320 switch n := n.(type) { 321 case *shortNode: 322 matchlen := prefixLen(key, n.Key) 323 // If the whole key matches, keep this short node as is 324 // and only update the value. 325 if matchlen == len(n.Key) { 326 dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value) 327 if !dirty || err != nil { 328 return false, n, err 329 } 330 return true, &shortNode{n.Key, nn, t.newFlag()}, nil 331 } 332 // Otherwise branch out at the index where they differ. 333 branch := &fullNode{flags: t.newFlag()} 334 var err error 335 _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) 336 if err != nil { 337 return false, nil, err 338 } 339 _, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value) 340 if err != nil { 341 return false, nil, err 342 } 343 // Replace this shortNode with the branch if it occurs at index 0. 344 if matchlen == 0 { 345 return true, branch, nil 346 } 347 // New branch node is created as a child of the original short node. 348 // Track the newly inserted node in the tracer. The node identifier 349 // passed is the path from the root node. 350 t.tracer.onInsert(append(prefix, key[:matchlen]...)) 351 352 // Replace it with a short node leading up to the branch. 353 return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil 354 355 case *fullNode: 356 dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) 357 if !dirty || err != nil { 358 return false, n, err 359 } 360 n = n.copy() 361 n.flags = t.newFlag() 362 n.Children[key[0]] = nn 363 return true, n, nil 364 365 case nil: 366 // New short node is created and track it in the tracer. The node identifier 367 // passed is the path from the root node. Note the valueNode won't be tracked 368 // since it's always embedded in its parent. 369 t.tracer.onInsert(prefix) 370 371 return true, &shortNode{key, value, t.newFlag()}, nil 372 373 case hashNode: 374 // We've hit a part of the trie that isn't loaded yet. Load 375 // the node and insert into it. This leaves all child nodes on 376 // the path to the value in the trie. 377 rn, err := t.resolveHash(n, prefix) 378 if err != nil { 379 return false, nil, err 380 } 381 dirty, nn, err := t.insert(rn, prefix, key, value) 382 if !dirty || err != nil { 383 return false, rn, err 384 } 385 return true, nn, nil 386 387 default: 388 panic(fmt.Sprintf("%T: invalid node: %v", n, n)) 389 } 390 } 391 392 // Delete removes any existing value for key from the trie. 393 func (t *Trie) Delete(key []byte) { 394 if err := t.TryDelete(key); err != nil { 395 log.Error(fmt.Sprintf("Unhandled trie error: %v", err)) 396 } 397 } 398 399 // TryDelete removes any existing value for key from the trie. 400 // If a node was not found in the database, a MissingNodeError is returned. 401 func (t *Trie) TryDelete(key []byte) error { 402 t.unhashed++ 403 k := keybytesToHex(key) 404 _, n, err := t.delete(t.root, nil, k) 405 if err != nil { 406 return err 407 } 408 t.root = n 409 return nil 410 } 411 412 // delete returns the new root of the trie with key deleted. 413 // It reduces the trie to minimal form by simplifying 414 // nodes on the way up after deleting recursively. 415 func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { 416 switch n := n.(type) { 417 case *shortNode: 418 matchlen := prefixLen(key, n.Key) 419 if matchlen < len(n.Key) { 420 return false, n, nil // don't replace n on mismatch 421 } 422 if matchlen == len(key) { 423 // The matched short node is deleted entirely and track 424 // it in the deletion set. The same the valueNode doesn't 425 // need to be tracked at all since it's always embedded. 426 t.tracer.onDelete(prefix) 427 428 return true, nil, nil // remove n entirely for whole matches 429 } 430 // The key is longer than n.Key. Remove the remaining suffix 431 // from the subtrie. Child can never be nil here since the 432 // subtrie must contain at least two other values with keys 433 // longer than n.Key. 434 dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):]) 435 if !dirty || err != nil { 436 return false, n, err 437 } 438 switch child := child.(type) { 439 case *shortNode: 440 // The child shortNode is merged into its parent, track 441 // is deleted as well. 442 t.tracer.onDelete(append(prefix, n.Key...)) 443 444 // Deleting from the subtrie reduced it to another 445 // short node. Merge the nodes to avoid creating a 446 // shortNode{..., shortNode{...}}. Use concat (which 447 // always creates a new slice) instead of append to 448 // avoid modifying n.Key since it might be shared with 449 // other nodes. 450 return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil 451 default: 452 return true, &shortNode{n.Key, child, t.newFlag()}, nil 453 } 454 455 case *fullNode: 456 dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) 457 if !dirty || err != nil { 458 return false, n, err 459 } 460 n = n.copy() 461 n.flags = t.newFlag() 462 n.Children[key[0]] = nn 463 464 // Because n is a full node, it must've contained at least two children 465 // before the delete operation. If the new child value is non-nil, n still 466 // has at least two children after the deletion, and cannot be reduced to 467 // a short node. 468 if nn != nil { 469 return true, n, nil 470 } 471 // Reduction: 472 // Check how many non-nil entries are left after deleting and 473 // reduce the full node to a short node if only one entry is 474 // left. Since n must've contained at least two children 475 // before deletion (otherwise it would not be a full node) n 476 // can never be reduced to nil. 477 // 478 // When the loop is done, pos contains the index of the single 479 // value that is left in n or -2 if n contains at least two 480 // values. 481 pos := -1 482 for i, cld := range &n.Children { 483 if cld != nil { 484 if pos == -1 { 485 pos = i 486 } else { 487 pos = -2 488 break 489 } 490 } 491 } 492 if pos >= 0 { 493 if pos != 16 { 494 // If the remaining entry is a short node, it replaces 495 // n and its key gets the missing nibble tacked to the 496 // front. This avoids creating an invalid 497 // shortNode{..., shortNode{...}}. Since the entry 498 // might not be loaded yet, resolve it just for this 499 // check. 500 cnode, err := t.resolve(n.Children[pos], prefix) 501 if err != nil { 502 return false, nil, err 503 } 504 if cnode, ok := cnode.(*shortNode); ok { 505 // Replace the entire full node with the short node. 506 // Mark the original short node as deleted since the 507 // value is embedded into the parent now. 508 t.tracer.onDelete(append(prefix, byte(pos))) 509 510 k := append([]byte{byte(pos)}, cnode.Key...) 511 return true, &shortNode{k, cnode.Val, t.newFlag()}, nil 512 } 513 } 514 // Otherwise, n is replaced by a one-nibble short node 515 // containing the child. 516 return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil 517 } 518 // n still contains at least two values and cannot be reduced. 519 return true, n, nil 520 521 case valueNode: 522 return true, nil, nil 523 524 case nil: 525 return false, nil, nil 526 527 case hashNode: 528 // We've hit a part of the trie that isn't loaded yet. Load 529 // the node and delete from it. This leaves all child nodes on 530 // the path to the value in the trie. 531 rn, err := t.resolveHash(n, prefix) 532 if err != nil { 533 return false, nil, err 534 } 535 dirty, nn, err := t.delete(rn, prefix, key) 536 if !dirty || err != nil { 537 return false, rn, err 538 } 539 return true, nn, nil 540 541 default: 542 panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) 543 } 544 } 545 546 func concat(s1 []byte, s2 ...byte) []byte { 547 r := make([]byte, len(s1)+len(s2)) 548 copy(r, s1) 549 copy(r[len(s1):], s2) 550 return r 551 } 552 553 func (t *Trie) resolve(n node, prefix []byte) (node, error) { 554 if n, ok := n.(hashNode); ok { 555 return t.resolveHash(n, prefix) 556 } 557 return n, nil 558 } 559 560 func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { 561 hash := common.BytesToHash(n) 562 if node := t.db.node(hash); node != nil { 563 return node, nil 564 } 565 return nil, &MissingNodeError{NodeHash: hash, Path: prefix} 566 } 567 568 func (t *Trie) resolveBlob(n hashNode, prefix []byte) ([]byte, error) { 569 hash := common.BytesToHash(n) 570 blob, _ := t.db.Node(hash) 571 if len(blob) != 0 { 572 return blob, nil 573 } 574 return nil, &MissingNodeError{NodeHash: hash, Path: prefix} 575 } 576 577 // Hash returns the root hash of the trie. It does not write to the 578 // database and can be used even if the trie doesn't have one. 579 func (t *Trie) Hash() common.Hash { 580 hash, cached, _ := t.hashRoot() 581 t.root = cached 582 return common.BytesToHash(hash.(hashNode)) 583 } 584 585 // Commit writes all nodes to the trie's memory database, tracking the internal 586 // and external (for account tries) references. 587 func (t *Trie) Commit(onleaf LeafCallback) (common.Hash, int, error) { 588 if t.db == nil { 589 panic("commit called on trie with nil database") 590 } 591 defer t.tracer.reset() 592 593 if t.root == nil { 594 return emptyRoot, 0, nil 595 } 596 // Derive the hash for all dirty nodes first. We hold the assumption 597 // in the following procedure that all nodes are hashed. 598 rootHash := t.Hash() 599 h := newCommitter() 600 defer returnCommitterToPool(h) 601 602 // Do a quick check if we really need to commit, before we spin 603 // up goroutines. This can happen e.g. if we load a trie for reading storage 604 // values, but don't write to it. 605 if _, dirty := t.root.cache(); !dirty { 606 return rootHash, 0, nil 607 } 608 var wg sync.WaitGroup 609 if onleaf != nil { 610 h.onleaf = onleaf 611 h.leafCh = make(chan *leaf, leafChanSize) 612 wg.Add(1) 613 go func() { 614 defer wg.Done() 615 h.commitLoop(t.db) 616 }() 617 } 618 newRoot, committed, err := h.Commit(t.root, t.db) 619 if onleaf != nil { 620 // The leafch is created in newCommitter if there was an onleaf callback 621 // provided. The commitLoop only _reads_ from it, and the commit 622 // operation was the sole writer. Therefore, it's safe to close this 623 // channel here. 624 close(h.leafCh) 625 wg.Wait() 626 } 627 if err != nil { 628 return common.Hash{}, 0, err 629 } 630 t.root = newRoot 631 return rootHash, committed, nil 632 } 633 634 // hashRoot calculates the root hash of the given trie 635 func (t *Trie) hashRoot() (node, node, error) { 636 if t.root == nil { 637 return hashNode(emptyRoot.Bytes()), nil, nil 638 } 639 // If the number of changes is below 100, we let one thread handle it 640 h := newHasher(t.unhashed >= 100) 641 defer returnHasherToPool(h) 642 hashed, cached := h.hash(t.root, true) 643 t.unhashed = 0 644 return hashed, cached, nil 645 } 646 647 // Reset drops the referenced root node and cleans all internal state. 648 func (t *Trie) Reset() { 649 t.root = nil 650 t.unhashed = 0 651 t.tracer.reset() 652 }