Skip to content

Commit

Permalink
fix: message reply formating and private rpc logic
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Jun 13, 2022
1 parent 10319c3 commit 4c4fcca
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 99 deletions.
2 changes: 1 addition & 1 deletion waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func main() {
},
&cli.StringFlag{
Name: "nat",
Usage: "TODO",
Usage: "TODO - Not implemented yet.", // This was added so js-waku test don't fail
Destination: &options.NAT,
},
&cli.StringFlag{
Expand Down
20 changes: 12 additions & 8 deletions waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/libp2p/go-libp2p"
"go.uber.org/zap"
Expand Down Expand Up @@ -95,19 +96,24 @@ func defaultStoreFactory(w *WakuNode) store.Store {
func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) {
params := new(WakuNodeParameters)

ctx, cancel := context.WithCancel(ctx)

params.libP2POpts = DefaultLibP2POptions

opts = append(DefaultWakuNodeOptions, opts...)
for _, opt := range opts {
err := opt(params)
if err != nil {
cancel()
return nil, err
}
}

if params.privKey == nil {
prvKey, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
params.privKey = prvKey
}

if params.enableWSS {
params.libP2POpts = append(params.libP2POpts, libp2p.Transport(ws.New, ws.WithTLSConfig(params.tlsConfig)))
} else if params.enableWS {
Expand All @@ -118,28 +124,26 @@ func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) {
if params.hostAddr == nil {
err := WithHostAddress(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})(params)
if err != nil {
cancel()
return nil, err
}
}
if len(params.multiAddr) > 0 {
params.libP2POpts = append(params.libP2POpts, libp2p.ListenAddrs(params.multiAddr...))
}

if params.privKey != nil {
params.libP2POpts = append(params.libP2POpts, params.Identity())
}
params.libP2POpts = append(params.libP2POpts, params.Identity())

if params.addressFactory != nil {
params.libP2POpts = append(params.libP2POpts, libp2p.AddrsFactory(params.addressFactory))
}

host, err := libp2p.New(params.libP2POpts...)
if err != nil {
cancel()
return nil, err
}

ctx, cancel := context.WithCancel(ctx)

w := new(WakuNode)
w.bcaster = v2.NewBroadcaster(1024)
w.host = host
Expand Down
5 changes: 4 additions & 1 deletion waku/v2/rpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ func (f *FilterService) GetV1Messages(req *http.Request, args *ContentTopicArgs,
return fmt.Errorf("topic %s not subscribed", args.ContentTopic)
}

reply.Messages = f.messages[args.ContentTopic]
for i := range f.messages[args.ContentTopic] {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(f.messages[args.ContentTopic][i]))
}

f.messages[args.ContentTopic] = make([]*pb.WakuMessage, 0)
return nil
}
4 changes: 2 additions & 2 deletions waku/v2/rpc/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ func TestFilterGetV1Messages(t *testing.T) {
&messagesReply,
)
require.NoError(t, err)
require.Len(t, messagesReply.Messages, 1)
require.Len(t, messagesReply, 1)

err = serviceB.GetV1Messages(
makeRequest(t),
&ContentTopicArgs{"ct"},
&messagesReply,
)
require.NoError(t, err)
require.Len(t, messagesReply.Messages, 0)
require.Len(t, messagesReply, 0)
}
Loading

0 comments on commit 4c4fcca

Please sign in to comment.