From 2d8a0d8f2255e4a153e4e3210c3535eeebd71831 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 20 Aug 2018 11:17:56 -0700 Subject: [PATCH 1/2] Add constructCollectiveSigMessage() function --- consensus/consensus_leader_msg.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/consensus/consensus_leader_msg.go b/consensus/consensus_leader_msg.go index a03f5d85d..3ff9b3e4f 100644 --- a/consensus/consensus_leader_msg.go +++ b/consensus/consensus_leader_msg.go @@ -75,6 +75,36 @@ func (consensus *Consensus) constructChallengeMessage() []byte { return proto_consensus.ConstructConsensusMessage(proto_consensus.CHALLENGE, buffer.Bytes()) } +// Construct the collective signature message +func (consensus *Consensus) constructCollectiveSigMessage(collectiveSig [64]byte, bitmap []byte) []byte { + buffer := bytes.NewBuffer([]byte{}) + + // 4 byte consensus id + fourBytes := make([]byte, 4) + binary.BigEndian.PutUint32(fourBytes, consensus.consensusId) + buffer.Write(fourBytes) + + // 32 byte block hash + buffer.Write(consensus.blockHash[:]) + + // 2 byte leader id + twoBytes := make([]byte, 2) + binary.BigEndian.PutUint16(twoBytes, consensus.nodeId) + buffer.Write(twoBytes) + + // 64 byte collective signature + buffer.Write(collectiveSig[:]) + + // N byte bitmap + buffer.Write(bitmap) + + // 64 byte of signature on previous data + signature := consensus.signMessage(buffer.Bytes()) + buffer.Write(signature) + + return proto_consensus.ConstructConsensusMessage(proto_consensus.COLLECTIVE_SIG, buffer.Bytes()) +} + func getAggregatedCommit(commitments []kyber.Point) (commitment kyber.Point, bytes []byte) { aggCommitment := crypto.AggregateCommitmentsOnly(crypto.Ed25519Curve, commitments) bytes, err := aggCommitment.MarshalBinary() From 25c99ea3a448b1f46c194c7ab45056f73bebde9e Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 20 Aug 2018 15:25:31 -0700 Subject: [PATCH 2/2] Refactor constructCommitMessage func so it can be used for final commit too --- consensus/consensus_validator.go | 2 +- consensus/consensus_validator_msg.go | 4 ++-- consensus/consensus_validator_msg_test.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index 3db72733d..61bd25bbf 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -120,7 +120,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) { return } - secret, msgToSend := consensus.constructCommitMessage() + secret, msgToSend := consensus.constructCommitMessage(proto_consensus.COMMIT) // Store the commitment secret consensus.secret = secret diff --git a/consensus/consensus_validator_msg.go b/consensus/consensus_validator_msg.go index fc0b1fa65..c999adcc4 100644 --- a/consensus/consensus_validator_msg.go +++ b/consensus/consensus_validator_msg.go @@ -9,7 +9,7 @@ import ( ) // Construct the commit message to send to leader (assumption the consensus data is already verified) -func (consensus *Consensus) constructCommitMessage() (secret kyber.Scalar, commitMsg []byte) { +func (consensus *Consensus) constructCommitMessage(msgType proto_consensus.MessageType) (secret kyber.Scalar, commitMsg []byte) { buffer := bytes.NewBuffer([]byte{}) // 4 byte consensus id @@ -33,7 +33,7 @@ func (consensus *Consensus) constructCommitMessage() (secret kyber.Scalar, commi signature := consensus.signMessage(buffer.Bytes()) buffer.Write(signature) - return secret, proto_consensus.ConstructConsensusMessage(proto_consensus.COMMIT, buffer.Bytes()) + return secret, proto_consensus.ConstructConsensusMessage(msgType, buffer.Bytes()) } // Construct the response message to send to leader (assumption the consensus data is already verified) diff --git a/consensus/consensus_validator_msg_test.go b/consensus/consensus_validator_msg_test.go index de4124ab9..6facaaccc 100644 --- a/consensus/consensus_validator_msg_test.go +++ b/consensus/consensus_validator_msg_test.go @@ -5,6 +5,7 @@ import ( "github.com/simple-rules/harmony-benchmark/crypto" "github.com/simple-rules/harmony-benchmark/p2p" + consensus_proto "github.com/simple-rules/harmony-benchmark/proto/consensus" ) func TestConstructCommitMessage(test *testing.T) { @@ -12,7 +13,7 @@ func TestConstructCommitMessage(test *testing.T) { validator := p2p.Peer{Ip: "3", Port: "5"} consensus := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus.blockHash = [32]byte{} - _, msg := consensus.constructCommitMessage() + _, msg := consensus.constructCommitMessage(consensus_proto.COMMIT) if len(msg) != 1+1+1+4+32+2+32+64 { test.Errorf("Commit message is not constructed in the correct size: %d", len(msg))