|
|
@ -1,6 +1,8 @@ |
|
|
|
package consensus |
|
|
|
package consensus |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock" |
|
|
|
"github.com/golang/mock/gomock" |
|
|
|
"github.com/harmony-one/harmony/crypto" |
|
|
|
"github.com/harmony-one/harmony/crypto" |
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
@ -15,19 +17,25 @@ import ( |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
ip = "127.0.0.1" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func TestProcessMessageLeaderCommit(test *testing.T) { |
|
|
|
func TestProcessMessageLeaderCommit(test *testing.T) { |
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
defer ctrl.Finish() |
|
|
|
defer ctrl.Finish() |
|
|
|
|
|
|
|
|
|
|
|
leader := p2p.Peer{IP: "127.0.0.1", Port: "7777"} |
|
|
|
leader := p2p.Peer{IP: ip, Port: "7777"} |
|
|
|
_, leader.PubKey = utils.GenKey(leader.IP, leader.Port) |
|
|
|
_, leader.PubKey = utils.GenKey(leader.IP, leader.Port) |
|
|
|
|
|
|
|
|
|
|
|
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7778", ValidatorID: 1} |
|
|
|
validators := make([]p2p.Peer, 3) |
|
|
|
_, validator1.PubKey = utils.GenKey(validator1.IP, validator1.Port) |
|
|
|
hosts := make([]p2p.Host, 3) |
|
|
|
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7776", ValidatorID: 2} |
|
|
|
|
|
|
|
_, validator2.PubKey = utils.GenKey(validator2.IP, validator2.Port) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7779", ValidatorID: 3} |
|
|
|
port := fmt.Sprintf("%d", 7788+i) |
|
|
|
_, validator3.PubKey = utils.GenKey(validator3.IP, validator3.Port) |
|
|
|
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1} |
|
|
|
|
|
|
|
_, validators[i].PubKey = utils.GenKey(validators[i].IP, validators[i].Port) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m := mock_host.NewMockHost(ctrl) |
|
|
|
m := mock_host.NewMockHost(ctrl) |
|
|
|
// Asserts that the first and only call to Bar() is passed 99.
|
|
|
|
// Asserts that the first and only call to Bar() is passed 99.
|
|
|
@ -35,26 +43,23 @@ func TestProcessMessageLeaderCommit(test *testing.T) { |
|
|
|
m.EXPECT().GetSelfPeer().Return(leader) |
|
|
|
m.EXPECT().GetSelfPeer().Return(leader) |
|
|
|
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(3) |
|
|
|
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(3) |
|
|
|
|
|
|
|
|
|
|
|
consensusLeader := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
consensusLeader := New(m, "0", validators, leader) |
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
|
|
|
|
|
|
|
|
host1, _ := p2pimpl.NewHost(&validator1) |
|
|
|
consensusValidators := make([]*Consensus, 3) |
|
|
|
consensusValidator1 := New(host1, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
consensusValidator1.blockHash = [32]byte{} |
|
|
|
priKey, _, _ := utils.GenKeyP2P(validators[i].IP, validators[i].Port) |
|
|
|
_, msg := consensusValidator1.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
host, err := p2pimpl.NewHost(&validators[i], priKey) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
test.Fatalf("newhost error: %v", err) |
|
|
|
host2, _ := p2pimpl.NewHost(&validator2) |
|
|
|
} |
|
|
|
consensusValidator2 := New(host2, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
hosts[i] = host |
|
|
|
consensusValidator2.blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg = consensusValidator2.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
consensusValidators[i] = New(hosts[i], "0", validators, leader) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
consensusValidators[i].blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg := consensusValidators[i].constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
host3, _ := p2pimpl.NewHost(&validator3) |
|
|
|
|
|
|
|
consensusValidator3 := New(host3, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
|
|
|
|
consensusValidator3.blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg = consensusValidator3.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(test, ChallengeDone, consensusLeader.state) |
|
|
|
assert.Equal(test, ChallengeDone, consensusLeader.state) |
|
|
|
|
|
|
|
|
|
|
@ -65,15 +70,17 @@ func TestProcessMessageLeaderResponse(test *testing.T) { |
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
defer ctrl.Finish() |
|
|
|
defer ctrl.Finish() |
|
|
|
|
|
|
|
|
|
|
|
leader := p2p.Peer{IP: "127.0.0.1", Port: "8889"} |
|
|
|
leader := p2p.Peer{IP: ip, Port: "8889"} |
|
|
|
_, leader.PubKey = utils.GenKey(leader.IP, leader.Port) |
|
|
|
_, leader.PubKey = utils.GenKey(leader.IP, leader.Port) |
|
|
|
|
|
|
|
|
|
|
|
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "8887", ValidatorID: 1} |
|
|
|
validators := make([]p2p.Peer, 3) |
|
|
|
_, validator1.PubKey = utils.GenKey(validator1.IP, validator1.Port) |
|
|
|
hosts := make([]p2p.Host, 3) |
|
|
|
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "8888", ValidatorID: 2} |
|
|
|
|
|
|
|
_, validator2.PubKey = utils.GenKey(validator2.IP, validator2.Port) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "8899", ValidatorID: 3} |
|
|
|
port := fmt.Sprintf("%d", 8788+i) |
|
|
|
_, validator3.PubKey = utils.GenKey(validator3.IP, validator3.Port) |
|
|
|
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1} |
|
|
|
|
|
|
|
_, validators[i].PubKey = utils.GenKey(validators[i].IP, validators[i].Port) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
m := mock_host.NewMockHost(ctrl) |
|
|
|
m := mock_host.NewMockHost(ctrl) |
|
|
|
// Asserts that the first and only call to Bar() is passed 99.
|
|
|
|
// Asserts that the first and only call to Bar() is passed 99.
|
|
|
@ -81,35 +88,30 @@ func TestProcessMessageLeaderResponse(test *testing.T) { |
|
|
|
m.EXPECT().GetSelfPeer().Return(leader) |
|
|
|
m.EXPECT().GetSelfPeer().Return(leader) |
|
|
|
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(6) |
|
|
|
m.EXPECT().SendMessage(gomock.Any(), gomock.Any()).Times(6) |
|
|
|
|
|
|
|
|
|
|
|
consensusLeader := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
priKey, _, _ := utils.GenKeyP2P(validators[i].IP, validators[i].Port) |
|
|
|
|
|
|
|
host, err := p2pimpl.NewHost(&validators[i], priKey) |
|
|
|
host1, _ := p2pimpl.NewHost(&validator1) |
|
|
|
if err != nil { |
|
|
|
consensusValidator1 := New(host1, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
test.Fatalf("newhost error: %v", err) |
|
|
|
consensusValidator1.blockHash = [32]byte{} |
|
|
|
} |
|
|
|
_, msg := consensusValidator1.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
hosts[i] = host |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
host2, _ := p2pimpl.NewHost(&validator2) |
|
|
|
|
|
|
|
consensusValidator2 := New(host2, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
|
|
|
|
consensusValidator2.blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg = consensusValidator2.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
host3, _ := p2pimpl.NewHost(&validator3) |
|
|
|
consensusLeader := New(m, "0", validators, leader) |
|
|
|
consensusValidator3 := New(host3, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
consensusValidator3.blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg = consensusValidator3.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = consensusValidator1.constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg = consensusValidator2.constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
consensusValidators := make([]*Consensus, 3) |
|
|
|
|
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
|
|
|
consensusValidators[i] = New(hosts[i], "0", validators, leader) |
|
|
|
|
|
|
|
consensusValidators[i].blockHash = [32]byte{} |
|
|
|
|
|
|
|
_, msg := consensusValidators[i].constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
msg = consensusValidator3.constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
|
|
|
msg := consensusValidators[i].constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(test, CollectiveSigDone, consensusLeader.state) |
|
|
|
assert.Equal(test, CollectiveSigDone, consensusLeader.state) |
|
|
|
|
|
|
|
|
|
|
|