Make leader aggregate real commitments

pull/55/head
Rongjian Lan 6 years ago
parent b2ea1dacfa
commit 7e2634cdc0
  1. 17
      consensus/consensus_leader.go
  2. 20
      consensus/consensus_leader_test.go

@ -211,7 +211,11 @@ func (consensus *Consensus) constructChallengeMessage() []byte {
buffer.Write(twoBytes)
// 33 byte aggregated commit
buffer.Write(getAggregatedCommit(consensus.commitments))
commitments := make([]kyber.Point, 0)
for _, val := range consensus.commitments {
commitments = append(commitments, val)
}
buffer.Write(getAggregatedCommit(commitments, consensus.bitmap))
// 33 byte aggregated key
buffer.Write(getAggregatedKey(consensus.bitmap))
@ -226,10 +230,13 @@ func (consensus *Consensus) constructChallengeMessage() []byte {
return proto_consensus.ConstructConsensusMessage(proto_consensus.CHALLENGE, buffer.Bytes())
}
func getAggregatedCommit(commits map[uint16]kyber.Point) []byte {
// TODO: implement actual commit aggregation
commitment := sha256.Sum256([]byte("ABC"))
return append(commitment[:], byte(0))
func getAggregatedCommit(commitments []kyber.Point, bitmap *crypto.Mask) []byte {
aggCommitment := crypto.AggregateCommitmentsOnly(crypto.Curve, commitments)
bytes, err := aggCommitment.MarshalBinary()
if err != nil {
panic("Failed to deserialize the aggregated commitment")
}
return append(bytes[:], byte(0))
}
func getAggregatedKey(bitmap *crypto.Mask) []byte {

@ -1,6 +1,7 @@
package consensus
import (
"harmony-benchmark/crypto"
"harmony-benchmark/p2p"
"testing"
)
@ -19,10 +20,25 @@ func TestConstructAnnounceMessage(test *testing.T) {
}
func TestConstructChallengeMessage(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
leaderPriKey := crypto.Curve.Scalar()
priKeyInBytes := crypto.Hash("12")
leaderPriKey.UnmarshalBinary(priKeyInBytes[:]) // use ip:port as unique private key for now. TODO: use real private key
leaderPubKey := crypto.GetPublicKeyFromScalar(crypto.Curve, leaderPriKey)
leader := p2p.Peer{Ip: "1", Port: "2", PubKey: leaderPubKey}
validatorPriKey := crypto.Curve.Scalar()
priKeyInBytes = crypto.Hash("12")
validatorPriKey.UnmarshalBinary(priKeyInBytes[:]) // use ip:port as unique private key for now. TODO: use real private key
validatorPubKey := crypto.GetPublicKeyFromScalar(crypto.Curve, leaderPriKey)
validator := p2p.Peer{Ip: "3", Port: "5", PubKey: validatorPubKey}
consensus := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{}
consensus.commitments[0] = leaderPubKey
consensus.commitments[1] = validatorPubKey
consensus.bitmap.SetKey(leaderPubKey, true)
consensus.bitmap.SetKey(validatorPubKey, true)
msg := consensus.constructChallengeMessage()
if len(msg) != 1+1+1+4+32+2+33+33+32+64 {

Loading…
Cancel
Save