github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/trie/database.go (about) 1 // Copyright 2018 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 18 19 import ( 20 "fmt" 21 "io" 22 "sync" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/ethdb" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/metrics" 29 "github.com/ethereum/go-ethereum/rlp" 30 ) 31 32 var ( 33 memcacheFlushTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/flush/time", nil) 34 memcacheFlushNodesMeter = metrics.NewRegisteredMeter("trie/memcache/flush/nodes", nil) 35 memcacheFlushSizeMeter = metrics.NewRegisteredMeter("trie/memcache/flush/size", nil) 36 37 memcacheGCTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/gc/time", nil) 38 memcacheGCNodesMeter = metrics.NewRegisteredMeter("trie/memcache/gc/nodes", nil) 39 memcacheGCSizeMeter = metrics.NewRegisteredMeter("trie/memcache/gc/size", nil) 40 41 memcacheCommitTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/commit/time", nil) 42 memcacheCommitNodesMeter = metrics.NewRegisteredMeter("trie/memcache/commit/nodes", nil) 43 memcacheCommitSizeMeter = metrics.NewRegisteredMeter("trie/memcache/commit/size", nil) 44 ) 45 46 // secureKeyPrefix is the database key prefix used to store trie node preimages. 47 var secureKeyPrefix = []byte("secure-key-") 48 49 // secureKeyLength is the length of the above prefix + 32byte hash. 50 const secureKeyLength = 11 + 32 51 52 // DatabaseReader wraps the Get and Has method of a backing store for the trie. 53 type DatabaseReader interface { 54 // Get retrieves the value associated with key form the database. 55 Get(key []byte) (value []byte, err error) 56 57 // Has retrieves whether a key is present in the database. 58 Has(key []byte) (bool, error) 59 } 60 61 // Database is an intermediate write layer between the trie data structures and 62 // the disk database. The aim is to accumulate trie writes in-memory and only 63 // periodically flush a couple tries to disk, garbage collecting the remainder. 64 type Database struct { 65 diskdb ethdb.Database // Persistent storage for matured trie nodes 66 67 nodes map[common.Hash]*cachedNode // Data and references relationships of a node 68 oldest common.Hash // Oldest tracked node, flush-list head 69 newest common.Hash // Newest tracked node, flush-list tail 70 71 preimages map[common.Hash][]byte // Preimages of nodes from the secure trie 72 seckeybuf [secureKeyLength]byte // Ephemeral buffer for calculating preimage keys 73 74 gctime time.Duration // Time spent on garbage collection since last commit 75 gcnodes uint64 // Nodes garbage collected since last commit 76 gcsize common.StorageSize // Data storage garbage collected since last commit 77 78 flushtime time.Duration // Time spent on data flushing since last commit 79 flushnodes uint64 // Nodes flushed since last commit 80 flushsize common.StorageSize // Data storage flushed since last commit 81 82 nodesSize common.StorageSize // Storage size of the nodes cache (exc. flushlist) 83 preimagesSize common.StorageSize // Storage size of the preimages cache 84 85 lock sync.RWMutex 86 } 87 88 // rawNode is a simple binary blob used to differentiate between collapsed trie 89 // nodes and already encoded RLP binary blobs (while at the same time store them 90 // in the same cache fields). 91 type rawNode []byte 92 93 func (n rawNode) canUnload(uint16, uint16) bool { panic("this should never end up in a live trie") } 94 func (n rawNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 95 func (n rawNode) fstring(ind string) string { panic("this should never end up in a live trie") } 96 97 // rawFullNode represents only the useful data content of a full node, with the 98 // caches and flags stripped out to minimize its data storage. This type honors 99 // the same RLP encoding as the original parent. 100 type rawFullNode [17]node 101 102 func (n rawFullNode) canUnload(uint16, uint16) bool { panic("this should never end up in a live trie") } 103 func (n rawFullNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 104 func (n rawFullNode) fstring(ind string) string { panic("this should never end up in a live trie") } 105 106 func (n rawFullNode) EncodeRLP(w io.Writer) error { 107 var nodes [17]node 108 109 for i, child := range n { 110 if child != nil { 111 nodes[i] = child 112 } else { 113 nodes[i] = nilValueNode 114 } 115 } 116 return rlp.Encode(w, nodes) 117 } 118 119 // rawShortNode represents only the useful data content of a short node, with the 120 // caches and flags stripped out to minimize its data storage. This type honors 121 // the same RLP encoding as the original parent. 122 type rawShortNode struct { 123 Key []byte 124 Val node 125 } 126 127 func (n rawShortNode) canUnload(uint16, uint16) bool { panic("this should never end up in a live trie") } 128 func (n rawShortNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 129 func (n rawShortNode) fstring(ind string) string { panic("this should never end up in a live trie") } 130 131 // cachedNode is all the information we know about a single cached node in the 132 // memory database write layer. 133 type cachedNode struct { 134 node node // Cached collapsed trie node, or raw rlp data 135 size uint16 // Byte size of the useful cached data 136 137 parents uint16 // Number of live nodes referencing this one 138 children map[common.Hash]uint16 // External children referenced by this node 139 140 flushPrev common.Hash // Previous node in the flush-list 141 flushNext common.Hash // Next node in the flush-list 142 } 143 144 // rlp returns the raw rlp encoded blob of the cached node, either directly from 145 // the cache, or by regenerating it from the collapsed node. 146 func (n *cachedNode) rlp() []byte { 147 if node, ok := n.node.(rawNode); ok { 148 return node 149 } 150 blob, err := rlp.EncodeToBytes(n.node) 151 if err != nil { 152 panic(err) 153 } 154 return blob 155 } 156 157 // obj returns the decoded and expanded trie node, either directly from the cache, 158 // or by regenerating it from the rlp encoded blob. 159 func (n *cachedNode) obj(hash common.Hash, cachegen uint16) node { 160 if node, ok := n.node.(rawNode); ok { 161 return mustDecodeNode(hash[:], node, cachegen) 162 } 163 return expandNode(hash[:], n.node, cachegen) 164 } 165 166 // childs returns all the tracked children of this node, both the implicit ones 167 // from inside the node as well as the explicit ones from outside the node. 168 func (n *cachedNode) childs() []common.Hash { 169 children := make([]common.Hash, 0, 16) 170 for child := range n.children { 171 children = append(children, child) 172 } 173 if _, ok := n.node.(rawNode); !ok { 174 gatherChildren(n.node, &children) 175 } 176 return children 177 } 178 179 // gatherChildren traverses the node hierarchy of a collapsed storage node and 180 // retrieves all the hashnode children. 181 func gatherChildren(n node, children *[]common.Hash) { 182 switch n := n.(type) { 183 case *rawShortNode: 184 gatherChildren(n.Val, children) 185 186 case rawFullNode: 187 for i := 0; i < 16; i++ { 188 gatherChildren(n[i], children) 189 } 190 case hashNode: 191 *children = append(*children, common.BytesToHash(n)) 192 193 case valueNode, nil: 194 195 default: 196 panic(fmt.Sprintf("unknown node type: %T", n)) 197 } 198 } 199 200 // simplifyNode traverses the hierarchy of an expanded memory node and discards 201 // all the internal caches, returning a node that only contains the raw data. 202 func simplifyNode(n node) node { 203 switch n := n.(type) { 204 case *shortNode: 205 // Short nodes discard the flags and cascade 206 return &rawShortNode{Key: n.Key, Val: simplifyNode(n.Val)} 207 208 case *fullNode: 209 // Full nodes discard the flags and cascade 210 node := rawFullNode(n.Children) 211 for i := 0; i < len(node); i++ { 212 if node[i] != nil { 213 node[i] = simplifyNode(node[i]) 214 } 215 } 216 return node 217 218 case valueNode, hashNode, rawNode: 219 return n 220 221 default: 222 panic(fmt.Sprintf("unknown node type: %T", n)) 223 } 224 } 225 226 // expandNode traverses the node hierarchy of a collapsed storage node and converts 227 // all fields and keys into expanded memory form. 228 func expandNode(hash hashNode, n node, cachegen uint16) node { 229 switch n := n.(type) { 230 case *rawShortNode: 231 // Short nodes need key and child expansion 232 return &shortNode{ 233 Key: compactToHex(n.Key), 234 Val: expandNode(nil, n.Val, cachegen), 235 flags: nodeFlag{ 236 hash: hash, 237 gen: cachegen, 238 }, 239 } 240 241 case rawFullNode: 242 // Full nodes need child expansion 243 node := &fullNode{ 244 flags: nodeFlag{ 245 hash: hash, 246 gen: cachegen, 247 }, 248 } 249 for i := 0; i < len(node.Children); i++ { 250 if n[i] != nil { 251 node.Children[i] = expandNode(nil, n[i], cachegen) 252 } 253 } 254 return node 255 256 case valueNode, hashNode: 257 return n 258 259 default: 260 panic(fmt.Sprintf("unknown node type: %T", n)) 261 } 262 } 263 264 // NewDatabase creates a new trie database to store ephemeral trie content before 265 // its written out to disk or garbage collected. 266 func NewDatabase(diskdb ethdb.Database) *Database { 267 return &Database{ 268 diskdb: diskdb, 269 nodes: map[common.Hash]*cachedNode{{}: {}}, 270 preimages: make(map[common.Hash][]byte), 271 } 272 } 273 274 // DiskDB retrieves the persistent storage backing the trie database. 275 func (db *Database) DiskDB() DatabaseReader { 276 return db.diskdb 277 } 278 279 // InsertBlob writes a new reference tracked blob to the memory database if it's 280 // yet unknown. This method should only be used for non-trie nodes that require 281 // reference counting, since trie nodes are garbage collected directly through 282 // their embedded children. 283 func (db *Database) InsertBlob(hash common.Hash, blob []byte) { 284 db.lock.Lock() 285 defer db.lock.Unlock() 286 287 db.insert(hash, blob, rawNode(blob)) 288 } 289 290 // insert inserts a collapsed trie node into the memory database. This method is 291 // a more generic version of InsertBlob, supporting both raw blob insertions as 292 // well ex trie node insertions. The blob must always be specified to allow proper 293 // size tracking. 294 func (db *Database) insert(hash common.Hash, blob []byte, node node) { 295 // If the node's already cached, skip 296 if _, ok := db.nodes[hash]; ok { 297 return 298 } 299 // Create the cached entry for this node 300 entry := &cachedNode{ 301 node: simplifyNode(node), 302 size: uint16(len(blob)), 303 flushPrev: db.newest, 304 } 305 for _, child := range entry.childs() { 306 if c := db.nodes[child]; c != nil { 307 c.parents++ 308 } 309 } 310 db.nodes[hash] = entry 311 312 // Update the flush-list endpoints 313 if db.oldest == (common.Hash{}) { 314 db.oldest, db.newest = hash, hash 315 } else { 316 db.nodes[db.newest].flushNext, db.newest = hash, hash 317 } 318 db.nodesSize += common.StorageSize(common.HashLength + entry.size) 319 } 320 321 // insertPreimage writes a new trie node pre-image to the memory database if it's 322 // yet unknown. The method will make a copy of the slice. 323 // 324 // Note, this method assumes that the database's lock is held! 325 func (db *Database) insertPreimage(hash common.Hash, preimage []byte) { 326 if _, ok := db.preimages[hash]; ok { 327 return 328 } 329 db.preimages[hash] = common.CopyBytes(preimage) 330 db.preimagesSize += common.StorageSize(common.HashLength + len(preimage)) 331 } 332 333 // node retrieves a cached trie node from memory, or returns nil if none can be 334 // found in the memory cache. 335 // 从内存中检索缓存的MPT节点,如果在内存缓存中找不到任何节点,则返回nil。 336 func (db *Database) node(hash common.Hash, cachegen uint16) node { 337 // Retrieve the node from cache if available 338 db.lock.RLock() 339 node := db.nodes[hash] 340 db.lock.RUnlock() 341 342 if node != nil { 343 return node.obj(hash, cachegen) 344 } 345 // Content unavailable in memory, attempt to retrieve from disk 346 enc, err := db.diskdb.Get(hash[:]) 347 if err != nil || enc == nil { 348 return nil 349 } 350 351 // 真正根据hash接触node的函数 352 return mustDecodeNode(hash[:], enc, cachegen) 353 } 354 355 // Node retrieves an encoded cached trie node from memory. If it cannot be found 356 // cached, the method queries the persistent database for the content. 357 func (db *Database) Node(hash common.Hash) ([]byte, error) { 358 // Retrieve the node from cache if available 359 db.lock.RLock() 360 node := db.nodes[hash] 361 db.lock.RUnlock() 362 363 if node != nil { 364 return node.rlp(), nil 365 } 366 // Content unavailable in memory, attempt to retrieve from disk 367 return db.diskdb.Get(hash[:]) 368 } 369 370 // preimage retrieves a cached trie node pre-image from memory. If it cannot be 371 // found cached, the method queries the persistent database for the content. 372 func (db *Database) preimage(hash common.Hash) ([]byte, error) { 373 // Retrieve the node from cache if available 374 db.lock.RLock() 375 preimage := db.preimages[hash] 376 db.lock.RUnlock() 377 378 if preimage != nil { 379 return preimage, nil 380 } 381 // Content unavailable in memory, attempt to retrieve from disk 382 return db.diskdb.Get(db.secureKey(hash[:])) 383 } 384 385 // secureKey returns the database key for the preimage of key, as an ephemeral 386 // buffer. The caller must not hold onto the return value because it will become 387 // invalid on the next call. 388 func (db *Database) secureKey(key []byte) []byte { 389 buf := append(db.seckeybuf[:0], secureKeyPrefix...) 390 buf = append(buf, key...) 391 return buf 392 } 393 394 // Nodes retrieves the hashes of all the nodes cached within the memory database. 395 // This method is extremely expensive and should only be used to validate internal 396 // states in test code. 397 func (db *Database) Nodes() []common.Hash { 398 db.lock.RLock() 399 defer db.lock.RUnlock() 400 401 var hashes = make([]common.Hash, 0, len(db.nodes)) 402 for hash := range db.nodes { 403 if hash != (common.Hash{}) { // Special case for "root" references/nodes 404 hashes = append(hashes, hash) 405 } 406 } 407 return hashes 408 } 409 410 // Reference adds a new reference from a parent node to a child node. 411 func (db *Database) Reference(child common.Hash, parent common.Hash) { 412 db.lock.RLock() 413 defer db.lock.RUnlock() 414 415 db.reference(child, parent) 416 } 417 418 // reference is the private locked version of Reference. 419 func (db *Database) reference(child common.Hash, parent common.Hash) { 420 // If the node does not exist, it's a node pulled from disk, skip 421 node, ok := db.nodes[child] 422 if !ok { 423 return 424 } 425 // If the reference already exists, only duplicate for roots 426 if db.nodes[parent].children == nil { 427 db.nodes[parent].children = make(map[common.Hash]uint16) 428 } else if _, ok = db.nodes[parent].children[child]; ok && parent != (common.Hash{}) { 429 return 430 } 431 node.parents++ 432 db.nodes[parent].children[child]++ 433 } 434 435 // Dereference removes an existing reference from a root node. 436 func (db *Database) Dereference(root common.Hash) { 437 db.lock.Lock() 438 defer db.lock.Unlock() 439 440 nodes, storage, start := len(db.nodes), db.nodesSize, time.Now() 441 db.dereference(root, common.Hash{}) 442 443 db.gcnodes += uint64(nodes - len(db.nodes)) 444 db.gcsize += storage - db.nodesSize 445 db.gctime += time.Since(start) 446 447 memcacheGCTimeTimer.Update(time.Since(start)) 448 memcacheGCSizeMeter.Mark(int64(storage - db.nodesSize)) 449 memcacheGCNodesMeter.Mark(int64(nodes - len(db.nodes))) 450 451 log.Debug("Dereferenced trie from memory database", "nodes", nodes-len(db.nodes), "size", storage-db.nodesSize, "time", time.Since(start), 452 "gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.nodes), "livesize", db.nodesSize) 453 } 454 455 // dereference is the private locked version of Dereference. 456 func (db *Database) dereference(child common.Hash, parent common.Hash) { 457 // Dereference the parent-child 458 node := db.nodes[parent] 459 460 if node.children != nil && node.children[child] > 0 { 461 node.children[child]-- 462 if node.children[child] == 0 { 463 delete(node.children, child) 464 } 465 } 466 // If the child does not exist, it's a previously committed node. 467 node, ok := db.nodes[child] 468 if !ok { 469 return 470 } 471 // If there are no more references to the child, delete it and cascade 472 if node.parents > 0 { 473 // This is a special cornercase where a node loaded from disk (i.e. not in the 474 // memcache any more) gets reinjected as a new node (short node split into full, 475 // then reverted into short), causing a cached node to have no parents. That is 476 // no problem in itself, but don't make maxint parents out of it. 477 node.parents-- 478 } 479 if node.parents == 0 { 480 // Remove the node from the flush-list 481 if child == db.oldest { 482 db.oldest = node.flushNext 483 } else { 484 db.nodes[node.flushPrev].flushNext = node.flushNext 485 db.nodes[node.flushNext].flushPrev = node.flushPrev 486 } 487 // Dereference all children and delete the node 488 for _, hash := range node.childs() { 489 db.dereference(hash, child) 490 } 491 delete(db.nodes, child) 492 db.nodesSize -= common.StorageSize(common.HashLength + int(node.size)) 493 } 494 } 495 496 // Cap iteratively flushes old but still referenced trie nodes until the total 497 // memory usage goes below the given threshold. 498 func (db *Database) Cap(limit common.StorageSize) error { 499 // Create a database batch to flush persistent data out. It is important that 500 // outside code doesn't see an inconsistent state (referenced data removed from 501 // memory cache during commit but not yet in persistent storage). This is ensured 502 // by only uncaching existing data when the database write finalizes. 503 db.lock.RLock() 504 505 nodes, storage, start := len(db.nodes), db.nodesSize, time.Now() 506 batch := db.diskdb.NewBatch() 507 508 // db.nodesSize only contains the useful data in the cache, but when reporting 509 // the total memory consumption, the maintenance metadata is also needed to be 510 // counted. For every useful node, we track 2 extra hashes as the flushlist. 511 size := db.nodesSize + common.StorageSize((len(db.nodes)-1)*2*common.HashLength) 512 513 // If the preimage cache got large enough, push to disk. If it's still small 514 // leave for later to deduplicate writes. 515 flushPreimages := db.preimagesSize > 4*1024*1024 516 if flushPreimages { 517 for hash, preimage := range db.preimages { 518 if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil { 519 log.Error("Failed to commit preimage from trie database", "err", err) 520 db.lock.RUnlock() 521 return err 522 } 523 if batch.ValueSize() > ethdb.IdealBatchSize { 524 if err := batch.Write(); err != nil { 525 db.lock.RUnlock() 526 return err 527 } 528 batch.Reset() 529 } 530 } 531 } 532 // Keep committing nodes from the flush-list until we're below allowance 533 oldest := db.oldest 534 for size > limit && oldest != (common.Hash{}) { 535 // Fetch the oldest referenced node and push into the batch 536 node := db.nodes[oldest] 537 if err := batch.Put(oldest[:], node.rlp()); err != nil { 538 db.lock.RUnlock() 539 return err 540 } 541 // If we exceeded the ideal batch size, commit and reset 542 if batch.ValueSize() >= ethdb.IdealBatchSize { 543 if err := batch.Write(); err != nil { 544 log.Error("Failed to write flush list to disk", "err", err) 545 db.lock.RUnlock() 546 return err 547 } 548 batch.Reset() 549 } 550 // Iterate to the next flush item, or abort if the size cap was achieved. Size 551 // is the total size, including both the useful cached data (hash -> blob), as 552 // well as the flushlist metadata (2*hash). When flushing items from the cache, 553 // we need to reduce both. 554 size -= common.StorageSize(3*common.HashLength + int(node.size)) 555 oldest = node.flushNext 556 } 557 // Flush out any remainder data from the last batch 558 if err := batch.Write(); err != nil { 559 log.Error("Failed to write flush list to disk", "err", err) 560 db.lock.RUnlock() 561 return err 562 } 563 db.lock.RUnlock() 564 565 // Write successful, clear out the flushed data 566 db.lock.Lock() 567 defer db.lock.Unlock() 568 569 if flushPreimages { 570 db.preimages = make(map[common.Hash][]byte) 571 db.preimagesSize = 0 572 } 573 for db.oldest != oldest { 574 node := db.nodes[db.oldest] 575 delete(db.nodes, db.oldest) 576 db.oldest = node.flushNext 577 578 db.nodesSize -= common.StorageSize(common.HashLength + int(node.size)) 579 } 580 if db.oldest != (common.Hash{}) { 581 db.nodes[db.oldest].flushPrev = common.Hash{} 582 } 583 db.flushnodes += uint64(nodes - len(db.nodes)) 584 db.flushsize += storage - db.nodesSize 585 db.flushtime += time.Since(start) 586 587 memcacheFlushTimeTimer.Update(time.Since(start)) 588 memcacheFlushSizeMeter.Mark(int64(storage - db.nodesSize)) 589 memcacheFlushNodesMeter.Mark(int64(nodes - len(db.nodes))) 590 591 log.Debug("Persisted nodes from memory database", "nodes", nodes-len(db.nodes), "size", storage-db.nodesSize, "time", time.Since(start), 592 "flushnodes", db.flushnodes, "flushsize", db.flushsize, "flushtime", db.flushtime, "livenodes", len(db.nodes), "livesize", db.nodesSize) 593 594 return nil 595 } 596 597 // Commit iterates over all the children of a particular node, writes them out 598 // to disk, forcefully tearing down all references in both directions. 599 // 600 // As a side effect, all pre-images accumulated up to this point are also written. 601 func (db *Database) Commit(node common.Hash, report bool) error { 602 // Create a database batch to flush persistent data out. It is important that 603 // outside code doesn't see an inconsistent state (referenced data removed from 604 // memory cache during commit but not yet in persistent storage). This is ensured 605 // by only uncaching existing data when the database write finalizes. 606 db.lock.RLock() 607 608 start := time.Now() 609 batch := db.diskdb.NewBatch() 610 611 // Move all of the accumulated preimages into a write batch 612 for hash, preimage := range db.preimages { 613 if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil { 614 log.Error("Failed to commit preimage from trie database", "err", err) 615 db.lock.RUnlock() 616 return err 617 } 618 if batch.ValueSize() > ethdb.IdealBatchSize { 619 if err := batch.Write(); err != nil { 620 return err 621 } 622 batch.Reset() 623 } 624 } 625 // Move the trie itself into the batch, flushing if enough data is accumulated 626 nodes, storage := len(db.nodes), db.nodesSize 627 if err := db.commit(node, batch); err != nil { 628 log.Error("Failed to commit trie from trie database", "err", err) 629 db.lock.RUnlock() 630 return err 631 } 632 // Write batch ready, unlock for readers during persistence 633 if err := batch.Write(); err != nil { 634 log.Error("Failed to write trie to disk", "err", err) 635 db.lock.RUnlock() 636 return err 637 } 638 db.lock.RUnlock() 639 640 // Write successful, clear out the flushed data 641 db.lock.Lock() 642 defer db.lock.Unlock() 643 644 db.preimages = make(map[common.Hash][]byte) 645 db.preimagesSize = 0 646 647 db.uncache(node) 648 649 memcacheCommitTimeTimer.Update(time.Since(start)) 650 memcacheCommitSizeMeter.Mark(int64(storage - db.nodesSize)) 651 memcacheCommitNodesMeter.Mark(int64(nodes - len(db.nodes))) 652 653 logger := log.Info 654 if !report { 655 logger = log.Debug 656 } 657 logger("Persisted trie from memory database", "nodes", nodes-len(db.nodes)+int(db.flushnodes), "size", storage-db.nodesSize+db.flushsize, "time", time.Since(start)+db.flushtime, 658 "gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.nodes), "livesize", db.nodesSize) 659 660 // Reset the garbage collection statistics 661 db.gcnodes, db.gcsize, db.gctime = 0, 0, 0 662 db.flushnodes, db.flushsize, db.flushtime = 0, 0, 0 663 664 return nil 665 } 666 667 // commit is the private locked version of Commit. 668 func (db *Database) commit(hash common.Hash, batch ethdb.Batch) error { 669 // If the node does not exist, it's a previously committed node 670 node, ok := db.nodes[hash] 671 if !ok { 672 return nil 673 } 674 for _, child := range node.childs() { 675 if err := db.commit(child, batch); err != nil { 676 return err 677 } 678 } 679 if err := batch.Put(hash[:], node.rlp()); err != nil { 680 return err 681 } 682 // If we've reached an optimal batch size, commit and start over 683 if batch.ValueSize() >= ethdb.IdealBatchSize { 684 if err := batch.Write(); err != nil { 685 return err 686 } 687 batch.Reset() 688 } 689 return nil 690 } 691 692 // uncache is the post-processing step of a commit operation where the already 693 // persisted trie is removed from the cache. The reason behind the two-phase 694 // commit is to ensure consistent data availability while moving from memory 695 // to disk. 696 func (db *Database) uncache(hash common.Hash) { 697 // If the node does not exist, we're done on this path 698 node, ok := db.nodes[hash] 699 if !ok { 700 return 701 } 702 // Node still exists, remove it from the flush-list 703 if hash == db.oldest { 704 db.oldest = node.flushNext 705 } else { 706 db.nodes[node.flushPrev].flushNext = node.flushNext 707 db.nodes[node.flushNext].flushPrev = node.flushPrev 708 } 709 // Uncache the node's subtries and remove the node itself too 710 for _, child := range node.childs() { 711 db.uncache(child) 712 } 713 delete(db.nodes, hash) 714 db.nodesSize -= common.StorageSize(common.HashLength + int(node.size)) 715 } 716 717 // Size returns the current storage size of the memory cache in front of the 718 // persistent database layer. 719 func (db *Database) Size() (common.StorageSize, common.StorageSize) { 720 db.lock.RLock() 721 defer db.lock.RUnlock() 722 723 // db.nodesSize only contains the useful data in the cache, but when reporting 724 // the total memory consumption, the maintenance metadata is also needed to be 725 // counted. For every useful node, we track 2 extra hashes as the flushlist. 726 var flushlistSize = common.StorageSize((len(db.nodes) - 1) * 2 * common.HashLength) 727 return db.nodesSize + flushlistSize, db.preimagesSize 728 } 729 730 // verifyIntegrity is a debug method to iterate over the entire trie stored in 731 // memory and check whether every node is reachable from the meta root. The goal 732 // is to find any errors that might cause memory leaks and or trie nodes to go 733 // missing. 734 // 735 // This method is extremely CPU and memory intensive, only use when must. 736 func (db *Database) verifyIntegrity() { 737 // Iterate over all the cached nodes and accumulate them into a set 738 reachable := map[common.Hash]struct{}{{}: {}} 739 740 for child := range db.nodes[common.Hash{}].children { 741 db.accumulate(child, reachable) 742 } 743 // Find any unreachable but cached nodes 744 unreachable := []string{} 745 for hash, node := range db.nodes { 746 if _, ok := reachable[hash]; !ok { 747 unreachable = append(unreachable, fmt.Sprintf("%x: {Node: %v, Parents: %d, Prev: %x, Next: %x}", 748 hash, node.node, node.parents, node.flushPrev, node.flushNext)) 749 } 750 } 751 if len(unreachable) != 0 { 752 panic(fmt.Sprintf("trie cache memory leak: %v", unreachable)) 753 } 754 } 755 756 // accumulate iterates over the trie defined by hash and accumulates all the 757 // cached children found in memory. 758 func (db *Database) accumulate(hash common.Hash, reachable map[common.Hash]struct{}) { 759 // Mark the node reachable if present in the memory cache 760 node, ok := db.nodes[hash] 761 if !ok { 762 return 763 } 764 reachable[hash] = struct{}{} 765 766 // Iterate over all the children and accumulate them too 767 for _, child := range node.childs() { 768 db.accumulate(child, reachable) 769 } 770 }