Skip to content

Commit

Permalink
consortium/v2: add VerifyVote and fix blsPublicKey access (axieinfini…
Browse files Browse the repository at this point in the history
…ty#327)

* consortium: add VerifyVote to the outer consortium

As required by the FastFinalityPoSA interface, VerifyVote is needed in the outer
consortium. We add VerifyVote to outer consortium which simply forwards to the
consortium v2 function.

* consortium/v2: access blsPublicKey only after Shillin

The BLS public key is only available in checkpoint header after Shillin, so
don't access this field before Shillin.
  • Loading branch information
minh-bq committed Sep 7, 2023
1 parent 4a11959 commit c46cf70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 6 additions & 0 deletions consensus/consortium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ func (c *Consortium) IsActiveValidatorAt(chain consensus.ChainHeaderReader, head
return false
}

// VerifyVote check if the finality voter is in the validator set, it assumes the signature is
// already verified
func (c *Consortium) VerifyVote(chain consensus.ChainHeaderReader, vote *types.VoteEnvelope) error {
return c.v2.VerifyVote(chain, vote)
}

// GetActiveValidatorAt always return false before Shillin
// See the comment for GetActiveValidatorAt in v2 package
// for more information
Expand Down
25 changes: 17 additions & 8 deletions consensus/consortium/v2/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ func (c *Consortium) getCheckpointValidatorsFromContract(
filteredValidators []common.Address = newValidators
)

if c.chainConfig.IsShillin(header.Number) {
isShillin := c.chainConfig.IsShillin(header.Number)
if isShillin {
// The filteredValidators shares the same underlying array with newValidators
// See more: https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
filteredValidators = filteredValidators[:0]
Expand All @@ -647,10 +648,14 @@ func (c *Consortium) getCheckpointValidatorsFromContract(
}

for i := range filteredValidators {
checkpointValidator = append(checkpointValidator, finality.ValidatorWithBlsPub{
Address: filteredValidators[i],
BlsPublicKey: blsPublicKeys[i],
})
validatorWithBlsPub := finality.ValidatorWithBlsPub{
Address: filteredValidators[i],
}
if isShillin {
validatorWithBlsPub.BlsPublicKey = blsPublicKeys[i]
}

checkpointValidator = append(checkpointValidator, validatorWithBlsPub)
}

// sort validator by address
Expand Down Expand Up @@ -814,14 +819,16 @@ func (c *Consortium) Finalize(chain consensus.ChainHeaderReader, header *types.H
EthAPI: c.ethAPI,
}

isShillin := c.chainConfig.IsShillin(header.Number)

// If the block is an epoch end block, verify the validator list
// The verification can only be done when the state is ready, it can't be done in VerifyHeader.
if header.Number.Uint64()%c.config.EpochV2 == 0 {
checkpointValidator, err := c.getCheckpointValidatorsFromContract(header)
if err != nil {
return err
}
extraData, err := finality.DecodeExtra(header.Extra, c.chainConfig.IsShillin(header.Number))
extraData, err := finality.DecodeExtra(header.Extra, isShillin)
if err != nil {
return err
}
Expand All @@ -835,8 +842,10 @@ func (c *Consortium) Finalize(chain consensus.ChainHeaderReader, header *types.H
return errMismatchingEpochValidators
}

if !checkpointValidator[i].BlsPublicKey.Equals(extraData.CheckpointValidators[i].BlsPublicKey) {
return errMismatchingEpochValidators
if isShillin {
if !checkpointValidator[i].BlsPublicKey.Equals(extraData.CheckpointValidators[i].BlsPublicKey) {
return errMismatchingEpochValidators
}
}
}
}
Expand Down

0 comments on commit c46cf70

Please sign in to comment.