Skip to content

Commit

Permalink
Got rid of redundant logic with isBackup. (#4639)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen authored Mar 7, 2024
1 parent 56ad3fa commit 8d2b36d
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 21 deletions.
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions consensus/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const (
Syncing
// Listening ..
Listening
// NormalBackup Backup Node ..
NormalBackup
)

// FBFTPhase : different phases of consensus
Expand All @@ -34,7 +32,6 @@ var (
ViewChanging: "ViewChanging",
Syncing: "Syncing",
Listening: "Listening",
NormalBackup: "NormalBackup",
}
phaseNames = map[FBFTPhase]string{
FBFTAnnounce: "Announce",
Expand Down
4 changes: 2 additions & 2 deletions consensus/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (consensus *Consensus) validateNewBlock(recvMsg *FBFTMessage) (*types.Block
}

func (consensus *Consensus) prepare() {
if consensus.IsBackup() {
if consensus.isBackup {
return
}

Expand All @@ -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
}

Expand Down
10 changes: 0 additions & 10 deletions consensus/view_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8d2b36d

Please sign in to comment.