disallow duplicate bls keys as internal keys (#2633)

getting rid of global and fetching information on the fly

simplifying the check
pull/2636/head
Ganesha Upadhyaya 5 years ago committed by GitHub
parent dfdfb515c0
commit cb42bdb767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      core/staking_verifier.go
  2. 37
      staking/types/validator.go
  3. 4
      staking/types/validator_test.go

@ -52,7 +52,7 @@ func VerifyAndCreateValidatorFromMsg(
if !CanTransfer(stateDB, msg.ValidatorAddress, msg.Amount) {
return nil, errInsufficientBalanceForStake
}
v, err := staking.CreateValidatorFromNewMsg(msg, blockNum)
v, err := staking.CreateValidatorFromNewMsg(msg, blockNum, epoch)
if err != nil {
return nil, err
}
@ -97,7 +97,7 @@ func VerifyAndEditValidatorFromMsg(
if err != nil {
return nil, err
}
if err := staking.UpdateValidatorFromEditMsg(&wrapper.Validator, msg); err != nil {
if err := staking.UpdateValidatorFromEditMsg(&wrapper.Validator, msg, epoch); err != nil {
return nil, err
}
newRate := wrapper.Validator.Rate

@ -12,6 +12,7 @@ import (
"github.com/harmony-one/harmony/crypto/hash"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/genesis"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/staking/effective"
@ -470,8 +471,31 @@ func VerifyBLSKey(pubKey *shard.BlsPublicKey, pubKeySig *shard.BLSSignature) err
return nil
}
func containsHarmonyBlsKeys(blsKeys []shard.BlsPublicKey,
hmyAccounts []genesis.DeployAccount) error {
for i := range blsKeys {
if err := matchesHarmonyBlsKey(&blsKeys[i], hmyAccounts); err != nil {
return err
}
}
return nil
}
func matchesHarmonyBlsKey(blsKey *shard.BlsPublicKey,
hmyAccounts []genesis.DeployAccount) error {
for i := range hmyAccounts {
if blsKey.Hex() == hmyAccounts[i].BlsPublicKey {
return errors.Wrapf(errDuplicateSlotKeys,
"slot key %#v conflicts with internal keys",
hmyAccounts[i].BlsPublicKey,
)
}
}
return nil
}
// CreateValidatorFromNewMsg creates validator from NewValidator message
func CreateValidatorFromNewMsg(val *CreateValidator, blockNum *big.Int) (*Validator, error) {
func CreateValidatorFromNewMsg(val *CreateValidator, blockNum, epoch *big.Int) (*Validator, error) {
desc, err := val.Description.EnsureLength()
if err != nil {
return nil, err
@ -479,6 +503,11 @@ func CreateValidatorFromNewMsg(val *CreateValidator, blockNum *big.Int) (*Valida
commission := Commission{val.CommissionRates, blockNum}
pubKeys := append(val.SlotPubKeys[0:0], val.SlotPubKeys...)
instance := shard.Schedule.InstanceForEpoch(epoch)
if err := containsHarmonyBlsKeys(pubKeys, instance.HmyAccounts()); err != nil {
return nil, err
}
if err = VerifyBLSKeys(pubKeys, val.SlotKeySigs); err != nil {
return nil, err
}
@ -498,7 +527,7 @@ func CreateValidatorFromNewMsg(val *CreateValidator, blockNum *big.Int) (*Valida
}
// UpdateValidatorFromEditMsg updates validator from EditValidator message
func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error {
func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator, epoch *big.Int) error {
if validator.Address != edit.ValidatorAddress {
return errAddressNotMatch
}
@ -548,6 +577,10 @@ func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error
}
}
if !found {
instance := shard.Schedule.InstanceForEpoch(epoch)
if err := matchesHarmonyBlsKey(edit.SlotKeyToAdd, instance.HmyAccounts()); err != nil {
return err
}
if err := VerifyBLSKey(edit.SlotKeyToAdd, edit.SlotKeyToAddSig); err != nil {
return err
}

@ -400,7 +400,7 @@ func TestCreateValidatorFromNewMsg(t *testing.T) {
Amount: big.NewInt(1e18),
}
blockNum := big.NewInt(1000)
_, err := CreateValidatorFromNewMsg(&v, blockNum)
_, err := CreateValidatorFromNewMsg(&v, blockNum, new(big.Int))
if err != nil {
t.Errorf("CreateValidatorFromNewMsg failed")
}
@ -413,7 +413,7 @@ func TestUpdateValidatorFromEditMsg(t *testing.T) {
MinSelfDelegation: tenK,
MaxTotalDelegation: twelveK,
}
UpdateValidatorFromEditMsg(&validator, &ev)
UpdateValidatorFromEditMsg(&validator, &ev, new(big.Int))
if validator.MinSelfDelegation.Cmp(tenK) != 0 {
t.Errorf("UpdateValidatorFromEditMsg failed")

Loading…
Cancel
Save