Skip to content

Commit

Permalink
MatchingFilters tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaidashenko committed Nov 25, 2024
1 parent f3dae35 commit 219d0da
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/smartcontractkit/chainlink-solana

go 1.22.0
go 1.23

toolchain go1.23.1

Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/logpoller/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (lp *filters) removeFilterFromIndexes(filter Filter) {

// MatchingFilters - returns iterator to go through all matching filters.
// Requires LoadFilters to be called at least once.
func (lp *filters) MatchingFilters(ctx context.Context, addr PublicKey, eventSignature EventSignature) iter.Seq[Filter] {
func (lp *filters) MatchingFilters(addr PublicKey, eventSignature EventSignature) iter.Seq[Filter] {
return func(yield func(Filter) bool) {
lp.filtersMutex.RLock()
defer lp.filtersMutex.RUnlock()
Expand Down
42 changes: 41 additions & 1 deletion pkg/solana/logpoller/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestFilters_UnregisterFilter(t *testing.T) {
})
}

func TestFilters_pruneFilters(t *testing.T) {
func TestFilters_PruneFilters(t *testing.T) {
lggr := logger.Sugared(logger.Test(t))
t.Run("Happy path", func(t *testing.T) {
orm := newMockORM(t)
Expand Down Expand Up @@ -249,3 +249,43 @@ func TestFilters_pruneFilters(t *testing.T) {
require.Equal(t, fs.filtersToDelete, []Filter{newToDelete, toDelete})
})
}

func TestFilters_MatchingFilters(t *testing.T) {
orm := newMockORM(t)
lggr := logger.Sugared(logger.Test(t))
expectedFilter1 := Filter{
Name: "expectedFilter1",
Address: newRandomPublicKey(t),
EventSig: newRandomEventSignature(t),
}
expectedFilter2 := Filter{
Name: "expectedFilter2",
Address: expectedFilter1.Address,
EventSig: expectedFilter1.EventSig,
}
sameAddress := Filter{
Name: "sameAddressWrongEventSig",
Address: expectedFilter1.Address,
EventSig: newRandomEventSignature(t),
}

sameEventSig := Filter{
Name: "wrongAddressSameEventSig",
Address: newRandomPublicKey(t),
EventSig: expectedFilter1.EventSig,
}
orm.On("SelectFilters", mock.Anything).Return([]Filter{expectedFilter1, expectedFilter2, sameAddress, sameEventSig}, nil).Once()
expectedFilters := map[string]struct{}{
expectedFilter1.Name: {},
expectedFilter2.Name: {},
}
filters := newFilters(lggr, orm)
err := filters.LoadFilters(tests.Context(t))
require.NoError(t, err)
for filter := range filters.MatchingFilters(expectedFilter1.Address, expectedFilter1.EventSig) {
_, ok := expectedFilters[filter.Name]
require.True(t, ok, "MatchingFilters returned unexpected filter %s", filter.Name)
delete(expectedFilters, filter.Name)
}
require.Len(t, expectedFilters, 0)
}
58 changes: 58 additions & 0 deletions pkg/solana/logpoller/models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package logpoller

import (
"testing"
"time"

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

func newRandomFilter(t *testing.T) Filter {
return Filter{
Name: uuid.NewString(),
Address: newRandomPublicKey(t),
EventName: "event",
EventSig: newRandomEventSignature(t),
StartingBlock: 1,
EventIDL: "{}",
SubkeyPaths: [][]string{{"a", "b"}, {"c"}},
Retention: 1000,
MaxLogsKept: 3,
}
}

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) 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: 1,
BlockHash: Hash(pubKey),
BlockNumber: 10,
BlockTimestamp: time.Unix(1731590113, 0),
Address: PublicKey(pubKey),
EventSig: EventSignature{3, 2, 1},
SubkeyValues: [][]byte{{3, 2, 1}, {1}, {1, 2}, pubKey.Bytes()},
TxHash: Signature(signature),
Data: data,
}
}
40 changes: 0 additions & 40 deletions pkg/solana/logpoller/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package logpoller
import (
"os"
"testing"
"time"

"github.com/gagliardetto/solana-go"
"github.com/google/uuid"
Expand Down Expand Up @@ -155,45 +154,6 @@ func TestLogPollerFilters(t *testing.T) {
})
}

func newRandomFilter(t *testing.T) Filter {
privateKey, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
pubKey := privateKey.PublicKey()
return Filter{
Name: uuid.NewString(),
Address: PublicKey(pubKey),
EventName: "event",
EventSig: EventSignature{1, 2, 3},
StartingBlock: 1,
EventIDL: "{}",
SubkeyPaths: [][]string{{"a", "b"}, {"c"}},
Retention: 1000,
MaxLogsKept: 3,
}
}

func newRandomLog(t *testing.T, filterID int64, chainID 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: 1,
BlockHash: Hash(pubKey),
BlockNumber: 10,
BlockTimestamp: time.Unix(1731590113, 0),
Address: PublicKey(pubKey),
EventSig: EventSignature{3, 2, 1},
SubkeyValues: [][]byte{{3, 2, 1}, {1}, {1, 2}, pubKey.Bytes()},
TxHash: Signature(signature),
Data: data,
}
}

func TestLogPollerLogs(t *testing.T) {
lggr := logger.Test(t)
dbURL, ok := os.LookupEnv("CL_DATABASE_URL")
Expand Down

0 comments on commit 219d0da

Please sign in to comment.