github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/protocol_state/epochs/fallback_statemachine_test.go (about) 1 package epochs 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 "github.com/stretchr/testify/suite" 8 9 "github.com/onflow/flow-go/utils/unittest" 10 ) 11 12 func TestEpochFallbackStateMachine(t *testing.T) { 13 suite.Run(t, new(EpochFallbackStateMachineSuite)) 14 } 15 16 // ProtocolStateMachineSuite is a dedicated test suite for testing happy path state machine. 17 type EpochFallbackStateMachineSuite struct { 18 BaseStateMachineSuite 19 stateMachine *FallbackStateMachine 20 } 21 22 func (s *EpochFallbackStateMachineSuite) SetupTest() { 23 s.BaseStateMachineSuite.SetupTest() 24 s.parentProtocolState.InvalidEpochTransitionAttempted = true 25 s.stateMachine = NewFallbackStateMachine(s.candidate.View, s.parentProtocolState.Copy()) 26 } 27 28 // ProcessEpochSetupIsNoop ensures that processing epoch setup event is noop. 29 func (s *EpochFallbackStateMachineSuite) TestProcessEpochSetupIsNoop() { 30 setup := unittest.EpochSetupFixture() 31 applied, err := s.stateMachine.ProcessEpochSetup(setup) 32 require.NoError(s.T(), err) 33 require.False(s.T(), applied) 34 updatedState, stateID, hasChanges := s.stateMachine.Build() 35 require.False(s.T(), hasChanges) 36 require.Equal(s.T(), s.parentProtocolState.ID(), updatedState.ID()) 37 require.Equal(s.T(), updatedState.ID(), stateID) 38 require.Equal(s.T(), s.parentProtocolState.ID(), s.stateMachine.ParentState().ID()) 39 } 40 41 // ProcessEpochCommitIsNoop ensures that processing epoch commit event is noop. 42 func (s *EpochFallbackStateMachineSuite) TestProcessEpochCommitIsNoop() { 43 commit := unittest.EpochCommitFixture() 44 applied, err := s.stateMachine.ProcessEpochCommit(commit) 45 require.NoError(s.T(), err) 46 require.False(s.T(), applied) 47 updatedState, stateID, hasChanges := s.stateMachine.Build() 48 require.False(s.T(), hasChanges) 49 require.Equal(s.T(), s.parentProtocolState.ID(), updatedState.ID()) 50 require.Equal(s.T(), updatedState.ID(), stateID) 51 require.Equal(s.T(), s.parentProtocolState.ID(), s.stateMachine.ParentState().ID()) 52 } 53 54 // TestTransitionToNextEpoch ensures that transition to next epoch is not possible. 55 func (s *EpochFallbackStateMachineSuite) TestTransitionToNextEpoch() { 56 err := s.stateMachine.TransitionToNextEpoch() 57 require.NoError(s.T(), err) 58 updatedState, updateStateID, hasChanges := s.stateMachine.Build() 59 require.False(s.T(), hasChanges) 60 require.Equal(s.T(), updatedState.ID(), updateStateID) 61 require.Equal(s.T(), s.parentProtocolState.ID(), updateStateID) 62 } 63 64 // TestNewEpochFallbackStateMachine tests that creating epoch fallback state machine sets 65 // `InvalidEpochTransitionAttempted` to true to record that we have entered epoch fallback mode[EFM]. 66 func (s *EpochFallbackStateMachineSuite) TestNewEpochFallbackStateMachine() { 67 s.parentProtocolState.InvalidEpochTransitionAttempted = false 68 s.stateMachine = NewFallbackStateMachine(s.candidate.View, s.parentProtocolState.Copy()) 69 require.Equal(s.T(), s.parentProtocolState.ID(), s.stateMachine.ParentState().ID()) 70 require.Equal(s.T(), s.candidate.View, s.stateMachine.View()) 71 72 updatedState, stateID, hasChanges := s.stateMachine.Build() 73 require.True(s.T(), hasChanges, "InvalidEpochTransitionAttempted has to be updated") 74 require.True(s.T(), updatedState.InvalidEpochTransitionAttempted, "InvalidEpochTransitionAttempted has to be set") 75 require.Equal(s.T(), updatedState.ID(), stateID) 76 require.NotEqual(s.T(), s.parentProtocolState.ID(), stateID) 77 }