Skip to content

Commit

Permalink
consortium-v2: add parent list parameter to IsTrippEffective (#651)
Browse files Browse the repository at this point in the history
This is the same as commit b718105 ("consortium-v2: add parent list
parameter to IsPeriodBlock") but with the IsTrippEffective function. The parent
list is passed to this function when it is called in the VerifyHeader path.
  • Loading branch information
minh-bq authored Jan 6, 2025
1 parent 147727d commit e14b3cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (c *Consortium) verifyFinalitySignatures(
header *types.Header,
parents []*types.Header,
) error {
isTrippEffective := c.IsTrippEffective(chain, header)
isTrippEffective := c.IsTrippEffective(chain, header, parents)
snap, err := c.snapshot(chain, header.Number.Uint64()-1, header.ParentHash, parents)
if err != nil {
return err
Expand Down Expand Up @@ -457,7 +457,7 @@ func (c *Consortium) verifyValidatorFieldsInExtraData(
}
}

if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, parents) {
if c.chainConfig.IsAaron(header.Number) {
if isEpoch && (extraData.BlockProducersBitSet == 0 || len(extraData.BlockProducers) != 0) {
return fmt.Errorf(
Expand Down Expand Up @@ -936,7 +936,7 @@ func (c *Consortium) getCheckpointValidatorsFromContract(

var checkpointValidators []finality.ValidatorWithBlsPub

if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
isAaron := c.chainConfig.IsAaron(header.Number)
if !isAaron {
sort.Sort(validatorsAscending(blockProducers))
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func (c *Consortium) Prepare(chain consensus.ChainHeaderReader, header *types.He
// the start of new period and does not change over the whole
// period; whereas block producer list is changed and read at
// the start of every new epoch.
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
// latestValidatorCandidates is the latest validator candidate list at the
// current epoch, which is used to calculate block producer bit set later on.
var latestValidatorCandidates []finality.ValidatorWithBlsPub
Expand Down Expand Up @@ -1298,7 +1298,7 @@ func (c *Consortium) Finalize(chain consensus.ChainHeaderReader, header *types.H

// If isTripp and new period, read all validator candidates and
// their amounts, check with stored data in header
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
isPeriodBlock, err := c.IsPeriodBlock(chain, header, nil)
if err != nil {
log.Error("Failed to check IsPeriodBlock", "blocknum", header.Number, "err", err)
Expand Down Expand Up @@ -1668,7 +1668,7 @@ func (c *Consortium) assembleFinalityVote(chain consensus.ChainHeaderReader, hea
accumulatedVoteWeight int
)

isTrippEffective := c.IsTrippEffective(chain, header)
isTrippEffective := c.IsTrippEffective(chain, header, nil)
if isTrippEffective {
finalityThreshold = int(math.Floor(finalityRatio*float64(consortiumCommon.MaxFinalityVotePercentage))) + 1
} else {
Expand Down Expand Up @@ -1818,7 +1818,7 @@ func (c *Consortium) IsFinalityVoterAt(chain consensus.ChainHeaderReader, header
nodeValidator, _, _, _ := c.readSignerAndContract()
// After Tripp, voting process is openned for a wider set of validator candidates
// (at most 64 validators), which are stored in ValidatorsWithBlsPub of HeaderExtraData
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
return snap.inVoterSet(nodeValidator)
}
return snap.inInValidatorSet(nodeValidator)
Expand Down Expand Up @@ -2026,7 +2026,7 @@ func (c *Consortium) IsPeriodBlock(chain consensus.ChainHeaderReader, header *ty
// IsTrippEffective returns indicator whether the Tripp consensus rule is effective,
// which is the first period that is greater than Tripp period, calculated by formula:
// period := timestamp / dayInSeconds.
func (c *Consortium) IsTrippEffective(chain consensus.ChainHeaderReader, header *types.Header) bool {
func (c *Consortium) IsTrippEffective(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) bool {
if c.isTest {
return c.testTrippEffective
}
Expand All @@ -2042,7 +2042,7 @@ func (c *Consortium) IsTrippEffective(chain consensus.ChainHeaderReader, header
}

// else check the period number of the last checkpoint header with the configured one
snap, err := c.snapshot(chain, header.Number.Uint64()-1, header.ParentHash, nil)
snap, err := c.snapshot(chain, header.Number.Uint64()-1, header.ParentHash, parents)
if err != nil {
log.Error("Failed to get snapshot", "err", err)
parent := c.getLastCheckpointHeader(chain, header)
Expand Down
14 changes: 7 additions & 7 deletions consensus/consortium/v2/consortium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2587,28 +2587,28 @@ func TestIsTrippEffective(t *testing.T) {
// header of block 30
header = bs[29].Header()
// this header must not be Tripp effective
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}

// header of block 201
// this header must not be Tripp effective
header = bs[201].Header()
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}

// header of block 200
// this header must not be Tripp effective
header = bs[200].Header()
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}

// header of block 399
// this header must not be Tripp effective
header = bs[398].Header()
if c.IsTrippEffective(chain, header) {
if c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}

Expand All @@ -2628,20 +2628,20 @@ func TestIsTrippEffective(t *testing.T) {
// header of block 400
// this header must be Tripp effective
header = bs[399].Header()
if !c.IsTrippEffective(nil, header) {
if !c.IsTrippEffective(nil, header, nil) {
t.Error("fail test Tripp effective")
}

// header of block 402
// this header must be Tripp effective
header = bs[401].Header()
if !c.IsTrippEffective(chain, header) {
if !c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}

header = bs[599].Header()
// this header must be Tripp effective
if !c.IsTrippEffective(chain, header) {
if !c.IsTrippEffective(chain, header, nil) {
t.Error("fail test Tripp effective")
}
}
Expand Down

0 comments on commit e14b3cf

Please sign in to comment.