Skip to content

Commit

Permalink
update go-libp2p to v0.19.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Apr 19, 2022
1 parent d59730f commit e395f42
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 133 deletions.
13 changes: 10 additions & 3 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

blockstore "github.com/ipfs/go-ipfs-blockstore"
util "github.com/ipfs/go-ipfs-util"
config "github.com/ipfs/go-ipfs/config"
"github.com/ipfs/go-ipfs/config"
"github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
Expand Down Expand Up @@ -38,6 +38,8 @@ var BaseLibP2P = fx.Options(
fx.Invoke(libp2p.PNetChecker),
)

type AddrInfoChan chan peer.AddrInfo

func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
// parse ConnMgr config

Expand Down Expand Up @@ -140,10 +142,14 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
"If you want to continue running a circuit v1 relay, please use the standalone relay daemon: https://github.com/libp2p/go-libp2p-relay-daemon (with RelayV1.Enabled: true)")
}

peerChan := make(chan peer.AddrInfo)
usesRelayClient := cfg.Swarm.RelayClient.Enabled.WithDefault(true)
// Gather all the options
opts := fx.Options(
BaseLibP2P,

fx.Supply(peerChan),

// Services (resource management)
fx.Provide(libp2p.ResourceManager(cfg.Swarm)),
fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)),
Expand All @@ -155,7 +161,6 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
fx.Invoke(libp2p.StartListening(cfg.Addresses.Swarm)),
fx.Invoke(libp2p.SetupDiscovery(cfg.Discovery.MDNS.Enabled, cfg.Discovery.MDNS.Interval)),
fx.Provide(libp2p.ForceReachability(cfg.Internal.Libp2pForceReachability)),
fx.Provide(libp2p.StaticRelays(cfg.Swarm.RelayClient.StaticRelays)),
fx.Provide(libp2p.HolePunching(cfg.Swarm.EnableHolePunching, cfg.Swarm.RelayClient.Enabled.WithDefault(false))),

fx.Provide(libp2p.Security(!bcfg.DisableEncryptedConnections, cfg.Swarm.Transports)),
Expand All @@ -166,7 +171,9 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

maybeProvide(libp2p.BandwidthCounter, !cfg.Swarm.DisableBandwidthMetrics),
maybeProvide(libp2p.NatPortMap, !cfg.Swarm.DisableNatPortMap),
maybeProvide(libp2p.AutoRelay(len(cfg.Swarm.RelayClient.StaticRelays) == 0), cfg.Swarm.RelayClient.Enabled.WithDefault(false)),
maybeProvide(libp2p.AutoRelay(cfg.Swarm.RelayClient.StaticRelays, peerChan), usesRelayClient),

maybeInvoke(libp2p.AutoRelayFeeder, usesRelayClient),
autonat,
connmgr,
ps,
Expand Down
42 changes: 19 additions & 23 deletions core/node/libp2p/relay.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package libp2p

import (
config "github.com/ipfs/go-ipfs/config"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/ipfs/go-ipfs/config"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
)

Expand Down Expand Up @@ -43,30 +43,26 @@ func RelayService(enable bool, relayOpts config.RelayService) func() (opts Libp2
}
}

func StaticRelays(relays []string) func() (opts Libp2pOpts, err error) {
func AutoRelay(staticRelays []string, peerChan <-chan peer.AddrInfo) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
staticRelays := make([]peer.AddrInfo, 0, len(relays))
for _, s := range relays {
var addr *peer.AddrInfo
addr, err = peer.AddrInfoFromString(s)
if err != nil {
return
}
staticRelays = append(staticRelays, *addr)
}
var autoRelayOpts []autorelay.Option
if len(staticRelays) > 0 {
opts.Opts = append(opts.Opts, libp2p.StaticRelays(staticRelays))
static := make([]peer.AddrInfo, 0, len(staticRelays))
for _, s := range staticRelays {
var addr *peer.AddrInfo
addr, err = peer.AddrInfoFromString(s)
if err != nil {
return
}
static = append(static, *addr)
}
autoRelayOpts = append(autoRelayOpts, autorelay.WithStaticRelays(static))
autoRelayOpts = append(autoRelayOpts, autorelay.WithCircuitV1Support())
}
return
}
}

func AutoRelay(addDefaultRelays bool) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
opts.Opts = append(opts.Opts, libp2p.EnableAutoRelay())
if addDefaultRelays {
opts.Opts = append(opts.Opts, libp2p.DefaultStaticRelays())
if peerChan != nil {
autoRelayOpts = append(autoRelayOpts, autorelay.WithPeerSource(peerChan))
}
opts.Opts = append(opts.Opts, libp2p.EnableAutoRelay(autoRelayOpts...))
return
}
}
Expand Down
51 changes: 48 additions & 3 deletions core/node/libp2p/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package libp2p

import (
"context"
"fmt"
"sort"
"time"

"github.com/ipfs/go-ipfs/core/node/helpers"

"github.com/ipfs/go-ipfs/repo"
host "github.com/libp2p/go-libp2p-core/host"
routing "github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
dht "github.com/libp2p/go-libp2p-kad-dht"
ddht "github.com/libp2p/go-libp2p-kad-dht/dual"
"github.com/libp2p/go-libp2p-kad-dht/fullrt"
"github.com/libp2p/go-libp2p-pubsub"
pubsub "github.com/libp2p/go-libp2p-pubsub"
namesys "github.com/libp2p/go-libp2p-pubsub-router"
record "github.com/libp2p/go-libp2p-record"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
Expand Down Expand Up @@ -179,3 +181,46 @@ func PubsubRouter(mctx helpers.MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (
},
}, psRouter, nil
}

func AutoRelayFeeder(lc fx.Lifecycle, h host.Host, peerChan chan peer.AddrInfo, dht *ddht.DHT) {
ctx, cancel := context.WithCancel(context.Background())

done := make(chan struct{})
go func() {
defer close(done)

t := time.NewTicker(time.Minute / 3) // TODO: this is way too frequent. Should probably use some kind of backoff
defer t.Stop()
for {
select {
case <-t.C:
case <-ctx.Done():
return
}
closestPeers, err := dht.WAN.GetClosestPeers(ctx, h.ID().String())
if err != nil {
fmt.Println(err)
continue
}
for _, p := range closestPeers {
addrs := h.Peerstore().Addrs(p)
if len(addrs) == 0 {
continue
}
select {
case peerChan <- peer.AddrInfo{ID: p, Addrs: addrs}:
case <-ctx.Done():
return
}
}
}
}()

lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
cancel()
<-done
return nil
},
})
}
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ This is likely the feature you're looking for, but see also:
- [`Swarm.RelayService.Enabled`](#swarmrelayserviceenabled) if your node should act as a limited relay for other peers
- Docs: [Libp2p Circuit Relay](https://docs.libp2p.io/concepts/circuit-relay/)

Default: `false`
Default: `true`

Type: `bool`

Expand Down
Loading

0 comments on commit e395f42

Please sign in to comment.