diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 4cb843743..6a6da898e 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -139,7 +139,7 @@ func (consensus *Consensus) UpdateBitmaps() { // ResetState resets the state of the consensus func (consensus *Consensus) ResetState() { - consensus.switchPhase("ResetState", FBFTAnnounce, true) + consensus.switchPhase("ResetState", FBFTAnnounce) consensus.blockHash = [32]byte{} consensus.block = []byte{} consensus.Decider.ResetPrepareAndCommitVotes() @@ -494,31 +494,14 @@ func (consensus *Consensus) GetFinality() int64 { } // switchPhase will switch FBFTPhase to nextPhase if the desirePhase equals the nextPhase -// it can be override to force transition -func (consensus *Consensus) switchPhase(subject string, desired FBFTPhase, override bool) { +func (consensus *Consensus) switchPhase(subject string, desired FBFTPhase) { consensus.getLogger().Info(). Str("from:", consensus.phase.String()). Str("to:", desired.String()). - Bool("override:", override). - Msg(subject) + Str("switchPhase:", subject) - if override { - consensus.phase = desired - return - } - - var nextPhase FBFTPhase - switch consensus.phase { - case FBFTAnnounce: - nextPhase = FBFTPrepare - case FBFTPrepare: - nextPhase = FBFTCommit - case FBFTCommit: - nextPhase = FBFTAnnounce - } - if nextPhase == desired { - consensus.phase = nextPhase - } + consensus.phase = desired + return } // getLogger returns logger for consensus contexts added diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 1d6e50df4..10f52a16a 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -306,7 +306,7 @@ func (consensus *Consensus) tryCatchup() { break } if currentBlockNum < consensus.blockNum { - consensus.switchPhase("TryCatchup", FBFTAnnounce, true) + consensus.switchPhase("TryCatchup", FBFTAnnounce) } // catup up and skip from view change trap if currentBlockNum < consensus.blockNum && diff --git a/consensus/leader.go b/consensus/leader.go index f08cdfa57..3e787835f 100644 --- a/consensus/leader.go +++ b/consensus/leader.go @@ -92,7 +92,7 @@ func (consensus *Consensus) announce(block *types.Block) { Msg("[Announce] Sent Announce Message!!") } - consensus.switchPhase("Announce", FBFTPrepare, true) + consensus.switchPhase("Announce", FBFTPrepare) } func (consensus *Consensus) onPrepare(msg *msg_pb.Message) { @@ -182,7 +182,7 @@ func (consensus *Consensus) onPrepare(msg *msg_pb.Message) { if err := consensus.didReachPrepareQuorum(); err != nil { return } - consensus.switchPhase("onPrepare", FBFTCommit, true) + consensus.switchPhase("onPrepare", FBFTCommit) } //// Read - End } diff --git a/consensus/validator.go b/consensus/validator.go index 8ba9e56fb..d57bd765f 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -86,7 +86,7 @@ func (consensus *Consensus) prepare() { } } } - consensus.switchPhase("Announce", FBFTPrepare, true) + consensus.switchPhase("Announce", FBFTPrepare) } // if onPrepared accepts the prepared message from the leader, then @@ -234,7 +234,7 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) { } } } - consensus.switchPhase("onPrepared", FBFTCommit, true) + consensus.switchPhase("onPrepared", FBFTCommit) } func (consensus *Consensus) onCommitted(msg *msg_pb.Message) { diff --git a/consensus/view_change.go b/consensus/view_change.go index e34a5ac98..6896e49ca 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -253,7 +253,7 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) { consensus.ReadySignal <- struct{}{} }() } else { - consensus.switchPhase("onViewChange", FBFTCommit, true) + consensus.switchPhase("onViewChange", FBFTCommit) payload := consensus.vc.GetM1Payload() copy(consensus.blockHash[:], payload[:32]) aggSig, mask, err := consensus.ReadSignatureBitmapPayload(payload, 32) @@ -437,7 +437,7 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) { p2p.ConstructMessage(msgToSend), ) } - consensus.switchPhase("onNewView", FBFTCommit, true) + consensus.switchPhase("onNewView", FBFTCommit) } else { consensus.ResetState() consensus.getLogger().Info().Msg("onNewView === announce") diff --git a/consensus/view_change_test.go b/consensus/view_change_test.go index 62446d225..2e0b6319b 100644 --- a/consensus/view_change_test.go +++ b/consensus/view_change_test.go @@ -45,8 +45,6 @@ func TestPhaseSwitching(t *testing.T) { assert.Equal(t, FBFTAnnounce, consensus.phase) // It's a new consensus, we should be at the FBFTAnnounce phase - override := false - switches := []phaseSwitch{ {start: FBFTAnnounce, end: FBFTPrepare}, {start: FBFTPrepare, end: FBFTCommit}, @@ -54,13 +52,11 @@ func TestPhaseSwitching(t *testing.T) { } for _, sw := range switches { - testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end, override) + testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end) } - override = true - for _, sw := range switches { - testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end, override) + testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end) } switches = []phaseSwitch{ @@ -70,39 +66,19 @@ func TestPhaseSwitching(t *testing.T) { } for _, sw := range switches { - testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end, override) + testPhaseGroupSwitching(t, consensus, phases, sw.start, sw.end) } } -func testPhaseGroupSwitching(t *testing.T, consensus *Consensus, phases []FBFTPhase, startPhase FBFTPhase, desiredPhase FBFTPhase, override bool) { - phaseMapping := make(map[FBFTPhase]bool) - - if override { - for range phases { - consensus.switchPhase("test", desiredPhase, override) - assert.Equal(t, desiredPhase, consensus.phase) - } - +func testPhaseGroupSwitching(t *testing.T, consensus *Consensus, phases []FBFTPhase, startPhase FBFTPhase, desiredPhase FBFTPhase) { + for range phases { + consensus.switchPhase("test", desiredPhase) assert.Equal(t, desiredPhase, consensus.phase) - - return - } - - phaseMapping[FBFTAnnounce] = false - phaseMapping[FBFTPrepare] = false - phaseMapping[FBFTCommit] = false - phaseMapping[startPhase] = false - phaseMapping[desiredPhase] = true - - assert.Equal(t, startPhase, consensus.phase) - - for _, phase := range phases { - consensus.switchPhase("test", desiredPhase, override) - expected := phaseMapping[phase] - assert.Equal(t, expected, (phase == consensus.phase)) } assert.Equal(t, desiredPhase, consensus.phase) + + return } func TestGetNextLeaderKeyShouldFailForStandardGeneratedConsensus(t *testing.T) {