@ -57,7 +57,7 @@ func (consensus *Consensus) WaitForNewBlock(blockChannel chan *types.Block) {
// ProcessMessageLeader dispatches consensus message for the leader.
// ProcessMessageLeader dispatches consensus message for the leader.
func ( consensus * Consensus ) ProcessMessageLeader ( payload [ ] byte ) {
func ( consensus * Consensus ) ProcessMessageLeader ( payload [ ] byte ) {
message := consensus_proto . Message { }
message := consensus_proto . Message { }
err := message . XXX_ Unmarshal( payload )
err := protobuf . Unmarshal ( payload , & message )
if err != nil {
if err != nil {
utils . GetLogInstance ( ) . Error ( "Failed to unmarshal message payload." , "err" , err , "consensus" , consensus )
utils . GetLogInstance ( ) . Error ( "Failed to unmarshal message payload." , "err" , err , "consensus" , consensus )
@ -93,7 +93,10 @@ func (consensus *Consensus) startConsensus(newBlock *types.Block) {
// Set state to AnnounceDone
// Set state to AnnounceDone
consensus . state = AnnounceDone
consensus . state = AnnounceDone
// TODO: sign for leader itself
// Leader sign the multi-sig itself
( * consensus . prepareSigs ) [ consensus . nodeID ] = consensus . priKey . SignHash ( consensus . blockHash [ : ] )
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
}
}
@ -103,7 +106,6 @@ func (consensus *Consensus) processPrepareMessage(message consensus_proto.Messag
blockHash := message . BlockHash
blockHash := message . BlockHash
validatorID := message . SenderId
validatorID := message . SenderId
prepareSig := message . Payload
prepareSig := message . Payload
signature := message . Signature
// Verify signature
// Verify signature
v , ok := consensus . validators . Load ( validatorID )
v , ok := consensus . validators . Load ( validatorID )
@ -117,18 +119,12 @@ func (consensus *Consensus) processPrepareMessage(message consensus_proto.Messag
return
return
}
}
message . Signature = nil
// Verify message signature
messageBytes , err := protobuf . Marshal ( & message )
err := verifyMessageSig ( value . PubKey , message )
if err != nil {
if err != nil {
utils . GetLogInstance ( ) . Warn ( "Failed to marshal the prepare message" , "error" , err )
utils . GetLogInstance ( ) . Warn ( "Failed to verify the message signature" , "Error" , err , "validatorID" , validatorID )
return
}
}
_ = messageBytes
_ = signature
// TODO: verify message signature
//if schnorr.Verify(crypto.Ed25519Curve, value.PubKey, messageBytes, signature) != nil {
// consensus.Log.Warn("Received message with invalid signature", "validatorKey", consensus.leader.PubKey, "consensus", consensus)
// return
//}
// check consensus Id
// check consensus Id
consensus . mutex . Lock ( )
consensus . mutex . Lock ( )
@ -180,6 +176,10 @@ func (consensus *Consensus) processPrepareMessage(message consensus_proto.Messag
msgToSend , aggSig := consensus . constructPreparedMessage ( )
msgToSend , aggSig := consensus . constructPreparedMessage ( )
consensus . aggregatedPrepareSig = aggSig
consensus . aggregatedPrepareSig = aggSig
// Leader sign the multi-sig itself
// TODO: sign on the prepared multi-sig, rather than the block hash
( * consensus . commitSigs ) [ consensus . nodeID ] = consensus . priKey . SignHash ( consensus . blockHash [ : ] )
// Broadcast prepared message
// Broadcast prepared message
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
@ -194,7 +194,6 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
blockHash := message . BlockHash
blockHash := message . BlockHash
validatorID := message . SenderId
validatorID := message . SenderId
commitSig := message . Payload
commitSig := message . Payload
signature := message . Signature
shouldProcess := true
shouldProcess := true
consensus . mutex . Lock ( )
consensus . mutex . Lock ( )
@ -203,11 +202,11 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
// check consensus Id
// check consensus Id
if consensusID != consensus . consensusID {
if consensusID != consensus . consensusID {
shouldProcess = false
shouldProcess = false
utils . GetLogInstance ( ) . Warn ( "Received Response with wrong consensus Id" , "myConsensusId" , consensus . consensusID , "theirConsensusId" , consensusID , "consensus" , consensus )
utils . GetLogInstance ( ) . Warn ( "Received Commit with wrong consensus Id" , "myConsensusId" , consensus . consensusID , "theirConsensusId" , consensusID , "consensus" , consensus )
}
}
if ! bytes . Equal ( blockHash , consensus . blockHash [ : ] ) {
if ! bytes . Equal ( blockHash , consensus . blockHash [ : ] ) {
utils . GetLogInstance ( ) . Warn ( "Received Response with wrong blockHash" , "myConsensusId" , consensus . consensusID , "theirConsensusId" , consensusID , "consensus" , consensus )
utils . GetLogInstance ( ) . Warn ( "Received Commit with wrong blockHash" , "myConsensusId" , consensus . consensusID , "theirConsensusId" , consensusID , "consensus" , consensus )
return
return
}
}
@ -222,18 +221,13 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
utils . GetLogInstance ( ) . Warn ( "Invalid validator" , "validatorID" , validatorID , "consensus" , consensus )
utils . GetLogInstance ( ) . Warn ( "Invalid validator" , "validatorID" , validatorID , "consensus" , consensus )
return
return
}
}
message . Signature = nil
messageBytes , err := protobuf . Marshal ( & message )
// Verify message signature
err := verifyMessageSig ( value . PubKey , message )
if err != nil {
if err != nil {
utils . GetLogInstance ( ) . Warn ( "Failed to marshal the commit message" , "error" , err )
utils . GetLogInstance ( ) . Warn ( "Failed to verify the message signature" , "Error" , err , "validatorID" , validatorID )
return
}
}
_ = messageBytes
_ = signature
// TODO: verify message signature
//if schnorr.Verify(crypto.Ed25519Curve, value.PubKey, messageBytes, signature) != nil {
// consensus.Log.Warn("Received message with invalid signature", "validatorKey", consensus.leader.PubKey, "consensus", consensus)
// return
//}
commitSigs := consensus . commitSigs
commitSigs := consensus . commitSigs
commitBitmap := consensus . commitBitmap
commitBitmap := consensus . commitBitmap
@ -276,7 +270,7 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
var blockObj types . Block
var blockObj types . Block
err = rlp . DecodeBytes ( consensus . block , & blockObj )
err : = rlp . DecodeBytes ( consensus . block , & blockObj )
if err != nil {
if err != nil {
utils . GetLogInstance ( ) . Debug ( "failed to construct the new block after consensus" )
utils . GetLogInstance ( ) . Debug ( "failed to construct the new block after consensus" )
}
}