Skip to content

Commit

Permalink
pathdb: handle persistent id when using nodebufferlist (bnb-chain#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysvm authored Jul 5, 2024
1 parent 685031a commit aead14e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions trie/triedb/pathdb/disklayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
var (
overflow bool
oldest uint64
limit = dl.db.config.StateHistory
)
if dl.db.freezer != nil {
err := writeHistory(dl.db.freezer, bottom)
Expand All @@ -293,7 +294,6 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
if err != nil {
return nil, err
}
limit := dl.db.config.StateHistory
if limit != 0 && bottom.stateID()-tail > limit {
overflow = true
oldest = bottom.stateID() - limit + 1 // track the id of history **after truncation**
Expand Down Expand Up @@ -322,17 +322,31 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
if !force && rawdb.ReadPersistentStateID(dl.db.diskdb) < oldest {
force = true
}

if err := ndl.buffer.flush(ndl.db.diskdb, ndl.cleans, ndl.id, force); err != nil {
return nil, err
}
// To remove outdated history objects from the end, we set the 'tail' parameter
// to 'oldest-1' due to the offset between the freezer index and the history ID.
if overflow {
if _, ok := dl.buffer.(*nodebufferlist); ok {
persistentID := rawdb.ReadPersistentStateID(dl.db.diskdb)
if persistentID > limit {
oldest = persistentID - limit + 1
log.Info("Forcing prune ancient under nodebufferlist", "disk_persistent_state_id",
persistentID, "truncate_tail", oldest)
} else {
log.Info("No prune ancient under nodebufferlist, less than db config state history limit")
return ndl, nil
}
}

pruned, err := truncateFromTail(ndl.db.diskdb, ndl.db.freezer, oldest-1)
if err != nil {
log.Error("Failed to truncate from tail", "ntail", oldest-1, "error", err)
return nil, err
}
log.Debug("Pruned state history", "items", pruned, "tailid", oldest)
log.Debug("Pruned state history", "items", pruned, "tail_id", oldest)
}

// The bottom has been eaten by disklayer, releasing the hash cache of bottom difflayer.
Expand Down
7 changes: 7 additions & 0 deletions trie/triedb/pathdb/nodebufferlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,18 @@ func (nf *nodebufferlist) flush(db ethdb.KeyValueStore, clean *fastcache.Cache,
}

commitFunc := func(buffer *multiDifflayer) bool {
if nf.count <= nf.rsevMdNum {
log.Info("keep multiDiffLayer in node bufferList for getting withdrawal proof",
"reserved_multidiflayer", nf.rsevMdNum, "bufferList_count", nf.count)
return false
}
if err := nf.base.commit(buffer.root, buffer.id, buffer.block, buffer.layers, buffer.nodes); err != nil {
log.Crit("failed to commit nodes to base node buffer", "error", err)
}
_ = nf.popBack()
return true
}

nf.traverseReverse(commitFunc)
persistID := nf.persistID + nf.base.layers
err := nf.base.flush(nf.db, nf.clean, persistID)
Expand All @@ -285,6 +291,7 @@ func (nf *nodebufferlist) flush(db ethdb.KeyValueStore, clean *fastcache.Cache,
nf.isFlushing.Store(false)
nf.base.reset()
nf.persistID = persistID

return nil
}

Expand Down

0 comments on commit aead14e

Please sign in to comment.