-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
212 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package read | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/chainlink-common/pkg/types/query" | ||
"github.com/smartcontractkit/chainlink-common/pkg/values" | ||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" | ||
) | ||
|
||
type EventQuery struct { | ||
Filter query.KeyFilter | ||
EventBinding *EventBinding | ||
SequenceDataType any | ||
IsValuePtr bool | ||
Address common.Address | ||
} | ||
|
||
func MultiEventTypeQuery(ctx context.Context, lp logpoller.LogPoller, eventQueries []EventQuery, limitAndSort query.LimitAndSort) (sequences []commontypes.SequenceWithKey, err error) { | ||
|
||
defer func() { | ||
if err != nil { | ||
if len(eventQueries) > 0 { | ||
// TODO need to figure out what would be a sensible way to capture the error for all the queries | ||
eq := eventQueries[0] | ||
|
||
err = newErrorFromCall(err, Call{ | ||
ContractAddress: eq.Address, | ||
ContractName: eq.EventBinding.contractName, | ||
ReadName: eq.EventBinding.eventName, | ||
ReturnVal: eq.SequenceDataType, | ||
}, "", eventReadType) | ||
} else { | ||
err = fmt.Errorf("no event queries provided: %v", err) | ||
} | ||
} | ||
}() | ||
|
||
for _, eq := range eventQueries { | ||
if err = eq.EventBinding.validateBound(eq.Address); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var allFilterExpressions []query.Expression | ||
for _, eq := range eventQueries { | ||
var expressions []query.Expression | ||
|
||
defaultExpressions := []query.Expression{ | ||
logpoller.NewAddressFilter(eq.Address), | ||
logpoller.NewEventSigFilter(eq.EventBinding.hash), | ||
} | ||
expressions = append(expressions, defaultExpressions...) | ||
|
||
remapped, err := eq.EventBinding.remap(eq.Filter) | ||
if err != nil { | ||
return nil, fmt.Errorf("error remapping filter: %v", err) | ||
} | ||
expressions = append(expressions, remapped.Expressions...) | ||
|
||
filterExpression := query.And(expressions...) | ||
|
||
allFilterExpressions = append(allFilterExpressions, filterExpression) | ||
} | ||
|
||
eventQuery := query.Or(allFilterExpressions...) | ||
|
||
queryName := "" | ||
for _, eq := range eventQueries { | ||
queryName += eq.EventBinding.contractName + "-" + eq.Address.String() + "-" + eq.EventBinding.eventName + "-" | ||
} | ||
strings.TrimSuffix(queryName, "-") | ||
|
||
logs, err := lp.FilteredLogs(ctx, []query.Expression{eventQuery}, limitAndSort, queryName) | ||
if err != nil { | ||
return nil, wrapInternalErr(err) | ||
} | ||
|
||
// no need to return an error. an empty list is fine | ||
if len(logs) == 0 { | ||
return []commontypes.SequenceWithKey{}, nil | ||
} | ||
|
||
sequences, err = decodeMultiEventTypeLogsIntoSequences(ctx, logs, eventQueries) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sequences, nil | ||
} | ||
|
||
func decodeMultiEventTypeLogsIntoSequences(ctx context.Context, logs []logpoller.Log, eventQueries []EventQuery) ([]commontypes.SequenceWithKey, error) { | ||
sequences := make([]commontypes.SequenceWithKey, len(logs)) | ||
|
||
eventSigToEventQuery := map[common.Hash]EventQuery{} | ||
for _, eq := range eventQueries { | ||
eventSigToEventQuery[eq.EventBinding.hash] = eq | ||
} | ||
|
||
for idx := range logs { | ||
|
||
logEntry := logs[idx] | ||
eventSignatureHash := logEntry.EventSig | ||
|
||
eq, exists := eventSigToEventQuery[eventSignatureHash] | ||
if !exists { | ||
return nil, fmt.Errorf("no event query found for log with event signature %s", eventSignatureHash) | ||
} | ||
|
||
sequences[idx] = commontypes.SequenceWithKey{ | ||
Key: eq.Filter.Key, | ||
Sequence: commontypes.Sequence{ | ||
Cursor: logpoller.FormatContractReaderCursor(logEntry), | ||
Head: commontypes.Head{ | ||
Height: fmt.Sprint(logEntry.BlockNumber), | ||
Hash: logEntry.BlockHash.Bytes(), | ||
Timestamp: uint64(logEntry.BlockTimestamp.Unix()), | ||
}, | ||
}, | ||
} | ||
|
||
var typeVal reflect.Value | ||
|
||
typeInto := reflect.TypeOf(eq.SequenceDataType) | ||
if typeInto.Kind() == reflect.Pointer { | ||
typeVal = reflect.New(typeInto.Elem()) | ||
} else { | ||
typeVal = reflect.Indirect(reflect.New(typeInto)) | ||
} | ||
|
||
// create a new value of the same type as 'into' for the data to be extracted to | ||
sequences[idx].Data = typeVal.Interface() | ||
|
||
if err := eq.EventBinding.decodeLog(ctx, &logs[idx], sequences[idx].Data); err != nil { | ||
return nil, err | ||
} | ||
|
||
if eq.IsValuePtr { | ||
wrappedValue, err := values.Wrap(sequences[idx].Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sequences[idx].Data = &wrappedValue | ||
} | ||
|
||
} | ||
|
||
return sequences, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters