From f69656815a1681b8a4e52a073c05498d16b48cc6 Mon Sep 17 00:00:00 2001 From: VM Date: Fri, 15 Nov 2024 23:37:37 +0800 Subject: [PATCH] test: fix unit tests --- core/blockchain.go | 1 + core/blockchain_snapshot_test.go | 30 ++++++++++++++++++++++-------- triedb/pathdb/database.go | 3 +-- triedb/pathdb/database_test.go | 3 ++- triedb/pathdb/journal.go | 11 +++-------- triedb/pathdb/nodebufferlist.go | 12 ++++-------- 6 files changed, 33 insertions(+), 27 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 825dce4e0d..dec63d2c40 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -195,6 +195,7 @@ func (c *CacheConfig) triedbConfig(keepFunc pathdb.NotifyKeepFunc) *triedb.Confi NotifyKeep: keepFunc, JournalFilePath: c.JournalFilePath, JournalFile: c.JournalFile, + UseBase: c.UseBase, } } return config diff --git a/core/blockchain_snapshot_test.go b/core/blockchain_snapshot_test.go index 348cc3f473..e2a5a2c9c7 100644 --- a/core/blockchain_snapshot_test.go +++ b/core/blockchain_snapshot_test.go @@ -81,7 +81,9 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo } engine = ethash.NewFullFaker() ) - chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(basic.scheme), gspec, nil, engine, vm.Config{}, nil, nil) + cacheConfig := DefaultCacheConfigWithScheme(basic.scheme) + cacheConfig.UseBase = true + chain, err := NewBlockChain(db, cacheConfig, gspec, nil, engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to create chain: %v", err) } @@ -180,11 +182,11 @@ func (basic *snapshotTestBasic) dump() string { } fmt.Fprint(buffer, "\n") - //if crash { + // if crash { // fmt.Fprintf(buffer, "\nCRASH\n\n") - //} else { + // } else { // fmt.Fprintf(buffer, "\nSetHead(%d)\n\n", basic.setHead) - //} + // } fmt.Fprintf(buffer, "------------------------------\n\n") fmt.Fprint(buffer, "Expected in leveldb:\n G") @@ -228,7 +230,10 @@ func (snaptest *snapshotTest) test(t *testing.T) { // Restart the chain normally chain.Stop() - 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) } @@ -313,6 +318,7 @@ func (snaptest *gappedSnapshotTest) test(t *testing.T) { SnapshotLimit: 0, StateScheme: 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) @@ -321,7 +327,9 @@ func (snaptest *gappedSnapshotTest) test(t *testing.T) { newchain.Stop() // Restart the chain with enabling the snapshot - newchain, err = NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + config := DefaultCacheConfigWithScheme(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) } @@ -349,7 +357,9 @@ func (snaptest *setHeadSnapshotTest) test(t *testing.T) { chain.SetHead(snaptest.setHead) chain.Stop() - 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) } @@ -385,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) @@ -402,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) @@ -411,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..e9d5c82308 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. @@ -369,7 +368,7 @@ func (db *Database) Enable(root common.Hash) error { // Re-construct a new disk layer backed by persistent state // with **empty clean cache and node buffer**. nb, err := NewTrieNodeBuffer(db.diskdb, db.config.TrieNodeBufferType, db.bufferSize, nil, 0, db.config.ProposeBlockInterval, - db.config.NotifyKeep, nil, false, false) + db.config.NotifyKeep, db.freezer, false, false) if err != nil { log.Error("Failed to new trie node buffer", "error", err) return err diff --git a/triedb/pathdb/database_test.go b/triedb/pathdb/database_test.go index a51f339be6..3674064cf3 100644 --- a/triedb/pathdb/database_test.go +++ b/triedb/pathdb/database_test.go @@ -319,6 +319,7 @@ func (t *tester) verifyState(root common.Hash) error { } _, err = reader.Node(common.Hash{}, nil, root) if err != nil { + fmt.Println("error: ", err) return errors.New("root node is not available") } for addrHash, account := range t.snapAccounts[root] { @@ -459,6 +460,7 @@ func TestDisable(t *testing.T) { t.Fatalf("Invalid activation should be rejected") } if err := tester.db.Enable(stored); err != nil { + fmt.Println(err) t.Fatal("Failed to activate database") } @@ -516,7 +518,6 @@ func TestJournal(t *testing.T) { } tester.db.Close() pathConfig := Defaults - pathConfig.UseBase = true tester.db = New(tester.db.diskdb, pathConfig) // Verify states including disk layer and all diff on top. diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index a24a9c1c2b..4579bb41aa 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -250,13 +250,12 @@ func (db *Database) loadLayers() layer { _, root := rawdb.ReadAccountTrieNode(db.diskdb, nil) root = types.TrieRootHash(root) + fmt.Println("1 useBase, fastRecovery", db.useBase, db.fastRecovery) // Load the layers by resolving the journal head, err := db.loadJournal(root) 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 +269,9 @@ func (db *Database) loadLayers() layer { stateID = rawdb.ReadPersistentStateID(db.diskdb) ) - fmt.Println("use base: ", db.useBase) + fmt.Println("2 useBase, fastRecovery", db.useBase, db.fastRecovery) 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 +286,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) @@ -365,6 +362,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalTyp } } + fmt.Println("3 useBase, fastRecovery", db.useBase, db.fastRecovery) // Calculate the internal state transitions by id difference. nb, err := NewTrieNodeBuffer(db.diskdb, db.config.TrieNodeBufferType, db.bufferSize, nodes, id-stored, db.config.ProposeBlockInterval, db.config.NotifyKeep, db.freezer, db.fastRecovery, db.useBase) @@ -373,13 +371,10 @@ 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") - fmt.Println("recoveredRoot, root, recoveredStateID, id", recoveredRoot, root, recoveredStateID, id) return nil, errors.New("Unmatched root and state id with recovered") } diff --git a/triedb/pathdb/nodebufferlist.go b/triedb/pathdb/nodebufferlist.go index 71cb52ddf2..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{}), @@ -127,6 +123,7 @@ func newNodeBufferList( keepFunc: keepFunc, } + fmt.Println("useBase, fastRecovery", useBase, fastRecovery) if !useBase && fastRecovery { if freezer == nil { log.Crit("Use unopened freezer db to recover node buffer list") @@ -164,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) @@ -841,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...) }