Skip to content

Commit

Permalink
fix: recover node buffer list trie nodes for graceful kill
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Nov 16, 2024
1 parent ed46c9a commit c6fda3c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,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
4 changes: 3 additions & 1 deletion triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ 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 {
Expand All @@ -268,6 +269,7 @@ func (db *Database) loadLayers() layer {
stateID = rawdb.ReadPersistentStateID(db.diskdb)
)

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 {
start := time.Now()
Expand Down Expand Up @@ -360,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 @@ -372,7 +375,6 @@ func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalTyp
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

0 comments on commit c6fda3c

Please sign in to comment.