From 6a72ca14ad6afafc2ae7747ee6becacbd7050b8b Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Fri, 30 Oct 2020 18:12:16 -0700 Subject: [PATCH] make block sync trigger less frequent --- api/service/syncing/syncing.go | 2 +- consensus/consensus_service.go | 6 ++++++ consensus/construct_test.go | 10 ++++++---- consensus/validator.go | 19 +++++++++++-------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/api/service/syncing/syncing.go b/api/service/syncing/syncing.go index 873da9e90..03a3b46ca 100644 --- a/api/service/syncing/syncing.go +++ b/api/service/syncing/syncing.go @@ -32,7 +32,7 @@ const ( TimesToFail = 5 // downloadBlocks service retry limit RegistrationNumber = 3 SyncingPortDifference = 3000 - inSyncThreshold = 0 // when peerBlockHeight - myBlockHeight <= inSyncThreshold, it's ready to join consensus + inSyncThreshold = 1 // when peerBlockHeight - myBlockHeight <= inSyncThreshold, it's ready to join consensus SyncLoopBatchSize uint32 = 1000 // maximum size for one query of block hashes verifyHeaderBatchSize uint64 = 100 // block chain header verification batch size SyncLoopFrequency = 1 // unit in second diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index ffbb2f370..b3ede1404 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -421,6 +421,12 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { Msg("[UpdateConsensusInformation] changing committee") // take care of possible leader change during the epoch + // TODO: in a very rare case, when a M1 view change happened, the block contains coinbase for last leader + // but the new leader is actually recognized by most of the nodes. At this time, if a node sync to this + // exact block and set its leader, it will set with the failed leader as in the coinbase of the block. + // This is a very rare case scenario and not likely to cause any issue in mainnet. But we need to think about + // a solution to take care of this case because the coinbase of the latest block doesn't really represent the + // the real current leader in case of M1 view change. if !curHeader.IsLastBlockInEpoch() && curHeader.Number().Uint64() != 0 { leaderPubKey, err := consensus.getLeaderPubKeyFromCoinbase(curHeader) if err != nil || leaderPubKey == nil { diff --git a/consensus/construct_test.go b/consensus/construct_test.go index a0111fb5d..27d1e82f4 100644 --- a/consensus/construct_test.go +++ b/consensus/construct_test.go @@ -71,19 +71,21 @@ func TestConstructPreparedMessage(test *testing.T) { message := "test string" leaderKey := bls.SerializedPublicKey{} leaderKey.FromLibBLSPublicKey(leaderPubKey) + leaderKeyWrapper := bls.PublicKeyWrapper{Object: leaderPubKey, Bytes: leaderKey} validatorKey := bls.SerializedPublicKey{} validatorKey.FromLibBLSPublicKey(validatorPubKey) - consensus.Decider.submitVote( + validatorKeyWrapper := bls.PublicKeyWrapper{Object: validatorPubKey, Bytes: validatorKey} + consensus.Decider.AddNewVote( quorum.Prepare, - []bls.SerializedPublicKey{leaderKey}, + []*bls.PublicKeyWrapper{&leaderKeyWrapper}, leaderPriKey.Sign(message), common.BytesToHash(consensus.blockHash[:]), consensus.blockNum, consensus.GetCurBlockViewID(), ) - if _, err := consensus.Decider.submitVote( + if _, err := consensus.Decider.AddNewVote( quorum.Prepare, - []bls.SerializedPublicKey{validatorKey}, + []*bls.PublicKeyWrapper{&validatorKeyWrapper}, validatorPriKey.Sign(message), common.BytesToHash(consensus.blockHash[:]), consensus.blockNum, diff --git a/consensus/validator.go b/consensus/validator.go index bff01506e..a1f83de65 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -114,10 +114,6 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) { Msg("Wrong BlockNum Received, ignoring!") return } - if recvMsg.BlockNum > consensus.blockNum { - consensus.getLogger().Warn().Msgf("[OnPrepared] low consensus block number. Spin sync") - consensus.spinUpStateSync() - } // check validity of prepared signature blockHash := recvMsg.BlockHash @@ -153,6 +149,12 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) { if !consensus.onPreparedSanityChecks(&blockObj, recvMsg) { return } + + if recvMsg.BlockNum > consensus.blockNum { + consensus.getLogger().Warn().Msgf("[OnPrepared] low consensus block number. Spin sync") + consensus.spinUpStateSync() + } + consensus.mutex.Lock() defer consensus.mutex.Unlock() @@ -236,10 +238,6 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) { if !consensus.isRightBlockNumCheck(recvMsg) { return } - if recvMsg.BlockNum > consensus.blockNum { - consensus.getLogger().Info().Msg("[OnCommitted] low consensus block number. Spin up state sync") - consensus.spinUpStateSync() - } aggSig, mask, err := consensus.ReadSignatureBitmapPayload(recvMsg.Payload, 0) if err != nil { @@ -272,6 +270,11 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) { consensus.FBFTLog.AddMessage(recvMsg) + if recvMsg.BlockNum > consensus.blockNum { + consensus.getLogger().Info().Msg("[OnCommitted] low consensus block number. Spin up state sync") + consensus.spinUpStateSync() + } + consensus.mutex.Lock() defer consensus.mutex.Unlock()