github.com/koko1123/flow-go-1@v0.29.6/module/epochs/epoch_lookup_test.go (about)

     1  package epochs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	mockprotocol "github.com/koko1123/flow-go-1/state/protocol/mock"
     9  	"github.com/koko1123/flow-go-1/utils/unittest/mocks"
    10  )
    11  
    12  func TestEpochForView(t *testing.T) {
    13  	// epoch 100: 0    -> 999
    14  	// epoch 101: 1000 -> 1999
    15  	// epoch 102: 2000 -> 2999
    16  	epochs := []*mockprotocol.Epoch{}
    17  	for i := 0; i < 3; i++ {
    18  		firstView := i * 1000
    19  		lastView := i*1000 + 999
    20  		counter := 100 + i
    21  		epoch := new(mockprotocol.Epoch)
    22  		epoch.On("FirstView").Return(uint64(firstView), nil)
    23  		epoch.On("FinalView").Return(uint64(lastView), nil)
    24  		epoch.On("Counter").Return(uint64(counter), nil)
    25  		// return nil error to indicate a committed epoch
    26  		epoch.On("DKG").Return(nil, nil)
    27  		epochs = append(epochs, epoch)
    28  	}
    29  
    30  	epochQuery := mocks.NewEpochQuery(t, 101)
    31  	for _, e := range epochs {
    32  		epochQuery.Add(e)
    33  	}
    34  
    35  	snapshot := new(mockprotocol.Snapshot)
    36  	snapshot.On("Epochs").Return(epochQuery)
    37  
    38  	state := new(mockprotocol.State)
    39  	state.On("Final").Return(snapshot)
    40  
    41  	lookup := NewEpochLookup(state)
    42  
    43  	type testCase struct {
    44  		view  uint64
    45  		epoch uint64
    46  	}
    47  
    48  	testCases := []testCase{
    49  		{view: 0, epoch: 100},
    50  		{view: 500, epoch: 100},
    51  		{view: 999, epoch: 100},
    52  		{view: 1000, epoch: 101},
    53  		{view: 1500, epoch: 101},
    54  		{view: 1999, epoch: 101},
    55  		{view: 2000, epoch: 102},
    56  		{view: 2500, epoch: 102},
    57  		{view: 2999, epoch: 102},
    58  	}
    59  
    60  	for _, tc := range testCases {
    61  		epoch, err := lookup.EpochForView(tc.view)
    62  		require.NoError(t, err)
    63  		require.Equal(t, tc.epoch, epoch)
    64  		epoch, err = lookup.EpochForViewWithFallback(tc.view)
    65  		require.NoError(t, err)
    66  		require.Equal(t, tc.epoch, epoch)
    67  	}
    68  }