Skip to content

Commit

Permalink
Created a GetFilters function for the logpoller (#12621)
Browse files Browse the repository at this point in the history
* Created a GetFilters function for the logpoller

* Changeset

* Added mock

* Address feedback to improve test
  • Loading branch information
KuphJr authored Mar 29, 2024
1 parent 84465d6 commit 9c2764a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-penguins-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

Add GetFilters function to the log_poller
2 changes: 2 additions & 0 deletions core/chains/evm/logpoller/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (disabled) UnregisterFilter(ctx context.Context, name string) error { retur

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

func (disabled) GetFilters() map[string]Filter { return nil }

func (disabled) LatestBlock(ctx context.Context) (LogPollerBlock, error) {
return LogPollerBlock{}, ErrDisabled
}
Expand Down
30 changes: 30 additions & 0 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type LogPoller interface {
RegisterFilter(ctx context.Context, filter Filter) error
UnregisterFilter(ctx context.Context, name string) error
HasFilter(name string) bool
GetFilters() map[string]Filter
LatestBlock(ctx context.Context) (LogPollerBlock, error)
GetBlocksRange(ctx context.Context, numbers []uint64) ([]LogPollerBlock, error)

Expand Down Expand Up @@ -316,6 +317,35 @@ func (lp *logPoller) HasFilter(name string) bool {
return ok
}

// GetFilters returns a deep copy of the filters map.
func (lp *logPoller) GetFilters() map[string]Filter {
lp.filterMu.RLock()
defer lp.filterMu.RUnlock()

filters := make(map[string]Filter)
for k, v := range lp.filters {
deepCopyFilter := Filter{
Name: v.Name,
Addresses: make(evmtypes.AddressArray, len(v.Addresses)),
EventSigs: make(evmtypes.HashArray, len(v.EventSigs)),
Topic2: make(evmtypes.HashArray, len(v.Topic2)),
Topic3: make(evmtypes.HashArray, len(v.Topic3)),
Topic4: make(evmtypes.HashArray, len(v.Topic4)),
Retention: v.Retention,
MaxLogsKept: v.MaxLogsKept,
LogsPerBlock: v.LogsPerBlock,
}
copy(deepCopyFilter.Addresses, v.Addresses)
copy(deepCopyFilter.EventSigs, v.EventSigs)
copy(deepCopyFilter.Topic2, v.Topic2)
copy(deepCopyFilter.Topic3, v.Topic3)
copy(deepCopyFilter.Topic4, v.Topic4)

filters[k] = deepCopyFilter
}
return filters
}

func (lp *logPoller) Filter(from, to *big.Int, bh *common.Hash) ethereum.FilterQuery {
lp.filterMu.Lock()
defer lp.filterMu.Unlock()
Expand Down
14 changes: 14 additions & 0 deletions core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,20 @@ func TestLogPoller_LoadFilters(t *testing.T) {
assert.True(t, th.LogPoller.HasFilter("third Filter"))
assert.False(t, th.LogPoller.HasFilter("fourth Filter"))
})

t.Run("GetFilters", func(t *testing.T) {
filters := th.LogPoller.GetFilters()
assert.Equal(t, 3, len(filters))
assert.Equal(t, filters["first Filter"].Name, "first Filter")
assert.Equal(t, filters["first Filter"].EventSigs, filter1.EventSigs)
assert.Equal(t, filters["first Filter"].Addresses, filter1.Addresses)
assert.Equal(t, filters["second Filter"].Name, "second Filter")
assert.Equal(t, filters["second Filter"].EventSigs, filter2.EventSigs)
assert.Equal(t, filters["second Filter"].Addresses, filter2.Addresses)
assert.Equal(t, filters["third Filter"].Name, "third Filter")
assert.Equal(t, filters["third Filter"].EventSigs, filter3.EventSigs)
assert.Equal(t, filters["third Filter"].Addresses, filter3.Addresses)
})
}

func TestLogPoller_GetBlocks_Range(t *testing.T) {
Expand Down
20 changes: 20 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 9c2764a

Please sign in to comment.