@ -12,6 +12,7 @@ import (
consensus_proto "github.com/harmony-one/harmony/api/consensus"
"github.com/harmony-one/harmony/api/services/explorer"
"github.com/harmony-one/harmony/core/types"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/profiler"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
@ -94,7 +95,7 @@ func (consensus *Consensus) startConsensus(newBlock *types.Block) {
// Set state to AnnounceDone
consensus . state = AnnounceDone
// Leader sign the multi-sig itself
// Leader sign the block hash itself
( * consensus . prepareSigs ) [ consensus . nodeID ] = consensus . priKey . SignHash ( consensus . blockHash [ : ] )
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
@ -144,41 +145,46 @@ func (consensus *Consensus) processPrepareMessage(message consensus_proto.Messag
// proceed only when the message is not received before
_ , ok = ( * prepareSigs ) [ validatorID ]
shouldProcess := ! ok
if len ( ( * prepareSigs ) ) >= ( ( len ( consensus . PublicKeys ) * 2 ) / 3 + 1 ) {
shouldProcess = false
if ok {
utils . GetLogInstance ( ) . Debug ( "Already received prepare message from the validator" , "validatorID" , validatorID )
return
}
if shouldProcess {
var sign bls . Sign
err := sign . Deserialize ( prepareSig )
if err != nil {
utils . GetLogInstance ( ) . Error ( "Failed to deserialize bls signature" , "validatorID" , validatorID )
}
// TODO: check bls signature
( * prepareSigs ) [ validatorID ] = & sign
utils . GetLogInstance ( ) . Debug ( "Received new prepare signature" , "numReceivedSoFar" , len ( * prepareSigs ) , "validatorID" , validatorID , "PublicKeys" , len ( consensus . PublicKeys ) )
if len ( ( * prepareSigs ) ) >= ( ( len ( consensus . PublicKeys ) * 2 ) / 3 + 1 ) {
utils . GetLogInstance ( ) . Debug ( "Received additional new prepare message" , "validatorID" , validatorID )
return
}
// Set the bitmap indicate this validate signed.
prepareBitmap . SetKey ( value . PubKey , true )
// Check BLS signature for the multi-sig
var sign bls . Sign
err = sign . Deserialize ( prepareSig )
if err != nil {
utils . GetLogInstance ( ) . Error ( "Failed to deserialize bls signature" , "validatorID" , validatorID )
return
}
if ! shouldProcess {
utils . GetLogInstance ( ) . Debug ( "Received additional new commit message" , "validatorID" , validatorID )
if ! sign . VerifyHash ( value . PubKey , consensus . blockHash [ : ] ) {
utils . GetLogInstance ( ) . Error ( "Received invalid BLS signatur e", "validatorID" , validatorID )
return
}
( * prepareSigs ) [ validatorID ] = & sign
utils . GetLogInstance ( ) . Debug ( "Received new prepare signature" , "numReceivedSoFar" , len ( * prepareSigs ) , "validatorID" , validatorID , "PublicKeys" , len ( consensus . PublicKeys ) )
// Set the bitmap indicate this validate signed.
prepareBitmap . SetKey ( value . PubKey , true )
targetState := PreparedDone
if len ( ( * prepareSigs ) ) >= ( ( len ( consensus . PublicKeys ) * 2 ) / 3 + 1 ) && consensus . state < targetState {
utils . GetLogInstance ( ) . Debug ( "Enough commitments received with signatures" , "num" , len ( * prepareSigs ) , "state" , consensus . state )
utils . GetLogInstance ( ) . Debug ( "Enough prepare s received with signatures" , "num" , len ( * prepareSigs ) , "state" , consensus . state )
// Construct prepared message
msgToSend , aggSig := consensus . constructPreparedMessage ( )
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 [ : ] )
// Leader sign the multi-sig and bitmap
multiSigAndBitmap := append ( aggSig . Serialize ( ) , prepareBitmap . Bitmap ... )
( * consensus . commitSigs ) [ consensus . nodeID ] = consensus . priKey . SignHash ( multiSigAndBitmap )
// Broadcast prepared message
host . BroadcastMessageFromLeader ( consensus . host , consensus . GetValidatorPeers ( ) , msgToSend , consensus . OfflinePeers )
@ -195,14 +201,13 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
validatorID := message . SenderId
commitSig := message . Payload
shouldProcess := true
consensus . mutex . Lock ( )
defer consensus . mutex . Unlock ( )
// check consensus Id
if consensusID != consensus . consensusID {
shouldProcess = false
utils . GetLogInstance ( ) . Warn ( "Received Commit with wrong consensus Id" , "myConsensusId" , consensus . consensusID , "theirConsensusId" , consensusID , "consensus" , consensus )
return
}
if ! bytes . Equal ( blockHash , consensus . blockHash [ : ] ) {
@ -234,30 +239,34 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
// proceed only when the message is not received before
_ , ok = ( * commitSigs ) [ validatorID ]
shouldProcess = shouldProcess && ! ok
if ok {
utils . GetLogInstance ( ) . Debug ( "Already received commit message from the validator" , "validatorID" , validatorID )
return
}
if len ( ( * commitSigs ) ) >= ( ( len ( consensus . PublicKeys ) * 2 ) / 3 + 1 ) {
shouldProcess = false
utils . GetLogInstance ( ) . Debug ( "Received additional new commit message" , "validatorID" , strconv . Itoa ( int ( validatorID ) ) )
return
}
if shouldProcess {
var sign bls . Sign
err := sign . Deserialize ( commitSig )
if err != nil {
utils . GetLogInstance ( ) . Debug ( "Failed to deserialize bls signature" , "validatorID" , validatorID )
}
// TODO: check bls signature
( * commitSigs ) [ validatorID ] = & sign
utils . GetLogInstance ( ) . Debug ( "Received new commit message" , "numReceivedSoFar" , len ( * commitSigs ) , "validatorID" , strconv . Itoa ( int ( validatorID ) ) )
// Set the bitmap indicate this validate signed.
commitBitmap . SetKey ( value . PubKey , true )
var sign bls . Sign
err = sign . Deserialize ( commitSig )
if err != nil {
utils . GetLogInstance ( ) . Debug ( "Failed to deserialize bls signature" , "validatorID" , validatorID )
return
}
if ! shouldProcess {
utils . GetLogInstance ( ) . Debug ( "Received additional new commit message" , "validatorID" , strconv . Itoa ( int ( validatorID ) ) )
// Verify the signature on prepare multi-sig and bitmap is correct
aggSig := bls_cosi . AggregateSig ( consensus . GetPrepareSigsArray ( ) )
if ! sign . VerifyHash ( value . PubKey , append ( aggSig . Serialize ( ) , consensus . prepareBitmap . Bitmap ... ) ) {
utils . GetLogInstance ( ) . Error ( "Received invalid BLS signature" , "validatorID" , validatorID )
return
}
( * commitSigs ) [ validatorID ] = & sign
utils . GetLogInstance ( ) . Debug ( "Received new commit message" , "numReceivedSoFar" , len ( * commitSigs ) , "validatorID" , strconv . Itoa ( int ( validatorID ) ) )
// Set the bitmap indicate this validate signed.
commitBitmap . SetKey ( value . PubKey , true )
targetState := CommittedDone
if len ( * commitSigs ) >= ( ( len ( consensus . PublicKeys ) * 2 ) / 3 + 1 ) && consensus . state != targetState {
utils . GetLogInstance ( ) . Info ( "Enough commits received!" , "num" , len ( * commitSigs ) , "state" , consensus . state )