Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore_: remove waku's type duplication #6264

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 3 additions & 50 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 All @@ -68,44 +54,11 @@ func (w *GethPublicWakuAPIWrapper) BloomFilter() []byte {
// GetFilterMessages returns the messages that match the filter criteria and
// are received between the last poll and now.
func (w *GethPublicWakuAPIWrapper) GetFilterMessages(id string) ([]*wakutypes.Message, error) {
msgs, err := w.api.GetFilterMessages(id)
if err != nil {
return nil, err
}

wrappedMsgs := make([]*wakutypes.Message, len(msgs))
for index, msg := range msgs {
wrappedMsgs[index] = &wakutypes.Message{
Sig: msg.Sig,
TTL: msg.TTL,
Timestamp: msg.Timestamp,
Topic: wakutypes.TopicType(msg.Topic),
Payload: msg.Payload,
Padding: msg.Padding,
PoW: msg.PoW,
Hash: msg.Hash,
Dst: msg.Dst,
P2P: msg.P2P,
}
}
return wrappedMsgs, nil
return w.api.GetFilterMessages(id)
}

// Post posts a message on the network.
// returns the hash of the message in case of success.
func (w *GethPublicWakuAPIWrapper) Post(ctx context.Context, req wakutypes.NewMessage) ([]byte, error) {
msg := wakuv1.NewMessage{
SymKeyID: req.SymKeyID,
PublicKey: req.PublicKey,
Sig: req.SigID, // Sig is really a SigID
TTL: req.TTL,
Topic: wakuv1common.TopicType(req.Topic),
Payload: req.Payload,
Padding: req.Padding,
PowTime: req.PowTime,
PowTarget: req.PowTarget,
TargetPeer: req.TargetPeer,
Ephemeral: req.Ephemeral,
}
return w.api.Post(ctx, msg)
return w.api.Post(ctx, req)
}
49 changes: 3 additions & 46 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,59 +48,17 @@ 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
// are received between the last poll and now.
func (w *gethPublicWakuV2APIWrapper) GetFilterMessages(id string) ([]*wakutypes.Message, error) {
msgs, err := w.api.GetFilterMessages(id)
if err != nil {
return nil, err
}

wrappedMsgs := make([]*wakutypes.Message, len(msgs))
for index, msg := range msgs {
wrappedMsgs[index] = &wakutypes.Message{
Sig: msg.Sig,
Timestamp: msg.Timestamp,
PubsubTopic: msg.PubsubTopic,
Topic: wakutypes.TopicType(msg.ContentTopic),
Payload: msg.Payload,
Padding: msg.Padding,
Hash: msg.Hash,
Dst: msg.Dst,
}
}
return wrappedMsgs, nil
return w.api.GetFilterMessages(id)
}

// Post posts a message on the network.
// returns the hash of the message in case of success.
func (w *gethPublicWakuV2APIWrapper) Post(ctx context.Context, req wakutypes.NewMessage) ([]byte, error) {
msg := wakuv2.NewMessage{
SymKeyID: req.SymKeyID,
PublicKey: req.PublicKey,
Sig: req.SigID, // Sig is really a SigID
PubsubTopic: req.PubsubTopic,
ContentTopic: wakucommon.TopicType(req.Topic),
Payload: req.Payload,
Padding: req.Padding,
TargetPeer: req.TargetPeer,
Ephemeral: req.Ephemeral,
Priority: req.Priority,
}
return w.api.Post(ctx, msg)
return w.api.Post(ctx, req)
}
66 changes: 14 additions & 52 deletions wakuv1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.uber.org/zap"

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

"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -209,24 +210,9 @@ func (api *PublicWakuAPI) CancelLightClient(ctx context.Context) bool {
return !api.w.LightClientMode()
}

// NewMessage represents a new waku message that is posted through the RPC.
type NewMessage struct {
SymKeyID string `json:"symKeyID"`
PublicKey []byte `json:"pubKey"`
Sig string `json:"sig"`
TTL uint32 `json:"ttl"`
Topic common.TopicType `json:"topic"`
Payload []byte `json:"payload"`
Padding []byte `json:"padding"`
PowTime uint32 `json:"powTime"`
PowTarget float64 `json:"powTarget"`
TargetPeer string `json:"targetPeer"`
Ephemeral bool `json:"ephemeral"`
}

