github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/inmem/dynamic_protocol_state_test.go (about)

     1  package inmem_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/state/protocol/inmem"
    10  	"github.com/onflow/flow-go/state/protocol/mock"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  // TestDynamicProtocolStateAdapter tests if the DynamicProtocolStateAdapter returns expected values when created
    15  // using constructor passing a RichProtocolStateEntry.
    16  func TestDynamicProtocolStateAdapter(t *testing.T) {
    17  	// construct a valid protocol state entry that has semantically correct DKGParticipantKeys
    18  	entry := unittest.EpochStateFixture(unittest.WithValidDKG())
    19  
    20  	globalParams := mock.NewGlobalParams(t)
    21  	adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    22  
    23  	t.Run("identities", func(t *testing.T) {
    24  		assert.Equal(t, entry.CurrentEpochIdentityTable, adapter.Identities())
    25  	})
    26  	t.Run("global-params", func(t *testing.T) {
    27  		expectedChainID := flow.Testnet
    28  		globalParams.On("ChainID").Return(expectedChainID, nil).Once()
    29  		actualChainID := adapter.GlobalParams().ChainID()
    30  		assert.Equal(t, expectedChainID, actualChainID)
    31  	})
    32  	t.Run("epoch-phase-staking", func(t *testing.T) {
    33  		entry := unittest.EpochStateFixture()
    34  		adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    35  		assert.Equal(t, flow.EpochPhaseStaking, adapter.EpochPhase())
    36  		assert.True(t, adapter.PreviousEpochExists())
    37  		assert.False(t, adapter.InvalidEpochTransitionAttempted())
    38  	})
    39  	t.Run("epoch-phase-setup", func(t *testing.T) {
    40  		entry := unittest.EpochStateFixture(unittest.WithNextEpochProtocolState())
    41  		// cleanup the commit event, so we are in setup phase
    42  		entry.NextEpoch.CommitID = flow.ZeroID
    43  
    44  		adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    45  		assert.Equal(t, flow.EpochPhaseSetup, adapter.EpochPhase())
    46  		assert.True(t, adapter.PreviousEpochExists())
    47  		assert.False(t, adapter.InvalidEpochTransitionAttempted())
    48  	})
    49  	t.Run("epoch-phase-commit", func(t *testing.T) {
    50  		entry := unittest.EpochStateFixture(unittest.WithNextEpochProtocolState())
    51  		adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    52  		assert.Equal(t, flow.EpochPhaseCommitted, adapter.EpochPhase())
    53  		assert.True(t, adapter.PreviousEpochExists())
    54  		assert.False(t, adapter.InvalidEpochTransitionAttempted())
    55  	})
    56  	t.Run("invalid-state-transition-attempted", func(t *testing.T) {
    57  		entry := unittest.EpochStateFixture(func(entry *flow.RichProtocolStateEntry) {
    58  			entry.InvalidEpochTransitionAttempted = true
    59  		})
    60  		adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    61  		assert.True(t, adapter.InvalidEpochTransitionAttempted())
    62  	})
    63  	t.Run("no-previous-epoch", func(t *testing.T) {
    64  		entry := unittest.EpochStateFixture(func(entry *flow.RichProtocolStateEntry) {
    65  			entry.PreviousEpoch = nil
    66  			entry.PreviousEpochSetup = nil
    67  			entry.PreviousEpochCommit = nil
    68  		})
    69  		adapter := inmem.NewDynamicProtocolStateAdapter(entry, globalParams)
    70  		assert.False(t, adapter.PreviousEpochExists())
    71  	})
    72  }