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