diff --git a/consensus/consensus.go b/consensus/consensus.go index 4b01834fa..12ae7d8b4 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -2,6 +2,7 @@ package consensus // consensus import ( + "fmt" "math/big" "sync" "time" @@ -256,6 +257,9 @@ func New(host p2p.Host, ShardID uint32, leader p2p.Peer, blsPriKey *bls.SecretKe consensus.priKey = blsPriKey consensus.PubKey = blsPriKey.GetPublicKey() utils.GetLogInstance().Info("my pubkey is", "pubkey", consensus.PubKey.SerializeToHexStr()) + } else { + utils.GetLogInstance().Error("the bls key is nil") + return nil, fmt.Errorf("nil bls key, aborting") } // viewID has to be initialized as the height of the blockchain during initialization diff --git a/node/node_handler_test.go b/node/node_handler_test.go index 6f3b54dbf..c1aa5c982 100644 --- a/node/node_handler_test.go +++ b/node/node_handler_test.go @@ -12,14 +12,15 @@ import ( ) func TestAddNewBlock(t *testing.T) { - pubKey := bls.RandPrivateKey().GetPublicKey() + blsKey := bls.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "9882", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") host, err := p2pimpl.NewHost(&leader, priKey) if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } @@ -37,14 +38,15 @@ func TestAddNewBlock(t *testing.T) { } func TestVerifyNewBlock(t *testing.T) { - pubKey := bls.RandPrivateKey().GetPublicKey() + blsKey := bls.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") host, err := p2pimpl.NewHost(&leader, priKey) if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } diff --git a/node/node_test.go b/node/node_test.go index cca7fc5d0..143bdb8b8 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -24,14 +24,15 @@ import ( var testDBFactory = &shardchain.MemDBFactory{} func TestNewNode(t *testing.T) { - pubKey := bls2.RandPrivateKey().GetPublicKey() + blsKey := bls2.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") host, err := p2pimpl.NewHost(&leader, priKey) if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } @@ -50,7 +51,8 @@ func TestNewNode(t *testing.T) { } func TestGetSyncingPeers(t *testing.T) { - pubKey := bls2.RandPrivateKey().GetPublicKey() + blsKey := bls2.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") host, err := p2pimpl.NewHost(&leader, priKey) @@ -58,7 +60,7 @@ func TestGetSyncingPeers(t *testing.T) { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } @@ -92,7 +94,8 @@ func TestAddPeers(t *testing.T) { ConsensusPubKey: pubKey2, }, } - pubKey := bls2.RandPrivateKey().GetPublicKey() + blsKey := bls2.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey} validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") @@ -100,7 +103,7 @@ func TestAddPeers(t *testing.T) { if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } @@ -138,7 +141,8 @@ func TestAddBeaconPeer(t *testing.T) { PeerID: "4567", }, } - pubKey := bls2.RandPrivateKey().GetPublicKey() + blsKey := bls2.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey} validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") @@ -146,7 +150,7 @@ func TestAddBeaconPeer(t *testing.T) { if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } @@ -212,7 +216,8 @@ func exitServer() { } func TestPingPongHandler(t *testing.T) { - pubKey := bls2.RandPrivateKey().GetPublicKey() + blsKey := bls2.RandPrivateKey() + pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8881", ConsensusPubKey: pubKey} // validator := p2p.Peer{IP: "127.0.0.1", Port: "9991"} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") @@ -220,7 +225,7 @@ func TestPingPongHandler(t *testing.T) { if err != nil { t.Fatalf("newhost failure: %v", err) } - consensus, err := consensus.New(host, 0, leader, nil) + consensus, err := consensus.New(host, 0, leader, blsKey) if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) }