2021-11-07 12:08:29 +00:00
|
|
|
package lightpush
|
|
|
|
|
|
|
|
import (
|
2023-11-15 14:26:55 +00:00
|
|
|
"errors"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-11-15 14:26:55 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-08-10 12:58:22 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peermanager"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2023-10-30 16:30:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-07 12:08:29 +00:00
|
|
|
)
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
type lightPushParameters struct {
|
2023-10-16 16:42:01 +00:00
|
|
|
host host.Host
|
2023-11-15 14:26:55 +00:00
|
|
|
peerAddr multiaddr.Multiaddr
|
2023-10-16 16:42:01 +00:00
|
|
|
selectedPeer peer.ID
|
|
|
|
peerSelectionType peermanager.PeerSelection
|
|
|
|
preferredPeers peer.IDSlice
|
|
|
|
requestID []byte
|
|
|
|
pm *peermanager.PeerManager
|
|
|
|
log *zap.Logger
|
|
|
|
pubsubTopic string
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// Option is the type of options accepted when performing LightPush protocol requests
|
2023-11-15 14:26:55 +00:00
|
|
|
type Option func(*lightPushParameters) error
|
2021-11-07 12:08:29 +00:00
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithPeer is an option used to specify the peerID to push a waku message to
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithPeer(p peer.ID) Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2021-11-07 12:08:29 +00:00
|
|
|
params.selectedPeer = p
|
2023-11-15 14:26:55 +00:00
|
|
|
if params.peerAddr != nil {
|
|
|
|
return errors.New("peerAddr and peerId options are mutually exclusive")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPeerAddr is an option used to specify a peerAddress
|
|
|
|
// This new peer will be added to peerStore.
|
|
|
|
// Note that this option is mutually exclusive to WithPeerAddr, only one of them can be used.
|
|
|
|
func WithPeerAddr(pAddr multiaddr.Multiaddr) Option {
|
|
|
|
return func(params *lightPushParameters) error {
|
|
|
|
params.peerAddr = pAddr
|
|
|
|
if params.selectedPeer != "" {
|
|
|
|
return errors.New("peerAddr and peerId options are mutually exclusive")
|
|
|
|
}
|
|
|
|
return nil
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithAutomaticPeerSelection is an option used to randomly select a peer from the peer store
|
2022-11-24 21:50:43 +00:00
|
|
|
// to push a waku message to. If a list of specific peers is passed, the peer will be chosen
|
|
|
|
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
|
|
|
|
// from the node peerstore
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-10-16 16:42:01 +00:00
|
|
|
params.peerSelectionType = peermanager.Automatic
|
|
|
|
params.preferredPeers = fromThesePeers
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// WithFastestPeerSelection is an option used to select a peer from the peer store
|
2022-11-24 21:50:43 +00:00
|
|
|
// with the lowest ping. If a list of specific peers is passed, the peer will be chosen
|
|
|
|
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
|
|
|
|
// from the node peerstore
|
2023-10-16 16:42:01 +00:00
|
|
|
func WithFastestPeerSelection(fromThesePeers ...peer.ID) Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-10-16 16:42:01 +00:00
|
|
|
params.peerSelectionType = peermanager.LowestRTT
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2021-11-09 23:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:30:25 +00:00
|
|
|
// WithPubSubTopic is used to specify the pubsub topic on which a WakuMessage will be broadcasted
|
|
|
|
func WithPubSubTopic(pubsubTopic string) Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-10-30 16:30:25 +00:00
|
|
|
params.pubsubTopic = pubsubTopic
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2023-10-30 16:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDefaultPubsubTopic is used to indicate that the message should be broadcasted in the default pubsub topic
|
|
|
|
func WithDefaultPubsubTopic() Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-10-30 16:30:25 +00:00
|
|
|
params.pubsubTopic = relay.DefaultWakuTopic
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2023-10-30 16:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// WithRequestID is an option to set a specific request ID to be used when
|
2022-07-25 15:28:17 +00:00
|
|
|
// publishing a message
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithRequestID(requestID []byte) Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-07-19 16:25:35 +00:00
|
|
|
params.requestID = requestID
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:35 +00:00
|
|
|
// WithAutomaticRequestID is an option to automatically generate a request ID
|
2022-07-25 15:28:17 +00:00
|
|
|
// when publishing a message
|
2023-07-19 16:25:35 +00:00
|
|
|
func WithAutomaticRequestID() Option {
|
2023-11-15 14:26:55 +00:00
|
|
|
return func(params *lightPushParameters) error {
|
2023-09-11 14:24:05 +00:00
|
|
|
params.requestID = protocol.GenerateRequestID()
|
2023-11-15 14:26:55 +00:00
|
|
|
return nil
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:28:17 +00:00
|
|
|
// DefaultOptions are the default options to be used when using the lightpush protocol
|
2023-07-19 16:25:35 +00:00
|
|
|
func DefaultOptions(host host.Host) []Option {
|
|
|
|
return []Option{
|
|
|
|
WithAutomaticRequestID(),
|
2022-08-15 17:13:45 +00:00
|
|
|
WithAutomaticPeerSelection(),
|
2021-11-07 12:08:29 +00:00
|
|
|
}
|
|
|
|
}
|