Skip to content

Commit

Permalink
[BCI-3863] - Use filtered logs in eventBinding GetLatestValue instead…
Browse files Browse the repository at this point in the history
… of manual filtering (#14096)

* Use filtered logs in eventBinding GetLatestValue instead of manual filtering

* handle one filter simple expression case for topic filters

* and instead of or for matching all topic filters conditions

* add changeset

* nit comment to reduce createTopicFilters func

* add address to query name in GetLatestValue and QueryKey

* key not necessary as we are using logpoller filters
  • Loading branch information
Farber98 committed Aug 14, 2024
1 parent 83e576f commit 3f0fad6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-glasses-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

use FilteredLogs in EventBinding GetLatestValue instead of manual filtering. #internal
67 changes: 27 additions & 40 deletions core/services/relay/evm/event_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (e *eventBinding) QueryKey(ctx context.Context, filter query.KeyFilter, lim
}
remapped.Expressions = append(defaultExpressions, remapped.Expressions...)

logs, err := e.lp.FilteredLogs(ctx, remapped, limitAndSort, e.contractName+"-"+e.eventName)
logs, err := e.lp.FilteredLogs(ctx, remapped, limitAndSort, e.contractName+"-"+e.address.String()+"-"+e.eventName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -227,32 +227,41 @@ func (e *eventBinding) getLatestValueWithFilters(
return err
}

fai := filtersAndIndices[0]
remainingFilters := filtersAndIndices[1:]

logs, err := e.lp.IndexedLogs(ctx, e.hash, e.address, 1, []common.Hash{fai}, confs)
// Create limiter and filter for the query.
limiter := query.NewLimitAndSort(query.CountLimit(1), query.NewSortBySequence(query.Desc))
filter, err := query.Where(
"",
logpoller.NewAddressFilter(e.address),
logpoller.NewEventSigFilter(e.hash),
logpoller.NewConfirmationsFilter(confs),
createTopicFilters(filtersAndIndices),
)
if err != nil {
return wrapInternalErr(err)
}

// TODO Use filtered logs here BCF-3316
// TODO: there should be a better way to ask log poller to filter these
// First, you should be able to ask for as many topics to match
// Second, you should be able to get the latest only
var logToUse *logpoller.Log
for _, log := range logs {
tmp := log
if compareLogs(&tmp, logToUse) > 0 && matchesRemainingFilters(&tmp, remainingFilters) {
// copy so that it's not pointing to the changing variable
logToUse = &tmp
}
// Gets the latest log that matches the filter and limiter.
logs, err := e.lp.FilteredLogs(ctx, filter, limiter, e.contractName+"-"+e.address.String()+"-"+e.eventName)
if err != nil {
return wrapInternalErr(err)
}

if logToUse == nil {
if len(logs) == 0 {
return fmt.Errorf("%w: no events found", commontypes.ErrNotFound)
}

return e.decodeLog(ctx, logToUse, into)
return e.decodeLog(ctx, &logs[0], into)
}

func createTopicFilters(filtersAndIndices []common.Hash) query.Expression {
var expressions []query.Expression
for topicID, fai := range filtersAndIndices {
// first topic index is 1-based, so we add 1.
expressions = append(expressions, logpoller.NewEventByTopicFilter(
uint64(topicID+1), []primitives.ValueComparator{{Value: fai.Hex(), Operator: primitives.Eq}},
))
}
return query.And(expressions...)
}

// convertToOffChainType creates a struct based on contract abi with applied codec modifiers.
Expand All @@ -270,28 +279,6 @@ func (e *eventBinding) convertToOffChainType(params any) (any, error) {
return offChain, nil
}

func compareLogs(log, use *logpoller.Log) int64 {
if use == nil {
return 1
}

if log.BlockNumber != use.BlockNumber {
return log.BlockNumber - use.BlockNumber
}

return log.LogIndex - use.LogIndex
}

func matchesRemainingFilters(log *logpoller.Log, filters []common.Hash) bool {
for i, rfai := range filters {
if !reflect.DeepEqual(rfai[:], log.Topics[i+2]) {
return false
}
}

return true
}

// encodeParams accepts nativeParams and encodes them to match onchain topics.
func (e *eventBinding) encodeParams(nativeParams reflect.Value) ([]common.Hash, error) {
for nativeParams.Kind() == reflect.Pointer {
Expand Down

0 comments on commit 3f0fad6

Please sign in to comment.