[core] simplify switchPhase as it is always called in override mode

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3379/head
Leo Chen 4 years ago
parent 5d7693d318
commit 5d4cdbdd06
  1. 27
      consensus/consensus_service.go
  2. 2
      consensus/consensus_v2.go
  3. 4
      consensus/leader.go
  4. 4
      consensus/validator.go
  5. 4
      consensus/view_change.go
  6. 40
      consensus/view_change_test.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

@ -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 &&

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

@ -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) {

@ -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")

@ -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) {

Loading…
Cancel
Save