2021-04-18 23:41:42 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"net"
|
2021-06-24 13:02:53 +00:00
|
|
|
"time"
|
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-10-01 17:49:50 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-04-18 23:41:42 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
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
|
|
|
wakurelay "github.com/status-im/go-wakurelay-pubsub"
|
|
|
|
)
|
|
|
|
|
2021-06-28 14:14:28 +00:00
|
|
|
// Default clientId
|
|
|
|
const clientId string = "Go Waku v2 node"
|
|
|
|
|
2021-04-18 23:41:42 +00:00
|
|
|
type WakuNodeParameters struct {
|
|
|
|
multiAddr []ma.Multiaddr
|
|
|
|
privKey *crypto.PrivKey
|
|
|
|
libP2POpts []libp2p.Option
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
enableRelay bool
|
|
|
|
enableFilter bool
|
|
|
|
wOpts []wakurelay.Option
|
2021-04-18 23:41:42 +00:00
|
|
|
|
2021-09-06 13:10:19 +00:00
|
|
|
enableStore bool
|
|
|
|
shouldResume bool
|
|
|
|
storeMsgs bool
|
|
|
|
store *store.WakuStore
|
2021-07-29 12:40:54 +00:00
|
|
|
// filter *filter.WakuFilter
|
2021-04-28 20:23:03 +00:00
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
enableRendezvous bool
|
|
|
|
enableRendezvousServer bool
|
|
|
|
rendezvousPeers []peer.ID
|
|
|
|
rendezvousOpts []wakurelay.DiscoverOpt
|
|
|
|
|
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
|
|
|
|
|
|
|
connStatusChan chan ConnStatus
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WakuNodeOption func(*WakuNodeParameters) error
|
|
|
|
|
2021-04-22 20:42:44 +00:00
|
|
|
// WithHostAddress is a WakuNodeOption that configures libp2p to listen on a list of net endpoint addresses
|
2021-04-18 23:41:42 +00:00
|
|
|
func WithHostAddress(hostAddr []net.Addr) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
var multiAddresses []ma.Multiaddr
|
|
|
|
for _, addr := range hostAddr {
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
multiAddresses = append(multiAddresses, hostAddrMA)
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:07:22 +00:00
|
|
|
params.multiAddr = append(params.multiAddr, multiAddresses...)
|
2021-04-18 23:41:42 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
privk := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))
|
|
|
|
params.privKey = &privk
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-18 23:41:42 +00:00
|
|
|
func WithWakuRelay(opts ...wakurelay.Option) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRelay = true
|
|
|
|
params.wOpts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:49:50 +00:00
|
|
|
func WithRendezvous(peers []peer.ID, discoverOpts ...wakurelay.DiscoverOpt) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRendezvous = true
|
|
|
|
params.rendezvousPeers = peers
|
|
|
|
params.rendezvousOpts = discoverOpts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithRendezvousServer() WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRendezvousServer = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:47:18 +00:00
|
|
|
// WithWakuFilter enables the Waku V2 Filter protocol.
|
|
|
|
func WithWakuFilter() WakuNodeOption {
|
2021-06-10 12:59:51 +00:00
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableFilter = true
|
|
|
|
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-04-22 00:09:37 +00:00
|
|
|
params.store = store.NewWakuStore(shouldStoreMessages, nil)
|
2021-09-06 13:10:19 +00:00
|
|
|
params.shouldResume = shouldResume
|
2021-04-18 23:41:42 +00:00
|
|
|
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 {
|
|
|
|
if params.store != nil {
|
|
|
|
params.store.SetMsgProvider(s)
|
|
|
|
} else {
|
2021-04-22 00:09:37 +00:00
|
|
|
params.store = store.NewWakuStore(true, 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-06-24 13:02:53 +00:00
|
|
|
func WithKeepAlive(t time.Duration) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.keepAliveInterval = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
func WithConnStatusChan(connStatusChan chan ConnStatus) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.connStatusChan = connStatusChan
|
|
|
|
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.NATPortMap(), // Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
libp2p.EnableNATService(), // TODO: is this needed?)
|
|
|
|
libp2p.ConnectionManager(connmgr.NewConnManager(200, 300, 0)),
|
|
|
|
}
|