mirror of
https://github.com/status-im/go-waku.git
synced 2025-01-27 22:15:38 +00:00
feat: implement rcp relay get messages
This commit is contained in:
parent
055227a4c6
commit
75516a8f96
@ -350,6 +350,10 @@ func (w *WakuNode) DiscV5() *discv5.DiscoveryV5 {
|
|||||||
return w.discoveryV5
|
return w.discoveryV5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WakuNode) Broadcaster() v2.Broadcaster {
|
||||||
|
return w.bcaster
|
||||||
|
}
|
||||||
|
|
||||||
func (w *WakuNode) mountRelay(opts ...pubsub.Option) error {
|
func (w *WakuNode) mountRelay(opts ...pubsub.Option) error {
|
||||||
var err error
|
var err error
|
||||||
w.relay, err = relay.NewWakuRelay(w.ctx, w.host, w.bcaster, opts...)
|
w.relay, err = relay.NewWakuRelay(w.ctx, w.host, w.bcaster, opts...)
|
||||||
|
@ -3,14 +3,22 @@ package rpc
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/status-im/go-waku/waku/v2/node"
|
"github.com/status-im/go-waku/waku/v2/node"
|
||||||
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
||||||
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
||||||
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RelayService struct {
|
type RelayService struct {
|
||||||
node *node.WakuNode
|
node *node.WakuNode
|
||||||
|
|
||||||
|
messages map[string][]*pb.WakuMessage
|
||||||
|
messagesMutex sync.RWMutex
|
||||||
|
|
||||||
|
ch chan *protocol.Envelope
|
||||||
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type RelayMessageArgs struct {
|
type RelayMessageArgs struct {
|
||||||
@ -22,6 +30,53 @@ type TopicsArgs struct {
|
|||||||
Topics []string `json:"topics,omitempty"`
|
Topics []string `json:"topics,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TopicArgs struct {
|
||||||
|
Topic string `json:"topic,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MessagesReply struct {
|
||||||
|
Messages []*pb.WakuMessage `json:"messages,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRelayService(node *node.WakuNode) *RelayService {
|
||||||
|
return &RelayService{
|
||||||
|
node: node,
|
||||||
|
messages: make(map[string][]*pb.WakuMessage),
|
||||||
|
quit: make(chan bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RelayService) addEnvelope(envelope *protocol.Envelope) {
|
||||||
|
r.messagesMutex.Lock()
|
||||||
|
defer r.messagesMutex.Unlock()
|
||||||
|
|
||||||
|
if _, ok := r.messages[envelope.PubsubTopic()]; !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.messages[envelope.PubsubTopic()] = append(r.messages[envelope.PubsubTopic()], envelope.Message())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RelayService) Start() {
|
||||||
|
r.ch = make(chan *protocol.Envelope, 1024)
|
||||||
|
r.node.Broadcaster().Register(r.ch)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-r.quit:
|
||||||
|
return
|
||||||
|
case envelope := <-r.ch:
|
||||||
|
r.addEnvelope(envelope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RelayService) Stop() {
|
||||||
|
r.quit <- true
|
||||||
|
r.node.Broadcaster().Unregister(r.ch)
|
||||||
|
close(r.ch)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
|
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
|
||||||
_, err := r.node.Relay().Publish(req.Context(), &args.Message, (*relay.Topic)(&args.Topic))
|
_, err := r.node.Relay().Publish(req.Context(), &args.Message, (*relay.Topic)(&args.Topic))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,6 +99,7 @@ func (r *RelayService) PostV1Subscription(req *http.Request, args *TopicsArgs, r
|
|||||||
reply.Error = err.Error()
|
reply.Error = err.Error()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
r.messages[topic] = make([]*pb.WakuMessage, 0)
|
||||||
}
|
}
|
||||||
reply.Success = true
|
reply.Success = true
|
||||||
return nil
|
return nil
|
||||||
@ -59,11 +115,22 @@ func (r *RelayService) DeleteV1Subscription(req *http.Request, args *TopicsArgs,
|
|||||||
reply.Error = err.Error()
|
reply.Error = err.Error()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete(r.messages, topic)
|
||||||
}
|
}
|
||||||
reply.Success = true
|
reply.Success = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RelayService) GetV1Messages(req *http.Request, args *Empty, reply *Empty) error {
|
func (r *RelayService) GetV1Messages(req *http.Request, args *TopicArgs, reply *MessagesReply) error {
|
||||||
return fmt.Errorf("not implemented")
|
r.messagesMutex.Lock()
|
||||||
|
defer r.messagesMutex.Unlock()
|
||||||
|
|
||||||
|
if _, ok := r.messages[args.Topic]; !ok {
|
||||||
|
return fmt.Errorf("topic %s not subscribed", args.Topic)
|
||||||
|
}
|
||||||
|
|
||||||
|
reply.Messages = r.messages[args.Topic]
|
||||||
|
r.messages[args.Topic] = make([]*pb.WakuMessage, 0)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,13 @@ package rpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/multiformats/go-multiaddr"
|
||||||
"github.com/status-im/go-waku/waku/v2/node"
|
"github.com/status-im/go-waku/waku/v2/node"
|
||||||
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,7 +18,7 @@ func makeRelayService(t *testing.T) *RelayService {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = n.Start()
|
err = n.Start()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return &RelayService{n}
|
return NewRelayService(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostV1Message(t *testing.T) {
|
func TestPostV1Message(t *testing.T) {
|
||||||
@ -53,3 +57,71 @@ func TestRelaySubscription(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, reply.Success)
|
require.True(t, reply.Success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRelayGetV1Messages(t *testing.T) {
|
||||||
|
serviceA := makeRelayService(t)
|
||||||
|
var reply SuccessReply
|
||||||
|
|
||||||
|
serviceB := makeRelayService(t)
|
||||||
|
go serviceB.Start()
|
||||||
|
defer serviceB.Stop()
|
||||||
|
|
||||||
|
hostInfo, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", serviceB.node.Host().ID().Pretty()))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var addr multiaddr.Multiaddr
|
||||||
|
for _, a := range serviceB.node.Host().Addrs() {
|
||||||
|
addr = a.Encapsulate(hostInfo)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = serviceA.node.DialPeerWithMultiAddress(context.Background(), addr)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Wait for the dial to complete
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
args := &TopicsArgs{Topics: []string{"test"}}
|
||||||
|
err = serviceB.PostV1Subscription(
|
||||||
|
makeRequest(t),
|
||||||
|
args,
|
||||||
|
&reply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, reply.Success)
|
||||||
|
|
||||||
|
// Wait for the subscription to be started
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
err = serviceA.PostV1Message(
|
||||||
|
makeRequest(t),
|
||||||
|
&RelayMessageArgs{
|
||||||
|
Topic: "test",
|
||||||
|
Message: pb.WakuMessage{
|
||||||
|
Payload: []byte("test"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&reply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, reply.Success)
|
||||||
|
|
||||||
|
// Wait for the message to be received
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
var messagesReply MessagesReply
|
||||||
|
err = serviceB.GetV1Messages(
|
||||||
|
makeRequest(t),
|
||||||
|
&TopicArgs{"test"},
|
||||||
|
&messagesReply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, messagesReply.Messages, 1)
|
||||||
|
|
||||||
|
err = serviceB.GetV1Messages(
|
||||||
|
makeRequest(t),
|
||||||
|
&TopicArgs{"test"},
|
||||||
|
&messagesReply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, messagesReply.Messages, 0)
|
||||||
|
}
|
||||||
|
@ -17,7 +17,7 @@ func makeStoreService(t *testing.T) *StoreService {
|
|||||||
return &StoreService{n}
|
return &StoreService{n}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetV1Message(t *testing.T) {
|
func TestStoreGetV1Messages(t *testing.T) {
|
||||||
var reply StoreMessagesReply
|
var reply StoreMessagesReply
|
||||||
|
|
||||||
s := makeStoreService(t)
|
s := makeStoreService(t)
|
||||||
|
@ -16,6 +16,8 @@ var log = logging.Logger("wakurpc")
|
|||||||
type WakuRpc struct {
|
type WakuRpc struct {
|
||||||
node *node.WakuNode
|
node *node.WakuNode
|
||||||
server *http.Server
|
server *http.Server
|
||||||
|
|
||||||
|
relayService *RelayService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
||||||
@ -28,7 +30,9 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.RegisterService(&RelayService{node}, "Relay")
|
relayService := NewRelayService(node)
|
||||||
|
|
||||||
|
err = s.RegisterService(relayService, "Relay")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
@ -67,10 +71,16 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
|||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &WakuRpc{node: node, server: server}
|
return &WakuRpc{
|
||||||
|
node: node,
|
||||||
|
server: server,
|
||||||
|
relayService: relayService,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *WakuRpc) Start() {
|
func (r *WakuRpc) Start() {
|
||||||
|
go r.relayService.Start()
|
||||||
|
defer r.relayService.Stop()
|
||||||
log.Info("Rpc server started at ", r.server.Addr)
|
log.Info("Rpc server started at ", r.server.Addr)
|
||||||
log.Info("server stopped ", r.server.ListenAndServe())
|
log.Info("server stopped ", r.server.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user