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