diff --git a/consensus/consensus.go b/consensus/consensus.go index 6f019b2a9..63f18a85e 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -254,6 +254,8 @@ func (consensus *Consensus) getConsensusLeaderPrivateKey() (*bls.PrivateKeyWrapp } func (consensus *Consensus) IsBackup() bool { + consensus.mutex.RLock() + defer consensus.mutex.RUnlock() return consensus.isBackup } diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 1ba1f6e2f..d658fe83d 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -190,10 +190,6 @@ func (consensus *Consensus) SetMode(m Mode) { // SetMode sets the mode of consensus func (consensus *Consensus) setMode(m Mode) { - if m == Normal && consensus.isBackup { - m = NormalBackup - } - consensus.getLogger().Debug(). Str("Mode", m.String()). Msg("[SetMode]") @@ -202,11 +198,12 @@ func (consensus *Consensus) setMode(m Mode) { // SetIsBackup sets the mode of consensus func (consensus *Consensus) SetIsBackup(isBackup bool) { + consensus.mutex.Lock() + defer consensus.mutex.Unlock() consensus.getLogger().Debug(). Bool("IsBackup", isBackup). Msg("[SetIsBackup]") consensus.isBackup = isBackup - consensus.current.SetIsBackup(isBackup) } // Mode returns the mode of consensus diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 0aec2537b..33ba54b1d 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -106,7 +106,7 @@ func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, peer libp2p consensus.isLeader() // if in backup normal mode, force ignore view change event and leader event. - if consensus.current.Mode() == NormalBackup { + if consensus.isBackup { canHandleViewChange = false intendedForLeader = false } diff --git a/consensus/enums.go b/consensus/enums.go index a8c4357c2..41eafba86 100644 --- a/consensus/enums.go +++ b/consensus/enums.go @@ -14,8 +14,6 @@ const ( Syncing // Listening .. Listening - // NormalBackup Backup Node .. - NormalBackup ) // FBFTPhase : different phases of consensus @@ -34,7 +32,6 @@ var ( ViewChanging: "ViewChanging", Syncing: "Syncing", Listening: "Listening", - NormalBackup: "NormalBackup", } phaseNames = map[FBFTPhase]string{ FBFTAnnounce: "Announce", diff --git a/consensus/validator.go b/consensus/validator.go index fa5cdac92..2f14f76b4 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -133,7 +133,7 @@ func (consensus *Consensus) validateNewBlock(recvMsg *FBFTMessage) (*types.Block } func (consensus *Consensus) prepare() { - if consensus.IsBackup() { + if consensus.isBackup { return } @@ -152,7 +152,7 @@ func (consensus *Consensus) prepare() { // sendCommitMessages send out commit messages to leader func (consensus *Consensus) sendCommitMessages(blockObj *types.Block) { - if consensus.IsBackup() || blockObj == nil { + if consensus.isBackup || blockObj == nil { return } diff --git a/consensus/view_change.go b/consensus/view_change.go index 1171b073e..f55c5400f 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -33,8 +33,6 @@ type State struct { // view changing id is used during view change mode // it is the next view id viewChangingID uint64 - - isBackup bool } // Mode return the current node mode @@ -44,10 +42,6 @@ func (pm *State) Mode() Mode { // SetMode set the node mode as required func (pm *State) SetMode(s Mode) { - if s == Normal && pm.isBackup { - s = NormalBackup - } - pm.mode = s } @@ -81,10 +75,6 @@ func (pm *State) GetViewChangeDuraion() time.Duration { return time.Duration(diff * diff * int64(viewChangeDuration)) } -func (pm *State) SetIsBackup(isBackup bool) { - pm.isBackup = isBackup -} - // 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) {