Skip to content

Commit

Permalink
add events source interface
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Jan 21, 2025
1 parent 743e40f commit 318aaa4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
23 changes: 12 additions & 11 deletions pkg/solana/chainreader/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller"
)

type EventSourcer interface {
type EventsReader interface {
FilteredLogs(context.Context, []query.Expression, query.LimitAndSort, string) ([]logpoller.Log, error)
}

Expand All @@ -34,14 +34,13 @@ type ContractReaderService struct {
// provided dependencies
lggr logger.Logger
client MultipleAccountGetter
events EventSourcer
events EventsReader

// internal values
bindings namespaceBindings
lookup *lookup
parsed *codec.ParsedTypes
codec types.RemoteCodec
codecWithoutModifiers types.RemoteCodec
bindings namespaceBindings
lookup *lookup
parsed *codec.ParsedTypes
codec types.RemoteCodec

// service state management
wg sync.WaitGroup
Expand All @@ -58,7 +57,7 @@ func NewContractReaderService(
lggr logger.Logger,
dataReader MultipleAccountGetter,
cfg config.ContractReader,
events EventSourcer,
events EventsReader,
) (*ContractReaderService, error) {
svc := &ContractReaderService{
lggr: logger.Named(lggr, ServiceName),
Expand Down Expand Up @@ -327,7 +326,7 @@ func (s *ContractReaderService) init(namespaces map[string]config.ChainContractR
return fmt.Errorf("unexpected type %T from IDL definition for event read: %q, with chainSpecificName: %q, of type: %q", eventIDlDef, genericName, read.ChainSpecificName, read.ReadType)
}

if err = s.addEventRead(namespace, genericName, nameSpaceDef.IDL, eventIDlDef, read); err != nil {
if err = s.addEventRead(namespace, genericName, nameSpaceDef.IDL, eventIDlDef, read, s.events); err != nil {
return err
}
default:
Expand Down Expand Up @@ -369,16 +368,18 @@ func (s *ContractReaderService) addAccountRead(

func (s *ContractReaderService) addEventRead(
namespace, genericName string,
idl codec.IDL,
idlType codec.IdlEvent,
_ codec.IDL,
_ codec.IdlEvent,
readDefinition config.ReadDefinition,
events EventsReader,
) error {
// TODO: set log poller filter

s.bindings.AddReadBinding(namespace, genericName, newEventReadBinding(
namespace,
genericName,
readDefinition.IndexedFields,
events,
))

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/chainreader/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ type chainReaderInterfaceTester struct {
conf config.ContractReader
address []string
reader *wrappedTestChainReader
eventSource chainreader.EventSourcer
eventSource chainreader.EventsReader
}

func (r *chainReaderInterfaceTester) GetAccountBytes(i int) []byte {
Expand Down
18 changes: 10 additions & 8 deletions pkg/solana/chainreader/event_read_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ import (
"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller"
)

type EventsReader interface {
FilteredLogs(context.Context, []query.Expression, query.LimitAndSort, string) ([]logpoller.Log, error)
}

type eventReadBinding struct {
namespace, genericName string
codec types.RemoteCodec
modifier commoncodec.Modifier
key solana.PublicKey
remapper remapHelper
indexedSubkeys map[string]string
reader EventsReader
events EventsReader
}

func newEventReadBinding(namespace, genericName string, indexedSubkeys map[string]string) *eventReadBinding {
func newEventReadBinding(
namespace, genericName string,
indexedSubkeys map[string]string,
events EventsReader,
) *eventReadBinding {
binding := &eventReadBinding{
namespace: namespace,
genericName: genericName,
indexedSubkeys: indexedSubkeys,
events: events,
}

binding.remapper = remapHelper{binding.remapPrimitive}
Expand Down Expand Up @@ -85,7 +86,7 @@ func (b *eventReadBinding) QueryKey(

itemType := strings.Join([]string{b.namespace, b.genericName}, ".")

logs, err := b.reader.FilteredLogs(ctx, filter.Expressions, limitAndSort, itemType)
logs, err := b.events.FilteredLogs(ctx, filter.Expressions, limitAndSort, itemType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -126,6 +127,7 @@ func (b *eventReadBinding) encodeComparator(comparator *primitives.Comparator) (
itemType := strings.Join([]string{b.namespace, b.genericName, comparator.Name}, ".")

for idx, comp := range comparator.ValueComparators {
// need to do a transform and then extract the value for the subkey
newValue, err := b.modifier.TransformToOnChain(comp.Value, itemType)
if err != nil {
return query.Expression{}, err
Expand All @@ -150,7 +152,7 @@ func (b *eventReadBinding) decodeLogsIntoSequences(
Head: types.Head{
Height: fmt.Sprint(logs[idx].BlockNumber),
Hash: solana.PublicKey(logs[idx].BlockHash).Bytes(),
Timestamp: uint64(logs[idx].BlockTimestamp.Unix()),
Timestamp: uint64(logs[idx].BlockTimestamp.Unix()), //nolint:gosec
},
}

Expand Down

0 comments on commit 318aaa4

Please sign in to comment.