Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit fb562b0

Browse files
committed
consortium-v2: explicitly name conditional check in snapshot's apply
This is a refactor commit that explicitly name some hard to understand conditional checks in snapshot's apply. It also adds more comments to snapshot fields.
1 parent 8e06b07 commit fb562b0

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

consensus/consortium/v2/snapshot.go

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ type Snapshot struct {
2828
ethAPI *ethapi.PublicBlockChainAPI
2929
sigCache *arc.ARCCache[common.Hash, common.Address] // Cache of recent block signatures to speed up ecrecover
3030

31-
Number uint64 `json:"number"` // Block number where the snapshot was created
32-
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
33-
Validators map[common.Address]struct{} `json:"validators,omitempty"` // Set of authorized validators at this moment before Shillin
34-
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
35-
36-
// Finality additional fields
37-
ValidatorsWithBlsPub []finality.ValidatorWithBlsPub `json:"validatorWithBlsPub,omitempty"` // Array of sorted authorized validators and BLS public keys after Shillin
38-
39-
// After Tripp, block producers are stored separately in a new field BlockProducers,
40-
// differentiating from validator candidates, which are stored in ValidatorsWithBlsPub.
41-
BlockProducers []common.Address `json:"blockProducers,omitempty"` // Array of sorted block producers After Tripp.
42-
JustifiedBlockNumber uint64 `json:"justifiedBlockNumber,omitempty"` // The justified block number
43-
JustifiedBlockHash common.Hash `json:"justifiedBlockHash,omitempty"` // The justified block hash
44-
CurrentPeriod uint64 `json:"currentPeriod,omitempty"` // Period number where the snapshot was created
31+
Number uint64 `json:"number"` // Block number where the snapshot was created
32+
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
33+
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
34+
35+
// The block producer list (is able to produce block) before Shillin
36+
Validators map[common.Address]struct{} `json:"validators,omitempty"`
37+
// After Shillin before Tripp, the block producer list with BLS key
38+
// After Tripp, the validator list (is able to finality vote) with BLS key and weight
39+
ValidatorsWithBlsPub []finality.ValidatorWithBlsPub `json:"validatorWithBlsPub,omitempty"`
40+
// After Tripp, the block producer list
41+
BlockProducers []common.Address `json:"blockProducers,omitempty"`
42+
43+
JustifiedBlockNumber uint64 `json:"justifiedBlockNumber,omitempty"` // The justified block number
44+
JustifiedBlockHash common.Hash `json:"justifiedBlockHash,omitempty"` // The justified block hash
45+
CurrentPeriod uint64 `json:"currentPeriod,omitempty"` // Period number where the snapshot was created
4546
}
4647

4748
// validatorsAscending implements the sort interface to allow sorting a list of addresses
@@ -159,6 +160,28 @@ func (s *Snapshot) copy() *Snapshot {
159160
return cpy
160161
}
161162

