The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/consensus/consensus_leader_msg.go

133 lines
4.1 KiB

package consensus
import (
"bytes"
6 years ago
protobuf "github.com/golang/protobuf/proto"
6 years ago
"github.com/harmony-one/bls/ffi/go/bls"
consensus_proto "github.com/harmony-one/harmony/api/consensus"
6 years ago
"github.com/harmony-one/harmony/api/proto"
6 years ago
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils"
)
// Constructs the announce message
func (consensus *Consensus) constructAnnounceMessage() []byte {
message := consensus_proto.Message{}
message.Type = consensus_proto.MessageType_ANNOUNCE
// 4 byte consensus id
message.ConsensusId = consensus.consensusID
// 32 byte block hash
message.BlockHash = consensus.blockHash[:]
// 4 byte sender id
message.SenderId = uint32(consensus.nodeID)
// n byte of block header
message.Payload = consensus.block
marshaledMessage, err := protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Announce message", "error", err)
}
// 64 byte of signature on previous data
signature := consensus.signMessage(marshaledMessage)
message.Signature = signature
marshaledMessage, err = protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Announce message", "error", err)
}
utils.GetLogInstance().Info("New Announce", "NodeID", consensus.nodeID)
return proto.ConstructConsensusMessage(marshaledMessage)
}
6 years ago
// Construct the prepared message, returning prepared message in bytes.
func (consensus *Consensus) constructPreparedMessage() ([]byte, *bls.Sign) {
message := consensus_proto.Message{}
6 years ago
message.Type = consensus_proto.MessageType_PREPARED
// 4 byte consensus id
message.ConsensusId = consensus.consensusID
// 32 byte block hash
message.BlockHash = consensus.blockHash[:]
// 4 byte sender id
message.SenderId = uint32(consensus.nodeID)
//// Payload
buffer := bytes.NewBuffer([]byte{})
6 years ago
// 48 bytes aggregated signature
aggSig := bls_cosi.AggregateSig(consensus.GetPrepareSigsArray())
buffer.Write(aggSig.Serialize())
6 years ago
// Bitmap
buffer.Write(consensus.prepareBitmap.Bitmap)
message.Payload = buffer.Bytes()
//// END Payload
6 years ago
// TODO: use custom serialization method rather than protobuf
marshaledMessage, err := protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Prepared message", "error", err)
}
6 years ago
// 48 byte of signature on previous data
signature := consensus.signMessage(marshaledMessage)
message.Signature = signature
marshaledMessage, err = protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Prepared message", "error", err)
}
utils.GetLogInstance().Info("New Prepared Message", "NodeID", consensus.nodeID, "bitmap", consensus.prepareBitmap)
6 years ago
return proto.ConstructConsensusMessage(marshaledMessage), aggSig
}
6 years ago
// Construct the committed message, returning committed message in bytes.
func (consensus *Consensus) constructCommittedMessage() ([]byte, *bls.Sign) {
message := consensus_proto.Message{}
6 years ago
message.Type = consensus_proto.MessageType_COMMITTED
// 4 byte consensus id
message.ConsensusId = consensus.consensusID
// 32 byte block hash
message.BlockHash = consensus.blockHash[:]
// 4 byte sender id
message.SenderId = uint32(consensus.nodeID)
//// Payload
buffer := bytes.NewBuffer([]byte{})
6 years ago
// 48 bytes aggregated signature
aggSig := bls_cosi.AggregateSig(consensus.GetCommitSigsArray())
buffer.Write(aggSig.Serialize())
6 years ago
// Bitmap
buffer.Write(consensus.commitBitmap.Bitmap)
message.Payload = buffer.Bytes()
//// END Payload
6 years ago
// TODO: use custom serialization method rather than protobuf
marshaledMessage, err := protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Committed message", "error", err)
}
6 years ago
// 48 byte of signature on previous data
signature := consensus.signMessage(marshaledMessage)
message.Signature = signature
marshaledMessage, err = protobuf.Marshal(&message)
if err != nil {
utils.GetLogInstance().Debug("Failed to marshal Committed message", "error", err)
}
utils.GetLogInstance().Info("New Prepared Message", "NodeID", consensus.nodeID, "bitmap", consensus.commitBitmap)
6 years ago
return proto.ConstructConsensusMessage(marshaledMessage), aggSig
}