Skip to content

Commit

Permalink
HasFilter to check whether some filter was loaded by the poller (#10469)
Browse files Browse the repository at this point in the history
Co-authored-by: Domino Valdano <[email protected]>
  • Loading branch information
amirylm and reductionista authored Sep 6, 2023
1 parent 43030bd commit 3bf936a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/chains/evm/logpoller/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (disabled) RegisterFilter(filter Filter, qopts ...pg.QOpt) error { return E

func (disabled) UnregisterFilter(name string, qopts ...pg.QOpt) error { return ErrDisabled }

func (disabled) HasFilter(name string) bool { return false }

func (disabled) LatestBlock(qopts ...pg.QOpt) (int64, error) { return -1, ErrDisabled }

func (disabled) GetBlocksRange(ctx context.Context, numbers []uint64, qopts ...pg.QOpt) ([]LogPollerBlock, error) {
Expand Down
10 changes: 10 additions & 0 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type LogPoller interface {
ReplayAsync(fromBlock int64)
RegisterFilter(filter Filter, qopts ...pg.QOpt) error
UnregisterFilter(name string, qopts ...pg.QOpt) error
HasFilter(name string) bool
LatestBlock(qopts ...pg.QOpt) (int64, error)
GetBlocksRange(ctx context.Context, numbers []uint64, qopts ...pg.QOpt) ([]LogPollerBlock, error)

Expand Down Expand Up @@ -258,6 +259,15 @@ func (lp *logPoller) UnregisterFilter(name string, qopts ...pg.QOpt) error {
return nil
}

// HasFilter returns true if the log poller has an active filter with the given name.
func (lp *logPoller) HasFilter(name string) bool {
lp.filterMu.RLock()
defer lp.filterMu.RUnlock()

_, ok := lp.filters[name]
return ok
}

func (lp *logPoller) Filter(from, to *big.Int, bh *common.Hash) ethereum.FilterQuery {
lp.filterMu.Lock()
defer lp.filterMu.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,13 @@ func TestLogPoller_LoadFilters(t *testing.T) {
require.True(t, ok)
assert.True(t, filter.Contains(&filter3))
assert.True(t, filter3.Contains(&filter))

t.Run("HasFilter", func(t *testing.T) {
assert.True(t, th.LogPoller.HasFilter("first Filter"))
assert.True(t, th.LogPoller.HasFilter("second Filter"))
assert.True(t, th.LogPoller.HasFilter("third Filter"))
assert.False(t, th.LogPoller.HasFilter("fourth Filter"))
})
}

func TestLogPoller_GetBlocks_Range(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 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.

0 comments on commit 3bf936a

Please sign in to comment.