Skip to content

Commit

Permalink
use proper context in the LogPoller's latestBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaidashenko committed May 14, 2024
1 parent 1b80553 commit 2d0a17d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (lp *logPoller) run() {
}
// Otherwise this is the first poll _ever_ on a new chain.
// Only safe thing to do is to start at the first finalized block.
latestBlock, latestFinalizedBlockNumber, err := lp.latestBlocks()
latestBlock, latestFinalizedBlockNumber, err := lp.latestBlocks(lp.ctx)
if err != nil {
lp.lggr.Warnw("Unable to get latest for first poll", "err", err)
continue
Expand Down Expand Up @@ -709,7 +709,7 @@ func (lp *logPoller) BackupPollAndSaveLogs(ctx context.Context) {
lp.backupPollerNextBlock = mathutil.Max(backupStartBlock, 0)
}

_, latestFinalizedBlockNumber, err := lp.latestBlocks()
_, latestFinalizedBlockNumber, err := lp.latestBlocks(ctx)
if err != nil {
lp.lggr.Warnw("Backup logpoller failed to get latest block", "err", err)
return
Expand Down Expand Up @@ -926,7 +926,7 @@ func (lp *logPoller) PollAndSaveLogs(ctx context.Context, currentBlockNumber int
lp.lggr.Debugw("Polling for logs", "currentBlockNumber", currentBlockNumber)
// Intentionally not using logPoller.finalityDepth directly but the latestFinalizedBlockNumber returned from lp.latestBlocks()
// latestBlocks knows how to pick a proper latestFinalizedBlockNumber based on the logPoller's configuration
latestBlock, latestFinalizedBlockNumber, err := lp.latestBlocks()
latestBlock, latestFinalizedBlockNumber, err := lp.latestBlocks(ctx)
if err != nil {
lp.lggr.Warnw("Unable to get latestBlockNumber block", "err", err, "currentBlockNumber", currentBlockNumber)
return
Expand Down Expand Up @@ -1011,8 +1011,8 @@ func (lp *logPoller) PollAndSaveLogs(ctx context.Context, currentBlockNumber int
}

// Returns information about latestBlock, latestFinalizedBlockNumber provided by HeadTracker
func (lp *logPoller) latestBlocks() (*evmtypes.Head, int64, error) {
latest, finalized, err := lp.headTracker.LatestAndFinalizedBlock(lp.ctx)
func (lp *logPoller) latestBlocks(ctx context.Context) (*evmtypes.Head, int64, error) {
latest, finalized, err := lp.headTracker.LatestAndFinalizedBlock(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to get latest and latest finalized block from HeadTracker: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions core/chains/evm/logpoller/log_poller_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
Expand Down Expand Up @@ -543,7 +545,7 @@ func Test_latestBlockAndFinalityDepth(t *testing.T) {
headTracker.On("LatestAndFinalizedBlock", mock.Anything).Return(&evmtypes.Head{}, &evmtypes.Head{}, fmt.Errorf(expectedError))

lp := NewLogPoller(nil, nil, lggr, headTracker, lpOpts)
_, _, err := lp.latestBlocks()
_, _, err := lp.latestBlocks(tests.Context(t))
require.ErrorContains(t, err, expectedError)
})
t.Run("headTracker returns valid chain", func(t *testing.T) {
Expand All @@ -553,7 +555,7 @@ func Test_latestBlockAndFinalityDepth(t *testing.T) {
headTracker.On("LatestAndFinalizedBlock", mock.Anything).Return(head, finalizedBlock, nil)

lp := NewLogPoller(nil, nil, lggr, headTracker, lpOpts)
latestBlock, finalizedBlockNumber, err := lp.latestBlocks()
latestBlock, finalizedBlockNumber, err := lp.latestBlocks(tests.Context(t))
require.NoError(t, err)
require.NotNil(t, latestBlock)
assert.Equal(t, head.BlockNumber(), latestBlock.BlockNumber())
Expand Down

0 comments on commit 2d0a17d

Please sign in to comment.