NewValidatorSet() initializes a ValidatorSet from a given set of consensus validators
(validators *ConsensusValidators, delegate ...bool)
| 23 | |
| 24 | // NewValidatorSet() initializes a ValidatorSet from a given set of consensus validators |
| 25 | func NewValidatorSet(validators *ConsensusValidators, delegate ...bool) (ValidatorSet, ErrorI) { |
| 26 | // handle empty set |
| 27 | if validators == nil { |
| 28 | // exit with error |
| 29 | return ValidatorSet{}, ErrNoValidators() |
| 30 | } |
| 31 | // assert whether is a delegator set |
| 32 | isDelegators := len(delegate) > 0 && delegate[0] |
| 33 | // define tracking variables |
| 34 | totalPower, count, pointList := uint64(0), uint64(0), make([]kyber.Point, 0) |
| 35 | // iterate through the ValidatorSet to get the count, total power, and convert |
| 36 | // the public keys to 'points' on an elliptic curve for the BLS multikey |
| 37 | for _, v := range validators.ValidatorSet { |
| 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 | } |
| 55 | } |
| 56 | // update total voting power |
| 57 | totalPower += v.VotingPower |
| 58 | // increment the count |
| 59 | count++ |
| 60 | } |
| 61 | // if the total voting power is 0 |
| 62 | if totalPower == 0 { |
| 63 | // exit with error |
| 64 | return ValidatorSet{}, ErrNoValidators() |
| 65 | } |
| 66 | // calculate the minimum power for a two-thirds majority (2f+1) |
| 67 | minPowerFor23Maj := (2*totalPower)/3 + 1 |
| 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 | } |
| 77 | } |
| 78 | // return the validator set |
| 79 | return ValidatorSet{ |
| 80 | ValidatorSet: validators, |
| 81 | MultiKey: multiPublicKey, |
| 82 | TotalPower: totalPower, |