Skip to content

Commit

Permalink
Update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Apr 2, 2024
1 parent 66a2808 commit e65effb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
29 changes: 28 additions & 1 deletion core/chains/evm/logpoller/log_poller_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -222,6 +223,7 @@ func TestLogPoller_BackupPollerStartup(t *testing.T) {
ec.On("HeadByNumber", mock.Anything, mock.Anything).Return(&head, nil)
ec.On("FilterLogs", mock.Anything, mock.Anything).Return([]types.Log{log1}, nil)
ec.On("ConfiguredChainID").Return(chainID, nil)
mockBatchCallContext(t, ec)

ctx := testutils.Context(t)
lpOpts := Opts{
Expand All @@ -243,7 +245,30 @@ func TestLogPoller_BackupPollerStartup(t *testing.T) {
require.Equal(t, int64(3), lastProcessed.BlockNumber)

lp.BackupPollAndSaveLogs(ctx)
assert.Equal(t, int64(1), lp.backupPollerNextBlock) // Ensure non-negative!
assert.Equal(t, int64(2), lp.backupPollerNextBlock)
}

func mockBatchCallContext(t *testing.T, ec *evmclimocks.Client) {
ec.On("BatchCallContext", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
elems := args.Get(1).([]rpc.BatchElem)
for _, e := range elems {
var num int64
block := e.Args[0].(string)
switch block {
case "latest":
num = 8
case "finalized":
num = 5
default:
n, err := hexutil.DecodeUint64(block)
require.NoError(t, err)
num = int64(n)
}
result := e.Result.(*evmtypes.Head)
*result = evmtypes.Head{Number: num, Hash: utils.NewHash()}

}
})
}

func TestLogPoller_Replay(t *testing.T) {
Expand Down Expand Up @@ -272,6 +297,8 @@ func TestLogPoller_Replay(t *testing.T) {
ec.On("HeadByNumber", mock.Anything, mock.Anything).Return(&head, nil)
ec.On("FilterLogs", mock.Anything, mock.Anything).Return([]types.Log{log1}, nil).Twice()
ec.On("ConfiguredChainID").Return(chainID, nil)
mockBatchCallContext(t, ec)

lpOpts := Opts{
PollPeriod: time.Hour,
FinalityDepth: 3,
Expand Down
34 changes: 18 additions & 16 deletions core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ func TestLogPoller_BlockTimestamps(t *testing.T) {
require.NoError(t, err)

// Logs should have correct timestamps
require.NotZero(t, len(lg1))
b, _ := th.Client.BlockByHash(ctx, lg1[0].BlockHash)
t.Log(len(lg1), lg1[0].BlockTimestamp)
assert.Equal(t, int64(b.Time()), lg1[0].BlockTimestamp.UTC().Unix(), time1)
Expand Down Expand Up @@ -1181,6 +1182,7 @@ func TestLogPoller_PollAndSaveLogsDeepReorg(t *testing.T) {
// Check that L1_1 has a proper data payload
lgs, err := th.ORM.SelectLogsByBlockRange(testutils.Context(t), 2, 2)
require.NoError(t, err)
require.NotZero(t, len(lgs))
assert.Equal(t, hexutil.MustDecode(`0x0000000000000000000000000000000000000000000000000000000000000001`), lgs[0].Data)

// Single block reorg and log poller not working for a while, mine blocks and progress with finalization
Expand Down Expand Up @@ -1301,6 +1303,7 @@ func TestLogPoller_LoadFilters(t *testing.T) {

func TestLogPoller_GetBlocks_Range(t *testing.T) {
t.Parallel()

lpOpts := logpoller.Opts{
UseFinalityTag: false,
FinalityDepth: 2,
Expand All @@ -1310,7 +1313,15 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
}
th := SetupTH(t, lpOpts)

err := th.LogPoller.RegisterFilter(testutils.Context(t), logpoller.Filter{
_, err := th.Emitter1.EmitLog1(th.Owner, []*big.Int{big.NewInt(1)})
require.NoError(t, err)
th.Client.Commit() // Commit block #2 with log in it

_, err = th.Emitter1.EmitLog1(th.Owner, []*big.Int{big.NewInt(2)})
require.NoError(t, err)
th.Client.Commit() // Commit block #3 with a different log

err = th.LogPoller.RegisterFilter(testutils.Context(t), logpoller.Filter{
Name: "GetBlocks Test",
EventSigs: []common.Hash{EmitterABI.Events["Log1"].ID, EmitterABI.Events["Log2"].ID},
Addresses: []common.Address{th.EmitterAddress1, th.EmitterAddress2},
Expand All @@ -1330,16 +1341,13 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.Equal(t, 1, len(blocks))
assert.Equal(t, 1, int(blocks[0].BlockNumber))

// LP fails to retrieve block 2 because it's neither in DB nor returned by RPC
// LP fails to return block 2 because it hasn't been finalized yet
blockNums = []uint64{2}
_, err = th.LogPoller.GetBlocksRange(testutils.Context(t), blockNums)
require.Error(t, err)
assert.Equal(t, "blocks were not found in db or RPC call: [2]", err.Error())
assert.Equal(t, "Received unfinalized block 2 while expecting finalized block (latestFinaliezdBlockNumber = 1)", err.Error())

// Emit a log and mine block #2
_, err = th.Emitter1.EmitLog1(th.Owner, []*big.Int{big.NewInt(1)})
require.NoError(t, err)
th.Client.Commit()
th.Client.Commit() // Commit block #4, so that block #2 is finalized

// Assert block 2 is not yet in DB
_, err = th.ORM.SelectBlockByNumber(testutils.Context(t), 2)
Expand All @@ -1351,10 +1359,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.Equal(t, 1, len(rpcBlocks))
assert.Equal(t, 2, int(rpcBlocks[0].BlockNumber))

// Emit a log and mine block #3
_, err = th.Emitter1.EmitLog1(th.Owner, []*big.Int{big.NewInt(2)})
require.NoError(t, err)
th.Client.Commit()
th.Client.Commit() // commit block #5 so that #3 becomes finalized

// Assert block 3 is not yet in DB
_, err = th.ORM.SelectBlockByNumber(testutils.Context(t), 3)
Expand All @@ -1368,12 +1373,9 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.Equal(t, 1, int(rpcBlocks2[0].BlockNumber))
assert.Equal(t, 3, int(rpcBlocks2[1].BlockNumber))

// after calling PollAndSaveLogs, block 2 & 3 are persisted in DB
// after calling PollAndSaveLogs, block 3 (latest finalized block) is persisted in DB
th.LogPoller.PollAndSaveLogs(testutils.Context(t), 1)
block, err := th.ORM.SelectBlockByNumber(testutils.Context(t), 2)
require.NoError(t, err)
assert.Equal(t, 2, int(block.BlockNumber))
block, err = th.ORM.SelectBlockByNumber(testutils.Context(t), 3)
block, err := th.ORM.SelectBlockByNumber(testutils.Context(t), 3)
require.NoError(t, err)
assert.Equal(t, 3, int(block.BlockNumber))

Expand Down

0 comments on commit e65effb

Please sign in to comment.