Skip to content

Commit

Permalink
Fix UnmatchedLogs query
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Sep 25, 2024
1 parent d02e394 commit 10eb4df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,20 @@ type Exp struct {

func (o *DSORM) SelectUnmatchedLogIDs(ctx context.Context, limit int64) (ids []uint64, err error) {
query := `
SELECT l.id FROM evm.logs l JOIN (
SELECT l.id FROM evm.logs l LEFT JOIN (
SELECT evm_chain_id, address, event
FROM evm.log_poller_filters
WHERE evm_chain_id = $1
GROUP BY evm_chain_id, address, event
) r ON l.evm_chain_id = r.evm_chain_id AND l.address = r.address AND l.event_sig = r.event
WHERE l.evm_chain_id = $1 AND r.id IS NULL
WHERE l.evm_chain_id = $1 AND r.evm_chain_id IS NULL
`

if limit == 0 {
err = o.ds.SelectContext(ctx, &ids, query, ubig.New(o.chainID))
return ids, err
}
err = o.ds.SelectContext(ctx, &ids, fmt.Sprintf("%s LIMIT %d", query, limit))
err = o.ds.SelectContext(ctx, &ids, fmt.Sprintf("%s LIMIT %d", query, limit), ubig.New(o.chainID))

return ids, err
}
Expand Down
15 changes: 10 additions & 5 deletions core/chains/evm/logpoller/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,20 @@ func TestORM(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, int64(1), deleted)

// Delete unmatched logs with page limit
// Select unmatched logs with page limit
ids, err := o1.SelectUnmatchedLogIDs(ctx, 2)
require.NoError(t, err)
assert.Equal(t, int64(2), ids)
assert.Len(t, ids, 2)

// Delete unmatched logs without page limit
// Select unmatched logs without page limit
ids, err = o1.SelectUnmatchedLogIDs(ctx, 0)
require.NoError(t, err)
assert.Equal(t, int64(2), ids)
assert.Len(t, ids, 3)

// Delete logs by row id
deleted, err = o1.DeleteLogsByRowID(ctx, ids)
require.NoError(t, err)
assert.Equal(t, int64(3), deleted)

// Ensure that both of the logs from the second chain are still there
logs, err = o2.SelectLogs(ctx, 0, 100, common.HexToAddress("0x1236"), topic2)
Expand All @@ -519,7 +524,7 @@ func TestORM(t *testing.T) {
// The only log which should be deleted is the one which matches filter1 (ret=1ms) but not filter12 (ret=1 hour)
// Importantly, it shouldn't delete any logs matching only filter0 (ret=0 meaning permanent retention). Anything
// matching filter12 should be kept regardless of what other filters it matches.
assert.Len(t, logs, 7)
assert.Len(t, logs, 4)

// Delete logs after should delete all logs.
err = o1.DeleteLogsAndBlocksAfter(ctx, 1)
Expand Down

0 comments on commit 10eb4df

Please sign in to comment.