adding support for validating multibls for same shard (#2402)

adding unit test

fixing golint issue

fixing minor issue
pull/2424/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent 71974e1440
commit 87963bb791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      cmd/harmony/main.go
  2. 20
      internal/configs/node/config.go
  3. 44
      internal/configs/node/config_test.go

@ -250,6 +250,9 @@ func setupStakingNodeAccount() error {
if err != nil {
return errors.Wrap(err, "cannot determine shard to join")
}
if err := nodeconfig.GetDefaultConfig().ValidateConsensusKeysForSameShard(pubKey.PublicKey, shardID); err != nil {
return err
}
for _, blsKey := range pubKey.PublicKey {
initialAccount := &genesis.DeployAccount{}
initialAccount.ShardID = shardID

@ -8,6 +8,7 @@ import (
"math/big"
"sync"
"github.com/harmony-one/bls/ffi/go/bls"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/multibls"
@ -292,6 +293,25 @@ func (conf *ConfigType) ShardIDFromConsensusKey() (uint32, error) {
return uint32(shardID.Uint64()), nil
}
// ValidateConsensusKeysForSameShard checks if all consensus public keys belong to the same shard
func (conf *ConfigType) ValidateConsensusKeysForSameShard(pubkeys []*bls.PublicKey, sID uint32) error {
var pubKey shard.BlsPublicKey
for _, key := range pubkeys {
if err := pubKey.FromLibBLSPublicKey(key); err != nil {
return errors.Wrapf(err,
"cannot convert libbls public key %s to internal form",
key.SerializeToHexStr())
}
epoch := conf.networkType.ChainConfig().StakingEpoch
numShards := conf.shardingSchedule.InstanceForEpoch(epoch).NumShards()
shardID := new(big.Int).Mod(pubKey.Big(), big.NewInt(int64(numShards)))
if uint32(shardID.Uint64()) != sID {
return errors.New("bls keys do not belong to the same shard")
}
}
return nil
}
// ChainConfig returns the chain configuration for the network type.
func (t NetworkType) ChainConfig() params.ChainConfig {
switch t {

@ -6,7 +6,10 @@ import (
"github.com/golang/mock/gomock"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/pkg/errors"
"github.com/harmony-one/harmony/internal/blsgen"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
mock_shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding/mock"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/multibls"
@ -131,3 +134,44 @@ func TestConfigType_ShardIDFromConsensusKey(t *testing.T) {
})
}
}
func TestValidateConsensusKeysForSameShard(t *testing.T) {
// set localnet config
networkType := "localnet"
schedule := shardingconfig.LocalnetSchedule
netType := NetworkType(networkType)
SetNetworkType(netType)
SetShardingSchedule(schedule)
// import two keys that belong to same shard and test ValidateConsensusKeysForSameShard
keyPath1 := "../../../.hmy/65f55eb3052f9e9f632b2923be594ba77c55543f5c58ee1454b9cfd658d25e06373b0f7d42a19c84768139ea294f6204.key"
priKey1, err := blsgen.LoadBlsKeyWithPassPhrase(keyPath1, "")
pubKey1 := priKey1.GetPublicKey()
if err != nil {
t.Error(err)
}
keyPath2 := "../../../.hmy/ca86e551ee42adaaa6477322d7db869d3e203c00d7b86c82ebee629ad79cb6d57b8f3db28336778ec2180e56a8e07296.key"
priKey2, err := blsgen.LoadBlsKeyWithPassPhrase(keyPath2, "")
pubKey2 := priKey2.GetPublicKey()
if err != nil {
t.Error(err)
}
keys := []*bls.PublicKey{}
keys = append(keys, pubKey1)
keys = append(keys, pubKey2)
if err := GetDefaultConfig().ValidateConsensusKeysForSameShard(keys, 0); err != nil {
t.Error("expected", nil, "got", err)
}
// add third key in different shard and test ValidateConsensusKeysForSameShard
keyPath3 := "../../../.hmy/68ae289d73332872ec8d04ac256ca0f5453c88ad392730c5741b6055bc3ec3d086ab03637713a29f459177aaa8340615.key"
priKey3, err := blsgen.LoadBlsKeyWithPassPhrase(keyPath3, "")
pubKey3 := priKey3.GetPublicKey()
if err != nil {
t.Error(err)
}
keys = append(keys, pubKey3)
if err := GetDefaultConfig().ValidateConsensusKeysForSameShard(keys, 0); err == nil {
e := errors.New("bls keys do not belong to the same shard")
t.Error("expected", e, "got", nil)
}
}

Loading…
Cancel
Save