Skip to content

Commit

Permalink
feat: modify peer-manager to consider relay target peers (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyaprem authored Jun 26, 2024
1 parent ee33baa commit 19a47a1
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 88 deletions.
6 changes: 6 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package logging

import (
"encoding/hex"
"fmt"
"net"
"time"

Expand Down Expand Up @@ -147,3 +148,8 @@ func TCPAddr(key string, ip net.IP, port int) zap.Field {
func UDPAddr(key string, ip net.IP, port int) zap.Field {
return zap.Stringer(key, &net.UDPAddr{IP: ip, Port: port})
}

func Uint64(key string, value uint64) zap.Field {
valueStr := fmt.Sprintf("%v", value)
return zap.String(key, valueStr)
}
6 changes: 3 additions & 3 deletions waku/v2/node/wakunode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
w.metadata = metadata

//Initialize peer manager.
w.peermanager = peermanager.NewPeerManager(w.opts.maxPeerConnections, w.opts.peerStoreCapacity, metadata, w.log)
w.peermanager = peermanager.NewPeerManager(w.opts.maxPeerConnections, w.opts.peerStoreCapacity, metadata, params.enableRelay, w.log)

w.peerConnector, err = peermanager.NewPeerConnectionStrategy(w.peermanager, w.opts.onlineChecker, discoveryConnectTimeout, w.log)
if err != nil {
Expand Down Expand Up @@ -554,9 +554,9 @@ func (w *WakuNode) watchENRChanges(ctx context.Context) {
currNodeVal := w.localNode.Node().String()
if prevNodeVal != currNodeVal {
if prevNodeVal == "" {
w.log.Info("enr record", logging.ENode("enr", w.localNode.Node()))
w.log.Info("local node enr record", logging.ENode("enr", w.localNode.Node()))
} else {
w.log.Info("new enr record", logging.ENode("enr", w.localNode.Node()))
w.log.Info("local node new enr record", logging.ENode("enr", w.localNode.Node()))
}
prevNodeVal = currNodeVal
}
Expand Down
5 changes: 4 additions & 1 deletion waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const UserAgent string = "go-waku"
const defaultMinRelayPeersToPublish = 0

const DefaultMaxConnectionsPerIP = 5
const DefaultMaxConnections = 300
const DefaultMaxPeerStoreCapacity = 300

type WakuNodeParameters struct {
hostAddr *net.TCPAddr
Expand Down Expand Up @@ -127,9 +129,10 @@ type WakuNodeOption func(*WakuNodeParameters) error
// Default options used in the libp2p node
var DefaultWakuNodeOptions = []WakuNodeOption{
WithPrometheusRegisterer(prometheus.NewRegistry()),
WithMaxPeerConnections(50),
WithMaxPeerConnections(DefaultMaxConnections),
WithMaxConnectionsPerIP(DefaultMaxConnectionsPerIP),
WithCircuitRelayParams(2*time.Second, 3*time.Minute),
WithPeerStoreCapacity(DefaultMaxPeerStoreCapacity),
WithOnlineChecker(onlinechecker.NewDefaultOnlineChecker(true)),
}

Expand Down
6 changes: 2 additions & 4 deletions waku/v2/peermanager/peer_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/onlinechecker"
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
waku_proto "github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/service"

"go.uber.org/zap"
Expand Down Expand Up @@ -134,10 +133,9 @@ func (c *PeerConnectionStrategy) consumeSubscription(s subscription) {
triggerImmediateConnection := false
//Not connecting to peer as soon as it is discovered,
// rather expecting this to be pushed from PeerManager based on the need.
if len(c.host.Network().Peers()) < waku_proto.GossipSubDMin {
if len(c.host.Network().Peers()) < c.pm.OutPeersTarget {
triggerImmediateConnection = true
}
c.logger.Debug("adding discovered peer", logging.HostID("peerID", p.AddrInfo.ID))
c.pm.AddDiscoveredPeer(p, triggerImmediateConnection)

case <-time.After(1 * time.Second):
Expand Down Expand Up @@ -238,7 +236,7 @@ func (c *PeerConnectionStrategy) addConnectionBackoff(peerID peer.ID) {
func (c *PeerConnectionStrategy) dialPeers() {
defer c.WaitGroup().Done()

maxGoRoutines := c.pm.OutRelayPeersTarget
maxGoRoutines := c.pm.OutPeersTarget
if maxGoRoutines > maxActiveDials {
maxGoRoutines = maxActiveDials
}
Expand Down
2 changes: 1 addition & 1 deletion waku/v2/peermanager/peer_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (pm *PeerManager) discoverOnDemand(cluster uint16,

wakuProtoInfo, ok := pm.wakuprotoToENRFieldMap[wakuProtocol]
if !ok {
pm.logger.Error("cannot do on demand discovery for non-waku protocol", zap.String("protocol", string(wakuProtocol)))
pm.logger.Warn("cannot do on demand discovery for non-waku protocol", zap.String("protocol", string(wakuProtocol)))
return nil, errors.New("cannot do on demand discovery for non-waku protocol")
}
iterator, err := pm.discoveryService.PeerIterator(
Expand Down
Loading

0 comments on commit 19a47a1

Please sign in to comment.