Skip to content

Commit 99b2991

Browse files
authored
Merge pull request #293 from canopy-network/delegate-fix
feat: add delegator support for NewValidatorSet
2 parents 4b3f8f6 + 44195b2 commit 99b2991

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

fsm/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func (s *StateMachine) getValidatorSet(chainId uint64, delegate bool) (vs lib.Va
491491
})
492492
}
493493
// convert list to a validator set (includes shared public key)
494-
return lib.NewValidatorSet(&lib.ConsensusValidators{ValidatorSet: members})
494+
return lib.NewValidatorSet(&lib.ConsensusValidators{ValidatorSet: members}, delegate)
495495
}
496496

497497
// pubKeyBytesToAddress() is a convenience function that converts a public key to an address

lib/consensus.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,37 @@ type ValidatorSet struct {
2222
}
2323

2424
// NewValidatorSet() initializes a ValidatorSet from a given set of consensus validators
25-
func NewValidatorSet(validators *ConsensusValidators) (ValidatorSet, ErrorI) {
25+
func NewValidatorSet(validators *ConsensusValidators, delegate ...bool) (ValidatorSet, ErrorI) {
2626
// handle empty set
2727
if validators == nil {
2828
// exit with error
2929
return ValidatorSet{}, ErrNoValidators()
3030
}
31+
// assert whether is a delegator set
32+
isDelegators := len(delegate) > 0 && delegate[0]
3133
// define tracking variables
3234
totalPower, count, pointList := uint64(0), uint64(0), make([]kyber.Point, 0)
3335
// iterate through the ValidatorSet to get the count, total power, and convert
3436
// the public keys to 'points' on an elliptic curve for the BLS multikey
3537
for _, v := range validators.ValidatorSet {
36-
// convert the public key into a BLS point
37-
point, err := crypto.BytesToBLS12381Point(v.PublicKey)
38-
// check for an error during the conversion
39-
if err != nil {
40-
// exit with error
41-
return ValidatorSet{}, ErrPubKeyFromBytes(err)
38+
// if not a delegator set, convert the public key into a BLS point
39+
if !isDelegators {
40+
// convert the public key into a BLS point
41+
point, err := crypto.BytesToBLS12381Point(v.PublicKey)
42+
// check for an error during the conversion
43+
if err != nil {
44+
// exit with error
45+
return ValidatorSet{}, ErrPubKeyFromBytes(err)
46+
}
47+
// add the point to the list
48+
pointList = append(pointList, point)
49+
} else {
50+
// otherwise just validate the public key
51+
if _, err := crypto.NewPublicKeyFromBytes(v.PublicKey); err != nil {
52+
// exit with error
53+
return ValidatorSet{}, ErrPubKeyFromBytes(err)
54+
}
4255
}
43-
// add the point to the list
44-
pointList = append(pointList, point)
4556
// update total voting power
4657
totalPower += v.VotingPower
4758
// increment the count
@@ -54,12 +65,15 @@ func NewValidatorSet(validators *ConsensusValidators) (ValidatorSet, ErrorI) {
5465
}
5566
// calculate the minimum power for a two-thirds majority (2f+1)
5667
minPowerFor23Maj := (2*totalPower)/3 + 1
57-
// create a composite multi-public key out of the public keys (in curve point format)
58-
multiPublicKey, err := crypto.NewMultiBLSFromPoints(pointList, nil)
59-
// if an error occurred during the conversion
60-
if err != nil {
61-
// exit with error
62-
return ValidatorSet{}, ErrNewMultiPubKey(err)
68+
var multiPublicKey crypto.MultiPublicKeyI
69+
// for validators, create a composite multi-public key out of the public
70+
// keys (in curve point format)
71+
if !isDelegators {
72+
var err error
73+
multiPublicKey, err = crypto.NewMultiBLSFromPoints(pointList, nil)
74+
if err != nil {
75+
return ValidatorSet{}, ErrNewMultiPubKey(err)
76+
}
6377
}
6478
// return the validator set
6579
return ValidatorSet{
@@ -254,8 +268,6 @@ func (x *AggregateSignature) LogNonSigners(validatorList *ConsensusValidators, p
254268
val.NetAddress, bls.Address().String(), height, chainId, producerNetAddress, producerAddress)
255269
}
256270
}
257-
// exit
258-
return
259271
}
260272

261273
// getSigners() returns the public keys and corresponding combined voting power of signers or non-signers

0 commit comments

Comments
 (0)