Skip to content

Commit

Permalink
Add Test_FetchBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Apr 24, 2024
1 parent f199234 commit 738716d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ func (lp *logPoller) fetchBlocks(ctx context.Context, blocksRequested []string,
blockRequested := r.Args[0].(string)
if blockRequested != string(latestBlock) && block.Number > latestFinalizedBlockNumber {
return nil, fmt.Errorf(
"Received unfinalized block %d while expecting finalized block (latestFinaliezdBlockNumber = %d)",
"Received unfinalized block %d while expecting finalized block (latestFinalizedBlockNumber = %d)",
block.Number, latestFinalizedBlockNumber)
}

Expand Down
72 changes: 72 additions & 0 deletions core/chains/evm/logpoller/log_poller_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logpoller

import (
"context"
"errors"
"fmt"
"math/big"
"reflect"
Expand Down Expand Up @@ -598,6 +599,77 @@ func Test_latestBlockAndFinalityDepth(t *testing.T) {
})
}

func Test_FetchBlocks(t *testing.T) {
lggr := logger.Test(t)
chainID := testutils.FixtureChainID
db := pgtest.NewSqlxDB(t)
orm := NewORM(chainID, db, lggr)
ctx := testutils.Context(t)

lpOpts := Opts{
PollPeriod: time.Hour,
BackfillBatchSize: 2,
RpcBatchSize: 2,
KeepFinalizedBlocksDepth: 50,
FinalityDepth: 3,
}

ec := evmclimocks.NewClient(t)
mockBatchCallContext(t, ec) // This will return 5 for "finalized" and 8 for "latest"

cases := []struct {
name string
blocksRequested []string
expectedErr error
}{{
"successful validation including finalized and latest",
[]string{"0x3", "latest", "0x5", "finalized", "0x1"},
nil,
}, {
"successful validation with all block numbers",
[]string{"0x2", "0x5", "0x3", "0x4"},
nil,
}, {
"finality violation including finalized and latest",
[]string{"0x8", "0x2", "latest", "finalized"},
errors.New("Received unfinalized block 8 while expecting finalized block (latestFinalizedBlockNumber = 5)"),
}, {
"finality violation with all block numbers",
[]string{"0x9", "0x2", "finalized", "latest"},
errors.New("Received unfinalized block 9 while expecting finalized block (latestFinalizedBlockNumber = 5)"),
}}

lp := NewLogPoller(orm, ec, lggr, lpOpts)
for _, tc := range cases {
for _, lp.useFinalityTag = range []bool{false, true} {
blockValidationReq := latestBlock
if lp.useFinalityTag {
blockValidationReq = finalizedBlock
}
t.Run(fmt.Sprintf("%s where useFinalityTag=%t", tc.name, lp.useFinalityTag), func(t *testing.T) {
blocks, err := lp.fetchBlocks(ctx, tc.blocksRequested, blockValidationReq)
if tc.expectedErr != nil {
require.Equal(t, err.Error(), tc.expectedErr.Error())
return // PASS
}
require.NoError(t, err)
for i, blockRequested := range tc.blocksRequested {
switch blockRequested {
case string(latestBlock):
assert.Equal(t, int64(8), blocks[i].Number)
case string(finalizedBlock):
assert.Equal(t, int64(5), blocks[i].Number)
default:
blockNum, err2 := hexutil.DecodeUint64(blockRequested)
require.NoError(t, err2)
assert.Equal(t, int64(blockNum), blocks[i].Number)
}
}
})
}
}
}

func benchmarkFilter(b *testing.B, nFilters, nAddresses, nEvents int) {
lggr := logger.Test(b)
lpOpts := Opts{
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
blockNums = []uint64{2}
_, err = th.LogPoller.GetBlocksRange(testutils.Context(t), blockNums)
require.Error(t, err)
assert.Equal(t, "Received unfinalized block 2 while expecting finalized block (latestFinaliezdBlockNumber = 1)", err.Error())
assert.Equal(t, "Received unfinalized block 2 while expecting finalized block (latestFinalizedBlockNumber = 1)", err.Error())

th.Client.Commit() // Commit block #4, so that block #2 is finalized

Expand Down

0 comments on commit 738716d

Please sign in to comment.