Skip to content

Commit

Permalink
Readd missing advertisement.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 8, 2024
1 parent ad665fe commit 0e3c840
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
16 changes: 12 additions & 4 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const (
defaultConnmgrHighWatermark int = 100
defaultConnmgrGracePeriod time.Duration = time.Minute * 1

defaultDiscoveryAdvertiseTTL time.Duration = time.Minute * 60
defaultDiscoveryAdvertiseLimit int = 100
DEFAULT_ALLOW_ALL bool = true // Allow all peers by default. This is the norm for now. Use connmgr threshold and protection instead.
defaultDiscoveryAdvertiseInterval time.Duration = time.Minute * 5
defaultDiscoveryAdvertiseTTL time.Duration = time.Minute * 60
defaultDiscoveryAdvertiseLimit int = 100
DEFAULT_ALLOW_ALL bool = true // Allow all peers by default. This is the norm for now. Use connmgr threshold and protection instead.

defaultListenPort int = 0
fakeP2PIdentity string = "NO_DEFAULT_NODE_IDENITY"
Expand All @@ -40,13 +41,16 @@ func init() {

// DISCOVERY
pflag.Int("p2p-discovery-advertise-limit", defaultDiscoveryAdvertiseLimit, "Limit for advertising peer discovery.")
pflag.Duration("p2p-discovery-advertise-ttl", defaultDiscoveryAdvertiseTTL, "Hint o TimeToLive for advertising peer discovery.")
pflag.Duration("p2p-discovery-advertise-ttl", defaultDiscoveryAdvertiseTTL, "Hint of TimeToLive for advertising peer discovery.")
pflag.Duration("p2p-discovery-advertise-interval", defaultDiscoveryAdvertiseInterval, "How often to advertise our presence to libp2p")
pflag.Bool("p2p-discovery-allow-all", DEFAULT_ALLOW_ALL, "Number of concurrent peer discovery routines.")

viper.BindPFlag("p2p.discovery.advetise-interval", pflag.Lookup("p2p-discovery-advertise-interval"))
viper.BindPFlag("p2p.discovery.advertise-limit", pflag.Lookup("p2p-discovery-advertise-limit"))
viper.BindPFlag("p2p.discovery.advertise-ttl", pflag.Lookup("p2p-discovery-advertise-ttl"))
viper.BindPFlag("p2p.discovery.allow-all", pflag.Lookup("p2p-discovery-allow-all"))

viper.SetDefault("p2p.discovery.advertise-interval", defaultDiscoveryAdvertiseInterval)
viper.SetDefault("p2p.discovery.advertise-limit", defaultDiscoveryAdvertiseLimit)
viper.SetDefault("p2p.discovery.advertise-ttl", defaultDiscoveryAdvertiseTTL)
viper.SetDefault("p2p.discovery.allow-all", DEFAULT_ALLOW_ALL)
Expand All @@ -68,6 +72,10 @@ func P2PIdentity() string {
return viper.GetString("p2p.identity")
}

func P2PDiscoveryAdvertiseInterval() time.Duration {
return viper.GetDuration("p2p.discovery.advertise-interval")
}

func P2PDiscoveryAdvertiseTTL() time.Duration {
return viper.GetDuration("p2p.discovery.advertise-ttl")
}
Expand Down
41 changes: 33 additions & 8 deletions p2p/dht/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import (
"context"
"fmt"

"time"

"github.com/bahner/go-ma"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/bahner/go-ma-actor/config"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/discovery/util"
log "github.com/sirupsen/logrus"
)

var (
ErrFailedToCreateRoutingDiscovery = fmt.Errorf("failed to create routing discovery")
ErrPeerChanClosed = fmt.Errorf("peer channel closed")
)

// Run a continuous discovery loop to find new peers
// The ctx should probably be a background context
func (d *DHT) DiscoveryLoop(ctx context.Context) error {
routingDiscovery := drouting.NewRoutingDiscovery(d.IpfsDHT)
routingDiscovery := routing.NewRoutingDiscovery(d.IpfsDHT)
if routingDiscovery == nil {
return ErrFailedToCreateRoutingDiscovery
}
Expand All @@ -27,25 +32,45 @@ func (d *DHT) DiscoveryLoop(ctx context.Context) error {
return fmt.Errorf("peer discovery error: %w", err)
}

go advertisementLoop(ctx, routingDiscovery) // Run advertisement continuously in the background
go discover(ctx, peerChan, d) // Run discovery continuously in the background

return nil
}

func discover(ctx context.Context, peerChan <-chan peer.AddrInfo, d *DHT) error {
for {
select {
case p, ok := <-peerChan:
if !ok {
if !(ctx == nil) {
log.Fatalf("DHT peer channel closed, but it was supposed to be running in the background.")
}
return ErrPeerChanClosed
log.Fatalf("DHT peer channel closed, but it was supposed to be running in the background.")
}

if p.ID == d.h.ID() {
continue // Skip self
}

if err := d.PeerConnectAndUpdateIfSuccessful(ctx, p); err != nil {
if err := d.PeerConnectAndUpdateIfSuccessful(context.Background(), p); err != nil {
log.Warnf("Failed to connect to discovered peer: %s: %v", p.ID.String(), err)
}
case <-ctx.Done():
return nil
}
}
}

func advertisementLoop(ctx context.Context, routingDiscovery *routing.RoutingDiscovery, discoveryOpts ...discovery.Option) {

ticker := time.NewTicker(config.P2PDiscoveryAdvertiseInterval())
defer ticker.Stop()

for {
select {
case <-ticker.C:
util.Advertise(ctx, routingDiscovery, ma.RENDEZVOUS, discoveryOpts...)
log.Debugf("Advertising rendezvous string: %s", ma.RENDEZVOUS)
case <-ctx.Done():
return
}
}
}

0 comments on commit 0e3c840

Please sign in to comment.