Skip to content

Commit

Permalink
consensus/parlia: support recovery when snapshot of parlia is gone in…
Browse files Browse the repository at this point in the history
… disk
  • Loading branch information
buddh0 committed Jul 16, 2024
1 parent c96fab0 commit 5c94a76
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
inMemoryHeaders = 86400 // Number of recent headers to keep in memory for double sign detection,

checkpointInterval = 1024 // Number of blocks after which to save the snapshot to the database
defaultEpochLength = uint64(100) // Default number of blocks of checkpoint to update validatorSet from contract
defaultEpochLength = uint64(200) // Default number of blocks of checkpoint to update validatorSet from contract

extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
Expand Down Expand Up @@ -713,25 +713,38 @@ func (p *Parlia) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
}
}

// If we're at the genesis, snapshot the initial state.
if number == 0 {
checkpoint := chain.GetHeaderByNumber(number)
if checkpoint != nil {
// get checkpoint data
hash := checkpoint.Hash()

// If we're at the genesis, snapshot the initial state. Alternatively if we have
// piled up more headers than allowed to be reorged (chain reinit from a freezer),
// consider the checkpoint trusted and snapshot it.
// even BEP-341 enabled, an offset `defaultEpochLength/2` can ensure getting the right validators.
if number == 0 || (number%p.config.Epoch == defaultEpochLength/2 && (len(headers) > int(params.FullImmutabilityThreshold)/10)) {
var (
checkpoint *types.Header
blockHash common.Hash
)
if number == 0 {
checkpoint := chain.GetHeaderByNumber(0)
blockHash = checkpoint.Hash()
} else {
checkpoint = chain.GetHeaderByNumber(number - defaultEpochLength/2)
blockHeader := chain.GetHeaderByNumber(number)
if blockHeader != nil {
blockHash = blockHeader.Hash()
}
}
if checkpoint != nil && blockHash != (common.Hash{}) {
// get validators from headers
validators, voteAddrs, err := parseValidators(checkpoint, p.chainConfig, p.config)
if err != nil {
return nil, err
}

// new snapshot
snap = newSnapshot(p.config, p.signatures, number, hash, validators, voteAddrs, p.ethAPI)
snap = newSnapshot(p.config, p.signatures, number, blockHash, validators, voteAddrs, p.ethAPI)
if err := snap.store(p.db); err != nil {
return nil, err
}
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", blockHash)
break
}
}
Expand Down

0 comments on commit 5c94a76

Please sign in to comment.