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