Skip to content

Commit

Permalink
fix: Fix pbss snapshot inconsistency with engine-sync enabled when st…
Browse files Browse the repository at this point in the history
…arting (bnb-chain#189)

Co-authored-by: welkin22 <[email protected]>
  • Loading branch information
krish-nr and welkin22 authored Oct 30, 2024
1 parent bdcfeec commit d3e4eb6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"sync/atomic"
"time"

"golang.org/x/exp/slices"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/common/mclock"
Expand All @@ -50,7 +52,6 @@ import (
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -398,6 +399,31 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
// Make sure the state associated with the block is available, or log out
// if there is no available state, waiting for state sync.
head := bc.CurrentBlock()

// Fix pbss snapshot if needed
if bc.triedb.Scheme() == rawdb.PathScheme {
log.Debug("pbss snapshot validation")
currentSafe := bc.CurrentSafeBlock()
currentFinalize := bc.CurrentFinalBlock()

// Check if either safe or finalized block is ahead of the head block
if currentSafe != nil && currentFinalize != nil {
if currentSafe.Number.Uint64() > head.Number.Uint64() || currentFinalize.Number.Uint64() > head.Number.Uint64() {
log.Info("current unsafe is behind safe, reset")
bc.HeaderChainForceSetHead(head.Number.Uint64())

// Update the safe and finalized block conditionally
if currentSafe.Number.Uint64() > head.Number.Uint64() {
bc.SetSafe(head)
}
if currentFinalize.Number.Uint64() > head.Number.Uint64() {
bc.SetFinalized(head)
}
}
}

}

if !bc.NoTries() && !bc.HasState(head.Root) {
if head.Number.Uint64() == 0 {
// The genesis state is missing, which is only possible in the path-based
Expand Down

0 comments on commit d3e4eb6

Please sign in to comment.