2021-04-18 23:41:42 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-10-15 02:15:02 +00:00
|
|
|
"fmt"
|
2021-04-18 23:41:42 +00:00
|
|
|
"net"
|
2021-06-24 13:02:53 +00:00
|
|
|
"time"
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2021-04-18 23:41:42 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
connmgr "github.com/libp2p/go-libp2p-connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2021-07-29 22:08:53 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2021-10-15 12:38:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2021-10-15 02:15:02 +00:00
|
|
|
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-04-18 23:41:42 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-10-08 13:50:56 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2021-10-05 02:13:54 +00:00
|
|
|
rendezvous "github.com/status-im/go-waku-rendezvous"
|
2021-12-08 13:00:20 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/filter"
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/store"
|
2021-04-18 23:41:42 +00:00
|
|
|
)
|
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
// Default clientId
|
|
|
|
const clientId string = "Go Waku v2 node"
|
|
|
|
|
2021-12-06 08:43:00 +00:00
|
|
|
// Default minRelayPeersToPublish
|
|
|
|
const defaultMinRelayPeersToPublish = 1
|
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
type WakuNodeParameters struct {
|
2021-11-17 16:19:42 +00:00
|
|
|
hostAddr *net.TCPAddr
|
|
|
|
advertiseAddr *net.IP
|
2021-10-15 02:15:02 +00:00
|
|
|
multiAddr []ma.Multiaddr
|
|
|
|
addressFactory basichost.AddrsFactory
|
2021-11-17 16:19:42 +00:00
|
|
|
privKey *ecdsa.PrivateKey
|
2021-10-15 02:15:02 +00:00
|
|
|
libP2POpts []libp2p.Option
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-10-30 14:29:34 +00:00
|
|
|
enableRelay bool
|
|
|
|
enableFilter bool
|
|
|
|
isFilterFullNode bool
|
2021-12-08 13:00:20 +00:00
|
|
|
filterOpts []filter.Option
|
2021-10-30 14:29:34 +00:00
|
|
|
wOpts []pubsub.Option
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-12-06 08:43:00 +00:00
|
|
|
minRelayPeersToPublish int
|
|
|
|
|
2021-11-01 12:38:03 +00:00
|
|
|
enableStore bool
|
|
|
|
shouldResume bool
|
|
|
|
storeMsgs bool
|
|
|
|
messageProvider store.MessageProvider
|
2021-11-05 14:27:30 +00:00
|
|
|
maxMessages int
|
2021-11-06 13:23:58 +00:00
|
|
|
maxDuration time.Duration
|
2021-04-28 20:23:03 +00:00
|
|
|
|
2021-12-06 10:49:13 +00:00
|
|
|
swapMode int
|
|
|
|
swapDisconnectThreshold int
|
|
|
|
swapPaymentThreshold int
|
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
enableRendezvous bool
|
|
|
|
enableRendezvousServer bool
|
2021-10-01 18:37:52 +00:00
|
|
|
rendevousStorage rendezvous.Storage
|
2021-10-08 13:50:56 +00:00
|
|
|
rendezvousOpts []pubsub.DiscoverOpt
|
2021-10-01 17:49:50 +00:00
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
enableDiscV5 bool
|
|
|
|
udpPort int
|
|
|
|
discV5bootnodes []*enode.Node
|
|
|
|
discV5Opts []pubsub.DiscoverOpt
|
|
|
|
discV5autoUpdate bool
|
|
|
|
|
2021-06-24 13:02:53 +00:00
|
|
|
keepAliveInterval time.Duration
|
|
|
|
|
2021-04-28 20:23:03 +00:00
|
|
|
enableLightPush bool
|
2021-06-16 10:14:22 +00:00
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
connStatusC chan ConnStatus
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WakuNodeOption func(*WakuNodeParameters) error
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// Default options used in the libp2p node
|
|
|
|
var DefaultWakuNodeOptions = []WakuNodeOption{
|
|
|
|
WithWakuRelay(),
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// MultiAddresses return the list of multiaddresses configured in the node
|
2021-10-14 18:17:01 +00:00
|
|
|
func (w WakuNodeParameters) MultiAddresses() []ma.Multiaddr {
|
|
|
|
return w.multiAddr
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// Identity returns a libp2p option containing the identity used by the node
|
2021-10-15 12:38:55 +00:00
|
|
|
func (w WakuNodeParameters) Identity() config.Option {
|
2021-11-17 16:19:42 +00:00
|
|
|
return libp2p.Identity(*w.GetPrivKey())
|
2021-10-15 12:38:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
func (w WakuNodeParameters) AddressFactory() basichost.AddrsFactory {
|
|
|
|
return w.addressFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHostAddress is a WakuNodeOption that configures libp2p to listen on a specific address
|
|
|
|
func WithHostAddress(hostAddr *net.TCPAddr) WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-17 16:19:42 +00:00
|
|
|
params.hostAddr = hostAddr
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(hostAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
2021-11-17 16:19:42 +00:00
|
|
|
params.multiAddr = append(params.multiAddr, hostAddrMA)
|
2021-04-18 23:41:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// WithAdvertiseAddress is a WakuNodeOption that allows overriding the address used in the waku node with custom value
|
|
|
|
func WithAdvertiseAddress(address *net.TCPAddr, enableWS bool, wsPort int) WakuNodeOption {
|
2021-10-15 02:15:02 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-17 16:19:42 +00:00
|
|
|
params.advertiseAddr = &address.IP
|
|
|
|
|
|
|
|
advertiseAddress, err := manet.FromNetAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-15 02:15:02 +00:00
|
|
|
params.addressFactory = func([]ma.Multiaddr) []ma.Multiaddr {
|
|
|
|
var result []multiaddr.Multiaddr
|
2021-11-17 16:19:42 +00:00
|
|
|
result = append(result, advertiseAddress)
|
|
|
|
if enableWS {
|
|
|
|
wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/ws", address, wsPort))
|
|
|
|
result = append(result, wsMa)
|
2021-10-15 02:15:02 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithMultiaddress is a WakuNodeOption that configures libp2p to listen on a list of multiaddresses
|
2021-04-22 13:07:22 +00:00
|
|
|
func WithMultiaddress(addresses []ma.Multiaddr) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.multiAddr = append(params.multiAddr, addresses...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithPrivateKey is used to set an ECDSA private key in a libp2p node
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-17 16:19:42 +00:00
|
|
|
params.privKey = privKey
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
func (w *WakuNodeParameters) GetPrivKey() *crypto.PrivKey {
|
|
|
|
privKey := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(w.privKey))
|
|
|
|
return &privKey
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithLibP2POptions is a WakuNodeOption used to configure the libp2p node.
|
|
|
|
// This can potentially override any libp2p config that was set with other
|
|
|
|
// WakuNodeOption
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithLibP2POptions(opts ...libp2p.Option) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.libP2POpts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithWakuRelay enables the Waku V2 Relay protocol. This WakuNodeOption
|
|
|
|
// accepts a list of WakuRelay gossipsub option to setup the protocol
|
2021-07-29 22:08:53 +00:00
|
|
|
func WithWakuRelay(opts ...pubsub.Option) WakuNodeOption {
|
2021-12-06 08:43:00 +00:00
|
|
|
return WithWakuRelayAndMinPeers(defaultMinRelayPeersToPublish, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithWakuRelayAndMinPeers enables the Waku V2 Relay protocol. This WakuNodeOption
|
|
|
|
// accepts a min peers require to publish and a list of WakuRelay gossipsub option to setup the protocol
|
|
|
|
func WithWakuRelayAndMinPeers(minRelayPeersToPublish int, opts ...pubsub.Option) WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRelay = true
|
|
|
|
params.wOpts = opts
|
2021-12-06 08:43:00 +00:00
|
|
|
params.minRelayPeersToPublish = minRelayPeersToPublish
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
// WithDiscoveryV5 is a WakuOption used to enable DiscV5 peer discovery
|
|
|
|
func WithDiscoveryV5(udpPort int, bootnodes []*enode.Node, autoUpdate bool, discoverOpts ...pubsub.DiscoverOpt) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableDiscV5 = true
|
|
|
|
params.udpPort = udpPort
|
|
|
|
params.discV5bootnodes = bootnodes
|
|
|
|
params.discV5Opts = discoverOpts
|
|
|
|
params.discV5autoUpdate = autoUpdate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithRendezvous is a WakuOption used to enable go-waku-rendezvous discovery.
|
|
|
|
// It accepts an optional list of DiscoveryOpt options
|
2021-07-29 22:08:53 +00:00
|
|
|
func WithRendezvous(discoverOpts ...pubsub.DiscoverOpt) WakuNodeOption {
|
2021-10-01 17:49:50 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRendezvous = true
|
|
|
|
params.rendezvousOpts = discoverOpts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithRendezvousServer is a WakuOption used to set the node as a rendezvous
|
|
|
|
// point, using an specific storage for the peer information
|
2021-10-01 18:37:52 +00:00
|
|
|
func WithRendezvousServer(storage rendezvous.Storage) WakuNodeOption {
|
2021-10-01 17:49:50 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRendezvousServer = true
|
2021-10-01 18:37:52 +00:00
|
|
|
params.rendevousStorage = storage
|
2021-10-01 17:49:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:08:53 +00:00
|
|
|
// WithWakuFilter enables the Waku V2 Filter protocol. This WakuNodeOption
|
|
|
|
// accepts a list of WakuFilter gossipsub options to setup the protocol
|
2021-12-08 13:00:20 +00:00
|
|
|
func WithWakuFilter(fullNode bool, filterOpts ...filter.Option) WakuNodeOption {
|
2021-06-10 12:59:51 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableFilter = true
|
2021-10-30 14:29:34 +00:00
|
|
|
params.isFilterFullNode = fullNode
|
2021-12-08 13:00:20 +00:00
|
|
|
params.filterOpts = filterOpts
|
2021-06-10 12:59:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithWakuStore enables the Waku V2 Store protocol and if the messages should
|
|
|
|
// be stored or not in a message provider
|
2021-09-06 13:10:19 +00:00
|
|
|
func WithWakuStore(shouldStoreMessages bool, shouldResume bool) WakuNodeOption {
|
2021-04-18 23:41:42 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableStore = true
|
|
|
|
params.storeMsgs = shouldStoreMessages
|
2021-09-06 13:10:19 +00:00
|
|
|
params.shouldResume = shouldResume
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:49:13 +00:00
|
|
|
// WithWakuSwap set the option of the Waku V2 Swap protocol
|
|
|
|
func WithWakuSwap(mode int, disconnectThreshold, paymentThreshold int) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.swapMode = mode
|
|
|
|
params.swapDisconnectThreshold = disconnectThreshold
|
|
|
|
params.swapPaymentThreshold = paymentThreshold
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:23:58 +00:00
|
|
|
// WithWakuStoreAndRetentionPolicy enables the Waku V2 Store protocol, storing them in an optional message provider
|
2021-11-05 14:38:35 +00:00
|
|
|
// applying an specific retention policy
|
2021-11-06 13:23:58 +00:00
|
|
|
func WithWakuStoreAndRetentionPolicy(shouldResume bool, maxDuration time.Duration, maxMessages int) WakuNodeOption {
|
2021-11-05 14:27:30 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableStore = true
|
2021-11-05 14:38:35 +00:00
|
|
|
params.storeMsgs = true
|
2021-11-05 14:27:30 +00:00
|
|
|
params.shouldResume = shouldResume
|
2021-11-06 13:23:58 +00:00
|
|
|
params.maxDuration = maxDuration
|
2021-11-05 14:27:30 +00:00
|
|
|
params.maxMessages = maxMessages
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithMessageProvider is a WakuNodeOption that sets the MessageProvider
|
|
|
|
// used to store and retrieve persisted messages
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithMessageProvider(s store.MessageProvider) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-01 12:38:03 +00:00
|
|
|
params.messageProvider = s
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:23:03 +00:00
|
|
|
// WithLightPush is a WakuNodeOption that enables the lightpush protocol
|
|
|
|
func WithLightPush() WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableLightPush = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithKeepAlive is a WakuNodeOption used to set the interval of time when
|
|
|
|
// each peer will be ping to keep the TCP connection alive
|
2021-06-24 13:02:53 +00:00
|
|
|
func WithKeepAlive(t time.Duration) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.keepAliveInterval = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// WithConnectionStatusChannel is a WakuNodeOption used to set a channel where the
|
|
|
|
// connection status changes will be pushed to. It's useful to identify when peer
|
|
|
|
// connections and disconnections occur
|
|
|
|
func WithConnectionStatusChannel(connStatus chan ConnStatus) WakuNodeOption {
|
2021-06-16 10:14:22 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
2021-11-05 20:09:48 +00:00
|
|
|
params.connStatusC = connStatus
|
2021-06-16 10:14:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// Default options used in the libp2p node
|
2021-04-18 23:41:42 +00:00
|
|
|
var DefaultLibP2POptions = []libp2p.Option{
|
|
|
|
libp2p.DefaultTransports,
|
2021-06-28 14:14:28 +00:00
|
|
|
libp2p.UserAgent(clientId),
|
2021-04-18 23:41:42 +00:00
|
|
|
libp2p.EnableNATService(), // TODO: is this needed?)
|
|
|
|
libp2p.ConnectionManager(connmgr.NewConnManager(200, 300, 0)),
|
|
|
|
}
|