// Post posts a message on the Waku network.
// returns the hash of the message in case of success.
func (api *PublicWakuAPI) Post(ctx context.Context, req NewMessage) (hexutil.Bytes, error) {
func (api *PublicWakuAPI) Post(ctx context.Context, req types.NewMessage) (hexutil.Bytes, error) {
var (
symKeyGiven = len(req.SymKeyID) > 0
pubKeyGiven = len(req.PublicKey) > 0
Expand All @@ -244,12 +230,12 @@ func (api *PublicWakuAPI) Post(ctx context.Context, req NewMessage) (hexutil.Byt
Padding: req.Padding,
WorkTime: req.PowTime,
PoW: req.PowTarget,
Topic: req.Topic,
Topic: common.TopicType(req.Topic),
}

// Set key that is used to sign the message
if len(req.Sig) > 0 {
if params.Src, err = api.w.GetPrivateKey(req.Sig); err != nil {
if len(req.SigID) > 0 {
if params.Src, err = api.w.GetPrivateKey(req.SigID); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -323,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 @@ -426,30 +402,16 @@ func (api *PublicWakuAPI) Messages(ctx context.Context, crit Criteria) (*rpc.Sub
return rpcSub, nil
}

// Message is the RPC representation of a waku message.
type Message struct {
Sig []byte `json:"sig,omitempty"`
TTL uint32 `json:"ttl"`
Timestamp uint32 `json:"timestamp"`
Topic common.TopicType `json:"topic"`
Payload []byte `json:"payload"`
Padding []byte `json:"padding"`
PoW float64 `json:"pow"`
Hash []byte `json:"hash"`
Dst []byte `json:"recipientPublicKey,omitempty"`
P2P bool `json:"bool,omitempty"`
}

// ToWakuMessage converts an internal message into an API version.
func ToWakuMessage(message *common.ReceivedMessage) *Message {
msg := Message{
func ToWakuMessage(message *common.ReceivedMessage) *types.Message {
msg := types.Message{
Payload: message.Payload,
Padding: message.Padding,
Timestamp: message.Sent,
TTL: message.TTL,
PoW: message.PoW,
Hash: message.EnvelopeHash.Bytes(),
Topic: message.Topic,
Topic: types.TopicType(message.Topic),
P2P: message.P2P,
}

Expand All @@ -471,8 +433,8 @@ func ToWakuMessage(message *common.ReceivedMessage) *Message {
}

// toMessage converts a set of messages to its RPC representation.
func toMessage(messages []*common.ReceivedMessage) []*Message {
msgs := make([]*Message, len(messages))
func toMessage(messages []*common.ReceivedMessage) []*types.Message {
msgs := make([]*types.Message, len(messages))
for i, msg := range messages {
msgs[i] = ToWakuMessage(msg)
}
Expand All @@ -481,7 +443,7 @@ func toMessage(messages []*common.ReceivedMessage) []*Message {

// GetFilterMessages returns the messages that match the filter criteria and
// are received between the last poll and now.
func (api *PublicWakuAPI) GetFilterMessages(id string) ([]*Message, error) {
func (api *PublicWakuAPI) GetFilterMessages(id string) ([]*types.Message, error) {
logger := api.w.logger.With(zap.String("site", "getFilterMessages"), zap.String("filterId", id))
api.mu.Lock()
f := api.w.GetFilter(id)
Expand All @@ -493,7 +455,7 @@ func (api *PublicWakuAPI) GetFilterMessages(id string) ([]*Message, error) {
api.mu.Unlock()

receivedMessages := f.Retrieve()
messages := make([]*Message, 0, len(receivedMessages))
messages := make([]*types.Message, 0, len(receivedMessages))
for _, msg := range receivedMessages {

logger.Debug("retrieved filter message", zap.String("hash", msg.EnvelopeHash.String()), zap.Bool("isP2P", msg.P2P), zap.String("topic", msg.Topic.String()))
Expand All @@ -514,7 +476,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
Loading
Loading