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