-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5853 from onflow/bastian/5852-fix-getblock
Do not return an error when block is unavailable due to expiry
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters