Skip to content

Commit

Permalink
consensus/parlia: improve the check whether switched
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Jul 3, 2024
1 parent d6ea2fc commit 904483d
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions consensus/parlia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"sort"

lru "github.com/hashicorp/golang-lru"
Expand Down Expand Up @@ -272,11 +271,12 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
snap.updateAttestation(header, chainConfig, s.config)
// change validator set
if number > 0 && number%s.config.Epoch == uint64(len(snap.Validators)/2) {
epochKey := math.MaxUint64 - header.Number.Uint64()/s.config.Epoch // impossible used as a block number
if chainConfig.IsBohr(header.Number, header.Time) {
// after switching the validator set, snap.Validators may become larger,
// then the unexpected second switch will happen, just skip it.
if _, ok := snap.Recents[epochKey]; ok {
// lastEpochBlock will be removed from Recents during the first switch.
lastEpochBlock := header.Number.Uint64() - uint64(len(snap.Validators)/2)
if _, ok := snap.Recents[lastEpochBlock]; !ok {
continue
}
}
Expand Down Expand Up @@ -304,7 +304,6 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
if chainConfig.IsBohr(header.Number, header.Time) {
// BEP-404: Clear Miner History when Switching Validators Set
snap.Recents = make(map[uint64]common.Address)
snap.Recents[epochKey] = common.Address{}
log.Debug("Recents are cleared up", "blockNumber", number)
} else {
oldLimit := len(snap.Validators)/2 + 1
Expand Down

0 comments on commit 904483d

Please sign in to comment.