Skip to content

Commit

Permalink
chore_: remove waku's Criteria duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
osmaczko committed Jan 21, 2025
1 parent c3c9d0f commit e78a612
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 61 deletions.
16 changes: 1 addition & 15 deletions waku/bridge/public_waku_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/wakuv1"
wakuv1common "github.com/status-im/status-go/wakuv1/common"

wakutypes "github.com/status-im/status-go/waku/types"
)
Expand Down Expand Up @@ -45,20 +44,7 @@ func (w *GethPublicWakuAPIWrapper) DeleteKeyPair(ctx context.Context, key string
// NewMessageFilter creates a new filter that can be used to poll for
// (new) messages that satisfy the given criteria.
func (w *GethPublicWakuAPIWrapper) NewMessageFilter(req wakutypes.Criteria) (string, error) {
topics := make([]wakuv1common.TopicType, len(req.Topics))
for index, tt := range req.Topics {
topics[index] = wakuv1common.TopicType(tt)
}

criteria := wakuv1.Criteria{
SymKeyID: req.SymKeyID,
PrivateKeyID: req.PrivateKeyID,
Sig: req.Sig,
MinPow: req.MinPow,
Topics: topics,
AllowP2P: req.AllowP2P,
}
return w.api.NewMessageFilter(criteria)
return w.api.NewMessageFilter(req)
}

func (w *GethPublicWakuAPIWrapper) BloomFilter() []byte {
Expand Down
15 changes: 1 addition & 14 deletions waku/bridge/public_wakuv2_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/wakuv2"
wakucommon "github.com/status-im/status-go/wakuv2/common"

wakutypes "github.com/status-im/status-go/waku/types"
)
Expand Down Expand Up @@ -49,19 +48,7 @@ func (w *gethPublicWakuV2APIWrapper) BloomFilter() []byte {
// NewMessageFilter creates a new filter that can be used to poll for
// (new) messages that satisfy the given criteria.
func (w *gethPublicWakuV2APIWrapper) NewMessageFilter(req wakutypes.Criteria) (string, error) {
topics := make([]wakucommon.TopicType, len(req.Topics))
for index, tt := range req.Topics {
topics[index] = wakucommon.TopicType(tt)
}

criteria := wakuv2.Criteria{
SymKeyID: req.SymKeyID,
PrivateKeyID: req.PrivateKeyID,
Sig: req.Sig,
PubsubTopic: req.PubsubTopic,
ContentTopics: topics,
}
return w.api.NewMessageFilter(criteria)
return w.api.NewMessageFilter(req)
}

// GetFilterMessages returns the messages that match the filter criteria and
Expand Down
14 changes: 2 additions & 12 deletions wakuv1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,9 @@ func (api *PublicWakuAPI) Unsubscribe(id string) {
api.w.Unsubscribe(id) // nolint: errcheck
}

// Criteria holds various filter options for inbound messages.
type Criteria struct {
SymKeyID string `json:"symKeyID"`
PrivateKeyID string `json:"privateKeyID"`
Sig []byte `json:"sig"`
MinPow float64 `json:"minPow"`
Topics []common.TopicType `json:"topics"`
AllowP2P bool `json:"allowP2P"`
}

// Messages set up a subscription that fires events when messages arrive that match
// the given set of criteria.
func (api *PublicWakuAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Subscription, error) {
func (api *PublicWakuAPI) Messages(ctx context.Context, crit types.Criteria) (*rpc.Subscription, error) {
var (
symKeyGiven = len(crit.SymKeyID) > 0
pubKeyGiven = len(crit.PrivateKeyID) > 0
Expand Down Expand Up @@ -500,7 +490,7 @@ func (api *PublicWakuAPI) DeleteMessageFilter(id string) (bool, error) {

// NewMessageFilter creates a new filter that can be used to poll for
// (new) messages that satisfy the given criteria.
func (api *PublicWakuAPI) NewMessageFilter(req Criteria) (string, error) {
func (api *PublicWakuAPI) NewMessageFilter(req types.Criteria) (string, error) {
var (
src *ecdsa.PublicKey
keySym []byte
Expand Down
6 changes: 3 additions & 3 deletions wakuv1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"github.com/status-im/status-go/wakuv1/common"
"github.com/status-im/status-go/waku/types"
)

func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
Expand All @@ -41,9 +41,9 @@ func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}

crit := Criteria{
crit := types.Criteria{
SymKeyID: keyID,
Topics: []common.TopicType{common.TopicType(t1), common.TopicType(t2)},
Topics: []types.TopicType{types.TopicType(t1), types.TopicType(t2)},
}

_, err = api.NewMessageFilter(crit)
Expand Down
27 changes: 14 additions & 13 deletions wakuv2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,9 @@ func (api *PublicWakuAPI) Unsubscribe(ctx context.Context, id string) {
api.w.Unsubscribe(ctx, id) // nolint: errcheck
}

// Criteria holds various filter options for inbound messages.
type Criteria struct {
SymKeyID string `json:"symKeyID"`
PrivateKeyID string `json:"privateKeyID"`
Sig []byte `json:"sig"`
PubsubTopic string `json:"pubsubTopic"`
ContentTopics []common.TopicType `json:"topics"`
}

// Messages set up a subscription that fires events when messages arrive that match
// the given set of criteria.
func (api *PublicWakuAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Subscription, error) {
func (api *PublicWakuAPI) Messages(ctx context.Context, crit types.Criteria) (*rpc.Subscription, error) {
var (
symKeyGiven = len(crit.SymKeyID) > 0
pubKeyGiven = len(crit.PrivateKeyID) > 0
Expand Down Expand Up @@ -305,8 +296,13 @@ func (api *PublicWakuAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Sub
}
}

contentTopics := make([]common.TopicType, len(crit.Topics))
for index, tt := range crit.Topics {
contentTopics[index] = common.TopicType(tt)
}

filter.PubsubTopic = crit.PubsubTopic
filter.ContentTopics = common.NewTopicSet(crit.ContentTopics)
filter.ContentTopics = common.NewTopicSet(contentTopics)

// listen for message that are encrypted with the given symmetric key
if symKeyGiven {
Expand Down Expand Up @@ -446,7 +442,7 @@ func (api *PublicWakuAPI) DeleteMessageFilter(id string) (bool, error) {

// NewMessageFilter creates a new filter that can be used to poll for
// (new) messages that satisfy the given criteria.
func (api *PublicWakuAPI) NewMessageFilter(req Criteria) (string, error) {
func (api *PublicWakuAPI) NewMessageFilter(req types.Criteria) (string, error) {
var (
src *ecdsa.PublicKey
keySym []byte
Expand Down Expand Up @@ -484,12 +480,17 @@ func (api *PublicWakuAPI) NewMessageFilter(req Criteria) (string, error) {
}
}

topics := make([]common.TopicType, len(req.Topics))
for index, tt := range req.Topics {
topics[index] = common.TopicType(tt)
}

f := &common.Filter{
Src: src,
KeySym: keySym,
KeyAsym: keyAsym,
PubsubTopic: req.PubsubTopic,
ContentTopics: common.NewTopicSet(req.ContentTopics),
ContentTopics: common.NewTopicSet(topics),
Messages: common.NewMemoryMessageStore(),
}

Expand Down
9 changes: 5 additions & 4 deletions wakuv2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/exp/maps"

"github.com/status-im/status-go/protocol/common/shard"
"github.com/status-im/status-go/waku/types"
"github.com/status-im/status-go/wakuv2/common"
)

Expand All @@ -46,9 +47,9 @@ func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
t1 := common.TopicType([4]byte{0xde, 0xea, 0xbe, 0xef})
t2 := common.TopicType([4]byte{0xca, 0xfe, 0xde, 0xca})

crit := Criteria{
SymKeyID: keyID,
ContentTopics: []common.TopicType{t1, t2},
crit := types.Criteria{
SymKeyID: keyID,
Topics: []types.TopicType{types.TopicType(t1), types.TopicType(t2)},
}

_, err = api.NewMessageFilter(crit)
Expand All @@ -59,7 +60,7 @@ func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
found := false
candidates := w.filters.GetWatchersByTopic(shard.DefaultShardPubsubTopic(), t1)
for _, f := range candidates {
if maps.Equal(f.ContentTopics, common.NewTopicSet(crit.ContentTopics)) {
if maps.Equal(f.ContentTopics, common.NewTopicSet([]common.TopicType{t1, t2})) {
found = true
break
}
Expand Down

0 comments on commit e78a612

Please sign in to comment.