@@ -377,8 +377,16 @@ func (consensus *Consensus) StartChannel() {
377377func (consensus * Consensus ) syncReadyChan (reason string ) {
378378 consensus .getLogger ().Info ().Msgf ("[ConsensusMainLoop] syncReadyChan %s" , reason )
379379 if consensus .getBlockNum () < consensus .Blockchain ().CurrentHeader ().Number ().Uint64 ()+ 1 {
380- consensus .setBlockNum (consensus .Blockchain ().CurrentHeader ().Number ().Uint64 () + 1 )
381- consensus .setViewIDs (consensus .Blockchain ().CurrentHeader ().ViewID ().Uint64 () + 1 )
380+ currentHeader := consensus .Blockchain ().CurrentHeader ()
381+ nextViewID , err := checkedNextViewID (currentHeader .ViewID ().Uint64 ())
382+ if err != nil {
383+ consensus .getLogger ().Error ().Err (err ).
384+ Uint64 ("headViewID" , currentHeader .ViewID ().Uint64 ()).
385+ Msg ("[syncReadyChan] refusing to reset to an exhausted ViewID" )
386+ return
387+ }
388+ consensus .setBlockNum (currentHeader .Number ().Uint64 () + 1 )
389+ consensus .setViewIDs (nextViewID )
382390 mode := consensus .updateConsensusInformation (reason )
383391 consensus .current .SetMode (mode )
384392 consensus .getLogger ().Info ().Msg ("[syncReadyChan] Start consensus timer" )
@@ -560,6 +568,11 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
560568 if blk == nil {
561569 return errors .New ("block to pre-commit is nil" )
562570 }
571+ if consensus .current .GetViewIDFloor () > 0 {
572+ if err := consensus .validateEmergencyRecoveryMessageBlockViewID (blk , consensus .getCurBlockViewID ()); err != nil {
573+ return err
574+ }
575+ }
563576
564577 leaderPriKey , err := consensus .getConsensusLeaderPrivateKey ()
565578 if err != nil {
@@ -579,13 +592,19 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
579592 network .Bytes ,
580593 network .FBFTMsg
581594 bareMinimumCommit := FBFTMsg .Payload
582- consensus .fBFTLog .AddVerifiedMessage (FBFTMsg )
583595
584596 if err := consensus .verifyLastCommitSig (bareMinimumCommit , blk ); err != nil {
585597 return errors .Wrap (err , "[preCommitAndPropose] failed verifying last commit sig" )
586598 }
599+ consensus .fBFTLog .AddVerifiedMessage (FBFTMsg )
587600
588601 go func () {
602+ if consensus .current .GetViewIDFloor () > 0 {
603+ if err := consensus .validateEmergencyRecoveryMessageBlockViewID (blk , FBFTMsg .ViewID ); err != nil {
604+ consensus .GetLogger ().Error ().Err (err ).Msg ("[preCommitAndPropose] unsafe recovery ViewID" )
605+ return
606+ }
607+ }
589608 blk .SetCurrentCommitSig (bareMinimumCommit )
590609
591610 // Send committed message to validators since 2/3 commit is already collected
@@ -675,6 +694,13 @@ func (consensus *Consensus) tryCatchup() error {
675694 if blk == nil {
676695 return nil
677696 }
697+ if msg == nil {
698+ return errors .New ("[TryCatchup] committed message is missing" )
699+ }
700+ if err := consensus .validateEmergencyRecoveryMessageBlockViewID (blk , msg .ViewID ); err != nil {
701+ consensus .getLogger ().Error ().Err (err ).Msg ("[TryCatchup] unsafe recovery ViewID" )
702+ return err
703+ }
678704 blk .SetCurrentCommitSig (msg .Payload )
679705
680706 if err := consensus .verifyBlock (blk ); err != nil {
@@ -700,6 +726,12 @@ func (consensus *Consensus) tryCatchup() error {
700726}
701727
702728func (consensus * Consensus ) commitBlock (blk * types.Block , committedMsg * FBFTMessage ) error {
729+ if committedMsg == nil {
730+ return errors .New ("committed message is missing" )
731+ }
732+ if err := consensus .validateEmergencyRecoveryMessageBlockViewID (blk , committedMsg .ViewID ); err != nil {
733+ return err
734+ }
703735 if consensus .Blockchain ().CurrentBlock ().NumberU64 () < blk .NumberU64 () {
704736 _ , err := consensus .Blockchain ().InsertChain ([]* types.Block {blk }, ! consensus .fBFTLog .IsBlockVerified (blk .Hash ()))
705737 if err != nil && ! errors .Is (err , core .ErrKnownBlock ) {
@@ -715,7 +747,9 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess
715747
716748 consensus .FinishFinalityCount ()
717749 consensus .postConsensusProcessing (blk )
718- consensus .setupForNewConsensus (blk , committedMsg )
750+ if err := consensus .setupForNewConsensus (blk , committedMsg ); err != nil {
751+ return err
752+ }
719753 consensus .getLogger ().Info ().Uint64 ("blockNum" , blk .NumberU64 ()).
720754 Str ("hash" , blk .Header ().Hash ().Hex ()).
721755 Msg ("Added New Block to Blockchain!!!" )
@@ -840,9 +874,13 @@ func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicK
840874}
841875
842876// SetupForNewConsensus sets the state for new consensus
843- func (consensus * Consensus ) setupForNewConsensus (blk * types.Block , committedMsg * FBFTMessage ) {
877+ func (consensus * Consensus ) setupForNewConsensus (blk * types.Block , committedMsg * FBFTMessage ) error {
878+ nextViewID , err := checkedNextViewID (committedMsg .ViewID )
879+ if err != nil {
880+ return errors .Wrap (err , "cannot advance consensus ViewID" )
881+ }
844882 consensus .setBlockNum (blk .NumberU64 () + 1 )
845- consensus .setCurBlockViewID (committedMsg . ViewID + 1 )
883+ consensus .setCurBlockViewID (nextViewID )
846884 var epoch * big.Int
847885 if blk .IsLastBlockInEpoch () {
848886 epoch = new (big.Int ).Add (blk .Epoch (), common .Big1 )
@@ -897,6 +935,7 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
897935 consensus .fBFTLog .PruneCacheBeforeBlock (blk .NumberU64 ())
898936 consensus .resetState ()
899937 consensus .sendLastSignPower ()
938+ return nil
900939}
901940
902941func (consensus * Consensus ) postCatchup (initBN uint64 ) {
0 commit comments