2021-11-06 10:49:47 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2023-09-07 18:00:59 +00:00
|
|
|
"errors"
|
2021-11-18 13:21:36 +00:00
|
|
|
"fmt"
|
2021-11-06 10:49:47 +00:00
|
|
|
"net/http"
|
2021-11-18 14:20:58 +00:00
|
|
|
"sync"
|
2021-11-06 10:49:47 +00:00
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
"github.com/waku-org/go-waku/cmd/waku/server"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-06 10:49:47 +00:00
|
|
|
)
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// RelayService represents the JSON RPC service for WakuRelay
|
2021-11-06 10:49:47 +00:00
|
|
|
type RelayService struct {
|
|
|
|
node *node.WakuNode
|
2021-11-18 14:20:58 +00:00
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2022-01-18 18:17:06 +00:00
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
messages map[string][]*pb.WakuMessage
|
2022-10-20 13:18:23 +00:00
|
|
|
cacheCapacity int
|
2021-11-18 14:20:58 +00:00
|
|
|
messagesMutex sync.RWMutex
|
|
|
|
|
2021-11-22 14:48:32 +00:00
|
|
|
runner *runnerService
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// RelayMessageArgs represents the requests used for posting messages
|
2021-11-06 10:49:47 +00:00
|
|
|
type RelayMessageArgs struct {
|
2023-02-13 12:52:53 +00:00
|
|
|
Topic string `json:"topic,omitempty"`
|
2023-02-17 03:35:22 +00:00
|
|
|
Message *RPCWakuMessage `json:"message,omitempty"`
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
// RelayAutoMessageArgs represents the requests used for posting messages
|
|
|
|
type RelayAutoMessageArgs struct {
|
|
|
|
Message *RPCWakuMessage `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// TopicsArgs represents the lists of topics to use when subscribing / unsubscribing
|
2021-11-06 10:49:47 +00:00
|
|
|
type TopicsArgs struct {
|
|
|
|
Topics []string `json:"topics,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// TopicArgs represents a request that contains a single topic
|
2021-11-18 14:20:58 +00:00
|
|
|
type TopicArgs struct {
|
|
|
|
Topic string `json:"topic,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// NewRelayService returns an instance of RelayService
|
2022-10-20 13:18:23 +00:00
|
|
|
func NewRelayService(node *node.WakuNode, cacheCapacity int, log *zap.Logger) *RelayService {
|
2021-11-22 14:48:32 +00:00
|
|
|
s := &RelayService{
|
2022-10-20 13:18:23 +00:00
|
|
|
node: node,
|
|
|
|
cacheCapacity: cacheCapacity,
|
|
|
|
log: log.Named("relay"),
|
|
|
|
messages: make(map[string][]*pb.WakuMessage),
|
2021-11-18 14:20:58 +00:00
|
|
|
}
|
2021-11-22 14:48:32 +00:00
|
|
|
|
|
|
|
s.runner = newRunnerService(node.Broadcaster(), s.addEnvelope)
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-11-22 14:48:32 +00:00
|
|
|
return s
|
2021-11-18 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayService) addEnvelope(envelope *protocol.Envelope) {
|
|
|
|
r.messagesMutex.Lock()
|
|
|
|
defer r.messagesMutex.Unlock()
|
|
|
|
|
|
|
|
if _, ok := r.messages[envelope.PubsubTopic()]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:18:23 +00:00
|
|
|
// Keep a specific max number of messages per topic
|
|
|
|
if len(r.messages[envelope.PubsubTopic()]) >= r.cacheCapacity {
|
|
|
|
r.messages[envelope.PubsubTopic()] = r.messages[envelope.PubsubTopic()][1:]
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
r.messages[envelope.PubsubTopic()] = append(r.messages[envelope.PubsubTopic()], envelope.Message())
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// Start starts the RelayService
|
2021-11-18 14:20:58 +00:00
|
|
|
func (r *RelayService) Start() {
|
2023-02-07 22:26:07 +00:00
|
|
|
r.messagesMutex.Lock()
|
2022-06-13 18:30:35 +00:00
|
|
|
// Node may already be subscribed to some topics when Relay API handlers are installed. Let's add these
|
|
|
|
for _, topic := range r.node.Relay().Topics() {
|
|
|
|
r.log.Info("adding topic handler for existing subscription", zap.String("topic", topic))
|
|
|
|
r.messages[topic] = make([]*pb.WakuMessage, 0)
|
|
|
|
}
|
2023-02-07 22:26:07 +00:00
|
|
|
r.messagesMutex.Unlock()
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-11-22 14:48:32 +00:00
|
|
|
r.runner.Start()
|
2021-11-18 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// Stop stops the RelayService
|
2021-11-18 14:20:58 +00:00
|
|
|
func (r *RelayService) Stop() {
|
2021-11-22 14:48:32 +00:00
|
|
|
r.runner.Stop()
|
2021-11-18 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// PostV1Message is invoked when the json rpc request uses the post_waku_v2_relay_v1_message method
|
2021-11-06 10:49:47 +00:00
|
|
|
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
|
2021-11-19 20:01:52 +00:00
|
|
|
var err error
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
topic := relay.DefaultWakuTopic
|
|
|
|
if args.Topic != "" {
|
|
|
|
topic = args.Topic
|
2021-11-19 20:01:52 +00:00
|
|
|
}
|
2023-09-07 18:00:59 +00:00
|
|
|
|
|
|
|
if !r.node.Relay().IsSubscribed(topic) {
|
|
|
|
return errors.New("not subscribed to pubsubTopic")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := args.Message.toProto()
|
|
|
|
|
|
|
|
if err = server.AppendRLNProof(r.node, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.node.Relay().PublishToTopic(req.Context(), msg, topic)
|
2021-11-06 10:49:47 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
r.log.Error("publishing message", zap.Error(err))
|
2022-06-14 15:36:34 +00:00
|
|
|
return err
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
2022-06-14 15:36:34 +00:00
|
|
|
|
|
|
|
*reply = true
|
2021-11-06 10:49:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
// PostV1AutoSubscription is invoked when the json rpc request uses the post_waku_v2_relay_v1_auto_subscription
|
|
|
|
// Note that this method takes contentTopics as an argument instead of pubsubtopics and uses autosharding to derive pubsubTopics.
|
|
|
|
func (r *RelayService) PostV1AutoSubscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
|
|
|
|
|
|
|
|
_, err := r.node.Relay().Subscribe(r.node.Relay().Context(), protocol.NewContentFilter("", args.Topics...))
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("subscribing to topics", zap.Strings("topics", args.Topics), zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//TODO: Handle partial errors.
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteV1AutoSubscription is invoked when the json rpc request uses the delete_waku_v2_relay_v1_auto_subscription
|
|
|
|
// Note that this method takes contentTopics as an argument instead of pubsubtopics and uses autosharding to derive pubsubTopics.
|
|
|
|
func (r *RelayService) DeleteV1AutoSubscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
|
|
|
|
ctx := req.Context()
|
|
|
|
|
|
|
|
err := r.node.Relay().Unsubscribe(ctx, protocol.NewContentFilter("", args.Topics...))
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("unsubscribing from topics", zap.Strings("topic", args.Topics), zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//TODO: Handle partial errors.
|
|
|
|
*reply = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostV1AutoMessage is invoked when the json rpc request uses the post_waku_v2_relay_v1_auto_message
|
|
|
|
func (r *RelayService) PostV1AutoMessage(req *http.Request, args *RelayAutoMessageArgs, reply *SuccessReply) error {
|
|
|
|
var err error
|
|
|
|
msg := args.Message.toProto()
|
|
|
|
if msg == nil {
|
|
|
|
err := fmt.Errorf("invalid message format received")
|
|
|
|
r.log.Error("publishing message", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2023-10-23 22:59:02 +00:00
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
if err = server.AppendRLNProof(r.node, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.node.Relay().Publish(req.Context(), msg)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("publishing message", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetV1AutoMessages is invoked when the json rpc request uses the get_waku_v2_relay_v1_auto_messages method
|
|
|
|
// Note that this method takes contentTopic as an argument instead of pubSubtopic and uses autosharding.
|
|
|
|
func (r *RelayService) GetV1AutoMessages(req *http.Request, args *TopicArgs, reply *MessagesReply) error {
|
|
|
|
sub, err := r.node.Relay().GetSubscription(args.Topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case msg := <-sub.Ch:
|
|
|
|
*reply = append(*reply, ProtoToRPC(msg.Message()))
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// PostV1Subscription is invoked when the json rpc request uses the post_waku_v2_relay_v1_subscription method
|
2021-11-06 10:49:47 +00:00
|
|
|
func (r *RelayService) PostV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
|
|
|
|
ctx := req.Context()
|
2023-10-20 19:56:18 +00:00
|
|
|
|
2021-11-06 10:49:47 +00:00
|
|
|
for _, topic := range args.Topics {
|
2021-11-19 20:01:52 +00:00
|
|
|
var err error
|
|
|
|
if topic == "" {
|
2023-10-20 19:56:18 +00:00
|
|
|
topic = relay.DefaultWakuTopic
|
2021-11-19 20:01:52 +00:00
|
|
|
}
|
2023-10-20 19:56:18 +00:00
|
|
|
var sub *relay.Subscription
|
|
|
|
subs, err := r.node.Relay().Subscribe(ctx, protocol.NewContentFilter(topic))
|
2021-11-06 10:49:47 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
r.log.Error("subscribing to topic", zap.String("topic", topic), zap.Error(err))
|
2022-06-14 15:36:34 +00:00
|
|
|
return err
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
2023-10-20 19:56:18 +00:00
|
|
|
sub = subs[0]
|
|
|
|
sub.Unsubscribe()
|
2022-11-09 14:13:21 +00:00
|
|
|
r.messagesMutex.Lock()
|
2021-11-18 14:20:58 +00:00
|
|
|
r.messages[topic] = make([]*pb.WakuMessage, 0)
|
2022-11-09 14:13:21 +00:00
|
|
|
r.messagesMutex.Unlock()
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
2022-06-14 15:36:34 +00:00
|
|
|
|
|
|
|
*reply = true
|
2021-11-06 10:49:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// DeleteV1Subscription is invoked when the json rpc request uses the delete_waku_v2_relay_v1_subscription method
|
2021-11-06 10:49:47 +00:00
|
|
|
func (r *RelayService) DeleteV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
|
|
|
|
ctx := req.Context()
|
|
|
|
for _, topic := range args.Topics {
|
2023-10-20 19:56:18 +00:00
|
|
|
err := r.node.Relay().Unsubscribe(ctx, protocol.NewContentFilter(topic))
|
2021-11-06 10:49:47 +00:00
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
r.log.Error("unsubscribing from topic", zap.String("topic", topic), zap.Error(err))
|
2022-06-14 15:36:34 +00:00
|
|
|
return err
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
2021-11-18 14:20:58 +00:00
|
|
|
|
|
|
|
delete(r.messages, topic)
|
2021-11-06 10:49:47 +00:00
|
|
|
}
|
2022-06-14 15:36:34 +00:00
|
|
|
|
|
|
|
*reply = true
|
2021-11-06 10:49:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-18 13:21:36 +00:00
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// GetV1Messages is invoked when the json rpc request uses the get_waku_v2_relay_v1_messages method
|
2023-02-17 03:35:22 +00:00
|
|
|
func (r *RelayService) GetV1Messages(req *http.Request, args *TopicArgs, reply *MessagesReply) error {
|
2021-11-18 14:20:58 +00:00
|
|
|
r.messagesMutex.Lock()
|
|
|
|
defer r.messagesMutex.Unlock()
|
|
|
|
|
|
|
|
if _, ok := r.messages[args.Topic]; !ok {
|
|
|
|
return fmt.Errorf("topic %s not subscribed", args.Topic)
|
|
|
|
}
|
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
for i := range r.messages[args.Topic] {
|
|
|
|
*reply = append(*reply, ProtoToRPC(r.messages[args.Topic][i]))
|
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
r.messages[args.Topic] = make([]*pb.WakuMessage, 0)
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-11-18 14:20:58 +00:00
|
|
|
return nil
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|