@ -6,6 +6,8 @@ import (
"testing"
"testing"
"time"
"time"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
"github.com/golang/mock/gomock"
"github.com/golang/mock/gomock"
protobuf "github.com/golang/protobuf/proto"
protobuf "github.com/golang/protobuf/proto"
@ -30,15 +32,17 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
defer ctrl . Finish ( )
defer ctrl . Finish ( )
leader := p2p . Peer { IP : ip , Port : "7777" }
leader := p2p . Peer { IP : ip , Port : "7777" }
_ , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
var priKey * bls . SecretKey
priKey , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
validators := make ( [ ] p2p . Peer , 3 )
validators := make ( [ ] p2p . Peer , 3 )
hosts := make ( [ ] p2p . Host , 3 )
hosts := make ( [ ] p2p . Host , 3 )
validatorsPriKeys := [ 3 ] * bls . SecretKey { }
for i := 0 ; i < 3 ; i ++ {
for i := 0 ; i < 3 ; i ++ {
port := fmt . Sprintf ( "%d" , 7788 + i )
port := fmt . Sprintf ( "%d" , 7788 + i )
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
_ , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
validatorsPriKeys [ i ] , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
}
}
m := mock_host . NewMockHost ( ctrl )
m := mock_host . NewMockHost ( ctrl )
@ -47,7 +51,7 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
m . EXPECT ( ) . GetSelfPeer ( ) . Return ( leader )
m . EXPECT ( ) . GetSelfPeer ( ) . Return ( leader )
m . EXPECT ( ) . SendMessageToGroups ( [ ] p2p . GroupID { p2p . GroupIDBeacon } , gomock . Any ( ) )
m . EXPECT ( ) . SendMessageToGroups ( [ ] p2p . GroupID { p2p . GroupIDBeacon } , gomock . Any ( ) )
consensusLeader := New ( m , "0" , validators , leader )
consensusLeader := New ( m , "0" , validators , leader , priKey )
consensusLeader . blockHash = blockHash
consensusLeader . blockHash = blockHash
consensusValidators := make ( [ ] * Consensus , 3 )
consensusValidators := make ( [ ] * Consensus , 3 )
@ -59,7 +63,7 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
}
}
hosts [ i ] = host
hosts [ i ] = host
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader )
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader , validatorsPriKeys [ i ] )
consensusValidators [ i ] . blockHash = blockHash
consensusValidators [ i ] . blockHash = blockHash
msg := consensusValidators [ i ] . constructPrepareMessage ( )
msg := consensusValidators [ i ] . constructPrepareMessage ( )
msgPayload , _ := proto . GetConsensusMessagePayload ( msg )
msgPayload , _ := proto . GetConsensusMessagePayload ( msg )
@ -76,15 +80,17 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
defer ctrl . Finish ( )
defer ctrl . Finish ( )
leader := p2p . Peer { IP : ip , Port : "7777" }
leader := p2p . Peer { IP : ip , Port : "7777" }
_ , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
var priKey * bls . SecretKey
priKey , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
validators := make ( [ ] p2p . Peer , 3 )
validators := make ( [ ] p2p . Peer , 3 )
hosts := make ( [ ] p2p . Host , 3 )
hosts := make ( [ ] p2p . Host , 3 )
validatorKeys := [ 3 ] * bls . SecretKey { }
for i := 0 ; i < 3 ; i ++ {
for i := 0 ; i < 3 ; i ++ {
port := fmt . Sprintf ( "%d" , 7788 + i )
port := fmt . Sprintf ( "%d" , 7788 + i )
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
_ , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
validatorKeys [ i ] , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
}
}
m := mock_host . NewMockHost ( ctrl )
m := mock_host . NewMockHost ( ctrl )
@ -92,7 +98,7 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
// Anything else will fail.
// Anything else will fail.
m . EXPECT ( ) . GetSelfPeer ( ) . Return ( leader )
m . EXPECT ( ) . GetSelfPeer ( ) . Return ( leader )
consensusLeader := New ( m , "0" , validators , leader )
consensusLeader := New ( m , "0" , validators , leader , priKey )
consensusLeader . blockHash = blockHash
consensusLeader . blockHash = blockHash
consensusValidators := make ( [ ] * Consensus , 3 )
consensusValidators := make ( [ ] * Consensus , 3 )
@ -104,7 +110,7 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
}
}
hosts [ i ] = host
hosts [ i ] = host
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader )
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader , validatorKeys [ i ] )
consensusValidators [ i ] . blockHash = blockHash
consensusValidators [ i ] . blockHash = blockHash
msgBytes := consensusValidators [ i ] . constructPrepareMessage ( )
msgBytes := consensusValidators [ i ] . constructPrepareMessage ( )
msgPayload , _ := proto . GetConsensusMessagePayload ( msgBytes )
msgPayload , _ := proto . GetConsensusMessagePayload ( msgBytes )
@ -130,15 +136,17 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
defer ctrl . Finish ( )
defer ctrl . Finish ( )
leader := p2p . Peer { IP : ip , Port : "8889" }
leader := p2p . Peer { IP : ip , Port : "8889" }
_ , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
var priKey * bls . SecretKey
priKey , leader . ConsensusPubKey = utils . GenKey ( leader . IP , leader . Port )
validators := make ( [ ] p2p . Peer , 3 )
validators := make ( [ ] p2p . Peer , 3 )
hosts := make ( [ ] p2p . Host , 3 )
hosts := make ( [ ] p2p . Host , 3 )
validatorKeys := [ 3 ] * bls . SecretKey { }
for i := 0 ; i < 3 ; i ++ {
for i := 0 ; i < 3 ; i ++ {
port := fmt . Sprintf ( "%d" , 8788 + i )
port := fmt . Sprintf ( "%d" , 8788 + i )
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
validators [ i ] = p2p . Peer { IP : ip , Port : port , ValidatorID : i + 1 }
_ , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
validatorKeys [ i ] , validators [ i ] . ConsensusPubKey = utils . GenKey ( validators [ i ] . IP , validators [ i ] . Port )
}
}
m := mock_host . NewMockHost ( ctrl )
m := mock_host . NewMockHost ( ctrl )
@ -156,7 +164,7 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
hosts [ i ] = host
hosts [ i ] = host
}
}
consensusLeader := New ( m , "0" , validators , leader )
consensusLeader := New ( m , "0" , validators , leader , priKey )
consensusLeader . state = PreparedDone
consensusLeader . state = PreparedDone
consensusLeader . blockHash = blockHash
consensusLeader . blockHash = blockHash
consensusLeader . OnConsensusDone = func ( newBlock * types . Block ) { }
consensusLeader . OnConsensusDone = func ( newBlock * types . Block ) { }
@ -174,7 +182,7 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
<- consensusLeader . ReadySignal
<- consensusLeader . ReadySignal
} ( )
} ( )
for i := 0 ; i < 3 ; i ++ {
for i := 0 ; i < 3 ; i ++ {
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader )
consensusValidators [ i ] = New ( hosts [ i ] , "0" , validators , leader , validatorKeys [ i ] )
consensusValidators [ i ] . blockHash = blockHash
consensusValidators [ i ] . blockHash = blockHash
payload := consensusValidators [ i ] . constructCommitMessage ( multiSigAndBitmap )
payload := consensusValidators [ i ] . constructCommitMessage ( multiSigAndBitmap )
msg , err := proto . GetConsensusMessagePayload ( payload )
msg , err := proto . GetConsensusMessagePayload ( payload )