From a1cb371d5a8104f08c277a369c1a33c15e3bb556 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Sun, 7 Nov 2021 13:08:29 +0100 Subject: [PATCH] refactor: lightpush options and be more explicit about the relay node being present or not --- waku/v2/protocol/lightpush/waku_lightpush.go | 66 ++++--------------- .../lightpush/waku_lightpush_option.go | 51 ++++++++++++++ 2 files changed, 62 insertions(+), 55 deletions(-) create mode 100644 waku/v2/protocol/lightpush/waku_lightpush_option.go diff --git a/waku/v2/protocol/lightpush/waku_lightpush.go b/waku/v2/protocol/lightpush/waku_lightpush.go index 9ab7adf5..df90caa6 100644 --- a/waku/v2/protocol/lightpush/waku_lightpush.go +++ b/waku/v2/protocol/lightpush/waku_lightpush.go @@ -9,14 +9,12 @@ import ( logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" - "github.com/libp2p/go-libp2p-core/peer" libp2pProtocol "github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-msgio/protoio" "github.com/status-im/go-waku/waku/v2/metrics" "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/relay" - utils "github.com/status-im/go-waku/waku/v2/utils" ) var log = logging.Logger("waku_lightpush") @@ -44,8 +42,8 @@ func NewWakuLightPush(ctx context.Context, h host.Host, relay *relay.WakuRelay) } func (wakuLP *WakuLightPush) Start() error { - if wakuLP.relay == nil { - return errors.New("relay is required") + if wakuLP.IsClientOnly() { + return errors.New("relay is required, without it, it is only a client and cannot be started") } wakuLP.h.SetStreamHandlerMatch(LightPushID_v20beta1, protocol.PrefixTextMatch(string(LightPushID_v20beta1)), wakuLP.onRequest) @@ -54,6 +52,10 @@ func (wakuLP *WakuLightPush) Start() error { return nil } +func (wakuLp *WakuLightPush) IsClientOnly() bool { + return wakuLp.relay == nil +} + func (wakuLP *WakuLightPush) onRequest(s network.Stream) { defer s.Close() @@ -73,11 +75,11 @@ func (wakuLP *WakuLightPush) onRequest(s network.Stream) { if requestPushRPC.Query != nil { log.Info("lightpush push request") - pubSubTopic := relay.Topic(requestPushRPC.Query.PubsubTopic) - message := requestPushRPC.Query.Message - response := new(pb.PushResponse) - if wakuLP.relay != nil { + if !wakuLP.IsClientOnly() { + pubSubTopic := relay.Topic(requestPushRPC.Query.PubsubTopic) + message := requestPushRPC.Query.Message + // TODO: Assumes success, should probably be extended to check for network, peers, etc // It might make sense to use WithReadiness option here? @@ -118,56 +120,10 @@ func (wakuLP *WakuLightPush) onRequest(s network.Stream) { } } -type LightPushParameters struct { - selectedPeer peer.ID - requestId []byte - - lp *WakuLightPush -} - -type LightPushOption func(*LightPushParameters) - -func WithPeer(p peer.ID) LightPushOption { - return func(params *LightPushParameters) { - params.selectedPeer = p - } -} - -func WithAutomaticPeerSelection() LightPushOption { - return func(params *LightPushParameters) { - p, err := utils.SelectPeer(params.lp.h, string(LightPushID_v20beta1)) - if err == nil { - params.selectedPeer = *p - } else { - log.Info("Error selecting peer: ", err) - } - } -} - -func WithRequestId(requestId []byte) LightPushOption { - return func(params *LightPushParameters) { - params.requestId = requestId - } -} - -func WithAutomaticRequestId() LightPushOption { - return func(params *LightPushParameters) { - params.requestId = protocol.GenerateRequestId() - } -} - -func DefaultOptions() []LightPushOption { - return []LightPushOption{ - WithAutomaticRequestId(), - WithAutomaticPeerSelection(), - } -} - func (wakuLP *WakuLightPush) request(ctx context.Context, req *pb.PushRequest, opts ...LightPushOption) (*pb.PushResponse, error) { params := new(LightPushParameters) - params.lp = wakuLP - optList := DefaultOptions() + optList := DefaultOptions(wakuLP.h) optList = append(optList, opts...) for _, opt := range optList { opt(params) diff --git a/waku/v2/protocol/lightpush/waku_lightpush_option.go b/waku/v2/protocol/lightpush/waku_lightpush_option.go new file mode 100644 index 00000000..13758d56 --- /dev/null +++ b/waku/v2/protocol/lightpush/waku_lightpush_option.go @@ -0,0 +1,51 @@ +package lightpush + +import ( + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/status-im/go-waku/waku/v2/protocol" + "github.com/status-im/go-waku/waku/v2/utils" +) + +type LightPushParameters struct { + selectedPeer peer.ID + requestId []byte +} + +type LightPushOption func(*LightPushParameters) + +func WithPeer(p peer.ID) LightPushOption { + return func(params *LightPushParameters) { + params.selectedPeer = p + } +} + +func WithAutomaticPeerSelection(host host.Host) LightPushOption { + return func(params *LightPushParameters) { + p, err := utils.SelectPeer(host, string(LightPushID_v20beta1)) + if err == nil { + params.selectedPeer = *p + } else { + log.Info("Error selecting peer: ", err) + } + } +} + +func WithRequestId(requestId []byte) LightPushOption { + return func(params *LightPushParameters) { + params.requestId = requestId + } +} + +func WithAutomaticRequestId() LightPushOption { + return func(params *LightPushParameters) { + params.requestId = protocol.GenerateRequestId() + } +} + +func DefaultOptions(host host.Host) []LightPushOption { + return []LightPushOption{ + WithAutomaticRequestId(), + WithAutomaticPeerSelection(host), + } +}