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