github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/block_info_test.go (about) 1 package environment_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/cmd/util/ledger/util" 10 "github.com/onflow/flow-go/fvm/environment" 11 "github.com/onflow/flow-go/fvm/tracing" 12 "github.com/onflow/flow-go/model/flow" 13 storageErr "github.com/onflow/flow-go/storage" 14 "github.com/onflow/flow-go/utils/unittest" 15 ) 16 17 func TestBlockInfo(t *testing.T) { 18 tracer := tracing.NewMockTracerSpan() 19 meter := &util.NopMeter{} 20 blocks := &mockBlocks{ 21 blocks: make(map[uint64]*flow.Header), 22 } 23 height := uint64(flow.DefaultTransactionExpiry) 24 header := unittest.BlockHeaderWithHeight(height) 25 26 bi := environment.NewBlockInfo(tracer, meter, header, blocks) 27 28 // verify the current block exists 29 blocks.Add(header) 30 b, exists, err := bi.GetBlockAtHeight(height) 31 require.NoError(t, err) 32 require.True(t, exists) 33 require.Equal(t, header.Height, b.Height) 34 35 // verify blocks that do not exist 36 b, exists, err = bi.GetBlockAtHeight(height + 1) 37 require.NoError(t, err) 38 require.False(t, exists) 39 40 // verify that the block at the height before the lowest accepted height exists 41 lowestAcceptedHeight := height - flow.DefaultTransactionExpiry 42 lowestHeader := unittest.BlockHeaderWithHeight(lowestAcceptedHeight) 43 blocks.Add(lowestHeader) 44 b, exists, err = bi.GetBlockAtHeight(lowestAcceptedHeight) 45 require.NoError(t, err) 46 require.True(t, exists) 47 require.Equal(t, lowestHeader.Height, b.Height) 48 49 // verify that the block at the height before the lowest accepted height does not exist 50 _, exists, err = bi.GetBlockAtHeight(lowestAcceptedHeight - 1) 51 require.NoError(t, err) 52 require.False(t, exists) 53 } 54 55 type mockBlocks struct { 56 blocks map[uint64]*flow.Header 57 } 58 59 func (m *mockBlocks) ByHeightFrom(height uint64, header *flow.Header) (*flow.Header, error) { 60 h, ok := m.blocks[height] 61 if !ok { 62 return nil, fmt.Errorf("block does not exist: %w", storageErr.ErrNotFound) 63 } 64 return h, nil 65 } 66 67 func (m *mockBlocks) Add(h *flow.Header) { 68 m.blocks[h.Height] = h 69 }