|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package consensus |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock" |
|
|
|
|
"github.com/harmony-one/harmony/crypto" |
|
|
|
|
"github.com/harmony-one/harmony/internal/utils" |
|
|
|
@ -15,19 +17,25 @@ import ( |
|
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
ip = "127.0.0.1" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestProcessMessageLeaderCommit(test *testing.T) { |
|
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7778", ValidatorID: 1} |
|
|
|
|
_, validator1.PubKey = utils.GenKey(validator1.IP, validator1.Port) |
|
|
|
|
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7776", ValidatorID: 2} |
|
|
|
|
_, validator2.PubKey = utils.GenKey(validator2.IP, validator2.Port) |
|
|
|
|
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7779", ValidatorID: 3} |
|
|
|
|
_, validator3.PubKey = utils.GenKey(validator3.IP, validator3.Port) |
|
|
|
|
validators := make([]p2p.Peer, 3) |
|
|
|
|
hosts := make([]p2p.Host, 3) |
|
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
port := fmt.Sprintf("%d", 7788+i) |
|
|
|
|
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) |
|
|
|
|
// 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().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{} |
|
|
|
|
|
|
|
|
|
host1, _ := p2pimpl.NewHost(&validator1) |
|
|
|
|
consensusValidator1 := New(host1, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
|
consensusValidator1.blockHash = [32]byte{} |
|
|
|
|
_, msg := consensusValidator1.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
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) |
|
|
|
|
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:]) |
|
|
|
|
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) |
|
|
|
|
consensusValidators[i].blockHash = [32]byte{} |
|
|
|
|
_, msg := consensusValidators[i].constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert.Equal(test, ChallengeDone, consensusLeader.state) |
|
|
|
|
|
|
|
|
@ -65,15 +70,17 @@ func TestProcessMessageLeaderResponse(test *testing.T) { |
|
|
|
|
ctrl := gomock.NewController(test) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "8887", ValidatorID: 1} |
|
|
|
|
_, validator1.PubKey = utils.GenKey(validator1.IP, validator1.Port) |
|
|
|
|
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "8888", ValidatorID: 2} |
|
|
|
|
_, validator2.PubKey = utils.GenKey(validator2.IP, validator2.Port) |
|
|
|
|
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "8899", ValidatorID: 3} |
|
|
|
|
_, validator3.PubKey = utils.GenKey(validator3.IP, validator3.Port) |
|
|
|
|
validators := make([]p2p.Peer, 3) |
|
|
|
|
hosts := make([]p2p.Host, 3) |
|
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
port := fmt.Sprintf("%d", 8788+i) |
|
|
|
|
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) |
|
|
|
|
// 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().SendMessage(gomock.Any(), gomock.Any()).Times(6) |
|
|
|
|
|
|
|
|
|
consensusLeader := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
|
|
|
|
|
|
host1, _ := p2pimpl.NewHost(&validator1) |
|
|
|
|
consensusValidator1 := New(host1, "0", []p2p.Peer{validator1, validator2, validator3}, leader) |
|
|
|
|
consensusValidator1.blockHash = [32]byte{} |
|
|
|
|
_, msg := consensusValidator1.constructCommitMessage(consensus_proto.MessageType_COMMIT) |
|
|
|
|
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:]) |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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:]) |
|
|
|
|
|
|
|
|
|
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()) |
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
consensusLeader := New(m, "0", validators, leader) |
|
|
|
|
consensusLeader.blockHash = [32]byte{} |
|
|
|
|
|
|
|
|
|
msg = consensusValidator3.constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
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:]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
msg := consensusValidators[i].constructResponseMessage(consensus_proto.MessageType_RESPONSE, crypto.Ed25519Curve.Scalar().One()) |
|
|
|
|
consensusLeader.ProcessMessageLeader(msg[1:]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert.Equal(test, CollectiveSigDone, consensusLeader.state) |
|
|
|
|
|
|
|
|
|