Skip to content

Commit

Permalink
Merge pull request #5853 from onflow/bastian/5852-fix-getblock
Browse files Browse the repository at this point in the history
Do not return an error when block is unavailable due to expiry
  • Loading branch information
zhangchiqing authored May 7, 2024
2 parents 65ca341 + 3ba3a7e commit 2828e97
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fvm/environment/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (info *blockInfo) GetBlockAtHeight(
}

if height+uint64(flow.DefaultTransactionExpiry) < info.blockHeader.Height {
return runtime.Block{}, false, errors.NewBlockHeightOutOfRangeError(height)
return runtime.Block{}, false, nil
}

header, err := info.blocks.ByHeightFrom(height, info.blockHeader)
Expand Down
69 changes: 69 additions & 0 deletions fvm/environment/block_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package environment_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
storageErr "github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/utils/unittest"
)

func TestBlockInfo(t *testing.T) {
tracer := tracing.NewMockTracerSpan()
meter := &util.NopMeter{}
blocks := &mockBlocks{
blocks: make(map[uint64]*flow.Header),
}
height := uint64(flow.DefaultTransactionExpiry)
header := unittest.BlockHeaderWithHeight(height)

bi := environment.NewBlockInfo(tracer, meter, header, blocks)

// verify the current block exists
blocks.Add(header)
b, exists, err := bi.GetBlockAtHeight(height)
require.NoError(t, err)
require.True(t, exists)
require.Equal(t, header.Height, b.Height)

// verify blocks that do not exist
b, exists, err = bi.GetBlockAtHeight(height + 1)
require.NoError(t, err)
require.False(t, exists)

// verify that the block at the height before the lowest accepted height exists
lowestAcceptedHeight := height - flow.DefaultTransactionExpiry
lowestHeader := unittest.BlockHeaderWithHeight(lowestAcceptedHeight)
blocks.Add(lowestHeader)
b, exists, err = bi.GetBlockAtHeight(lowestAcceptedHeight)
require.NoError(t, err)
require.True(t, exists)
require.Equal(t, lowestHeader.Height, b.Height)

// verify that the block at the height before the lowest accepted height does not exist
_, exists, err = bi.GetBlockAtHeight(lowestAcceptedHeight - 1)
require.NoError(t, err)
require.False(t, exists)
}

type mockBlocks struct {
blocks map[uint64]*flow.Header
}

func (m *mockBlocks) ByHeightFrom(height uint64, header *flow.Header) (*flow.Header, error) {
h, ok := m.blocks[height]
if !ok {
return nil, fmt.Errorf("block does not exist: %w", storageErr.ErrNotFound)
}
return h, nil
}

func (m *mockBlocks) Add(h *flow.Header) {
m.blocks[h.Height] = h
}
4 changes: 4 additions & 0 deletions utils/unittest/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ func BlockHeaderWithParentFixture(parent *flow.Header) *flow.Header {
}
}

func BlockHeaderWithHeight(height uint64) *flow.Header {
return BlockHeaderFixture(WithHeaderHeight(height))
}

func BlockHeaderWithParentWithSoRFixture(parent *flow.Header, source []byte) *flow.Header {
height := parent.Height + 1
view := parent.View + 1 + uint64(rand.Intn(10)) // Intn returns [0, n)
Expand Down

0 comments on commit 2828e97

Please sign in to comment.