Skip to content

Commit

Permalink
Remove lp.typeProvider, generate EventIdl for test event
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Jan 10, 2025
1 parent 19c6384 commit 8ef6ef8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
9 changes: 2 additions & 7 deletions pkg/solana/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
)

var (
ErrFilterNameConflict = errors.New("filter with such name already exists")
ErrMissingEventTypeProvider = errors.New("cannot start LogPoller without EventTypeProvider")
ErrFilterNameConflict = errors.New("filter with such name already exists")
)

type ORM interface {
Expand Down Expand Up @@ -51,8 +50,7 @@ type LogPoller struct {
client internal.Loader[client.Reader]
collector *EncodedLogCollector

filters *filters
typeProvider EventTypeProvider
filters *filters
}

func New(lggr logger.SugaredLogger, orm ORM, cl internal.Loader[client.Reader]) ILogPoller {
Expand All @@ -73,9 +71,6 @@ func New(lggr logger.SugaredLogger, orm ORM, cl internal.Loader[client.Reader])
}

func (lp *LogPoller) start(context.Context) error {
if lp.typeProvider == nil {
return ErrMissingEventTypeProvider
}
cl, err := lp.client.Get()
if err != nil {
return err
Expand Down
39 changes: 33 additions & 6 deletions pkg/solana/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logpoller
import (
"context"
"encoding/base64"
"encoding/json"
"math/rand"
"testing"

Expand All @@ -18,6 +19,7 @@ import (

"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
clientmocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/client/mocks"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/codec"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/utils"
)

Expand All @@ -44,10 +46,35 @@ func TestProcess(t *testing.T) {
lggr := logger.Sugared(logger.Test(t))
lp := New(lggr, orm, loader)

var idlTypeInt64 codec.IdlType
var idlTypeString codec.IdlType

err := json.Unmarshal([]byte("\"i64\""), &idlTypeInt64)
require.NoError(t, err)
err = json.Unmarshal([]byte("\"string\""), &idlTypeString)
require.NoError(t, err)

idl := EventIdl{
codec.IdlEvent{
Name: "myEvent",
Fields: []codec.IdlEventField{{
Name: "A",
Type: idlTypeInt64,
}, {
Name: "B",
Type: idlTypeString,
}},
},
[]codec.IdlTypeDef{},
}

filter := Filter{
Name: "test filter",
Address: addr,
EventSig: eventSig,
Name: "test filter",
EventName: eventName,
Address: addr,
EventSig: eventSig,
EventIdl: idl,
SubkeyPaths: [][]string{{"A"}, {"B"}},
}
orm.EXPECT().SelectFilters(mock.Anything).Return([]Filter{filter}, nil)
orm.EXPECT().SelectSeqNums(mock.Anything).Return(map[int64]int64{}, nil)
Expand All @@ -62,12 +89,12 @@ func TestProcess(t *testing.T) {
assert.Equal(t, log, expectedLog)
return nil
})
err := lp.RegisterFilter(ctx, filter)
err = lp.RegisterFilter(ctx, filter)
require.NoError(t, err)

event := struct {
a int
b string
A int64
B string
}{55, "hello"}

data, err := bin.MarshalBorsh(&event)
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/logpoller/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (f Filter) MatchSameLogs(other Filter) bool {
}

func (f Filter) Discriminator() string {
d := utils.Discriminator("event", f.Name)
d := utils.Discriminator("event", f.EventName)
return base64.StdEncoding.EncodeToString(d[:])
}

Expand Down
47 changes: 47 additions & 0 deletions pkg/solana/logpoller/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package logpoller

import (
"math/rand"
"testing"
"time"

"github.com/gagliardetto/solana-go"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/utils"
)

func newRandomPublicKey(t *testing.T) PublicKey {
privateKey, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
pubKey := privateKey.PublicKey()
return PublicKey(pubKey)
}

func newRandomEventSignature(t *testing.T) EventSignature {
pubKey := newRandomPublicKey(t)
return EventSignature(pubKey[:8])
}

func newRandomLog(t *testing.T, filterID int64, chainID string, eventName string) Log {
privateKey, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
pubKey := privateKey.PublicKey()
data := []byte("solana is fun")
signature, err := privateKey.Sign(data)
require.NoError(t, err)
return Log{
FilterID: filterID,
ChainID: chainID,
LogIndex: rand.Int63n(1000),
BlockHash: Hash(pubKey),
BlockNumber: rand.Int63n(1000000),
BlockTimestamp: time.Unix(1731590113, 0),
Address: PublicKey(pubKey),
EventSig: utils.Discriminator("event", eventName),
SubkeyValues: [][]byte{{3, 2, 1}, {1}, {1, 2}, pubKey.Bytes()},
TxHash: Signature(signature),
Data: data,
SequenceNum: rand.Int63n(500),
}
}
4 changes: 0 additions & 4 deletions pkg/solana/logpoller/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ func (s EventSignature) Value() (driver.Value, error) {
return s[:], nil
}

type EventTypeProvider interface {
CreateType(eventIdl codec.IdlEvent, typedefSlice codec.IdlTypeDefSlice, subKeyPath []string) (any, error)
}

type Decoder interface {
CreateType(itemType string, _ bool) (any, error)
Decode(_ context.Context, raw []byte, into any, itemType string) error
Expand Down

0 comments on commit 8ef6ef8

Please sign in to comment.