From 2aed5e16df0e3c8fdc1a5844b477ab1afffa347e Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 7 Nov 2020 15:40:47 -0800 Subject: [PATCH 1/3] fix finality count for leader --- consensus/consensus_v2.go | 2 ++ node/node_newblock.go | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index f9a5dc910..2bb3793d8 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -343,6 +343,8 @@ func (consensus *Consensus) Start( // Sleep to wait for the full block time consensus.getLogger().Info().Msg("[ConsensusMainLoop] Waiting for Block Time") <-time.After(time.Until(consensus.NextBlockDue)) + consensus.StartFinalityCount() + // Update time due for next block consensus.NextBlockDue = time.Now().Add(consensus.BlockPeriod) diff --git a/node/node_newblock.go b/node/node_newblock.go index e8f8698d9..ef9fd35f2 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -80,7 +80,6 @@ func (node *Node) WaitForConsensusReadyV2(readySignal chan consensus.ProposalTyp } } }() - node.Consensus.StartFinalityCount() newBlock, err := node.ProposeNewBlock(newCommitSigsChan) if err == nil { From 925d2c0e95c4e379bf0f74e5e38c86c9932c342c Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 7 Nov 2020 18:03:28 -0800 Subject: [PATCH 2/3] make precommit messaging in the same thread as consensus --- consensus/consensus_v2.go | 61 ++++++++++++++++++++------------------- consensus/leader.go | 8 ++--- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 2bb3793d8..515646987 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -535,47 +535,48 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error { } // Construct committed message - consensus.mutex.Lock() network, err := consensus.construct(msg_pb.MessageType_COMMITTED, nil, []*bls.PrivateKeyWrapper{leaderPriKey}) - consensus.mutex.Unlock() if err != nil { consensus.getLogger().Warn().Err(err). Msg("[preCommitAndPropose] Unable to construct Committed message") return err } - msgToSend, FBFTMsg := - network.Bytes, - network.FBFTMsg - bareMinimumCommit := FBFTMsg.Payload - consensus.FBFTLog.AddMessage(FBFTMsg) - blk.SetCurrentCommitSig(bareMinimumCommit) + go func() { + msgToSend, FBFTMsg := + network.Bytes, + network.FBFTMsg + bareMinimumCommit := FBFTMsg.Payload + consensus.FBFTLog.AddMessage(FBFTMsg) - if _, err := consensus.Blockchain.InsertChain([]*types.Block{blk}, true); err != nil { - consensus.getLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain") - return err - } + blk.SetCurrentCommitSig(bareMinimumCommit) - // if leader successfully finalizes the block, send committed message to validators - if err := consensus.msgSender.SendWithRetry( - blk.NumberU64(), - msg_pb.MessageType_COMMITTED, []nodeconfig.GroupID{ - nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)), - }, - p2p.ConstructMessage(msgToSend)); err != nil { - consensus.getLogger().Warn().Err(err).Msg("[preCommitAndPropose] Cannot send committed message") - } else { - consensus.getLogger().Info(). - Str("blockHash", blk.Hash().Hex()). - Uint64("blockNum", consensus.blockNum). - Msg("[preCommitAndPropose] Sent Committed Message") - } - consensus.consensusTimeout[timeoutConsensus].Start() + if _, err := consensus.Blockchain.InsertChain([]*types.Block{blk}, true); err != nil { + consensus.getLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain") + } + + // if leader successfully finalizes the block, send committed message to validators + if err := consensus.msgSender.SendWithRetry( + blk.NumberU64(), + msg_pb.MessageType_COMMITTED, []nodeconfig.GroupID{ + nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)), + }, + p2p.ConstructMessage(msgToSend)); err != nil { + consensus.getLogger().Warn().Err(err).Msg("[preCommitAndPropose] Cannot send committed message") + } else { + consensus.getLogger().Info(). + Str("blockHash", blk.Hash().Hex()). + Uint64("blockNum", consensus.blockNum). + Msg("[preCommitAndPropose] Sent Committed Message") + } + consensus.consensusTimeout[timeoutConsensus].Start() + + // Send signal to Node to propose the new block for consensus + consensus.getLogger().Info().Msg("[preCommitAndPropose] sending block proposal signal") - // Send signal to Node to propose the new block for consensus - consensus.getLogger().Info().Msg("[preCommitAndPropose] sending block proposal signal") + consensus.ReadySignal <- AsyncProposal + }() - consensus.ReadySignal <- AsyncProposal return nil } diff --git a/consensus/leader.go b/consensus/leader.go index 9c5d24c9e..fde1419a6 100644 --- a/consensus/leader.go +++ b/consensus/leader.go @@ -302,10 +302,10 @@ func (consensus *Consensus) onCommit(msg *msg_pb.Message) { if !blockObj.IsLastBlockInEpoch() { // only do early commit if it's not epoch block to avoid problems - go func() { - // TODO: make it synchronized with commitFinishChan - consensus.preCommitAndPropose(blockObj) - }() + + // TODO: make it synchronized with commitFinishChan + consensus.preCommitAndPropose(blockObj) + } consensus.getLogger().Info().Msg("[OnCommit] Starting Grace Period") From e8e8e77a8aba99438a86b9b84507c0d9cb108489 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 7 Nov 2020 18:08:05 -0800 Subject: [PATCH 3/3] remove unsync update --- api/service/syncing/syncing.go | 1 - 1 file changed, 1 deletion(-) diff --git a/api/service/syncing/syncing.go b/api/service/syncing/syncing.go index 08cdf38a9..1f9bc0e40 100644 --- a/api/service/syncing/syncing.go +++ b/api/service/syncing/syncing.go @@ -1025,7 +1025,6 @@ func (ss *StateSync) SyncLoop(bc *core.BlockChain, worker *worker.Worker, isBeac if err := ss.addConsensusLastMile(bc, consensus); err != nil { utils.Logger().Error().Err(err).Msg("[SYNC] Add consensus last mile") } - consensus.SetMode(consensus.UpdateConsensusInformation()) } ss.purgeAllBlocksFromCache() }