From ed46c9a343fd021c912e93e87e9cb8fe5a35c988 Mon Sep 17 00:00:00 2001 From: VM Date: Sat, 16 Nov 2024 12:13:12 +0800 Subject: [PATCH] fix: recover node buffer list trie nodes for graceful kill --- core/blockchain_snapshot_test.go | 6 +++++- triedb/pathdb/database.go | 1 - triedb/pathdb/journal.go | 7 ------- triedb/pathdb/nodebufferlist.go | 13 +++---------- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/core/blockchain_snapshot_test.go b/core/blockchain_snapshot_test.go index 3c8baf159f..e2a5a2c9c7 100644 --- a/core/blockchain_snapshot_test.go +++ b/core/blockchain_snapshot_test.go @@ -395,6 +395,7 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { SnapshotLimit: 0, StateScheme: snaptest.scheme, } + config.UseBase = true newchain, err := NewBlockChain(snaptest.db, config, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) @@ -412,6 +413,7 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { SnapshotWait: false, // Don't wait rebuild StateScheme: snaptest.scheme, } + config.UseBase = true tmp, err := NewBlockChain(snaptest.db, config, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) @@ -421,7 +423,9 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { tmp.triedb.Close() tmp.stopWithoutSaving() - newchain, err = NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + cacheConfig := DefaultCacheConfigWithScheme(snaptest.scheme) + cacheConfig.UseBase = true + newchain, err = NewBlockChain(snaptest.db, cacheConfig, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 6834fe3554..d2cd0b9975 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -180,7 +180,6 @@ func New(diskdb ethdb.Database, config *Config) *Database { diskdb: diskdb, useBase: config.UseBase, } - fmt.Println("useBase", db.useBase) // Open the freezer for state history if the passed database contains an // ancient store. Otherwise, all the relevant functionalities are disabled. diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index a848b36179..9ae036def6 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -255,8 +255,6 @@ func (db *Database) loadLayers() layer { if err == nil { return head } - fmt.Println("load layers error: ", err) - log.Error("print load journal error", "error", err) // journal is not matched(or missing) with the persistent state, discard // it. Display log for discarding journal, but try to avoid showing // useless information when the db is created from scratch. @@ -270,10 +268,8 @@ func (db *Database) loadLayers() layer { stateID = rawdb.ReadPersistentStateID(db.diskdb) ) - fmt.Println("use base1111: ", db.useBase) if (errors.Is(err, errMissJournal) || errors.Is(err, errUnmatchedJournal)) && db.fastRecovery && db.config.TrieNodeBufferType == NodeBufferList && !db.useBase { - fmt.Println("3j3erj321") start := time.Now() log.Info("Recover node buffer list from ancient db") @@ -288,7 +284,6 @@ func (db *Database) loadLayers() layer { } } if nb == nil || err != nil { - fmt.Println("r23k9321k9") // 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.useBase) @@ -373,9 +368,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalTyp return nil, err } - fmt.Println("111") if db.config.TrieNodeBufferType == NodeBufferList && !db.useBase { - fmt.Println("222") recoveredRoot, recoveredStateID, _ := nb.getLatestStatus() if recoveredRoot != root && recoveredStateID != id { log.Error("unequal state root and state id") diff --git a/triedb/pathdb/nodebufferlist.go b/triedb/pathdb/nodebufferlist.go index 40de1b0e44..d1b43d70e0 100644 --- a/triedb/pathdb/nodebufferlist.go +++ b/triedb/pathdb/nodebufferlist.go @@ -108,17 +108,13 @@ func newNodeBufferList( dlInMd = wpBlocks } - if nodes == nil { - nodes = make(map[common.Hash]map[string]*trienode.Node) - } - nf := &nodebufferlist{ db: db, wpBlocks: wpBlocks, rsevMdNum: rsevMdNum, dlInMd: dlInMd, limit: limit, - base: newMultiDifflayer(limit, 0, common.Hash{}, nodes, 0), + base: newMultiDifflayer(limit, 0, common.Hash{}, make(map[common.Hash]map[string]*trienode.Node), 0), persistID: rawdb.ReadPersistentStateID(db), stopCh: make(chan struct{}), waitStopCh: make(chan struct{}), @@ -129,7 +125,6 @@ func newNodeBufferList( fmt.Println("useBase, fastRecovery", useBase, fastRecovery) if !useBase && fastRecovery { - fmt.Println("ndnuenu") if freezer == nil { log.Crit("Use unopened freezer db to recover node buffer list") } @@ -139,7 +134,6 @@ func newNodeBufferList( return nil, err } } else { - fmt.Println("ndiednie") ele := newMultiDifflayer(limit, 0, common.Hash{}, make(map[common.Hash]map[string]*trienode.Node), 0) nf.head = ele nf.tail = ele @@ -167,7 +161,6 @@ func (nf *nodebufferlist) recoverNodeBufferList(freezer *rawdb.ResettableFreezer log.Error("Failed to get freezer tail", "error", err) return err } - fmt.Println() log.Info("Ancient db meta info", "persistent_state_id", nf.persistID, "head_state_id", head, "tail_state_id", tail, "waiting_recover_num", head-nf.persistID) @@ -844,8 +837,8 @@ func (nf *nodebufferlist) proposedBlockReader(blockRoot common.Hash) (layer, err func (nf *nodebufferlist) report() { context := []interface{}{ "number", nf.block, "count", nf.count, "layers", nf.layers, - "stateid", nf.stateId, "persist", nf.persistID, "size", common.StorageSize(nf.size), - "basesize", common.StorageSize(nf.base.size), "baselayers", nf.base.layers, + "state_id", nf.stateId, "persist", nf.persistID, "size", common.StorageSize(nf.size), + "base_size", common.StorageSize(nf.base.size), "base_layers", nf.base.layers, } log.Info("node buffer list info", context...) }