github.com/FusionFoundation/efsn/v4@v4.2.0/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 "errors" 21 "fmt" 22 "io" 23 "reflect" 24 "runtime" 25 "sync" 26 "time" 27 28 "github.com/FusionFoundation/efsn/v4/common" 29 "github.com/FusionFoundation/efsn/v4/core/rawdb" 30 "github.com/FusionFoundation/efsn/v4/ethdb" 31 "github.com/FusionFoundation/efsn/v4/log" 32 "github.com/FusionFoundation/efsn/v4/metrics" 33 "github.com/FusionFoundation/efsn/v4/rlp" 34 "github.com/VictoriaMetrics/fastcache" 35 ) 36 37 var ( 38 memcacheCleanHitMeter = metrics.NewRegisteredMeter("trie/memcache/clean/hit", nil) 39 memcacheCleanMissMeter = metrics.NewRegisteredMeter("trie/memcache/clean/miss", nil) 40 memcacheCleanReadMeter = metrics.NewRegisteredMeter("trie/memcache/clean/read", nil) 41 memcacheCleanWriteMeter = metrics.NewRegisteredMeter("trie/memcache/clean/write", nil) 42 43 memcacheDirtyHitMeter = metrics.NewRegisteredMeter("trie/memcache/dirty/hit", nil) 44 memcacheDirtyMissMeter = metrics.NewRegisteredMeter("trie/memcache/dirty/miss", nil) 45 memcacheDirtyReadMeter = metrics.NewRegisteredMeter("trie/memcache/dirty/read", nil) 46 memcacheDirtyWriteMeter = metrics.NewRegisteredMeter("trie/memcache/dirty/write", nil) 47 48 memcacheFlushTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/flush/time", nil) 49 memcacheFlushNodesMeter = metrics.NewRegisteredMeter("trie/memcache/flush/nodes", nil) 50 memcacheFlushSizeMeter = metrics.NewRegisteredMeter("trie/memcache/flush/size", nil) 51 52 memcacheGCTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/gc/time", nil) 53 memcacheGCNodesMeter = metrics.NewRegisteredMeter("trie/memcache/gc/nodes", nil) 54 memcacheGCSizeMeter = metrics.NewRegisteredMeter("trie/memcache/gc/size", nil) 55 56 memcacheCommitTimeTimer = metrics.NewRegisteredResettingTimer("trie/memcache/commit/time", nil) 57 memcacheCommitNodesMeter = metrics.NewRegisteredMeter("trie/memcache/commit/nodes", nil) 58 memcacheCommitSizeMeter = metrics.NewRegisteredMeter("trie/memcache/commit/size", nil) 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 // 65 // Note, the trie Database is **not** thread safe in its mutations, but it **is** 66 // thread safe in providing individual, independent node access. The rationale 67 // behind this split design is to provide read access to RPC handlers and sync 68 // servers even while the trie is executing expensive garbage collection. 69 type Database struct { 70 diskdb ethdb.KeyValueStore // Persistent storage for matured trie nodes 71 72 cleans *fastcache.Cache // GC friendly memory cache of clean node RLPs 73 dirties map[common.Hash]*cachedNode // Data and references relationships of dirty trie nodes 74 oldest common.Hash // Oldest tracked node, flush-list head 75 newest common.Hash // Newest tracked node, flush-list tail 76 77 preimages map[common.Hash][]byte // Preimages of nodes from the secure trie 78 79 gctime time.Duration // Time spent on garbage collection since last commit 80 gcnodes uint64 // Nodes garbage collected since last commit 81 gcsize common.StorageSize // Data storage garbage collected since last commit 82 83 flushtime time.Duration // Time spent on data flushing since last commit 84 flushnodes uint64 // Nodes flushed since last commit 85 flushsize common.StorageSize // Data storage flushed since last commit 86 87 dirtiesSize common.StorageSize // Storage size of the dirty node cache (exc. metadata) 88 childrenSize common.StorageSize // Storage size of the external children tracking 89 preimagesSize common.StorageSize // Storage size of the preimages cache 90 91 lock sync.RWMutex 92 } 93 94 // rawNode is a simple binary blob used to differentiate between collapsed trie 95 // nodes and already encoded RLP binary blobs (while at the same time store them 96 // in the same cache fields). 97 type rawNode []byte 98 99 func (n rawNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 100 func (n rawNode) fstring(ind string) string { panic("this should never end up in a live trie") } 101 102 func (n rawNode) EncodeRLP(w io.Writer) error { 103 _, err := w.Write(n) 104 return err 105 } 106 107 // rawFullNode represents only the useful data content of a full node, with the 108 // caches and flags stripped out to minimize its data storage. This type honors 109 // the same RLP encoding as the original parent. 110 type rawFullNode [17]node 111 112 func (n rawFullNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 113 func (n rawFullNode) fstring(ind string) string { panic("this should never end up in a live trie") } 114 115 func (n rawFullNode) EncodeRLP(w io.Writer) error { 116 var nodes [17]node 117 118 for i, child := range n { 119 if child != nil { 120 nodes[i] = child 121 } else { 122 nodes[i] = nilValueNode 123 } 124 } 125 return rlp.Encode(w, nodes) 126 } 127 128 // rawShortNode represents only the useful data content of a short node, with the 129 // caches and flags stripped out to minimize its data storage. This type honors 130 // the same RLP encoding as the original parent. 131 type rawShortNode struct { 132 Key []byte 133 Val node 134 } 135 136 func (n rawShortNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") } 137 func (n rawShortNode) fstring(ind string) string { panic("this should never end up in a live trie") } 138 139 // cachedNode is all the information we know about a single cached trie node 140 // in the memory database write layer. 141 type cachedNode struct { 142 node node // Cached collapsed trie node, or raw rlp data 143 size uint16 // Byte size of the useful cached data 144 145 parents uint32 // Number of live nodes referencing this one 146 children map[common.Hash]uint16 // External children referenced by this node 147 148 flushPrev common.Hash // Previous node in the flush-list 149 flushNext common.Hash // Next node in the flush-list 150 } 151 152 // cachedNodeSize is the raw size of a cachedNode data structure without any 153 // node data included. It's an approximate size, but should be a lot better 154 // than not counting them. 155 var cachedNodeSize = int(reflect.TypeOf(cachedNode{}).Size()) 156 157 // cachedNodeChildrenSize is the raw size of an initialized but empty external 158 // reference map. 159 const cachedNodeChildrenSize = 48 160 161 // rlp returns the raw rlp encoded blob of the cached trie node, either directly 162 // from the cache, or by regenerating it from the collapsed node. 163 func (n *cachedNode) rlp() []byte { 164 if node, ok := n.node.(rawNode); ok { 165 return node 166 } 167 blob, err := rlp.EncodeToBytes(n.node) 168 if err != nil { 169 panic(err) 170 } 171 return blob 172 } 173 174 // obj returns the decoded and expanded trie node, either directly from the cache, 175 // or by regenerating it from the rlp encoded blob. 176 func (n *cachedNode) obj(hash common.Hash) node { 177 if node, ok := n.node.(rawNode); ok { 178 return mustDecodeNode(hash[:], node) 179 } 180 return expandNode(hash[:], n.node) 181 } 182 183 // forChilds invokes the callback for all the tracked children of this node, 184 // both the implicit ones from inside the node as well as the explicit ones 185 // from outside the node. 186 func (n *cachedNode) forChilds(onChild func(hash common.Hash)) { 187 for child := range n.children { 188 onChild(child) 189 } 190 if _, ok := n.node.(rawNode); !ok { 191 forGatherChildren(n.node, onChild) 192 } 193 } 194 195 // forGatherChildren traverses the node hierarchy of a collapsed storage node and 196 // invokes the callback for all the hashnode children. 197 func forGatherChildren(n node, onChild func(hash common.Hash)) { 198 switch n := n.(type) { 199 case *rawShortNode: 200 forGatherChildren(n.Val, onChild) 201 case rawFullNode: 202 for i := 0; i < 16; i++ { 203 forGatherChildren(n[i], onChild) 204 } 205 case hashNode: 206 onChild(common.BytesToHash(n)) 207 case valueNode, nil, rawNode: 208 default: 209 panic(fmt.Sprintf("unknown node type: %T", n)) 210 } 211 } 212 213 // simplifyNode traverses the hierarchy of an expanded memory node and discards 214 // all the internal caches, returning a node that only contains the raw data. 215 func simplifyNode(n node) node { 216 switch n := n.(type) { 217 case *shortNode: 218 // Short nodes discard the flags and cascade 219 return &rawShortNode{Key: n.Key, Val: simplifyNode(n.Val)} 220 221 case *fullNode: 222 // Full nodes discard the flags and cascade 223 node := rawFullNode(n.Children) 224 for i := 0; i < len(node); i++ { 225 if node[i] != nil { 226 node[i] = simplifyNode(node[i]) 227 } 228 } 229 return node 230 231 case valueNode, hashNode, rawNode: 232 return n 233 234 default: 235 panic(fmt.Sprintf("unknown node type: %T", n)) 236 } 237 } 238 239 // expandNode traverses the node hierarchy of a collapsed storage node and converts 240 // all fields and keys into expanded memory form. 241 func expandNode(hash hashNode, n node) node { 242 switch n := n.(type) { 243 case *rawShortNode: 244 // Short nodes need key and child expansion 245 return &shortNode{ 246 Key: compactToHex(n.Key), 247 Val: expandNode(nil, n.Val), 248 flags: nodeFlag{ 249 hash: hash, 250 }, 251 } 252 253 case rawFullNode: 254 // Full nodes need child expansion 255 node := &fullNode{ 256 flags: nodeFlag{ 257 hash: hash, 258 }, 259 } 260 for i := 0; i < len(node.Children); i++ { 261 if n[i] != nil { 262 node.Children[i] = expandNode(nil, n[i]) 263 } 264 } 265 return node 266 267 case valueNode, hashNode: 268 return n 269 270 default: 271 panic(fmt.Sprintf("unknown node type: %T", n)) 272 } 273 } 274 275 // Config defines all necessary options for database. 276 type Config struct { 277 Cache int // Memory allowance (MB) to use for caching trie nodes in memory 278 Journal string // Journal of clean cache to survive node restarts 279 Preimages bool // Flag whether the preimage of trie key is recorded 280 } 281 282 // NewDatabase creates a new trie database to store ephemeral trie content before 283 // its written out to disk or garbage collected. No read cache is created, so all 284 // data retrievals will hit the underlying disk database. 285 func NewDatabase(diskdb ethdb.KeyValueStore) *Database { 286 return NewDatabaseWithConfig(diskdb, nil) 287 } 288 289 // NewDatabaseWithConfig creates a new trie database to store ephemeral trie content 290 // before its written out to disk or garbage collected. It also acts as a read cache 291 // for nodes loaded from disk. 292 func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database { 293 var cleans *fastcache.Cache 294 if config != nil && config.Cache > 0 { 295 if config.Journal == "" { 296 cleans = fastcache.New(config.Cache * 1024 * 1024) 297 } else { 298 cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024) 299 } 300 } 301 db := &Database{ 302 diskdb: diskdb, 303 cleans: cleans, 304 dirties: map[common.Hash]*cachedNode{{}: { 305 children: make(map[common.Hash]uint16), 306 }}, 307 } 308 if config == nil || config.Preimages { // TODO(karalabe): Flip to default off in the future 309 db.preimages = make(map[common.Hash][]byte) 310 } 311 return db 312 } 313 314 // DiskDB retrieves the persistent storage backing the trie database. 315 func (db *Database) DiskDB() ethdb.KeyValueStore { 316 return db.diskdb 317 } 318 319 // InsertBlob writes a new reference tracked blob to the memory database if it's 320 // yet unknown. This method should only be used for non-trie nodes that require 321 // reference counting, since trie nodes are garbage collected directly through 322 // their embedded children. 323 func (db *Database) InsertBlob(hash common.Hash, blob []byte) { 324 db.lock.Lock() 325 defer db.lock.Unlock() 326 327 db.insert(hash, len(blob), rawNode(blob)) 328 } 329 330 // insert inserts a collapsed trie node into the memory database. 331 // The blob size must be specified to allow proper size tracking. 332 // All nodes inserted by this function will be reference tracked 333 // and in theory should only used for **trie nodes** insertion. 334 func (db *Database) insert(hash common.Hash, size int, node node) { 335 // If the node's already cached, skip 336 if _, ok := db.dirties[hash]; ok { 337 return 338 } 339 memcacheDirtyWriteMeter.Mark(int64(size)) 340 341 // Create the cached entry for this node 342 entry := &cachedNode{ 343 node: simplifyNode(node), 344 size: uint16(size), 345 flushPrev: db.newest, 346 } 347 entry.forChilds(func(child common.Hash) { 348 if c := db.dirties[child]; c != nil { 349 c.parents++ 350 } 351 }) 352 db.dirties[hash] = entry 353 354 // Update the flush-list endpoints 355 if db.oldest == (common.Hash{}) { 356 db.oldest, db.newest = hash, hash 357 } else { 358 db.dirties[db.newest].flushNext, db.newest = hash, hash 359 } 360 db.dirtiesSize += common.StorageSize(common.HashLength + entry.size) 361 } 362 363 // insertPreimage writes a new trie node pre-image to the memory database if it's 364 // yet unknown. The method will NOT make a copy of the slice, 365 // only use if the preimage will NOT be changed later on. 366 // 367 // Note, this method assumes that the database's lock is held! 368 func (db *Database) insertPreimage(hash common.Hash, preimage []byte) { 369 // Short circuit if preimage collection is disabled 370 if db.preimages == nil { 371 return 372 } 373 // Track the preimage if a yet unknown one 374 if _, ok := db.preimages[hash]; ok { 375 return 376 } 377 db.preimages[hash] = preimage 378 db.preimagesSize += common.StorageSize(common.HashLength + len(preimage)) 379 } 380 381 // node retrieves a cached trie node from memory, or returns nil if none can be 382 // found in the memory cache. 383 func (db *Database) node(hash common.Hash) node { 384 // Retrieve the node from the clean cache if available 385 if db.cleans != nil { 386 if enc := db.cleans.Get(nil, hash[:]); enc != nil { 387 memcacheCleanHitMeter.Mark(1) 388 memcacheCleanReadMeter.Mark(int64(len(enc))) 389 return mustDecodeNode(hash[:], enc) 390 } 391 } 392 // Retrieve the node from the dirty cache if available 393 db.lock.RLock() 394 dirty := db.dirties[hash] 395 db.lock.RUnlock() 396 397 if dirty != nil { 398 memcacheDirtyHitMeter.Mark(1) 399 memcacheDirtyReadMeter.Mark(int64(dirty.size)) 400 return dirty.obj(hash) 401 } 402 memcacheDirtyMissMeter.Mark(1) 403 404 // Content unavailable in memory, attempt to retrieve from disk 405 enc, err := db.diskdb.Get(hash[:]) 406 if err != nil || enc == nil { 407 return nil 408 } 409 if db.cleans != nil { 410 db.cleans.Set(hash[:], enc) 411 memcacheCleanMissMeter.Mark(1) 412 memcacheCleanWriteMeter.Mark(int64(len(enc))) 413 } 414 return mustDecodeNode(hash[:], enc) 415 } 416 417 // Node retrieves an encoded cached trie node from memory. If it cannot be found 418 // cached, the method queries the persistent database for the content. 419 func (db *Database) Node(hash common.Hash) ([]byte, error) { 420 // It doesn't make sense to retrieve the metaroot 421 if hash == (common.Hash{}) { 422 return nil, errors.New("not found") 423 } 424 // Retrieve the node from the clean cache if available 425 if db.cleans != nil { 426 if enc := db.cleans.Get(nil, hash[:]); enc != nil { 427 memcacheCleanHitMeter.Mark(1) 428 memcacheCleanReadMeter.Mark(int64(len(enc))) 429 return enc, nil 430 } 431 } 432 // Retrieve the node from the dirty cache if available 433 db.lock.RLock() 434 dirty := db.dirties[hash] 435 db.lock.RUnlock() 436 437 if dirty != nil { 438 memcacheDirtyHitMeter.Mark(1) 439 memcacheDirtyReadMeter.Mark(int64(dirty.size)) 440 return dirty.rlp(), nil 441 } 442 memcacheDirtyMissMeter.Mark(1) 443 444 // Content unavailable in memory, attempt to retrieve from disk 445 enc := rawdb.ReadTrieNode(db.diskdb, hash) 446 if len(enc) != 0 { 447 if db.cleans != nil { 448 db.cleans.Set(hash[:], enc) 449 memcacheCleanMissMeter.Mark(1) 450 memcacheCleanWriteMeter.Mark(int64(len(enc))) 451 } 452 return enc, nil 453 } 454 return nil, errors.New("not found") 455 } 456 457 // preimage retrieves a cached trie node pre-image from memory. If it cannot be 458 // found cached, the method queries the persistent database for the content. 459 func (db *Database) preimage(hash common.Hash) []byte { 460 // Short circuit if preimage collection is disabled 461 if db.preimages == nil { 462 return nil 463 } 464 // Retrieve the node from cache if available 465 db.lock.RLock() 466 preimage := db.preimages[hash] 467 db.lock.RUnlock() 468 469 if preimage != nil { 470 return preimage 471 } 472 return rawdb.ReadPreimage(db.diskdb, hash) 473 } 474 475 // Nodes retrieves the hashes of all the nodes cached within the memory database. 476 // This method is extremely expensive and should only be used to validate internal 477 // states in test code. 478 func (db *Database) Nodes() []common.Hash { 479 db.lock.RLock() 480 defer db.lock.RUnlock() 481 482 var hashes = make([]common.Hash, 0, len(db.dirties)) 483 for hash := range db.dirties { 484 if hash != (common.Hash{}) { // Special case for "root" references/nodes 485 hashes = append(hashes, hash) 486 } 487 } 488 return hashes 489 } 490 491 // Reference adds a new reference from a parent node to a child node. 492 // This function is used to add reference between internal trie node 493 // and external node(e.g. storage trie root), all internal trie nodes 494 // are referenced together by database itself. 495 func (db *Database) Reference(child common.Hash, parent common.Hash) { 496 db.lock.Lock() 497 defer db.lock.Unlock() 498 499 db.reference(child, parent) 500 } 501 502 // reference is the private locked version of Reference. 503 func (db *Database) reference(child common.Hash, parent common.Hash) { 504 // If the node does not exist, it's a node pulled from disk, skip 505 node, ok := db.dirties[child] 506 if !ok { 507 return 508 } 509 // If the reference already exists, only duplicate for roots 510 if db.dirties[parent].children == nil { 511 db.dirties[parent].children = make(map[common.Hash]uint16) 512 db.childrenSize += cachedNodeChildrenSize 513 } else if _, ok = db.dirties[parent].children[child]; ok && parent != (common.Hash{}) { 514 return 515 } 516 node.parents++ 517 db.dirties[parent].children[child]++ 518 if db.dirties[parent].children[child] == 1 { 519 db.childrenSize += common.HashLength + 2 // uint16 counter 520 } 521 } 522 523 // Dereference removes an existing reference from a root node. 524 func (db *Database) Dereference(root common.Hash) { 525 // Sanity check to ensure that the meta-root is not removed 526 if root == (common.Hash{}) { 527 log.Error("Attempted to dereference the trie cache meta root") 528 return 529 } 530 db.lock.Lock() 531 defer db.lock.Unlock() 532 533 nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now() 534 db.dereference(root, common.Hash{}) 535 536 db.gcnodes += uint64(nodes - len(db.dirties)) 537 db.gcsize += storage - db.dirtiesSize 538 db.gctime += time.Since(start) 539 540 memcacheGCTimeTimer.Update(time.Since(start)) 541 memcacheGCSizeMeter.Mark(int64(storage - db.dirtiesSize)) 542 memcacheGCNodesMeter.Mark(int64(nodes - len(db.dirties))) 543 544 log.Debug("Dereferenced trie from memory database", "nodes", nodes-len(db.dirties), "size", storage-db.dirtiesSize, "time", time.Since(start), 545 "gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.dirties), "livesize", db.dirtiesSize) 546 } 547 548 // dereference is the private locked version of Dereference. 549 func (db *Database) dereference(child common.Hash, parent common.Hash) { 550 // Dereference the parent-child 551 node := db.dirties[parent] 552 553 if node.children != nil && node.children[child] > 0 { 554 node.children[child]-- 555 if node.children[child] == 0 { 556 delete(node.children, child) 557 db.childrenSize -= (common.HashLength + 2) // uint16 counter 558 } 559 } 560 // If the child does not exist, it's a previously committed node. 561 node, ok := db.dirties[child] 562 if !ok { 563 return 564 } 565 // If there are no more references to the child, delete it and cascade 566 if node.parents > 0 { 567 // This is a special cornercase where a node loaded from disk (i.e. not in the 568 // memcache any more) gets reinjected as a new node (short node split into full, 569 // then reverted into short), causing a cached node to have no parents. That is 570 // no problem in itself, but don't make maxint parents out of it. 571 node.parents-- 572 } 573 if node.parents == 0 { 574 // Remove the node from the flush-list 575 switch child { 576 case db.oldest: 577 db.oldest = node.flushNext 578 db.dirties[node.flushNext].flushPrev = common.Hash{} 579 case db.newest: 580 db.newest = node.flushPrev 581 db.dirties[node.flushPrev].flushNext = common.Hash{} 582 default: 583 db.dirties[node.flushPrev].flushNext = node.flushNext 584 db.dirties[node.flushNext].flushPrev = node.flushPrev 585 } 586 // Dereference all children and delete the node 587 node.forChilds(func(hash common.Hash) { 588 db.dereference(hash, child) 589 }) 590 delete(db.dirties, child) 591 db.dirtiesSize -= common.StorageSize(common.HashLength + int(node.size)) 592 if node.children != nil { 593 db.childrenSize -= cachedNodeChildrenSize 594 } 595 } 596 } 597 598 // Cap iteratively flushes old but still referenced trie nodes until the total 599 // memory usage goes below the given threshold. 600 // 601 // Note, this method is a non-synchronized mutator. It is unsafe to call this 602 // concurrently with other mutators. 603 func (db *Database) Cap(limit common.StorageSize) error { 604 // Create a database batch to flush persistent data out. It is important that 605 // outside code doesn't see an inconsistent state (referenced data removed from 606 // memory cache during commit but not yet in persistent storage). This is ensured 607 // by only uncaching existing data when the database write finalizes. 608 nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now() 609 batch := db.diskdb.NewBatch() 610 611 // db.dirtiesSize only contains the useful data in the cache, but when reporting 612 // the total memory consumption, the maintenance metadata is also needed to be 613 // counted. 614 size := db.dirtiesSize + common.StorageSize((len(db.dirties)-1)*cachedNodeSize) 615 size += db.childrenSize - common.StorageSize(len(db.dirties[common.Hash{}].children)*(common.HashLength+2)) 616 617 // If the preimage cache got large enough, push to disk. If it's still small 618 // leave for later to deduplicate writes. 619 flushPreimages := db.preimagesSize > 4*1024*1024 620 if flushPreimages { 621 if db.preimages == nil { 622 log.Error("Attempted to write preimages whilst disabled") 623 } else { 624 rawdb.WritePreimages(batch, db.preimages) 625 if batch.ValueSize() > ethdb.IdealBatchSize { 626 if err := batch.Write(); err != nil { 627 return err 628 } 629 batch.Reset() 630 } 631 } 632 } 633 // Keep committing nodes from the flush-list until we're below allowance 634 oldest := db.oldest 635 for size > limit && oldest != (common.Hash{}) { 636 // Fetch the oldest referenced node and push into the batch 637 node := db.dirties[oldest] 638 rawdb.WriteTrieNode(batch, oldest, node.rlp()) 639 640 // If we exceeded the ideal batch size, commit and reset 641 if batch.ValueSize() >= ethdb.IdealBatchSize { 642 if err := batch.Write(); err != nil { 643 log.Error("Failed to write flush list to disk", "err", err) 644 return err 645 } 646 batch.Reset() 647 } 648 // Iterate to the next flush item, or abort if the size cap was achieved. Size 649 // is the total size, including the useful cached data (hash -> blob), the 650 // cache item metadata, as well as external children mappings. 651 size -= common.StorageSize(common.HashLength + int(node.size) + cachedNodeSize) 652 if node.children != nil { 653 size -= common.StorageSize(cachedNodeChildrenSize + len(node.children)*(common.HashLength+2)) 654 } 655 oldest = node.flushNext 656 } 657 // Flush out any remainder data from the last batch 658 if err := batch.Write(); err != nil { 659 log.Error("Failed to write flush list to disk", "err", err) 660 return err 661 } 662 // Write successful, clear out the flushed data 663 db.lock.Lock() 664 defer db.lock.Unlock() 665 666 if flushPreimages { 667 if db.preimages == nil { 668 log.Error("Attempted to reset preimage cache whilst disabled") 669 } else { 670 db.preimages, db.preimagesSize = make(map[common.Hash][]byte), 0 671 } 672 } 673 for db.oldest != oldest { 674 node := db.dirties[db.oldest] 675 delete(db.dirties, db.oldest) 676 db.oldest = node.flushNext 677 678 db.dirtiesSize -= common.StorageSize(common.HashLength + int(node.size)) 679 if node.children != nil { 680 db.childrenSize -= common.StorageSize(cachedNodeChildrenSize + len(node.children)*(common.HashLength+2)) 681 } 682 } 683 if db.oldest != (common.Hash{}) { 684 db.dirties[db.oldest].flushPrev = common.Hash{} 685 } 686 db.flushnodes += uint64(nodes - len(db.dirties)) 687 db.flushsize += storage - db.dirtiesSize 688 db.flushtime += time.Since(start) 689 690 memcacheFlushTimeTimer.Update(time.Since(start)) 691 memcacheFlushSizeMeter.Mark(int64(storage - db.dirtiesSize)) 692 memcacheFlushNodesMeter.Mark(int64(nodes - len(db.dirties))) 693 694 log.Debug("Persisted nodes from memory database", "nodes", nodes-len(db.dirties), "size", storage-db.dirtiesSize, "time", time.Since(start), 695 "flushnodes", db.flushnodes, "flushsize", db.flushsize, "flushtime", db.flushtime, "livenodes", len(db.dirties), "livesize", db.dirtiesSize) 696 697 return nil 698 } 699 700 // Commit iterates over all the children of a particular node, writes them out 701 // to disk, forcefully tearing down all references in both directions. As a side 702 // effect, all pre-images accumulated up to this point are also written. 703 // 704 // Note, this method is a non-synchronized mutator. It is unsafe to call this 705 // concurrently with other mutators. 706 func (db *Database) Commit(node common.Hash, report bool, callback func(common.Hash)) error { 707 // Create a database batch to flush persistent data out. It is important that 708 // outside code doesn't see an inconsistent state (referenced data removed from 709 // memory cache during commit but not yet in persistent storage). This is ensured 710 // by only uncaching existing data when the database write finalizes. 711 start := time.Now() 712 batch := db.diskdb.NewBatch() 713 714 // Move all of the accumulated preimages into a write batch 715 if db.preimages != nil { 716 rawdb.WritePreimages(batch, db.preimages) 717 // Since we're going to replay trie node writes into the clean cache, flush out 718 // any batched pre-images before continuing. 719 if err := batch.Write(); err != nil { 720 return err 721 } 722 batch.Reset() 723 } 724 // Move the trie itself into the batch, flushing if enough data is accumulated 725 nodes, storage := len(db.dirties), db.dirtiesSize 726 727 uncacher := &cleaner{db} 728 if err := db.commit(node, batch, uncacher, callback); err != nil { 729 log.Error("Failed to commit trie from trie database", "err", err) 730 return err 731 } 732 // Trie mostly committed to disk, flush any batch leftovers 733 if err := batch.Write(); err != nil { 734 log.Error("Failed to write trie to disk", "err", err) 735 return err 736 } 737 // Uncache any leftovers in the last batch 738 db.lock.Lock() 739 defer db.lock.Unlock() 740 741 batch.Replay(uncacher) 742 batch.Reset() 743 744 // Reset the storage counters and bumped metrics 745 if db.preimages != nil { 746 db.preimages, db.preimagesSize = make(map[common.Hash][]byte), 0 747 } 748 memcacheCommitTimeTimer.Update(time.Since(start)) 749 memcacheCommitSizeMeter.Mark(int64(storage - db.dirtiesSize)) 750 memcacheCommitNodesMeter.Mark(int64(nodes - len(db.dirties))) 751 752 logger := log.Info 753 if !report { 754 logger = log.Debug 755 } 756 logger("Persisted trie from memory database", "nodes", nodes-len(db.dirties)+int(db.flushnodes), "size", storage-db.dirtiesSize+db.flushsize, "time", time.Since(start)+db.flushtime, 757 "gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.dirties), "livesize", db.dirtiesSize) 758 759 // Reset the garbage collection statistics 760 db.gcnodes, db.gcsize, db.gctime = 0, 0, 0 761 db.flushnodes, db.flushsize, db.flushtime = 0, 0, 0 762 763 return nil 764 } 765 766 // commit is the private locked version of Commit. 767 func (db *Database) commit(hash common.Hash, batch ethdb.Batch, uncacher *cleaner, callback func(common.Hash)) error { 768 // If the node does not exist, it's a previously committed node 769 node, ok := db.dirties[hash] 770 if !ok { 771 return nil 772 } 773 var err error 774 node.forChilds(func(child common.Hash) { 775 if err == nil { 776 err = db.commit(child, batch, uncacher, callback) 777 } 778 }) 779 if err != nil { 780 return err 781 } 782 // If we've reached an optimal batch size, commit and start over 783 rawdb.WriteTrieNode(batch, hash, node.rlp()) 784 if callback != nil { 785 callback(hash) 786 } 787 if batch.ValueSize() >= ethdb.IdealBatchSize { 788 if err := batch.Write(); err != nil { 789 return err 790 } 791 db.lock.Lock() 792 batch.Replay(uncacher) 793 batch.Reset() 794 db.lock.Unlock() 795 } 796 return nil 797 } 798 799 // cleaner is a database batch replayer that takes a batch of write operations 800 // and cleans up the trie database from anything written to disk. 801 type cleaner struct { 802 db *Database 803 } 804 805 // Put reacts to database writes and implements dirty data uncaching. This is the 806 // post-processing step of a commit operation where the already persisted trie is 807 // removed from the dirty cache and moved into the clean cache. The reason behind 808 // the two-phase commit is to ensure ensure data availability while moving from 809 // memory to disk. 810 func (c *cleaner) Put(key []byte, rlp []byte) error { 811 hash := common.BytesToHash(key) 812 813 // If the node does not exist, we're done on this path 814 node, ok := c.db.dirties[hash] 815 if !ok { 816 return nil 817 } 818 // Node still exists, remove it from the flush-list 819 switch hash { 820 case c.db.oldest: 821 c.db.oldest = node.flushNext 822 c.db.dirties[node.flushNext].flushPrev = common.Hash{} 823 case c.db.newest: 824 c.db.newest = node.flushPrev 825 c.db.dirties[node.flushPrev].flushNext = common.Hash{} 826 default: 827 c.db.dirties[node.flushPrev].flushNext = node.flushNext 828 c.db.dirties[node.flushNext].flushPrev = node.flushPrev 829 } 830 // Remove the node from the dirty cache 831 delete(c.db.dirties, hash) 832 c.db.dirtiesSize -= common.StorageSize(common.HashLength + int(node.size)) 833 if node.children != nil { 834 c.db.dirtiesSize -= common.StorageSize(cachedNodeChildrenSize + len(node.children)*(common.HashLength+2)) 835 } 836 // Move the flushed node into the clean cache to prevent insta-reloads 837 if c.db.cleans != nil { 838 c.db.cleans.Set(hash[:], rlp) 839 memcacheCleanWriteMeter.Mark(int64(len(rlp))) 840 } 841 return nil 842 } 843 844 func (c *cleaner) Delete(key []byte) error { 845 panic("not implemented") 846 } 847 848 // Size returns the current storage size of the memory cache in front of the 849 // persistent database layer. 850 func (db *Database) Size() (common.StorageSize, common.StorageSize) { 851 db.lock.RLock() 852 defer db.lock.RUnlock() 853 854 // db.dirtiesSize only contains the useful data in the cache, but when reporting 855 // the total memory consumption, the maintenance metadata is also needed to be 856 // counted. 857 var metadataSize = common.StorageSize((len(db.dirties) - 1) * cachedNodeSize) 858 var metarootRefs = common.StorageSize(len(db.dirties[common.Hash{}].children) * (common.HashLength + 2)) 859 return db.dirtiesSize + db.childrenSize + metadataSize - metarootRefs, db.preimagesSize 860 } 861 862 // saveCache saves clean state cache to given directory path 863 // using specified CPU cores. 864 func (db *Database) saveCache(dir string, threads int) error { 865 if db.cleans == nil { 866 return nil 867 } 868 log.Info("Writing clean trie cache to disk", "path", dir, "threads", threads) 869 870 start := time.Now() 871 err := db.cleans.SaveToFileConcurrent(dir, threads) 872 if err != nil { 873 log.Error("Failed to persist clean trie cache", "error", err) 874 return err 875 } 876 log.Info("Persisted the clean trie cache", "path", dir, "elapsed", common.PrettyDuration(time.Since(start))) 877 return nil 878 } 879 880 // SaveCache atomically saves fast cache data to the given dir using all 881 // available CPU cores. 882 func (db *Database) SaveCache(dir string) error { 883 return db.saveCache(dir, runtime.GOMAXPROCS(0)) 884 } 885 886 // SaveCachePeriodically atomically saves fast cache data to the given dir with 887 // the specified interval. All dump operation will only use a single CPU core. 888 func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, stopCh <-chan struct{}) { 889 ticker := time.NewTicker(interval) 890 defer ticker.Stop() 891 892 for { 893 select { 894 case <-ticker.C: 895 db.saveCache(dir, 1) 896 case <-stopCh: 897 return 898 } 899 } 900 }