Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Nov 27, 2024
1 parent 0e683ba commit 3bfbfc1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (c *CacheConfig) triedbConfig(keepFunc pathdb.NotifyKeepFunc) *triedb.Confi
NotifyKeep: keepFunc,
JournalFilePath: c.JournalFilePath,
JournalFile: c.JournalFile,
UseBase: c.UseBase,
}
}
return config
Expand Down
30 changes: 22 additions & 8 deletions core/blockchain_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion triedb/pathdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 3 additions & 8 deletions triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}

Expand Down
12 changes: 4 additions & 8 deletions triedb/pathdb/nodebufferlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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...)
}
Expand Down

0 comments on commit 3bfbfc1

Please sign in to comment.