github.com/koko1123/flow-go-1@v0.29.6/fvm/environment/blocks_test.go (about)

     1  package environment
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/storage"
    10  	storageMock "github.com/koko1123/flow-go-1/storage/mock"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  func doTest(t *testing.T, f func(*testing.T, *storageMock.Headers, Blocks, *flow.Header)) func(*testing.T) {
    15  	return func(t *testing.T) {
    16  
    17  		headers := new(storageMock.Headers)
    18  		blockFinder := NewBlockFinder(headers)
    19  		header := unittest.BlockHeaderFixture()
    20  		header.Height = 10
    21  
    22  		f(t, headers, blockFinder, header)
    23  
    24  		headers.AssertExpectations(t)
    25  	}
    26  }
    27  
    28  func Test_BlockFinder_ReturnsHeaderIfSameHeight(t *testing.T) {
    29  
    30  	t.Run("returns header is height is the same", doTest(t, func(t *testing.T, headers *storageMock.Headers, blockFinder Blocks, header *flow.Header) {
    31  		heightFrom, err := blockFinder.ByHeightFrom(10, header)
    32  		require.NoError(t, err)
    33  		require.Equal(t, *header, *heightFrom)
    34  	}))
    35  
    36  	t.Run("nil header defaults to ByHeight", doTest(t, func(t *testing.T, headers *storageMock.Headers, blockFinder Blocks, header *flow.Header) {
    37  
    38  		headers.On("ByHeight", uint64(10)).Return(header, nil)
    39  
    40  		heightFrom, err := blockFinder.ByHeightFrom(10, nil)
    41  		require.NoError(t, err)
    42  		require.Equal(t, *header, *heightFrom)
    43  	}))
    44  
    45  	t.Run("follows blocks chain", doTest(t, func(t *testing.T, headers *storageMock.Headers, blockFinder Blocks, header *flow.Header) {
    46  
    47  		header0 := unittest.BlockHeaderFixture()
    48  		header0.Height = 0
    49  		header1 := unittest.BlockHeaderWithParentFixture(header0)
    50  		header2 := unittest.BlockHeaderWithParentFixture(header1)
    51  		header3 := unittest.BlockHeaderWithParentFixture(header2)
    52  		header4 := unittest.BlockHeaderWithParentFixture(header3)
    53  		header5 := unittest.BlockHeaderWithParentFixture(header4)
    54  
    55  		headers.On("ByBlockID", header4.ID()).Return(header4, nil)
    56  		headers.On("ByHeight", uint64(4)).Return(nil, storage.ErrNotFound)
    57  		headers.On("ByBlockID", header3.ID()).Return(header3, nil)
    58  		headers.On("ByHeight", uint64(3)).Return(nil, storage.ErrNotFound)
    59  		headers.On("ByBlockID", header2.ID()).Return(header2, nil)
    60  		headers.On("ByHeight", uint64(2)).Return(nil, storage.ErrNotFound)
    61  		headers.On("ByBlockID", header1.ID()).Return(header1, nil)
    62  		headers.On("ByHeight", uint64(1)).Return(nil, storage.ErrNotFound)
    63  		headers.On("ByBlockID", header0.ID()).Return(header0, nil)
    64  		//headers.On("ByHeight", uint64(0)).Return(nil, storage.ErrNotFound)
    65  
    66  		heightFrom, err := blockFinder.ByHeightFrom(0, header5)
    67  		require.NoError(t, err)
    68  		require.Equal(t, *header0, *heightFrom)
    69  	}))
    70  
    71  	t.Run("skips heights once it get to finalized chain", doTest(t, func(t *testing.T, headers *storageMock.Headers, blockFinder Blocks, header *flow.Header) {
    72  
    73  		header0 := unittest.BlockHeaderFixture()
    74  		header0.Height = 0
    75  		header1 := unittest.BlockHeaderWithParentFixture(header0)
    76  		header2 := unittest.BlockHeaderWithParentFixture(header1)
    77  		header3 := unittest.BlockHeaderWithParentFixture(header2)
    78  		header4 := unittest.BlockHeaderWithParentFixture(header3)
    79  		header5 := unittest.BlockHeaderWithParentFixture(header4)
    80  
    81  		headers.On("ByBlockID", header4.ID()).Return(header4, nil)
    82  		headers.On("ByHeight", uint64(4)).Return(nil, storage.ErrNotFound)
    83  		headers.On("ByBlockID", header3.ID()).Return(header3, nil)
    84  		headers.On("ByHeight", uint64(3)).Return(header3, nil)
    85  		headers.On("ByHeight", uint64(0)).Return(header0, nil)
    86  
    87  		heightFrom, err := blockFinder.ByHeightFrom(0, header5)
    88  		require.NoError(t, err)
    89  		require.Equal(t, *header0, *heightFrom)
    90  	}))
    91  
    92  }