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