Skip to content

Commit

Permalink
Merge branch 'master' into chat_multi_peers
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shahi authored Dec 2, 2024
2 parents 4a931b7 + 7d83705 commit 9b8c0bb
Show file tree
Hide file tree
Showing 135 changed files with 6,193 additions and 1,717 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ concurrency:

jobs:
release-check:
uses: ipdxco/unified-github-workflows/.github/workflows/release-check.yml@v1.0
uses: marcopolo/unified-github-workflows/.github/workflows/release-check.yml@e66cb9667a2e1148efda4591e29c56258eaf385b
10 changes: 10 additions & 0 deletions FUNDING.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"opRetro": {
"projectId": "0xc71faa1bcb4ceb785816c3f22823377e9e5e7c48649badd9f0a0ce491f20d4b3"
},
"drips": {
"filecoin": {
"ownedBy": "0x53DCAf729e11022D5b8949946f6601211C662B38"
}
}
}
62 changes: 34 additions & 28 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"slices"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -35,12 +36,13 @@ import (
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcpreuse"
libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
"github.com/prometheus/client_golang/prometheus"

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/quic-go/quic-go"
"go.uber.org/fx"
Expand Down Expand Up @@ -114,7 +116,7 @@ type Config struct {
Peerstore peerstore.Peerstore
Reporter metrics.Reporter

MultiaddrResolver *madns.Resolver
MultiaddrResolver network.MultiaddrDNSResolver

DisablePing bool

Expand Down Expand Up @@ -142,6 +144,10 @@ type Config struct {
CustomUDPBlackHoleSuccessCounter bool
IPv6BlackHoleSuccessCounter *swarm.BlackHoleSuccessCounter
CustomIPv6BlackHoleSuccessCounter bool

UserFxOptions []fx.Option

ShareTCPListener bool
}

func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swarm, error) {
Expand Down Expand Up @@ -286,7 +292,12 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
fx.Provide(func() connmgr.ConnectionGater { return cfg.ConnectionGater }),
fx.Provide(func() pnet.PSK { return cfg.PSK }),
fx.Provide(func() network.ResourceManager { return cfg.ResourceManager }),
fx.Provide(func() *madns.Resolver { return cfg.MultiaddrResolver }),
fx.Provide(func(gater connmgr.ConnectionGater, rcmgr network.ResourceManager) *tcpreuse.ConnMgr {
if !cfg.ShareTCPListener {
return nil
}
return tcpreuse.NewConnMgr(tcpreuse.EnvReuseportVal, gater, rcmgr)
}),
fx.Provide(func(cm *quicreuse.ConnManager, sw *swarm.Swarm) libp2pwebrtc.ListenUDPFn {
hasQuicAddrPortFor := func(network string, laddr *net.UDPAddr) bool {
quicAddrPorts := map[string]struct{}{}
Expand Down Expand Up @@ -432,16 +443,6 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
if err != nil {
return nil, err
}
if cfg.Relay {
// If we've enabled the relay, we should filter out relay
// addresses by default.
//
// TODO: We shouldn't be doing this here.
originalAddrFactory := h.AddrsFactory
h.AddrsFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr {
return originalAddrFactory(autorelay.Filter(addrs))
}
}
return h, nil
}

Expand Down Expand Up @@ -493,6 +494,9 @@ func (cfg *Config) NewNode() (host.Host, error) {
return sw, nil
}),
fx.Provide(cfg.newBasicHost),
fx.Provide(func(bh *bhost.BasicHost) identify.IDService {
return bh.IDService()
}),
fx.Provide(func(bh *bhost.BasicHost) host.Host {
return bh
}),
Expand All @@ -514,17 +518,8 @@ func (cfg *Config) NewNode() (host.Host, error) {
)
}

// originalAddrFactory is the AddrFactory before it's modified by autorelay
// we need this for checking reachability via autonat
originalAddrFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr {
return addrs
}

// enable autorelay
fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost) {
originalAddrFactory = h.AddrsFactory
}),
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) error {
if cfg.EnableAutoRelay {
if !cfg.DisableMetrics {
Expand Down Expand Up @@ -556,12 +551,14 @@ func (cfg *Config) NewNode() (host.Host, error) {
fxopts = append(fxopts, fx.Invoke(func(bho *routed.RoutedHost) { rh = bho }))
}

fxopts = append(fxopts, cfg.UserFxOptions...)

app := fx.New(fxopts...)
if err := app.Start(context.Background()); err != nil {
return nil, err
}

if err := cfg.addAutoNAT(bh, originalAddrFactory); err != nil {
if err := cfg.addAutoNAT(bh); err != nil {
app.Stop(context.Background())
if cfg.Routing != nil {
rh.Close()
Expand All @@ -577,11 +574,20 @@ func (cfg *Config) NewNode() (host.Host, error) {
return &closableBasicHost{App: app, BasicHost: bh}, nil
}

func (cfg *Config) addAutoNAT(h *bhost.BasicHost, addrF AddrsFactory) error {
func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
// Only use public addresses for autonat
addrFunc := func() []ma.Multiaddr {
return slices.DeleteFunc(h.AllAddrs(), func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
}
if cfg.AddrsFactory != nil {
addrFunc = func() []ma.Multiaddr {
return slices.DeleteFunc(
slices.Clone(cfg.AddrsFactory(h.AllAddrs())),
func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
}
}
autonatOpts := []autonat.Option{
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
}),
autonat.UsingAddresses(addrFunc),
}
if !cfg.DisableMetrics {
autonatOpts = append(autonatOpts, autonat.WithMetricsTracer(
Expand Down Expand Up @@ -664,7 +670,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost, addrF AddrsFactory) error {

autonat, err := autonat.New(h, autonatOpts...)
if err != nil {
return fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
return fmt.Errorf("autonat init failed: %w", err)
}
h.SetAutoNat(autonat)
return nil
Expand Down
2 changes: 1 addition & 1 deletion core/connmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ConnManager interface {
// TagPeer tags a peer with a string, associating a weight with the tag.
TagPeer(peer.ID, string, int)

// Untag removes the tagged value from the peer.
// UntagPeer removes the tagged value from the peer.
UntagPeer(p peer.ID, tag string)

// UpsertTag updates an existing tag or inserts a new one.
Expand Down
50 changes: 10 additions & 40 deletions core/crypto/pb/crypto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions core/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Host interface {
// Peerstore returns the Host's repository of Peer Addresses and Keys.
Peerstore() peerstore.Peerstore

// Returns the listen addresses of the Host
// Addrs returns the listen addresses of the Host
Addrs() []ma.Multiaddr

// Networks returns the Network interface of the Host
// Network returns the Network interface of the Host
Network() network.Network

// Mux returns the Mux multiplexing incoming streams to protocol handlers
Expand All @@ -41,7 +41,7 @@ type Host interface {
// given peer.ID. Connect will absorb the addresses in pi into its internal
// peerstore. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is
// returned. // TODO: Relay + NAT.
// returned.
Connect(ctx context.Context, pi peer.AddrInfo) error

// SetStreamHandler sets the protocol handler on the Host's Mux.
Expand Down
17 changes: 9 additions & 8 deletions core/network/mocks/mock_conn_management_scope.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions core/network/mocks/mock_peer_scope.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9b8c0bb

Please sign in to comment.