@ -60,11 +60,11 @@ func (consensus *Consensus) handleMessageUpdate(payload []byte) {
func ( consensus * Consensus ) tryAnnounce ( block * types . Block ) {
func ( consensus * Consensus ) tryAnnounce ( block * types . Block ) {
// here we assume the leader should always be update to date
// here we assume the leader should always be update to date
if block . NumberU64 ( ) != consensus . blockNum {
if block . NumberU64 ( ) != consensus . blockNum {
utils . G etLogger( ) . Debug ( "tryAnnounce blockNum not match" , "blockNum" , block . NumberU64 ( ) , "myBlockNum" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "tryAnnounce blockNum not match" , "blockNum" , block . NumberU64 ( ) )
return
return
}
}
if ! consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) {
if ! consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) {
utils . G etLogger( ) . Debug ( "tryAnnounce key not match" , "myKey" , consensus . PubKey , "leaderKey" , consensus . LeaderPubKey )
consensus . g etLogger( ) . Debug ( "tryAnnounce key not match" , "myKey" , consensus . PubKey , "leaderKey" , consensus . LeaderPubKey )
return
return
}
}
blockHash := block . Hash ( )
blockHash := block . Hash ( )
@ -73,7 +73,7 @@ func (consensus *Consensus) tryAnnounce(block *types.Block) {
// prepare message and broadcast to validators
// prepare message and broadcast to validators
encodedBlock , err := rlp . EncodeToBytes ( block )
encodedBlock , err := rlp . EncodeToBytes ( block )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "tryAnnounce Failed encoding block" )
consensus . g etLogger( ) . Debug ( "tryAnnounce Failed encoding block" )
return
return
}
}
consensus . block = encodedBlock
consensus . block = encodedBlock
@ -86,7 +86,7 @@ func (consensus *Consensus) tryAnnounce(block *types.Block) {
_ = protobuf . Unmarshal ( msgPayload , msg )
_ = protobuf . Unmarshal ( msgPayload , msg )
pbftMsg , err := ParsePbftMessage ( msg )
pbftMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Warn ( "tryAnnounce unable to parse pbft message" , "error" , err )
consensus . g etLogger( ) . Warn ( "tryAnnounce unable to parse pbft message" , "error" , err )
return
return
}
}
@ -97,37 +97,36 @@ func (consensus *Consensus) tryAnnounce(block *types.Block) {
consensus . prepareSigs [ consensus . SelfAddress ] = consensus . priKey . SignHash ( consensus . blockHash [ : ] )
consensus . prepareSigs [ consensus . SelfAddress ] = consensus . priKey . SignHash ( consensus . blockHash [ : ] )
// Construct broadcast p2p message
// Construct broadcast p2p message
logger := utils . GetLogger ( ) . New ( "viewID" , consensus . viewID , "block" , consensus . blockNum , "groupID" , p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) )
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
ctxerror . Warn ( logger , err , "cannot send announce message" )
consensus . getLogger ( ) . Warn ( "cannot send announce message" , "groupID" , p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) )
} else {
} else {
l ogger. Debug ( "sent announce message" )
consensus . getL ogger( ) . Debug ( "sent announce message" )
}
}
}
}
func ( consensus * Consensus ) onAnnounce ( msg * msg_pb . Message ) {
func ( consensus * Consensus ) onAnnounce ( msg * msg_pb . Message ) {
utils . G etLogger( ) . Debug ( "receive announce message" , "phase" , consensus . phase , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "receive announce message" )
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
return
return
}
}
senderKey , err := consensus . verifySenderKey ( msg )
senderKey , err := consensus . verifySenderKey ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onAnnounce verifySenderKey failed" , "error" , err )
consensus . g etLogger( ) . Debug ( "onAnnounce verifySenderKey failed" , "error" , err )
return
return
}
}
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
utils . G etLogger( ) . Warn ( "onAnnounce senderKey not match leader PubKey" , "senderKey" , senderKey . SerializeToHexStr ( ) [ : 10 ] , "leaderKey" , consensus . LeaderPubKey . SerializeToHexStr ( ) [ : 10 ] )
consensus . g etLogger( ) . Warn ( "onAnnounce senderKey not match leader PubKey" , "senderKey" , senderKey . SerializeToHexStr ( ) , "leaderKey" , consensus . LeaderPubKey . SerializeToHexStr ( ) )
return
return
}
}
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
utils . G etLogger( ) . Debug ( "onAnnounce Failed to verify leader signature" , "error" , err )
consensus . g etLogger( ) . Debug ( "onAnnounce Failed to verify leader signature" , "error" , err )
return
return
}
}
recvMsg , err := ParsePbftMessage ( msg )
recvMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onAnnounce Unparseable leader message" , "error" , err )
consensus . g etLogger( ) . Debug ( "onAnnounce Unparseable leader message" , "error" , err )
return
return
}
}
block := recvMsg . Payload
block := recvMsg . Payload
@ -136,19 +135,19 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
var blockObj types . Block
var blockObj types . Block
err = rlp . DecodeBytes ( block , & blockObj )
err = rlp . DecodeBytes ( block , & blockObj )
if err != nil {
if err != nil {
utils . G etLogger( ) . Warn ( "onAnnounce Unparseable block header data" , "error" , err )
consensus . g etLogger( ) . Warn ( "onAnnounce Unparseable block header data" , "error" , err )
return
return
}
}
if blockObj . NumberU64 ( ) != recvMsg . BlockNum || recvMsg . BlockNum < consensus . blockNum {
if blockObj . NumberU64 ( ) != recvMsg . BlockNum || recvMsg . BlockNum < consensus . blockNum {
utils . G etLogger( ) . Warn ( "blockNum not match" , "recvBlockNum " , recvMsg . BlockNum , "blockObj Num" , blockObj . NumberU64 ( ) , "myBlockNum" , consensus . blockNum )
consensus . g etLogger( ) . Warn ( "blockNum not match" , "msgBlock " , recvMsg . BlockNum , "blockNum" , blockObj . NumberU64 ( ) )
return
return
}
}
if consensus . mode . Mode ( ) == Normal {
if consensus . mode . Mode ( ) == Normal {
// skip verify header when node is in Syncing mode
// skip verify header when node is in Syncing mode
if err := consensus . VerifyHeader ( consensus . ChainReader , blockObj . Header ( ) , false ) ; err != nil {
if err := consensus . VerifyHeader ( consensus . ChainReader , blockObj . Header ( ) , false ) ; err != nil {
utils . G etLogger( ) . Warn ( "onAnnounce block content is not verified successfully" , "error" , err , "inChain" , consensus . ChainReader . CurrentHeader ( ) . Number , "got" , blockObj . Header ( ) . Number )
consensus . g etLogger( ) . Warn ( "onAnnounce block content is not verified successfully" , "error" , err , "inChain" , consensus . ChainReader . CurrentHeader ( ) . Number , "got" , blockObj . Header ( ) . Number )
return
return
}
}
}
}
@ -164,11 +163,11 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
ctxerror . Log15 ( utils . GetLogger ( ) . Warn , err )
ctxerror . Log15 ( utils . GetLogger ( ) . Warn , err )
return
return
}
}
//blockObj.Logger(utils.G etLogger()).Debug("received announce", "viewID", recvMsg.ViewID, "msgBlockNum", recvMsg.BlockNum)
//blockObj.Logger(consensus.g etLogger()).Debug("received announce", "viewID", recvMsg.ViewID, "msgBlockNum", recvMsg.BlockNum)
logMsgs := consensus . pbftLog . GetMessagesByTypeSeqView ( msg_pb . MessageType_ANNOUNCE , recvMsg . BlockNum , recvMsg . ViewID )
logMsgs := consensus . pbftLog . GetMessagesByTypeSeqView ( msg_pb . MessageType_ANNOUNCE , recvMsg . BlockNum , recvMsg . ViewID )
if len ( logMsgs ) > 0 {
if len ( logMsgs ) > 0 {
if logMsgs [ 0 ] . BlockHash != blockObj . Header ( ) . Hash ( ) {
if logMsgs [ 0 ] . BlockHash != blockObj . Header ( ) . Hash ( ) {
utils . G etLogger( ) . Debug ( "onAnnounce leader is malicious" , "leaderKey" , utils . GetBlsAddress ( consensus . LeaderPubKey ) )
consensus . g etLogger( ) . Debug ( "onAnnounce leader is malicious" , "leaderKey" , consensus . LeaderPubKey )
consensus . startViewChange ( consensus . viewID + 1 )
consensus . startViewChange ( consensus . viewID + 1 )
}
}
return
return
@ -177,7 +176,7 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
copy ( blockPayload [ : ] , block [ : ] )
copy ( blockPayload [ : ] , block [ : ] )
consensus . block = blockPayload
consensus . block = blockPayload
consensus . blockHash = recvMsg . BlockHash
consensus . blockHash = recvMsg . BlockHash
utils . G etLogger( ) . Debug ( "announce block added" , "phase" , consensus . phase , "myViewID" , consensus . viewID , "myBlock" , consensus . blockNum , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . g etLogger( ) . Debug ( "announce block added" , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . pbftLog . AddMessage ( recvMsg )
consensus . pbftLog . AddMessage ( recvMsg )
consensus . pbftLog . AddBlock ( & blockObj )
consensus . pbftLog . AddBlock ( & blockObj )
@ -192,7 +191,7 @@ func (consensus *Consensus) onAnnounce(msg *msg_pb.Message) {
defer consensus . mutex . Unlock ( )
defer consensus . mutex . Unlock ( )
if consensus . checkViewID ( recvMsg ) != nil {
if consensus . checkViewID ( recvMsg ) != nil {
utils . G etLogger( ) . Debug ( "viewID check failed" , "v iewID" , recvMsg . ViewID , "myViewID" , consensus . viewID )
consensus . g etLogger( ) . Debug ( "viewID check failed" , "msgV iewID" , recvMsg . ViewID , "msgBlockNum" , recvMsg . BlockNum )
return
return
}
}
consensus . tryPrepare ( blockObj . Header ( ) . Hash ( ) )
consensus . tryPrepare ( blockObj . Header ( ) . Hash ( ) )
@ -210,7 +209,7 @@ func (consensus *Consensus) tryPrepare(blockHash common.Hash) {
}
}
if consensus . blockNum != block . NumberU64 ( ) || ! consensus . pbftLog . HasMatchingViewAnnounce ( consensus . blockNum , consensus . viewID , hash ) {
if consensus . blockNum != block . NumberU64 ( ) || ! consensus . pbftLog . HasMatchingViewAnnounce ( consensus . blockNum , consensus . viewID , hash ) {
utils . G etLogger( ) . Debug ( "not match" , "myPhase" , consensus . phase , "myBlock" , consensus . blockNum , "viewID" , consensus . viewID )
consensus . g etLogger( ) . Debug ( "blockNum or announce message not match" )
return
return
}
}
@ -218,12 +217,11 @@ func (consensus *Consensus) tryPrepare(blockHash common.Hash) {
// Construct and send prepare message
// Construct and send prepare message
msgToSend := consensus . constructPrepareMessage ( )
msgToSend := consensus . constructPrepareMessage ( )
logger := utils . GetLogger ( ) . New ( "viewID" , consensus . viewID , "block" , consensus . blockNum )
// TODO: this will not return immediatey, may block
// TODO: this will not return immediatey, may block
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
ctxerror . Warn ( logger , err , "cannot send prepare message" )
consensus . getLogger ( ) . Warn ( "cannot send prepare message" )
} else {
} else {
l ogger. Info ( "sent prepare message" )
consensus . getL ogger( ) . Info ( "sent prepare message" )
}
}
}
}
@ -235,28 +233,28 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
senderKey , err := consensus . verifySenderKey ( msg )
senderKey , err := consensus . verifySenderKey ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onPrepare verifySenderKey failed" , "error" , err )
consensus . g etLogger( ) . Debug ( "onPrepare verifySenderKey failed" , "error" , err )
return
return
}
}
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
utils . G etLogger( ) . Debug ( "onPrepare Failed to verify sender's signature" , "error" , err )
consensus . g etLogger( ) . Debug ( "onPrepare Failed to verify sender's signature" , "error" , err )
return
return
}
}
recvMsg , err := ParsePbftMessage ( msg )
recvMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "[Consensus] onPrepare Unparseable validator message" , "error" , err )
consensus . g etLogger( ) . Debug ( "[Consensus] onPrepare Unparseable validator message" , "error" , err )
return
return
}
}
if recvMsg . ViewID != consensus . viewID || recvMsg . BlockNum != consensus . blockNum {
if recvMsg . ViewID != consensus . viewID || recvMsg . BlockNum != consensus . blockNum {
utils . G etLogger( ) . Debug ( "onPrepare message not match" , "myPhase" , consensus . phase , "myViewID" , consensus . viewID ,
consensus . g etLogger( ) . Debug ( "onPrepare message not match" ,
"msgViewID" , recvMsg . ViewID , "myBlockNum" , consensus . blockNum , "m sgBlockNum " , recvMsg . BlockNum )
"msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
return
return
}
}
if ! consensus . pbftLog . HasMatchingViewAnnounce ( consensus . blockNum , consensus . viewID , recvMsg . BlockHash ) {
if ! consensus . pbftLog . HasMatchingViewAnnounce ( consensus . blockNum , consensus . viewID , recvMsg . BlockHash ) {
utils . G etLogger( ) . Debug ( "onPrepare no matching announce message" , "blockNum" , consensus . blockNum , "viewID" , consensus . viewID , "blockHash" , recvMsg . BlockHash )
consensus . g etLogger( ) . Debug ( "onPrepare no matching announce message" , "blockHash" , recvMsg . BlockHash )
return
return
}
}
@ -278,7 +276,7 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
// proceed only when the message is not received before
// proceed only when the message is not received before
_ , ok := prepareSigs [ validatorAddress ]
_ , ok := prepareSigs [ validatorAddress ]
if ok {
if ok {
utils . G etLogger( ) . Debug ( "Already received prepare message from the validator" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Debug ( "Already received prepare message from the validator" , "validatorAddress" , validatorAddress )
return
return
}
}
@ -286,19 +284,19 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
var sign bls . Sign
var sign bls . Sign
err = sign . Deserialize ( prepareSig )
err = sign . Deserialize ( prepareSig )
if err != nil {
if err != nil {
utils . G etLogger( ) . Error ( "Failed to deserialize bls signature" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Error ( "Failed to deserialize bls signature" , "validatorAddress" , validatorAddress )
return
return
}
}
if ! sign . VerifyHash ( validatorPubKey , consensus . blockHash [ : ] ) {
if ! sign . VerifyHash ( validatorPubKey , consensus . blockHash [ : ] ) {
utils . G etLogger( ) . Error ( "Received invalid BLS signature" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Error ( "Received invalid BLS signature" , "validatorAddress" , validatorAddress )
return
return
}
}
utils . G etLogger( ) . Debug ( "Received new prepare signature" , "numReceivedSoFar" , len ( prepareSigs ) , "validatorAddress" , validatorAddress , "PublicKeys" , len ( consensus . PublicKeys ) )
consensus . g etLogger( ) . Debug ( "Received new prepare signature" , "numReceivedSoFar" , len ( prepareSigs ) , "validatorAddress" , validatorAddress , "PublicKeys" , len ( consensus . PublicKeys ) )
prepareSigs [ validatorAddress ] = & sign
prepareSigs [ validatorAddress ] = & sign
// Set the bitmap indicating that this validator signed.
// Set the bitmap indicating that this validator signed.
if err := prepareBitmap . SetKey ( validatorPubKey , true ) ; err != nil {
if err := prepareBitmap . SetKey ( validatorPubKey , true ) ; err != nil {
ctxerror . Warn ( utils . G etLogger( ) , err , "prepareBitmap.SetKey failed" )
ctxerror . Warn ( consensus . g etLogger( ) , err , "prepareBitmap.SetKey failed" )
}
}
if len ( prepareSigs ) >= consensus . Quorum ( ) {
if len ( prepareSigs ) >= consensus . Quorum ( ) {
@ -313,16 +311,15 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
_ = protobuf . Unmarshal ( msgPayload , msg )
_ = protobuf . Unmarshal ( msgPayload , msg )
pbftMsg , err := ParsePbftMessage ( msg )
pbftMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Warn ( "onPrepare unable to parse pbft message" , "error" , err )
consensus . g etLogger( ) . Warn ( "onPrepare unable to parse pbft message" , "error" , err )
return
return
}
}
consensus . pbftLog . AddMessage ( pbftMsg )
consensus . pbftLog . AddMessage ( pbftMsg )
logger := utils . GetLogger ( ) . New ( "viewID" , consensus . viewID , "block" , consensus . blockNum )
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
ctxerror . Warn ( logger , err , "cannot send prepared message" )
consensus . getLogger ( ) . Warn ( "cannot send prepared message" )
} else {
} else {
l ogger. Debug ( "sent prepared message" )
consensus . getL ogger( ) . Debug ( "sent prepared message" )
}
}
// Leader add commit phase signature
// Leader add commit phase signature
@ -335,43 +332,42 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) {
}
}
func ( consensus * Consensus ) onPrepared ( msg * msg_pb . Message ) {
func ( consensus * Consensus ) onPrepared ( msg * msg_pb . Message ) {
utils . G etLogger( ) . Debug ( "receive prepared message" , "phase" , consensus . phase , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "receive prepared message" )
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
return
return
}
}
senderKey , err := consensus . verifySenderKey ( msg )
senderKey , err := consensus . verifySenderKey ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onPrepared verifySenderKey failed" , "error" , err )
consensus . g etLogger( ) . Debug ( "onPrepared verifySenderKey failed" , "error" , err )
return
return
}
}
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
utils . G etLogger( ) . Warn ( "onPrepared senderKey not match leader PubKey" )
consensus . g etLogger( ) . Warn ( "onPrepared senderKey not match leader PubKey" )
return
return
}
}
if err := verifyMessageSig ( senderKey , msg ) ; err != nil {
if err := verifyMessageSig ( senderKey , msg ) ; err != nil {
utils . G etLogger( ) . Debug ( "onPrepared Failed to verify sender's signature" , "error" , err )
consensus . g etLogger( ) . Debug ( "onPrepared Failed to verify sender's signature" , "error" , err )
return
return
}
}
recvMsg , err := ParsePbftMessage ( msg )
recvMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onPrepared U nparseable validator message" , "error" , err )
consensus . g etLogger( ) . Debug ( "onPrepared u nparseable validator message" , "error" , err )
return
return
}
}
utils . G etLogger( ) . Info ( "onPrepared received prepared message" , "myBlock" , consensus . blockNum , "myViewID" , consensus . viewID , "recv Block" , recvMsg . BlockNum , "msgViewID" , recvMsg . ViewID )
consensus . g etLogger( ) . Info ( "onPrepared received prepared message" , "msg Block" , recvMsg . BlockNum , "msgViewID" , recvMsg . ViewID )
if recvMsg . BlockNum < consensus . blockNum {
if recvMsg . BlockNum < consensus . blockNum {
utils . GetLogger ( ) . Debug ( "old block received, ignoring" ,
consensus . getLogger ( ) . Debug ( "old block received, ignoring" ,
"receivedNumber" , recvMsg . BlockNum ,
"msgBlock" , recvMsg . BlockNum )
"expectedNumber" , consensus . blockNum )
return
return
}
}
blockHash := recvMsg . BlockHash
blockHash := recvMsg . BlockHash
aggSig , mask , err := consensus . readSignatureBitmapPayload ( recvMsg . Payload , 0 )
aggSig , mask , err := consensus . readSignatureBitmapPayload ( recvMsg . Payload , 0 )
if err != nil {
if err != nil {
utils . G etLogger( ) . Error ( "readSignatureBitmapPayload failed" , "error" , err )
consensus . g etLogger( ) . Error ( "readSignatureBitmapPayload failed" , "error" , err )
return
return
}
}
@ -380,35 +376,34 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) {
// check has 2f+1 signatures
// check has 2f+1 signatures
if count := utils . CountOneBits ( mask . Bitmap ) ; count < consensus . Quorum ( ) {
if count := utils . CountOneBits ( mask . Bitmap ) ; count < consensus . Quorum ( ) {
utils . G etLogger( ) . Debug ( "not have enough signature" , "need" , consensus . Quorum ( ) , "have" , count )
consensus . g etLogger( ) . Debug ( "not have enough signature" , "need" , consensus . Quorum ( ) , "have" , count )
return
return
}
}
if ! aggSig . VerifyHash ( mask . AggregatePublic , blockHash [ : ] ) {
if ! aggSig . VerifyHash ( mask . AggregatePublic , blockHash [ : ] ) {
myBlockHash := common . Hash { }
myBlockHash := common . Hash { }
myBlockHash . SetBytes ( consensus . blockHash [ : ] )
myBlockHash . SetBytes ( consensus . blockHash [ : ] )
utils . G etLogger( ) . Warn ( "onPrepared failed to verify multi signature for prepare phase" , "blockHash" , blockHash , "myBlockHash" , myBlockHash )
consensus . g etLogger( ) . Warn ( "onPrepared failed to verify multi signature for prepare phase" , "blockHash" , blockHash , "myBlockHash" , myBlockHash )
return
return
}
}
utils . G etLogger( ) . Debug ( "prepared message added" , "phase" , consensus . phase , "myViewID" , consensus . viewID , "myBlock" , consensus . blockNum , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . g etLogger( ) . Debug ( "prepared message added" , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . pbftLog . AddMessage ( recvMsg )
consensus . pbftLog . AddMessage ( recvMsg )
if consensus . mode . Mode ( ) == ViewChanging {
if consensus . mode . Mode ( ) == ViewChanging {
utils . G etLogger( ) . Debug ( "viewchanging mode just exist after viewchanging" )
consensus . g etLogger( ) . Debug ( "viewchanging mode just exist after viewchanging" )
return
return
}
}
consensus . tryCatchup ( )
consensus . tryCatchup ( )
if consensus . checkViewID ( recvMsg ) != nil {
if consensus . checkViewID ( recvMsg ) != nil {
utils . G etLogger( ) . Debug ( "viewID check failed" , "v iewID" , recvMsg . ViewID , "myViewID" , consensus . viewID )
consensus . g etLogger( ) . Debug ( "viewID check failed" , "msgV iewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
return
return
}
}
if recvMsg . BlockNum > consensus . blockNum {
if recvMsg . BlockNum > consensus . blockNum {
utils . GetLogger ( ) . Debug ( "future block received, ignoring" ,
consensus . getLogger ( ) . Debug ( "future block received, ignoring" ,
"receivedNumber" , recvMsg . BlockNum ,
"msgBlock" , recvMsg . BlockNum )
"expectedNumber" , consensus . blockNum )
return
return
}
}
@ -420,11 +415,11 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) {
binary . LittleEndian . PutUint64 ( blockNumHash , consensus . blockNum )
binary . LittleEndian . PutUint64 ( blockNumHash , consensus . blockNum )
commitPayload := append ( blockNumHash , consensus . blockHash [ : ] ... )
commitPayload := append ( blockNumHash , consensus . blockHash [ : ] ... )
msgToSend := consensus . constructCommitMessage ( commitPayload )
msgToSend := consensus . constructCommitMessage ( commitPayload )
logger := utils . GetLogger ( ) . New ( "viewID" , consensus . viewID , "block" , consensus . blockNum )
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
ctxerror . Warn ( logger , err , "cannot send commit message" )
consensus . getLogger ( ) . Warn ( "cannot send commit message" )
} else {
} else {
l ogger. Debug ( "sent commit message" )
consensus . getL ogger( ) . Debug ( "sent commit message" )
}
}
consensus . switchPhase ( Commit , true )
consensus . switchPhase ( Commit , true )
@ -440,32 +435,32 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
senderKey , err := consensus . verifySenderKey ( msg )
senderKey , err := consensus . verifySenderKey ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onCommit verifySenderKey failed" , "error" , err )
consensus . g etLogger( ) . Debug ( "onCommit verifySenderKey failed" , "error" , err )
return
return
}
}
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
utils . G etLogger( ) . Debug ( "onCommit Failed to verify sender's signature" , "error" , err )
consensus . g etLogger( ) . Debug ( "onCommit Failed to verify sender's signature" , "error" , err )
return
return
}
}
recvMsg , err := ParsePbftMessage ( msg )
recvMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "onCommit parse pbft message failed" , "error" , err )
consensus . g etLogger( ) . Debug ( "onCommit parse pbft message failed" , "error" , err )
return
return
}
}
if recvMsg . ViewID != consensus . viewID || recvMsg . BlockNum != consensus . blockNum {
if recvMsg . ViewID != consensus . viewID || recvMsg . BlockNum != consensus . blockNum {
utils . G etLogger( ) . Debug ( "not match" , "my ViewID" , consensus . viewID , "viewID" , recvMsg . ViewID , "my Block" , consensus . blockNum , "block" , recvMsg . BlockNum , "myPhase" , consensus . phase , "phase" , Commit )
consensus . g etLogger( ) . Debug ( "blockNum/viewID not match" , "msg ViewID" , recvMsg . ViewID , "msg Block" , recvMsg . BlockNum )
return
return
}
}
if ! consensus . pbftLog . HasMatchingAnnounce ( consensus . blockNum , recvMsg . BlockHash ) {
if ! consensus . pbftLog . HasMatchingAnnounce ( consensus . blockNum , recvMsg . BlockHash ) {
utils . G etLogger( ) . Debug ( "cannot find matching blockhash" )
consensus . g etLogger( ) . Debug ( "cannot find matching blockhash" )
return
return
}
}
if ! consensus . pbftLog . HasMatchingViewPrepared ( consensus . blockNum , consensus . viewID , recvMsg . BlockHash ) {
if ! consensus . pbftLog . HasMatchingViewPrepared ( consensus . blockNum , consensus . viewID , recvMsg . BlockHash ) {
utils . G etLogger( ) . Debug ( "cannot find matching prepared message" )
consensus . g etLogger( ) . Debug ( "cannot find matching prepared message" )
return
return
}
}
@ -479,7 +474,7 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
defer consensus . mutex . Unlock ( )
defer consensus . mutex . Unlock ( )
if ! consensus . IsValidatorInCommittee ( validatorAddress ) {
if ! consensus . IsValidatorInCommittee ( validatorAddress ) {
utils . G etLogger( ) . Error ( "Invalid validator" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Error ( "Invalid validator" , "validatorAddress" , validatorAddress )
return
return
}
}
@ -489,7 +484,7 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
// proceed only when the message is not received before
// proceed only when the message is not received before
_ , ok := commitSigs [ validatorAddress ]
_ , ok := commitSigs [ validatorAddress ]
if ok {
if ok {
utils . G etLogger( ) . Debug ( "Already received commit message from the validator" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Debug ( "Already received commit message from the validator" , "validatorAddress" , validatorAddress )
return
return
}
}
@ -502,32 +497,32 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) {
var sign bls . Sign
var sign bls . Sign
err = sign . Deserialize ( commitSig )
err = sign . Deserialize ( commitSig )
if err != nil {
if err != nil {
utils . G etLogger( ) . Debug ( "Failed to deserialize bls signature" , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Debug ( "Failed to deserialize bls signature" , "validatorAddress" , validatorAddress )
return
return
}
}
blockNumHash := make ( [ ] byte , 8 )
blockNumHash := make ( [ ] byte , 8 )
binary . LittleEndian . PutUint64 ( blockNumHash , recvMsg . BlockNum )
binary . LittleEndian . PutUint64 ( blockNumHash , recvMsg . BlockNum )
commitPayload := append ( blockNumHash , recvMsg . BlockHash [ : ] ... )
commitPayload := append ( blockNumHash , recvMsg . BlockHash [ : ] ... )
if ! sign . VerifyHash ( validatorPubKey , commitPayload ) {
if ! sign . VerifyHash ( validatorPubKey , commitPayload ) {
utils . G etLogger( ) . Error ( "cannot verify commit message" , "v iewID" , recvMsg . ViewID , "b lock" , recvMsg . BlockNum )
consensus . g etLogger( ) . Error ( "cannot verify commit message" , "msgV iewID" , recvMsg . ViewID , "msgB lock" , recvMsg . BlockNum )
return
return
}
}
utils . G etLogger( ) . Debug ( "Received new commit message" , "numReceivedSoFar" , len ( commitSigs ) , "v iewID" , recvMsg . ViewID , "b lock" , recvMsg . BlockNum , "validatorAddress" , validatorAddress )
consensus . g etLogger( ) . Debug ( "Received new commit message" , "numReceivedSoFar" , len ( commitSigs ) , "msgV iewID" , recvMsg . ViewID , "msgB lock" , recvMsg . BlockNum , "validatorAddress" , validatorAddress )
commitSigs [ validatorAddress ] = & sign
commitSigs [ validatorAddress ] = & sign
// Set the bitmap indicating that this validator signed.
// Set the bitmap indicating that this validator signed.
if err := commitBitmap . SetKey ( validatorPubKey , true ) ; err != nil {
if err := commitBitmap . SetKey ( validatorPubKey , true ) ; err != nil {
ctxerror . Warn ( utils . G etLogger( ) , err , "commitBitmap.SetKey failed" )
ctxerror . Warn ( consensus . g etLogger( ) , err , "commitBitmap.SetKey failed" )
}
}
if len ( commitSigs ) >= consensus . Quorum ( ) {
if len ( commitSigs ) >= consensus . Quorum ( ) {
utils . G etLogger( ) . Info ( "Enough commits received!" , "num" , len ( commitSigs ) , "phase" , consensus . phase )
consensus . g etLogger( ) . Info ( "Enough commits received!" , "num" , len ( commitSigs ) )
consensus . finalizeCommits ( )
consensus . finalizeCommits ( )
}
}
}
}
func ( consensus * Consensus ) finalizeCommits ( ) {
func ( consensus * Consensus ) finalizeCommits ( ) {
utils . G etLogger( ) . Info ( "finalizing block" , "num" , len ( consensus . commitSigs ) , "phase" , consensus . phase )
consensus . g etLogger( ) . Info ( "finalizing block" , "num" , len ( consensus . commitSigs ) )
consensus . switchPhase ( Announce , true )
consensus . switchPhase ( Announce , true )
// Construct and broadcast committed message
// Construct and broadcast committed message
@ -535,15 +530,15 @@ func (consensus *Consensus) finalizeCommits() {
consensus . aggregatedCommitSig = aggSig
consensus . aggregatedCommitSig = aggSig
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
if err := consensus . host . SendMessageToGroups ( [ ] p2p . GroupID { p2p . NewGroupIDByShardID ( p2p . ShardID ( consensus . ShardID ) ) } , host . ConstructP2pMessage ( byte ( 17 ) , msgToSend ) ) ; err != nil {
ctxerror . Warn ( utils . G etLogger( ) , err , "cannot send committed message" )
ctxerror . Warn ( consensus . g etLogger( ) , err , "cannot send committed message" )
} else {
} else {
utils . G etLogger( ) . Debug ( "sent committed message" , "len" , len ( msgToSend ) )
consensus . g etLogger( ) . Debug ( "sent committed message" , "len" , len ( msgToSend ) )
}
}
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 . G etLogger( ) . Debug ( "failed to construct the new block after consensus" )
consensus . g etLogger( ) . Debug ( "failed to construct the new block after consensus" )
}
}
// Sign the block
// Sign the block
@ -557,7 +552,7 @@ func (consensus *Consensus) finalizeCommits() {
select {
select {
case consensus . VerifiedNewBlock <- & blockObj :
case consensus . VerifiedNewBlock <- & blockObj :
default :
default :
utils . G etLogger( ) . Info ( "[SYNC] Failed to send consensus verified block for state sync" , "blockHash" , blockObj . Hash ( ) )
consensus . g etLogger( ) . Info ( "[SYNC] Failed to send consensus verified block for state sync" , "blockHash" , blockObj . Hash ( ) )
}
}
consensus . reportMetrics ( blockObj )
consensus . reportMetrics ( blockObj )
@ -572,21 +567,21 @@ func (consensus *Consensus) finalizeCommits() {
if consensus . consensusTimeout [ timeoutBootstrap ] . IsActive ( ) {
if consensus . consensusTimeout [ timeoutBootstrap ] . IsActive ( ) {
consensus . consensusTimeout [ timeoutBootstrap ] . Stop ( )
consensus . consensusTimeout [ timeoutBootstrap ] . Stop ( )
utils . G etLogger( ) . Debug ( "start consensus timeout; stop bootstrap timeout only once" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "start consensus timeout; stop bootstrap timeout only once" )
} else {
} else {
utils . G etLogger( ) . Debug ( "start consensus timeout" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "start consensus timeout" )
}
}
consensus . consensusTimeout [ timeoutConsensus ] . Start ( )
consensus . consensusTimeout [ timeoutConsensus ] . Start ( )
consensus . OnConsensusDone ( & blockObj )
consensus . OnConsensusDone ( & blockObj )
utils . G etLogger( ) . Debug ( "HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!" , "viewID" , consensus . viewID , "numOfSignatures" , len ( consensus . commitSigs ) )
consensus . g etLogger( ) . Debug ( "HOORAY!!!!!!! CONSENSUS REACHED!!!!!!!" , "numOfSignatures" , len ( consensus . commitSigs ) )
// Send signal to Node so the new block can be added and new round of consensus can be triggered
// Send signal to Node so the new block can be added and new round of consensus can be triggered
consensus . ReadySignal <- struct { } { }
consensus . ReadySignal <- struct { } { }
}
}
func ( consensus * Consensus ) onCommitted ( msg * msg_pb . Message ) {
func ( consensus * Consensus ) onCommitted ( msg * msg_pb . Message ) {
utils . G etLogger( ) . Debug ( "receive committed message" , "phase" , consensus . phase , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "receive committed message" )
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
if consensus . PubKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal {
return
return
@ -594,21 +589,21 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
senderKey , err := consensus . verifySenderKey ( msg )
senderKey , err := consensus . verifySenderKey ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Warn ( "onCommitted verifySenderKey failed" , "error" , err )
consensus . g etLogger( ) . Warn ( "onCommitted verifySenderKey failed" , "error" , err )
return
return
}
}
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
if ! senderKey . IsEqual ( consensus . LeaderPubKey ) && consensus . mode . Mode ( ) == Normal && ! consensus . ignoreViewIDCheck {
utils . G etLogger( ) . Warn ( "onCommitted senderKey not match leader PubKey" )
consensus . g etLogger( ) . Warn ( "onCommitted senderKey not match leader PubKey" )
return
return
}
}
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
if err = verifyMessageSig ( senderKey , msg ) ; err != nil {
utils . G etLogger( ) . Warn ( "onCommitted Failed to verify sender's signature" , "error" , err )
consensus . g etLogger( ) . Warn ( "onCommitted Failed to verify sender's signature" , "error" , err )
return
return
}
}
recvMsg , err := ParsePbftMessage ( msg )
recvMsg , err := ParsePbftMessage ( msg )
if err != nil {
if err != nil {
utils . G etLogger( ) . Warn ( "onCommitted unable to parse msg" , "error" , err )
consensus . g etLogger( ) . Warn ( "onCommitted unable to parse msg" , "error" , err )
return
return
}
}
if recvMsg . BlockNum < consensus . blockNum {
if recvMsg . BlockNum < consensus . blockNum {
@ -617,13 +612,13 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
aggSig , mask , err := consensus . readSignatureBitmapPayload ( recvMsg . Payload , 0 )
aggSig , mask , err := consensus . readSignatureBitmapPayload ( recvMsg . Payload , 0 )
if err != nil {
if err != nil {
utils . G etLogger( ) . Error ( "readSignatureBitmapPayload failed" , "error" , err )
consensus . g etLogger( ) . Error ( "readSignatureBitmapPayload failed" , "error" , err )
return
return
}
}
// check has 2f+1 signatures
// check has 2f+1 signatures
if count := utils . CountOneBits ( mask . Bitmap ) ; count < consensus . Quorum ( ) {
if count := utils . CountOneBits ( mask . Bitmap ) ; count < consensus . Quorum ( ) {
utils . G etLogger( ) . Debug ( "not have enough signature" , "need" , consensus . Quorum ( ) , "have" , count )
consensus . g etLogger( ) . Debug ( "not have enough signature" , "need" , consensus . Quorum ( ) , "have" , count )
return
return
}
}
@ -631,16 +626,16 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
binary . LittleEndian . PutUint64 ( blockNumHash , recvMsg . BlockNum )
binary . LittleEndian . PutUint64 ( blockNumHash , recvMsg . BlockNum )
commitPayload := append ( blockNumHash , recvMsg . BlockHash [ : ] ... )
commitPayload := append ( blockNumHash , recvMsg . BlockHash [ : ] ... )
if ! aggSig . VerifyHash ( mask . AggregatePublic , commitPayload ) {
if ! aggSig . VerifyHash ( mask . AggregatePublic , commitPayload ) {
utils . G etLogger( ) . Error ( "Failed to verify the multi signature for commit phase" , "blockNum " , recvMsg . BlockNum )
consensus . g etLogger( ) . Error ( "Failed to verify the multi signature for commit phase" , "msgBlock " , recvMsg . BlockNum )
return
return
}
}
consensus . aggregatedCommitSig = aggSig
consensus . aggregatedCommitSig = aggSig
consensus . commitBitmap = mask
consensus . commitBitmap = mask
utils . G etLogger( ) . Debug ( "committed message added" , "phase" , consensus . phase , "myViewID" , consensus . viewID , "myBlock" , consensus . blockNum , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . g etLogger( ) . Debug ( "committed message added" , "msgViewID" , recvMsg . ViewID , "msgBlock" , recvMsg . BlockNum )
consensus . pbftLog . AddMessage ( recvMsg )
consensus . pbftLog . AddMessage ( recvMsg )
if recvMsg . BlockNum - consensus . blockNum > consensusBlockNumBuffer {
if recvMsg . BlockNum - consensus . blockNum > consensusBlockNumBuffer {
utils . G etLogger( ) . Debug ( "onCommitted out of sync" , "myBlock" , consensus . blockNum , "msgBlock" , recvMsg . BlockNum )
consensus . g etLogger( ) . Debug ( "onCommitted out of sync" , "msgBlock" , recvMsg . BlockNum )
go func ( ) {
go func ( ) {
select {
select {
case consensus . blockNumLowChan <- struct { } { } :
case consensus . blockNumLowChan <- struct { } { } :
@ -655,7 +650,7 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
}
}
// if consensus.checkViewID(recvMsg) != nil {
// if consensus.checkViewID(recvMsg) != nil {
// utils.G etLogger().Debug("viewID check failed", "viewID", recvMsg.ViewID, "myViewID", consensus.viewID)
// consensus.g etLogger().Debug("viewID check failed", "viewID", recvMsg.ViewID, "myViewID", consensus.viewID)
// return
// return
// }
// }
@ -665,9 +660,9 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
if consensus . consensusTimeout [ timeoutBootstrap ] . IsActive ( ) {
if consensus . consensusTimeout [ timeoutBootstrap ] . IsActive ( ) {
consensus . consensusTimeout [ timeoutBootstrap ] . Stop ( )
consensus . consensusTimeout [ timeoutBootstrap ] . Stop ( )
utils . G etLogger( ) . Debug ( "start consensus timeout; stop bootstrap timeout only once" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "start consensus timeout; stop bootstrap timeout only once" )
} else {
} else {
utils . G etLogger( ) . Debug ( "start consensus timeout" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "start consensus timeout" )
}
}
consensus . consensusTimeout [ timeoutConsensus ] . Start ( )
consensus . consensusTimeout [ timeoutConsensus ] . Start ( )
return
return
@ -675,7 +670,7 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
// try to catch up if fall behind
// try to catch up if fall behind
func ( consensus * Consensus ) tryCatchup ( ) {
func ( consensus * Consensus ) tryCatchup ( ) {
utils . G etLogger( ) . Info ( "tryCatchup: commit new blocks" , "blockNum" , consensus . blockNum )
consensus . g etLogger( ) . Info ( "tryCatchup: commit new blocks" )
// if consensus.phase != Commit && consensus.mode.Mode() == Normal {
// if consensus.phase != Commit && consensus.mode.Mode() == Normal {
// return
// return
// }
// }
@ -686,9 +681,9 @@ func (consensus *Consensus) tryCatchup() {
break
break
}
}
if len ( msgs ) > 1 {
if len ( msgs ) > 1 {
utils . G etLogger( ) . Error ( "[PBFT] DANGER!!! we should only get one committed message for a given blockNum" , "blockNum" , consensus . blockNum , "numMsgs" , len ( msgs ) )
consensus . g etLogger( ) . Error ( "[PBFT] DANGER!!! we should only get one committed message for a given blockNum" , "numMsgs" , len ( msgs ) )
}
}
utils . G etLogger( ) . Info ( "committed message found" , "block" , consensus . blockNum )
consensus . g etLogger( ) . Info ( "committed message found" )
block := consensus . pbftLog . GetBlockByHash ( msgs [ 0 ] . BlockHash )
block := consensus . pbftLog . GetBlockByHash ( msgs [ 0 ] . BlockHash )
if block == nil {
if block == nil {
@ -696,17 +691,17 @@ func (consensus *Consensus) tryCatchup() {
}
}
if block . ParentHash ( ) != consensus . ChainReader . CurrentHeader ( ) . Hash ( ) {
if block . ParentHash ( ) != consensus . ChainReader . CurrentHeader ( ) . Hash ( ) {
utils . G etLogger( ) . Debug ( "[PBFT] parent block hash not match" , "blockNum" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "[PBFT] parent block hash not match" )
break
break
}
}
utils . G etLogger( ) . Info ( "block found to commit" , "block" , consensus . blockNum )
consensus . g etLogger( ) . Info ( "block found to commit" )
preparedMsgs := consensus . pbftLog . GetMessagesByTypeSeqHash ( msg_pb . MessageType_PREPARED , msgs [ 0 ] . BlockNum , msgs [ 0 ] . BlockHash )
preparedMsgs := consensus . pbftLog . GetMessagesByTypeSeqHash ( msg_pb . MessageType_PREPARED , msgs [ 0 ] . BlockNum , msgs [ 0 ] . BlockHash )
msg := consensus . pbftLog . FindMessageByMaxViewID ( preparedMsgs )
msg := consensus . pbftLog . FindMessageByMaxViewID ( preparedMsgs )
if msg == nil {
if msg == nil {
break
break
}
}
utils . G etLogger( ) . Info ( "prepared message found to commit" , "block" , consensus . blockNum )
consensus . g etLogger( ) . Info ( "prepared message found to commit" )
consensus . blockHash = [ 32 ] byte { }
consensus . blockHash = [ 32 ] byte { }
consensus . blockNum = consensus . blockNum + 1
consensus . blockNum = consensus . blockNum + 1
@ -735,14 +730,14 @@ func (consensus *Consensus) tryCatchup() {
block . SetPrepareSig ( prepareSig , prepareBitmap )
block . SetPrepareSig ( prepareSig , prepareBitmap )
block . SetCommitSig ( aggSig , bitmap )
block . SetCommitSig ( aggSig , bitmap )
utils . G etLogger( ) . Info ( "Adding block to chain" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Info ( "Adding block to chain" )
consensus . OnConsensusDone ( block )
consensus . OnConsensusDone ( block )
consensus . ResetState ( )
consensus . ResetState ( )
select {
select {
case consensus . VerifiedNewBlock <- block :
case consensus . VerifiedNewBlock <- block :
default :
default :
utils . G etLogger( ) . Info ( "[SYNC] consensus verified block send to chan failed" , "blockHash" , block . Hash ( ) )
consensus . g etLogger( ) . Info ( "[SYNC] consensus verified block send to chan failed" , "blockHash" , block . Hash ( ) )
continue
continue
}
}
@ -767,11 +762,11 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
<- startChannel
<- startChannel
}
}
go func ( ) {
go func ( ) {
utils . G etLogger( ) . Info ( "start consensus" , "time" , time . Now ( ) )
consensus . g etLogger( ) . Info ( "start consensus" , "time" , time . Now ( ) )
defer close ( stoppedChan )
defer close ( stoppedChan )
ticker := time . NewTicker ( 3 * time . Second )
ticker := time . NewTicker ( 3 * time . Second )
consensus . consensusTimeout [ timeoutBootstrap ] . Start ( )
consensus . consensusTimeout [ timeoutBootstrap ] . Start ( )
utils . G etLogger( ) . Debug ( "start bootstrap timeout only once" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
consensus . g etLogger( ) . Debug ( "start bootstrap timeout only once" , "viewID" , consensus . viewID , "block" , consensus . blockNum )
for {
for {
select {
select {
case <- ticker . C :
case <- ticker . C :
@ -783,11 +778,11 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
continue
continue
}
}
if k != timeoutViewChange {
if k != timeoutViewChange {
utils . G etLogger( ) . Debug ( "ops" , "phase" , k , "mode" , consensus . mode . Mode ( ) , "viewChangeID" , consensus . viewID )
consensus . g etLogger( ) . Debug ( "ops consensus timeout" )
consensus . startViewChange ( consensus . viewID + 1 )
consensus . startViewChange ( consensus . viewID + 1 )
break
break
} else {
} else {
utils . G etLogger( ) . Debug ( "ops" , "phase" , k , "mode" , consensus . mode . Mode ( ) , "viewChangeID" , consensus . mode . ViewID ( ) )
consensus . g etLogger( ) . Debug ( "ops view change timeout" )
viewID := consensus . mode . ViewID ( )
viewID := consensus . mode . ViewID ( )
consensus . startViewChange ( viewID + 1 )
consensus . startViewChange ( viewID + 1 )
break
break
@ -800,14 +795,14 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
consensus . ignoreViewIDCheck = true
consensus . ignoreViewIDCheck = true
case newBlock := <- blockChannel :
case newBlock := <- blockChannel :
utils . G etLogger( ) . Info ( "receive newBlock" , "blockNum " , newBlock . NumberU64 ( ) )
consensus . g etLogger( ) . Info ( "receive newBlock" , "msgBlock " , newBlock . NumberU64 ( ) )
if consensus . ShardID == 0 {
if consensus . ShardID == 0 {
// TODO ek/rj - re-enable this after fixing DRand
// TODO ek/rj - re-enable this after fixing DRand
//if core.IsEpochBlock(newBlock) { // Only beacon chain do randomness generation
//if core.IsEpochBlock(newBlock) { // Only beacon chain do randomness generation
// // Receive pRnd from DRG protocol
// // Receive pRnd from DRG protocol
// utils.G etLogger().Debug("[DRG] Waiting for pRnd")
// consensus.g etLogger().Debug("[DRG] Waiting for pRnd")
// pRndAndBitmap := <-consensus.PRndChannel
// pRndAndBitmap := <-consensus.PRndChannel
// utils.G etLogger().Debug("[DRG] Got pRnd", "pRnd", pRndAndBitmap)
// consensus.g etLogger().Debug("[DRG] Got pRnd", "pRnd", pRndAndBitmap)
// pRnd := [32]byte{}
// pRnd := [32]byte{}
// copy(pRnd[:], pRndAndBitmap[:32])
// copy(pRnd[:], pRndAndBitmap[:32])
// bitmap := pRndAndBitmap[32:]
// bitmap := pRndAndBitmap[32:]
@ -822,15 +817,15 @@ func (consensus *Consensus) Start(blockChannel chan *types.Block, stopChan chan
if err == nil {
if err == nil {
// Verify the randomness
// Verify the randomness
_ = blockHash
_ = blockHash
utils . G etLogger( ) . Info ( "Adding randomness into new block" , "rnd" , rnd )
consensus . g etLogger( ) . Info ( "Adding randomness into new block" , "rnd" , rnd )
newBlock . AddVdf ( [ 258 ] byte { } ) // TODO(HB): add real vdf
newBlock . AddVdf ( [ 258 ] byte { } ) // TODO(HB): add real vdf
} else {
} else {
utils . G etLogger( ) . Info ( "Failed to get randomness" , "error" , err )
consensus . g etLogger( ) . Info ( "Failed to get randomness" , "error" , err )
}
}
}
}
startTime = time . Now ( )
startTime = time . Now ( )
utils . G etLogger( ) . Debug ( "STARTING CONSENSUS" , "numTxs" , len ( newBlock . Transactions ( ) ) , "consensus" , consensus , "startTime" , startTime , "publicKeys" , len ( consensus . PublicKeys ) )
consensus . g etLogger( ) . Debug ( "STARTING CONSENSUS" , "numTxs" , len ( newBlock . Transactions ( ) ) , "consensus" , consensus , "startTime" , startTime , "publicKeys" , len ( consensus . PublicKeys ) )
consensus . tryAnnounce ( newBlock )
consensus . tryAnnounce ( newBlock )
case msg := <- consensus . MsgChan :
case msg := <- consensus . MsgChan :