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/consensus/consensus_leader_test.go

198 lines
6.5 KiB

package consensus
import (
6 years ago
"crypto/sha256"
"fmt"
6 years ago
"testing"
"time"
"github.com/harmony-one/bls/ffi/go/bls"
6 years ago
"github.com/ethereum/go-ethereum/rlp"
"github.com/golang/mock/gomock"
protobuf "github.com/golang/protobuf/proto"
"github.com/harmony-one/harmony/api/proto"
6 years ago
msg_pb "github.com/harmony-one/harmony/api/proto/message"
6 years ago
"github.com/harmony-one/harmony/core/types"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils"
6 years ago
"github.com/harmony-one/harmony/p2p"
mock_host "github.com/harmony-one/harmony/p2p/host/mock"
"github.com/harmony-one/harmony/p2p/p2pimpl"
6 years ago
"github.com/stretchr/testify/assert"
)
var (
6 years ago
ip = "127.0.0.1"
blockHash = sha256.Sum256([]byte("test"))
)
6 years ago
func TestProcessMessageLeaderPrepare(test *testing.T) {
ctrl := gomock.NewController(test)
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "7777"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
validatorsPriKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 7788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorsPriKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
}
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
// Anything else will fail.
m.EXPECT().GetSelfPeer().Return(leader)
m.EXPECT().SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, gomock.Any())
consensusLeader := New(m, "0", validators, leader, priKey)
6 years ago
consensusLeader.blockHash = blockHash
consensusValidators := make([]*Consensus, 3)
for i := 0; i < 3; i++ {
priKey, _, _ := utils.GenKeyP2P(validators[i].IP, validators[i].Port)
host, err := p2pimpl.NewHost(&validators[i], priKey)
if err != nil {
test.Fatalf("newhost error: %v", err)
}
hosts[i] = host
consensusValidators[i] = New(hosts[i], "0", validators, leader, validatorsPriKeys[i])
6 years ago
consensusValidators[i].blockHash = blockHash
msg := consensusValidators[i].constructPrepareMessage()
msgPayload, _ := proto.GetConsensusMessagePayload(msg)
consensusLeader.ProcessMessageLeader(msgPayload)
}
6 years ago
assert.Equal(test, PreparedDone, consensusLeader.state)
time.Sleep(1 * time.Second)
}
func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
ctrl := gomock.NewController(test)
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "7777"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
validatorKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 7788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
}
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
// Anything else will fail.
m.EXPECT().GetSelfPeer().Return(leader)
consensusLeader := New(m, "0", validators, leader, priKey)
consensusLeader.blockHash = blockHash
consensusValidators := make([]*Consensus, 3)
for i := 0; i < 3; i++ {
priKey, _, _ := utils.GenKeyP2P(validators[i].IP, validators[i].Port)
host, err := p2pimpl.NewHost(&validators[i], priKey)
if err != nil {
test.Fatalf("newhost error: %v", err)
}
hosts[i] = host
consensusValidators[i] = New(hosts[i], "0", validators, leader, validatorKeys[i])
consensusValidators[i].blockHash = blockHash
6 years ago
msgBytes := consensusValidators[i].constructPrepareMessage()
msgPayload, _ := proto.GetConsensusMessagePayload(msgBytes)
6 years ago
message := &msg_pb.Message{}
if err = protobuf.Unmarshal(msgPayload, message); err != nil {
6 years ago
test.Error("Error when unmarshalling")
}
// Put invalid signature
message.Signature = consensusValidators[i].signMessage([]byte("random string"))
6 years ago
if msgBytes, err = protobuf.Marshal(message); err != nil {
test.Error("Error when marshalling")
}
consensusLeader.ProcessMessageLeader(msgBytes)
}
assert.Equal(test, Finished, consensusLeader.state)
time.Sleep(1 * time.Second)
}
6 years ago
func TestProcessMessageLeaderCommit(test *testing.T) {
ctrl := gomock.NewController(test)
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "8889"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
validatorKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 8788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
}
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
// Anything else will fail.
m.EXPECT().GetSelfPeer().Return(leader)
m.EXPECT().SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, gomock.Any())
for i := 0; i < 3; i++ {
priKey, _, _ := utils.GenKeyP2P(validators[i].IP, validators[i].Port)
host, err := p2pimpl.NewHost(&validators[i], priKey)
if err != nil {
test.Fatalf("newhost error: %v", err)
}
hosts[i] = host
}
consensusLeader := New(m, "0", validators, leader, priKey)
6 years ago
consensusLeader.state = PreparedDone
consensusLeader.blockHash = blockHash
consensusLeader.OnConsensusDone = func(newBlock *types.Block) {}
consensusLeader.block, _ = rlp.EncodeToBytes(types.NewBlock(&types.Header{}, nil, nil))
consensusLeader.prepareSigs[consensusLeader.nodeID] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
aggSig := bls_cosi.AggregateSig(consensusLeader.GetPrepareSigsArray())
multiSigAndBitmap := append(aggSig.Serialize(), consensusLeader.prepareBitmap.Bitmap...)
consensusLeader.aggregatedPrepareSig = aggSig
consensusValidators := make([]*Consensus, 3)
6 years ago
go func() {
<-consensusLeader.ReadySignal
<-consensusLeader.ReadySignal
}()
for i := 0; i < 3; i++ {
consensusValidators[i] = New(hosts[i], "0", validators, leader, validatorKeys[i])
6 years ago
consensusValidators[i].blockHash = blockHash
6 years ago
payload := consensusValidators[i].constructCommitMessage(multiSigAndBitmap)
msg, err := proto.GetConsensusMessagePayload(payload)
if err != nil {
test.Error("Error when getting consensus message", "error", err)
}
6 years ago
consensusLeader.ProcessMessageLeader(msg)
}
6 years ago
assert.Equal(test, Finished, consensusLeader.state)
time.Sleep(1 * time.Second)
}