Skip to content

Commit

Permalink
feat: force unreachability (#753)
Browse files Browse the repository at this point in the history
Co-authored-by: Prem Chaitanya Prathi <[email protected]>
  • Loading branch information
richard-ramos and chaitanyaprem authored Sep 20, 2023
1 parent 054bdae commit 003c90f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cmd/waku/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ var (
Destination: &options.CircuitRelay,
EnvVars: []string{"WAKUNODE2_CIRCUIT_RELAY"},
})
ForceUnreachable = altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "unreachable",
Usage: "Force the node to be unreachable. WARNING: This flag is created for testing circuit relay and is not meant to be used in production",
Value: false,
Hidden: true,
Destination: &options.ForceUnreachable,
EnvVars: []string{"WAKUNODE2_UNREACHABLE"},
})
ResourceScalingMemoryPercent = altsrc.NewFloat64Flag(&cli.Float64Flag{
Name: "resource-scaling-memory-percentage",
Usage: "Determines the percentage of total accessible memory that wil be dedicated to go-waku. A dedicated node with a lot of RAM could allocate 25% or more memory to go-waku",
Expand Down
1 change: 1 addition & 0 deletions cmd/waku/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
ExtMultiaddresses,
ShowAddresses,
CircuitRelay,
ForceUnreachable,
ResourceScalingMemoryPercent,
ResourceScalingFDPercent,
LogLevel,
Expand Down
6 changes: 6 additions & 0 deletions cmd/waku/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func Execute(options NodeOptions) {
libp2pOpts = append(libp2pOpts, libp2p.EnableRelayService())
}

if options.ForceUnreachable {
logger.Warn("node forced to be unreachable!")
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.ForceReachabilityPrivate())
nodeOpts = append(nodeOpts, node.WithCircuitRelayParams(2*time.Second, 2*time.Second))
}

if options.UserAgent != "" {
libp2pOpts = append(libp2pOpts, libp2p.UserAgent(options.UserAgent))
}
Expand Down
1 change: 1 addition & 0 deletions cmd/waku/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type NodeOptions struct {
AdvertiseAddresses []multiaddr.Multiaddr
ShowAddresses bool
CircuitRelay bool
ForceUnreachable bool
ResourceScalingMemoryPercent float64
ResourceScalingFDPercent float64
LogLevel string
Expand Down
7 changes: 4 additions & 3 deletions waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
}()
return r
},
autorelay.WithMinInterval(2*time.Second),
autorelay.WithMinInterval(params.circuitRelayMinInterval),
autorelay.WithBootDelay(params.circuitRelayBootDelay),
))

if params.enableNTP {
Expand Down Expand Up @@ -754,12 +755,12 @@ func (w *WakuNode) connect(ctx context.Context, info peer.AddrInfo) error {
// host.Connect adds the addresses with a TempAddressTTL
// however, identify will filter out all non IP addresses
// and expire all temporary addrs. So in the meantime, let's
// store dns4 addresses with a connectedAddressTTL, otherwise
// store dns4 addresses with a RecentlyConnectedAddrTTL, otherwise
// it will have trouble with the status fleet circuit relay addresses
// See https://github.com/libp2p/go-libp2p/issues/2550
_, err := addr.ValueForProtocol(ma.P_DNS4)
if err == nil {
w.host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.ConnectedAddrTTL)
w.host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.RecentlyConnectedAddrTTL)
}
}

Expand Down
12 changes: 12 additions & 0 deletions waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type WakuNodeParameters struct {
peerstore peerstore.Peerstore
prometheusReg prometheus.Registerer

circuitRelayMinInterval time.Duration
circuitRelayBootDelay time.Duration

enableNTP bool
ntpURLs []string

Expand Down Expand Up @@ -119,6 +122,7 @@ type WakuNodeOption func(*WakuNodeParameters) error
var DefaultWakuNodeOptions = []WakuNodeOption{
WithPrometheusRegisterer(prometheus.NewRegistry()),
WithMaxPeerConnections(50),
WithCircuitRelayParams(2*time.Second, 3*time.Minute),
}

// MultiAddresses return the list of multiaddresses configured in the node
Expand Down Expand Up @@ -517,6 +521,14 @@ func WithSecureWebsockets(address string, port int, certPath string, keyPath str
}
}

func WithCircuitRelayParams(minInterval time.Duration, bootDelay time.Duration) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.circuitRelayBootDelay = bootDelay
params.circuitRelayMinInterval = minInterval
return nil
}
}

// Default options used in the libp2p node
var DefaultLibP2POptions = []libp2p.Option{
libp2p.ChainOptions(
Expand Down
2 changes: 1 addition & 1 deletion waku/v2/peermanager/peer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (pm *PeerManager) peerEventLoop(ctx context.Context) {
err := wps.AddPubSubTopic(peerID, peerEvt.Topic)
if err != nil {
pm.logger.Error("failed to add pubSubTopic for peer",
logging.HostID("peerID", peerID), zap.Error(err))
logging.HostID("peerID", peerID), zap.String("topic", peerEvt.Topic), zap.Error(err))
}
} else if peerEvt.State == relay.PEER_LEFT {
err := wps.RemovePubSubTopic(peerID, peerEvt.Topic)
Expand Down
7 changes: 6 additions & 1 deletion waku/v2/peerstore/waku_peer_store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package peerstore

import (
"errors"
"sync"

"github.com/ethereum/go-ethereum/p2p/enode"
Expand Down Expand Up @@ -183,7 +184,11 @@ func (ps *WakuPeerstoreImpl) SetPubSubTopics(p peer.ID, topics []string) error {
func (ps *WakuPeerstoreImpl) PubSubTopics(p peer.ID) ([]string, error) {
result, err := ps.peerStore.Get(p, peerPubSubTopics)
if err != nil {
return nil, err
if errors.Is(err, peerstore.ErrNotFound) {
return nil, nil
} else {
return nil, err
}
}
return result.([]string), nil
}
Expand Down

0 comments on commit 003c90f

Please sign in to comment.