Skip to content

Commit

Permalink
Remove unused functions in anchor.go, add validation to TestProess test
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Jan 10, 2025
1 parent 1514f71 commit 056e40e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 282 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/smartcontractkit/chainlink-common v0.4.1-0.20241223143929-db7919d60550
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/stretchr/testify v1.9.0
github.com/test-go/testify v1.1.4
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/sync v0.10.0
Expand Down
11 changes: 1 addition & 10 deletions pkg/solana/logpoller/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,6 @@ func (fl *filters) RegisterFilter(ctx context.Context, filter Filter) error {

filter.ID = filterID

idl := codec.IDL{
Events: []codec.IdlEvent{filter.EventIdl.IdlEvent},
Types: filter.EventIdl.IdlTypeDefSlice,
}
fl.decoders[filter.ID], err = codec.NewIDLEventCodec(idl, binary.LittleEndian())
if err != nil {
return fmt.Errorf("failed to create event decoder: %w", err)
}

fl.filtersByName[filter.Name] = filter.ID
fl.filtersByID[filter.ID] = &filter
filtersForAddress, ok := fl.filtersByAddress[filter.Address]
Expand Down Expand Up @@ -162,7 +153,7 @@ func (fl *filters) RegisterFilter(ctx context.Context, filter Filter) error {
}

discriminatorHead := filter.Discriminator()[:10]
if _, ok := fl.knownPrograms[programID]; !ok {
if _, ok := fl.knownDiscriminators[discriminatorHead]; !ok {
fl.knownDiscriminators[discriminatorHead] = 1
} else {
fl.knownDiscriminators[discriminatorHead]++
Expand Down
64 changes: 54 additions & 10 deletions pkg/solana/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,94 @@ package logpoller

import (
"context"
"encoding/base64"
"math/rand"
"testing"

bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
"github.com/google/uuid"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
commonutils "github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"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/logpoller/utils"
)

func TestProcess(t *testing.T) {
ctx := tests.Context(t)

addr := newRandomPublicKey(t)
sig := newRandomEventSignature(t)
eventName := "myEvent"
eventSig := utils.Discriminator("event", eventName)

filterID := rand.Int63()
chainID := uuid.NewString()

txIndex := int(rand.Int31())
txLogIndex := uint(rand.Uint32())

expectedLog := newRandomLog(t, filterID, chainID, eventName)

expectedLog.LogIndex = makeLogIndex(txIndex, txLogIndex)

orm := newMockORM(t)
cl := clientmocks.NewReaderWriter(t)
loader := utils.NewLazyLoad(func() (client.Reader, error) { return cl, nil })
loader := commonutils.NewLazyLoad(func() (client.Reader, error) { return cl, nil })
lggr := logger.Sugared(logger.Test(t))
lp := New(lggr, orm, loader)

filter := Filter{
Name: "test filter",
Address: addr,
EventSig: sig,
EventSig: eventSig,
}
orm.EXPECT().SelectFilters(mock.Anything).Return([]Filter{filter}, nil)
orm.EXPECT().SelectSeqNums(mock.Anything).Return(map[int64]int64{}, nil)
orm.EXPECT().InsertFilter(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, f Filter) (int64, error) {
require.Equal(t, f, filter)
return 1, nil
return filterID, nil
}).Once()
// TODO orm.EXPECT().InsertLogs(mock.Anything, mock.Anything).RunAndReturn(nil) validate logs written properly

orm.EXPECT().InsertLogs(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, logs []Log) error {
require.Len(t, logs, 1)
log := logs[0]
assert.Equal(t, log, expectedLog)
return nil
})
lp.RegisterFilter(ctx, filter)

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

data, err := bin.MarshalBorsh(&event)
require.NoError(t, err)
data = append(eventSig[:], data...)

require.NoError(t, err)

ev := ProgramEvent{
Program: "myprog", // TODO: fix program address, so filters match
Prefix: "prefix",
BlockData: BlockData{SlotNumber: 3, BlockHeight: 5},
Program: addr.ToSolana().String(),
Prefix: ">",
BlockData: BlockData{
SlotNumber: 3,
BlockHeight: 5,
BlockHash: solana.HashFromBytes([]byte{1, 2, 3}),
BlockTime: solana.UnixTimeSeconds(expectedLog.BlockTimestamp.Unix()),
TransactionHash: expectedLog.TxHash.ToSolana(),
TransactionIndex: txIndex,
TransactionLogIndex: txLogIndex,
},
Data: base64.StdEncoding.EncodeToString(data),
}
err := lp.Process(ev)
err = lp.Process(ev)
require.NoError(t, err)

orm.EXPECT().MarkFilterDeleted(mock.Anything, mock.Anything).Return(nil).Once()
Expand Down
13 changes: 0 additions & 13 deletions pkg/solana/logpoller/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@ package logpoller
import (
"testing"

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

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 TestIndexedValue(t *testing.T) {
cases := []struct {
typeName string
Expand Down
Loading

0 comments on commit 056e40e

Please sign in to comment.