Skip to content

Commit

Permalink
apply event scoped filter and register on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Feb 13, 2025
1 parent e727e73 commit a93fa2c
Show file tree
Hide file tree
Showing 17 changed files with 1,102 additions and 221 deletions.
1 change: 1 addition & 0 deletions pkg/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type LogPoller interface {
Start(context.Context) error
Ready() error
Close() error
HasFilter(context.Context, string) bool
RegisterFilter(ctx context.Context, filter logpoller.Filter) error
UnregisterFilter(ctx context.Context, name string) error
FilteredLogs(context.Context, []query.Expression, query.LimitAndSort, string) ([]logpoller.Log, error)
Expand Down
45 changes: 34 additions & 11 deletions pkg/solana/chainreader/account_read_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ type accountReadBinding struct {
outputIDLTypeDef codec.IdlTypeDef
}

func newAccountReadBinding(namespace, genericName string, isPda bool, pdaPrefix []byte, idl codec.IDL, inputIDLType interface{}, outputIDLTypeDef codec.IdlTypeDef, readDefinition config.ReadDefinition) *accountReadBinding {
func newAccountReadBinding(
namespace, genericName string,
isPda bool,
pdaPrefix []byte,
idl codec.IDL,
inputIDLType interface{},
outputIDLTypeDef codec.IdlTypeDef,
readDefinition config.ReadDefinition,
) *accountReadBinding {
rb := &accountReadBinding{
namespace: namespace,
genericName: genericName,
Expand All @@ -52,20 +60,45 @@ func newAccountReadBinding(namespace, genericName string, isPda bool, pdaPrefix

var _ readBinding = &accountReadBinding{}

func (b *accountReadBinding) SetCodec(codec types.RemoteCodec) {
b.codec = codec
}

func (b *accountReadBinding) SetModifier(commoncodec.Modifier) {}

func (b *accountReadBinding) Register(context.Context) error { return nil }

func (b *accountReadBinding) Unregister(context.Context) error { return nil }

func (b *accountReadBinding) Bind(_ context.Context, address solana.PublicKey) error {
b.key = address

return nil
}

func (b *accountReadBinding) Unbind(_ context.Context) error {
b.key = solana.PublicKey{}

return nil
}

func (b *accountReadBinding) GetAddress(ctx context.Context, params any) (solana.PublicKey, error) {
// Return the bound key if normal account read
if !b.isPda {
return b.key, nil
}

// Calculate the public key if PDA account read
seedBytes, err := b.buildSeedsSlice(ctx, params)
if err != nil {
return solana.PublicKey{}, fmt.Errorf("failed build seeds list for PDA calculation: %w", err)
}

key, _, err := solana.FindProgramAddress(seedBytes, b.key)
if err != nil {
return solana.PublicKey{}, fmt.Errorf("failed find program address for PDA: %w", err)
}

return key, nil
}

Expand All @@ -85,16 +118,6 @@ func (b *accountReadBinding) GetAddressResponseHardCoder() *commoncodec.HardCode
return b.responseAddressHardCoder
}

func (b *accountReadBinding) SetAddress(key solana.PublicKey) {
b.key = key
}

func (b *accountReadBinding) SetCodec(codec types.RemoteCodec) {
b.codec = codec
}

func (b *accountReadBinding) SetModifier(commoncodec.Modifier) {}

func (b *accountReadBinding) CreateType(forEncoding bool) (any, error) {
return b.codec.CreateType(codec.WrapItemType(forEncoding, b.namespace, b.genericName), forEncoding)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/solana/chainreader/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type MultipleAccountGetter interface {
}

// doMultiRead aggregate results from multiple PDAs from the same contract into one result.
func doMultiRead(ctx context.Context, client MultipleAccountGetter, bindings bindingsRegistry, rv readValues, params, returnValue any) error {
func doMultiRead(ctx context.Context, client MultipleAccountGetter, bdRegistry *bindingsRegistry, rv readValues, params, returnValue any) error {
batch := make([]call, len(rv.reads))
for idx, r := range rv.reads {
batch[idx] = call{
Expand All @@ -46,7 +46,7 @@ func doMultiRead(ctx context.Context, client MultipleAccountGetter, bindings bin
}
}

results, err := doMethodBatchCall(ctx, client, bindings, batch)
results, err := doMethodBatchCall(ctx, client, bdRegistry, batch)
if err != nil {
return err
}
Expand All @@ -69,11 +69,11 @@ func doMultiRead(ctx context.Context, client MultipleAccountGetter, bindings bin
return nil
}

func doMethodBatchCall(ctx context.Context, client MultipleAccountGetter, bindingsRegistry bindingsRegistry, batch []call) ([]batchResultWithErr, error) {
func doMethodBatchCall(ctx context.Context, client MultipleAccountGetter, bdRegistry *bindingsRegistry, batch []call) ([]batchResultWithErr, error) {
// Create the list of public keys to fetch
keys := make([]solana.PublicKey, len(batch))
for idx, batchCall := range batch {
rBinding, err := bindingsRegistry.GetReadBinding(batchCall.Namespace, batchCall.ReadName)
rBinding, err := bdRegistry.GetReader(batchCall.Namespace, batchCall.ReadName)
if err != nil {
return nil, fmt.Errorf("%w: read binding not found for contract: %q read: %q: %w", types.ErrInvalidConfig, batchCall.Namespace, batchCall.ReadName, err)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func doMethodBatchCall(ctx context.Context, client MultipleAccountGetter, bindin
continue
}

rBinding, err := bindingsRegistry.GetReadBinding(results[idx].namespace, results[idx].readName)
rBinding, err := bdRegistry.GetReader(results[idx].namespace, results[idx].readName)
if err != nil {
results[idx].err = err

Expand Down
Loading

0 comments on commit a93fa2c

Please sign in to comment.