163+
// isTrippEffective returns true the next day after the Tripp hardfork. Here we depends on
164+
// header's extra data which is checked in verifyValidatorFieldsInExtraData already
165+
func isTrippEffective(chainRules *params.Rules, extraData *finality.HeaderExtraData) bool {
166+
return chainRules.IsTripp && len(extraData.BlockProducers) != 0
167+
}
168+
169+
// isAaronEffective returns true the next day after the Aaron hardfork. Here we depends on
170+
// header's extra data which is checked in verifyValidatorFieldsInExtraData already
171+
func isAaronEffective(chainRules *params.Rules, extraData *finality.HeaderExtraData) bool {
172+
return chainRules.IsAaron && extraData.BlockProducersBitSet != 0
173+
}
174+
175+
func newRecentListLimit(chainRules *params.Rules, extraData *finality.HeaderExtraData) int {
176+
if isAaronEffective(chainRules, extraData) {
177+
return len(extraData.BlockProducersBitSet.Indices())/2 + 1
178+
} else if isTrippEffective(chainRules, extraData) {
179+
return len(extraData.BlockProducers)/2 + 1
180+
} else {
181+
return len(extraData.CheckpointValidators)/2 + 1
182+
}
183+
}
184+
162185
// apply creates a new authorization snapshot by applying the given headers to
163186
// the original one.
164187
func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderReader, parents []*types.Header, chainId *big.Int) (*Snapshot, error) {
@@ -197,9 +220,10 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
197220
validator common.Address
198221
err error
199222
)
223+
chainRules := snap.chainConfig.Rules(header.Number)
200224
// If the headers come from v1 the block hash function does not include chainId,
201225
// we need to use the correct ecrecover function the get the correct signer
202-
if !snap.chainConfig.IsConsortiumV2(header.Number) {
226+
if !chainRules.IsConsortiumV2 {
203227
validator, err = v1.Ecrecover(header, s.sigCache)
204228
} else {
205229
validator, err = ecrecover(header, s.sigCache, chainId)
@@ -217,7 +241,7 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
217241
}
218242
snap.Recents[number] = validator
219243

220-
if chain.Config().IsShillin(header.Number) {
244+
if chainRules.IsShillin {
221245
extraData, err := finality.DecodeExtraV2(header.Extra, chain.Config(), header.Number)
222246
if err != nil {
223247
return nil, err
@@ -235,9 +259,7 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
235259
}
236260
}
237261

238-
isTripp := chain.Config().IsTripp(header.Number)
239-
isAaron := s.chainConfig.IsAaron(header.Number)
240-
if isTripp && number%s.config.EpochV2 == 0 && header.Time/dayInSeconds > snap.CurrentPeriod {
262+
if chainRules.IsTripp && number%s.config.EpochV2 == 0 && header.Time/dayInSeconds > snap.CurrentPeriod {
241263
snap.CurrentPeriod = header.Time / dayInSeconds
242264
}
243265
// Change the validator set base on the size of the validators set
@@ -263,16 +285,7 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
263285
}
264286

265287
oldLimit := len(snap.validators())/2 + 1
266-
var newLimit int
267-
// After Tripp, list of block producers is retrieved from the
268-
// field BlockProducer, instead of field CheckpointValidators.
269-
if isAaron && extraData.BlockProducersBitSet != 0 {
270-
newLimit = len(extraData.BlockProducersBitSet.Indices())/2 + 1
271-
} else if isTripp && len(extraData.BlockProducers) != 0 {
272-
newLimit = len(extraData.BlockProducers)/2 + 1
273-
} else {
274-
newLimit = len(extraData.CheckpointValidators)/2 + 1
275-
}
288+
newLimit := newRecentListLimit(&chainRules, extraData)
276289
if newLimit < oldLimit {
277290
for i := 0; i < oldLimit-newLimit; i++ {
278291
delete(snap.Recents, number-uint64(newLimit)-uint64(i))
@@ -281,13 +294,13 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
281294

282295
// After Aaron, block producer list in snapshot is
283296
// reconstructed from bit set and validator candidate list.
284-
if isAaron && extraData.BlockProducersBitSet != 0 {
297+
if isAaronEffective(&chainRules, extraData) {
285298
if len(extraData.CheckpointValidators) != 0 {
286299
snap.ValidatorsWithBlsPub = extraData.CheckpointValidators
287300
}
288301
snap.BlockProducers = decodeValidatorBitSet(extraData.BlockProducersBitSet, snap.ValidatorsWithBlsPub)
289302
snap.Validators = nil
290-
} else if isTripp && len(extraData.BlockProducers) != 0 {
303+
} else if isTrippEffective(&chainRules, extraData) {
291304
// After Tripp is effective, the checkpoint validators in header's extra data
292305
// is set only at the period block, not at all checkpoint blocks anymore. So
293306
// only update snapshot's validator with bls public key when checkpoint
@@ -297,11 +310,10 @@ func (s *Snapshot) apply(headers []*types.Header, chain consensus.ChainHeaderRea
297310
}
298311
snap.BlockProducers = extraData.BlockProducers
299312
snap.Validators = nil
300-
} else if chain.Config().IsShillin(checkpointHeader.Number) {
313+
} else if chainRules.IsShillin {
301314
// The validator information in checkpoint header is already sorted,
302315
// we don't need to sort here
303-
snap.ValidatorsWithBlsPub = make([]finality.ValidatorWithBlsPub, len(extraData.CheckpointValidators))
304-
copy(snap.ValidatorsWithBlsPub, extraData.CheckpointValidators)
316+
snap.ValidatorsWithBlsPub = extraData.CheckpointValidators
305317
snap.Validators = nil
306318
snap.BlockProducers = nil
307319
} else {

params/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,8 +1135,8 @@ type Rules struct {
11351135
ChainID *big.Int
11361136
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
11371137
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
1138-
IsBerlin, IsLondon bool
1139-
IsOdysseusFork, IsFenix, IsConsortiumV2, IsAntenna bool
1138+
IsBerlin, IsLondon, IsOdysseusFork bool
1139+
IsFenix, IsShillin, IsConsortiumV2, IsAntenna bool
11401140
IsMiko, IsTripp, IsAaron, IsShanghai, IsCancun bool
11411141
IsVenoki bool
11421142
}
@@ -1161,6 +1161,7 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
11611161
IsLondon: c.IsLondon(num),
11621162
IsOdysseusFork: c.IsOdysseus(num),
11631163
IsFenix: c.IsFenix(num),
1164+
IsShillin: c.IsShillin(num),
11641165
IsConsortiumV2: c.IsConsortiumV2(num),
11651166
IsAntenna: c.IsAntenna(num),
11661167
IsMiko: c.IsMiko(num),

0 commit comments

Comments
 (0)