The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/internal/configs/node/config_test.go

94 lines
3.0 KiB

package nodeconfig
import (
"testing"
8 months ago
"github.com/woop-chain/woop/crypto/bls"
8 months ago
"github.com/woop-chain/woop/internal/blsgen"
shardingconfig "github.com/woop-chain/woop/internal/configs/sharding"
"github.com/woop-chain/woop/multibls"
"github.com/pkg/errors"
)
func TestNodeConfigSingleton(t *testing.T) {
// init 3 configs
_ = GetShardConfig(2)
// get the singleton variable
c := GetShardConfig(Global)
c.SetBeaconGroupID(GroupIDBeacon)
d := GetShardConfig(Global)
g := d.GetBeaconGroupID()
if g != GroupIDBeacon {
t.Errorf("GetBeaconGroupID = %v, expected = %v", g, GroupIDBeacon)
}
}
func TestNodeConfigMultiple(t *testing.T) {
// init 3 configs
d := GetShardConfig(1)
e := GetShardConfig(0)
f := GetShardConfig(42)
if f != nil {
t.Errorf("expecting nil, got: %v", f)
}
d.SetShardGroupID("abcd")
if d.GetShardGroupID() != "abcd" {
t.Errorf("expecting abcd, got: %v", d.GetShardGroupID())
}
e.SetClientGroupID("client")
if e.GetClientGroupID() != "client" {
t.Errorf("expecting client, got: %v", d.GetClientGroupID())
}
}
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
8 months ago
keyPath1 := "../../../.wiki/65f55eb3052f9e9f632b2923be594ba77c55543f5c58ee1454b9cfd658d25e06373b0f7d42a19c84768139ea294f6204.key"
priKey1, err := blsgen.LoadBLSKeyWithPassPhrase(keyPath1, "")
pubKey1 := priKey1.GetPublicKey()
if err != nil {
t.Error(err)
}
8 months ago
keyPath2 := "../../../.wiki/ca86e551ee42adaaa6477322d7db869d3e203c00d7b86c82ebee629ad79cb6d57b8f3db28336778ec2180e56a8e07296.key"
priKey2, err := blsgen.LoadBLSKeyWithPassPhrase(keyPath2, "")
pubKey2 := priKey2.GetPublicKey()
if err != nil {
t.Error(err)
}
keys := multibls.PublicKeys{}
dummyKey := bls.SerializedPublicKey{}
dummyKey.FromLibBLSPublicKey(pubKey1)
keys = append(keys, bls.PublicKeyWrapper{Object: pubKey1, Bytes: dummyKey})
dummyKey = bls.SerializedPublicKey{}
dummyKey.FromLibBLSPublicKey(pubKey2)
keys = append(keys, bls.PublicKeyWrapper{Object: pubKey2, Bytes: dummyKey})
if err := GetDefaultConfig().ValidateConsensusKeysForSameShard(keys, 0); err != nil {
t.Error("expected", nil, "got", err)
}
// add third key in different shard and test ValidateConsensusKeysForSameShard
8 months ago
keyPath3 := "../../../.wiki/68ae289d73332872ec8d04ac256ca0f5453c88ad392730c5741b6055bc3ec3d086ab03637713a29f459177aaa8340615.key"
priKey3, err := blsgen.LoadBLSKeyWithPassPhrase(keyPath3, "")
pubKey3 := priKey3.GetPublicKey()
if err != nil {
t.Error(err)
}
dummyKey = bls.SerializedPublicKey{}
dummyKey.FromLibBLSPublicKey(pubKey3)
keys = append(keys, bls.PublicKeyWrapper{Object: pubKey3, Bytes: dummyKey})
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)
}
}