diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index c281f7dac8..310114b188 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -153,7 +153,7 @@ type Database struct { readOnly bool // Flag if database is opened in read only mode waitSync bool // Flag if database is deactivated due to initial state sync fastRecovery bool // Flag if recover nodebufferlist - first bool // Flag if recover nodebufferlist + isGenesis bool // Flag if in genesis state bufferSize int // Memory allowance (in bytes) for caching dirty nodes config *Config // Configuration for database diskdb ethdb.Database // Persistent storage for matured trie nodes @@ -183,7 +183,7 @@ func New(diskdb ethdb.Database, config *Config) *Database { // ancient store. Otherwise, all the relevant functionalities are disabled. if ancient, err := diskdb.AncientDatadir(); err == nil && ancient != "" && !db.readOnly { db.fastRecovery = checkAncientAndNodeBuffer(ancient, config.TrieNodeBufferType) - db.first = checkFirst(ancient) + db.isGenesis = checkGenesis(ancient) freezer, err := rawdb.NewStateFreezer(ancient, false, db.fastRecovery) if err != nil { log.Crit("Failed to open state history freezer", "err", err) diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index 1bdf046bfa..aa2190afa7 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -269,7 +269,7 @@ func (db *Database) loadLayers() layer { ) if (errors.Is(err, errMissJournal) || errors.Is(err, errUnmatchedJournal)) && db.fastRecovery && - db.config.TrieNodeBufferType == NodeBufferList && !db.first { + db.config.TrieNodeBufferType == NodeBufferList && !db.isGenesis { start := time.Now() if db.freezer == nil { log.Crit("Use unopened freezer db to recover node buffer list") @@ -277,7 +277,7 @@ func (db *Database) loadLayers() layer { log.Info("Recover node buffer list from ancient db") nb, err = NewTrieNodeBuffer(db.diskdb, db.config.TrieNodeBufferType, db.bufferSize, nil, 0, - db.config.ProposeBlockInterval, db.config.NotifyKeep, db.freezer, db.fastRecovery, db.first) + db.config.ProposeBlockInterval, db.config.NotifyKeep, db.freezer, db.fastRecovery, db.isGenesis) if err != nil { log.Error("Failed to new trie node buffer for recovery", "error", err) } else { @@ -289,7 +289,7 @@ func (db *Database) loadLayers() layer { if nb == nil || err != nil { // Return single layer with persistent state. nb, err = NewTrieNodeBuffer(db.diskdb, db.config.TrieNodeBufferType, db.bufferSize, nil, 0, - db.config.ProposeBlockInterval, db.config.NotifyKeep, nil, false, db.first) + db.config.ProposeBlockInterval, db.config.NotifyKeep, nil, false, db.isGenesis) if err != nil { log.Crit("Failed to new trie node buffer", "error", err) return nil @@ -676,8 +676,8 @@ func flattenTrieNodes(jn []journalNodes) map[common.Hash]map[string]*trienode.No } func checkAncientAndNodeBuffer(ancient string, nodeBufferType NodeBufferType) bool { - if !common.FileExist(filepath.Join(ancient, rawdb.StateFreezerName)) { - return true + if ok := checkGenesis(ancient); ok { + return ok } if rawdb.DetectTrieNodesFile(ancient) { @@ -694,7 +694,7 @@ func checkAncientAndNodeBuffer(ancient string, nodeBufferType NodeBufferType) bo return false } -func checkFirst(ancient string) bool { +func checkGenesis(ancient string) bool { if !common.FileExist(filepath.Join(ancient, rawdb.StateFreezerName)) { return true } diff --git a/triedb/pathdb/nodebufferlist.go b/triedb/pathdb/nodebufferlist.go index 547b4c3a9f..0fd0a6ff09 100644 --- a/triedb/pathdb/nodebufferlist.go +++ b/triedb/pathdb/nodebufferlist.go @@ -463,7 +463,7 @@ func (nf *nodebufferlist) flush(db ethdb.KeyValueStore, clean *fastcache.Cache, if !nf.isGenesis { nf.traverseReverse(commitFunc) } else { - nf.forceFlush() + nf.flushGenesis() } persistID := nf.persistID + nf.base.layers @@ -662,8 +662,8 @@ func (nf *nodebufferlist) traverseReverse(cb func(*multiDifflayer) bool) { return } -// forceFlush is used for init genesis -func (nf *nodebufferlist) forceFlush() { +// flushGenesis is used for flush genesis trie node +func (nf *nodebufferlist) flushGenesis() { commitFunc := func(buffer *multiDifflayer) bool { err := nf.base.commit(buffer.root, buffer.id, buffer.block, buffer.layers, buffer.nodes) if err != nil {