From abf5f5aa8c056510b49ebfb9f6f089538294b372 Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Sat, 28 Jan 2023 00:21:05 -0300 Subject: [PATCH] Fix usage of private methods. --- consensus/checks.go | 2 +- consensus/consensus_service.go | 12 ++++++++++++ consensus/consensus_v2.go | 16 ++++++++-------- consensus/construct.go | 4 ++-- consensus/debug.go | 7 +++++++ consensus/leader.go | 8 ++++---- consensus/view_change.go | 12 ++++++------ 7 files changed, 40 insertions(+), 21 deletions(-) diff --git a/consensus/checks.go b/consensus/checks.go index e4c9a7084..fadcfbd04 100644 --- a/consensus/checks.go +++ b/consensus/checks.go @@ -147,7 +147,7 @@ func (consensus *Consensus) onViewChangeSanityCheck(recvMsg *FBFTMessage) bool { consensus.getLogger().Debug(). Uint64("MsgBlockNum", recvMsg.BlockNum). - Uint64("MyViewChangingID", consensus.GetViewChangingID()). + Uint64("MyViewChangingID", consensus.getViewChangingID()). Uint64("MsgViewChangingID", recvMsg.ViewID). Interface("SendPubKeys", recvMsg.SenderPubkeys). Msg("[onViewChangeSanityCheck]") diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index c43c7fe12..1ce41392c 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -210,6 +210,13 @@ func (consensus *Consensus) SetIsBackup(isBackup bool) { // Mode returns the mode of consensus func (consensus *Consensus) Mode() Mode { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() + return consensus.mode() +} + +// mode returns the mode of consensus +func (consensus *Consensus) mode() Mode { return consensus.current.Mode() } @@ -254,6 +261,11 @@ func (consensus *Consensus) SetBlockNum(blockNum uint64) { atomic.StoreUint64(&consensus.blockNum, blockNum) } +// SetBlockNum sets the blockNum in consensus object, called at node bootstrap +func (consensus *Consensus) setBlockNum(blockNum uint64) { + atomic.StoreUint64(&consensus.blockNum, blockNum) +} + // ReadSignatureBitmapPayload read the payload for signature and bitmap; offset is the beginning position of reading func (consensus *Consensus) ReadSignatureBitmapPayload(recvPayload []byte, offset int) (*bls_core.Sign, *bls_cosi.Mask, error) { consensus.mutex.RLock() diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 1ab33d0bb..4244cb373 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -367,7 +367,7 @@ func (consensus *Consensus) syncReadyChan() { func (consensus *Consensus) syncNotReadyChan() { consensus.getLogger().Info().Msg("[ConsensusMainLoop] syncNotReadyChan") - consensus.SetBlockNum(consensus.Blockchain().CurrentHeader().Number().Uint64() + 1) + consensus.setBlockNum(consensus.Blockchain().CurrentHeader().Number().Uint64() + 1) consensus.current.SetMode(Syncing) consensus.getLogger().Info().Msg("[ConsensusMainLoop] Node is OUT OF SYNC") consensusSyncCounterVec.With(prometheus.Labels{"consensus": "out_of_sync"}).Inc() @@ -452,14 +452,14 @@ func (consensus *Consensus) BlockChannel(newBlock *types.Block) { // waitForCommit wait extra 2 seconds for commit phase to finish func (consensus *Consensus) waitForCommit() { - if consensus.Mode() != Normal || consensus.phase.Get() != FBFTCommit { + if consensus.mode() != Normal || consensus.phase.Get() != FBFTCommit { return } // We only need to wait consensus is in normal commit phase utils.Logger().Warn().Str("phase", consensus.phase.String()).Msg("[shutdown] commit phase has to wait") maxWait := time.Now().Add(2 * consensus.BlockPeriod) - for time.Now().Before(maxWait) && consensus.GetConsensusPhase() == "Commit" { + for time.Now().Before(maxWait) && consensus.getConsensusPhase() == "Commit" { utils.Logger().Warn().Msg("[shutdown] wait for consensus finished") time.Sleep(time.Millisecond * 100) } @@ -635,7 +635,7 @@ func (consensus *Consensus) tryCatchup() error { if consensus.BlockVerifier == nil { return errors.New("consensus haven't finished initialization") } - initBN := consensus.BlockNum() + initBN := consensus.getBlockNum() defer consensus.postCatchup(initBN) blks, msgs, err := consensus.getLastMileBlocksAndMsg(initBN) @@ -764,15 +764,15 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg } func (consensus *Consensus) postCatchup(initBN uint64) { - if initBN < consensus.BlockNum() { + if initBN < consensus.getBlockNum() { consensus.getLogger().Info(). Uint64("From", initBN). - Uint64("To", consensus.BlockNum()). + Uint64("To", consensus.getBlockNum()). Msg("[TryCatchup] Caught up!") consensus.switchPhase("TryCatchup", FBFTAnnounce) } // catch up and skip from view change trap - if initBN < consensus.BlockNum() && consensus.isViewChangingMode() { + if initBN < consensus.getBlockNum() && consensus.isViewChangingMode() { consensus.current.SetMode(Normal) consensus.consensusTimeout[timeoutViewChange].Stop() } @@ -833,7 +833,7 @@ func (consensus *Consensus) GenerateVdfAndProof(newBlock *types.Block, vrfBlockN start := time.Now() vdf.Execute() duration := time.Since(start) - consensus.getLogger().Info(). + consensus.GetLogger().Info(). Dur("duration", duration). Msg("[ConsensusMainLoop] VDF computation finished") output := <-outputChannel diff --git a/consensus/construct.go b/consensus/construct.go index 8d9f3838a..eaa6fd83f 100644 --- a/consensus/construct.go +++ b/consensus/construct.go @@ -29,8 +29,8 @@ type NetworkMessage struct { func (consensus *Consensus) populateMessageFields( request *msg_pb.ConsensusRequest, blockHash []byte, ) *msg_pb.ConsensusRequest { - request.ViewId = consensus.GetCurBlockViewID() - request.BlockNum = consensus.BlockNum() + request.ViewId = consensus.getCurBlockViewID() + request.BlockNum = consensus.getBlockNum() request.ShardId = consensus.ShardID // 32 byte block hash request.BlockHash = blockHash diff --git a/consensus/debug.go b/consensus/debug.go index da3587710..cba13cc01 100644 --- a/consensus/debug.go +++ b/consensus/debug.go @@ -2,6 +2,13 @@ package consensus // GetConsensusPhase returns the current phase of the consensus. func (consensus *Consensus) GetConsensusPhase() string { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() + return consensus.getConsensusPhase() +} + +// GetConsensusPhase returns the current phase of the consensus. +func (consensus *Consensus) getConsensusPhase() string { return consensus.phase.String() } diff --git a/consensus/leader.go b/consensus/leader.go index 548ec98c0..2f7766e19 100644 --- a/consensus/leader.go +++ b/consensus/leader.go @@ -74,7 +74,7 @@ func (consensus *Consensus) announce(block *types.Block) { } // Construct broadcast p2p message if err := consensus.msgSender.SendWithRetry( - consensus.BlockNum(), msg_pb.MessageType_ANNOUNCE, []nodeconfig.GroupID{ + consensus.getBlockNum(), msg_pb.MessageType_ANNOUNCE, []nodeconfig.GroupID{ nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)), }, p2p.ConstructMessage(msgToSend)); err != nil { consensus.getLogger().Warn(). @@ -95,7 +95,7 @@ func (consensus *Consensus) announce(block *types.Block) { func (consensus *Consensus) onPrepare(recvMsg *FBFTMessage) { // TODO(audit): make FBFT lookup using map instead of looping through all items. if !consensus.FBFTLog.HasMatchingViewAnnounce( - consensus.BlockNum(), consensus.GetCurBlockViewID(), recvMsg.BlockHash, + consensus.getBlockNum(), consensus.getCurBlockViewID(), recvMsg.BlockHash, ) { consensus.getLogger().Debug(). Uint64("MsgViewID", recvMsg.ViewID). @@ -280,7 +280,7 @@ func (consensus *Consensus) onCommit(recvMsg *FBFTMessage) { //// Write - End //// Read - Start - viewID := consensus.GetCurBlockViewID() + viewID := consensus.getCurBlockViewID() if consensus.Decider.IsAllSigsCollected() { logger.Info().Msg("[OnCommit] 100% Enough commits received") @@ -315,7 +315,7 @@ func (consensus *Consensus) onCommit(recvMsg *FBFTMessage) { consensus.mutex.Lock() defer consensus.mutex.Unlock() - if viewID == consensus.GetCurBlockViewID() { + if viewID == consensus.getCurBlockViewID() { consensus.finalCommit() } }(viewID) diff --git a/consensus/view_change.go b/consensus/view_change.go index 13acb86ae..d8b1058a0 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -88,14 +88,14 @@ func (pm *State) SetIsBackup(isBackup bool) { // fallbackNextViewID return the next view ID and duration when there is an exception // to calculate the time-based viewId func (consensus *Consensus) fallbackNextViewID() (uint64, time.Duration) { - diff := int64(consensus.GetViewChangingID() + 1 - consensus.GetCurBlockViewID()) + diff := int64(consensus.getViewChangingID() + 1 - consensus.getCurBlockViewID()) if diff <= 0 { diff = int64(1) } consensus.getLogger().Error(). Int64("diff", diff). Msg("[fallbackNextViewID] use legacy viewID algorithm") - return consensus.GetViewChangingID() + 1, time.Duration(diff * diff * int64(viewChangeDuration)) + return consensus.getViewChangingID() + 1, time.Duration(diff * diff * int64(viewChangeDuration)) } // getNextViewID return the next view ID based on the timestamp @@ -152,7 +152,7 @@ func (consensus *Consensus) getNextViewID() (uint64, time.Duration) { func (consensus *Consensus) getNextLeaderKey(viewID uint64) *bls.PublicKeyWrapper { gap := 1 - cur := consensus.GetCurBlockViewID() + cur := consensus.getCurBlockViewID() if viewID > cur { gap = int(viewID - cur) } @@ -196,7 +196,7 @@ func (consensus *Consensus) getNextLeaderKey(viewID uint64) *bls.PublicKeyWrappe Str("leaderPubKey", consensus.LeaderPubKey.Bytes.Hex()). Int("gap", gap). Uint64("newViewID", viewID). - Uint64("myCurBlockViewID", consensus.GetCurBlockViewID()). + Uint64("myCurBlockViewID", consensus.getCurBlockViewID()). Msg("[getNextLeaderKey] got leaderPubKey from coinbase") // wasFound, next := consensus.Decider.NthNext(lastLeaderPubKey, gap) // FIXME: rotate leader on harmony nodes only before fully externalization @@ -234,7 +234,7 @@ func createTimeout() map[TimeoutType]*utils.Timeout { // startViewChange start the view change process func (consensus *Consensus) startViewChange() { - if consensus.disableViewChange || consensus.IsBackup() { + if consensus.disableViewChange || consensus.isBackup { return } @@ -242,7 +242,7 @@ func (consensus *Consensus) startViewChange() { consensus.consensusTimeout[timeoutBootstrap].Stop() consensus.current.SetMode(ViewChanging) nextViewID, duration := consensus.getNextViewID() - consensus.SetViewChangingID(nextViewID) + consensus.setViewChangingID(nextViewID) // TODO: set the Leader PubKey to the next leader for view change // this is dangerous as the leader change is not succeeded yet // we use it this way as in many code we validate the messages