Skip to content

Commit

Permalink
[CCIP-843] LogPoller - LatestBlock query improvement (smartcontractki…
Browse files Browse the repository at this point in the history
…t#10150)

* Introducing block_number lower bound filter. This will make life easier for Postgres by limiting number of records to be scanned.

* Spec
  • Loading branch information
mateusz-sekara authored Aug 10, 2023
1 parent 10f3984 commit ecf288e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func (d disabled) IndexedLogsCreatedAfter(eventSig common.Hash, address common.A
return nil, ErrDisabled
}

func (d disabled) LatestBlockByEventSigsAddrsWithConfs(eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
func (d disabled) LatestBlockByEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
return 0, ErrDisabled
}
6 changes: 3 additions & 3 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type LogPoller interface {
LogsCreatedAfter(eventSig common.Hash, address common.Address, time time.Time, confs int, qopts ...pg.QOpt) ([]Log, error)
LatestLogByEventSigWithConfs(eventSig common.Hash, address common.Address, confs int, qopts ...pg.QOpt) (*Log, error)
LatestLogEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) ([]Log, error)
LatestBlockByEventSigsAddrsWithConfs(eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error)
LatestBlockByEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error)

// Content based querying
IndexedLogs(eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error)
Expand Down Expand Up @@ -994,8 +994,8 @@ func (lp *logPoller) LatestLogEventSigsAddrsWithConfs(fromBlock int64, eventSigs
return lp.orm.SelectLatestLogEventSigsAddrsWithConfs(fromBlock, addresses, eventSigs, confs, qopts...)
}

func (lp *logPoller) LatestBlockByEventSigsAddrsWithConfs(eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
return lp.orm.SelectLatestBlockNumberEventSigsAddrsWithConfs(eventSigs, addresses, confs, qopts...)
func (lp *logPoller) LatestBlockByEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
return lp.orm.SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock, eventSigs, addresses, confs, qopts...)
}

// GetBlocksRange tries to get the specified block numbers from the log pollers
Expand Down
18 changes: 9 additions & 9 deletions core/chains/evm/logpoller/mocks/log_poller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/chains/evm/logpoller/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (o *ObservedLogPoller) LatestLogEventSigsAddrsWithConfs(fromBlock int64, ev
})
}

func (o *ObservedLogPoller) LatestBlockByEventSigsAddrsWithConfs(eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
func (o *ObservedLogPoller) LatestBlockByEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
return withObservedQuery(o, "LatestBlockByEventSigsAddrsWithConfs", func() (int64, error) {
return o.LogPoller.LatestBlockByEventSigsAddrsWithConfs(eventSigs, addresses, confs, qopts...)
return o.LogPoller.LatestBlockByEventSigsAddrsWithConfs(0, eventSigs, addresses, confs, qopts...)
})
}

Expand Down
7 changes: 4 additions & 3 deletions core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (o *ORM) SelectLatestLogEventSigsAddrsWithConfs(fromBlock int64, addresses
}

// SelectLatestBlockNumberEventSigsAddrsWithConfs finds the latest block number that matches a list of Addresses and list of events. It returns 0 if there is no matching block
func (o *ORM) SelectLatestBlockNumberEventSigsAddrsWithConfs(eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
func (o *ORM) SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
var blockNumber int64
sigs := concatBytes(eventSigs)
addrs := concatBytes(addresses)
Expand All @@ -331,8 +331,9 @@ func (o *ORM) SelectLatestBlockNumberEventSigsAddrsWithConfs(eventSigs []common.
WHERE evm_chain_id = $1 AND
event_sig = ANY($2) AND
address = ANY($3) AND
block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $4`,
o.chainID.Int64(), sigs, addrs, confs)
block_number > $4 AND
block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5`,
o.chainID.Int64(), sigs, addrs, fromBlock, confs)
if err != nil {
return 0, err
}
Expand Down
24 changes: 23 additions & 1 deletion core/chains/evm/logpoller/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,47 +1022,69 @@ func TestSelectLatestBlockNumberEventSigsAddrsWithConfs(t *testing.T) {
events []common.Hash
addrs []common.Address
confs int
fromBlock int64
expectedBlockNumber int64
}{
{
name: "no matching logs returns 0 block number",
events: []common.Hash{event2},
addrs: []common.Address{address1},
confs: 0,
fromBlock: 0,
expectedBlockNumber: 0,
},
{
name: "not enough confirmations block returns 0 block number",
events: []common.Hash{event2},
addrs: []common.Address{address2},
confs: 5,
fromBlock: 0,
expectedBlockNumber: 0,
},
{
name: "single matching event and address returns last block",
events: []common.Hash{event1},
addrs: []common.Address{address1},
confs: 0,
fromBlock: 0,
expectedBlockNumber: 1,
},
{
name: "picks max block from two events",
events: []common.Hash{event1, event2},
addrs: []common.Address{address1, address2},
confs: 0,
fromBlock: 0,
expectedBlockNumber: 3,
},
{
name: "picks previous block number for confirmations set to 1",
events: []common.Hash{event2},
addrs: []common.Address{address2},
confs: 1,
fromBlock: 0,
expectedBlockNumber: 2,
},
{
name: "returns 0 if from block is not matching",
events: []common.Hash{event1, event2},
addrs: []common.Address{address1, address2},
confs: 0,
fromBlock: 3,
expectedBlockNumber: 0,
},
{
name: "picks max block from two events when from block is lower",
events: []common.Hash{event1, event2},
addrs: []common.Address{address1, address2},
confs: 0,
fromBlock: 2,
expectedBlockNumber: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
blockNumber, err := th.ORM.SelectLatestBlockNumberEventSigsAddrsWithConfs(tt.events, tt.addrs, tt.confs)
blockNumber, err := th.ORM.SelectLatestBlockNumberEventSigsAddrsWithConfs(tt.fromBlock, tt.events, tt.addrs, tt.confs)
require.NoError(t, err)
assert.Equal(t, tt.expectedBlockNumber, blockNumber)
})
Expand Down

0 comments on commit ecf288e

Please sign in to comment.