Skip to content

Commit 919a2d4

Browse files
committed
Fix relay connectivity issues
Signed-off-by: Gustavo Sampaio <[email protected]>
1 parent 9e10bb9 commit 919a2d4

File tree

4 files changed

+65
-82
lines changed

4 files changed

+65
-82
lines changed

examples/relay/relay.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
)
1515

1616
func relayStart(ctx context.Context, bootstrapPeers []string, id identity.Identity) {
17-
relay, err := crabfs.RelayNew(ctx, 1717, bootstrapPeers, id)
17+
relay, err := crabfs.RelayNew(ctx, 1700, bootstrapPeers, id)
1818
if err != nil {
1919
panic(err)
2020
}
2121

22-
log.Printf("Relay id: %s\n", relay.GetRelayID())
22+
log.Printf("ID: %s\n", relay.GetHostID())
2323
for i, addr := range relay.GetAddrs() {
24-
log.Printf("Relay addr [%d]: %s\n", i, addr)
24+
log.Printf("Addr [%d]: %s\n", i, addr)
2525
}
2626
}
2727

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/libp2p/go-libp2p-kad-dht v0.1.0
2121
github.com/libp2p/go-libp2p-peerstore v0.1.0
2222
github.com/libp2p/go-libp2p-record v0.1.0
23+
github.com/libp2p/go-libp2p-routing v0.1.0
2324
github.com/libp2p/go-libp2p-swarm v0.1.0
2425
github.com/multiformats/go-multiaddr v0.0.4
2526
github.com/multiformats/go-multihash v0.0.5

host.go

+57-43
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ import (
2626
blockstore "github.com/ipfs/go-ipfs-blockstore"
2727
"github.com/libp2p/go-libp2p"
2828
libp2pCircuit "github.com/libp2p/go-libp2p-circuit"
29+
"github.com/libp2p/go-libp2p-core/host"
2930
libp2pHost "github.com/libp2p/go-libp2p-core/host"
3031
libp2pNet "github.com/libp2p/go-libp2p-core/network"
3132
libp2pRouting "github.com/libp2p/go-libp2p-core/routing"
3233
discovery "github.com/libp2p/go-libp2p-discovery"
3334
libp2pDht "github.com/libp2p/go-libp2p-kad-dht"
3435
libp2pDhtOptions "github.com/libp2p/go-libp2p-kad-dht/opts"
3536
libp2pPeerstore "github.com/libp2p/go-libp2p-peerstore"
37+
routing "github.com/libp2p/go-libp2p-routing"
38+
libp2pSwarm "github.com/libp2p/go-libp2p-swarm"
3639
libp2pRoutedHost "github.com/libp2p/go-libp2p/p2p/host/routed"
3740
)
3841

@@ -58,6 +61,24 @@ type hostImpl struct {
5861

5962
// HostNew creates a new host
6063
func HostNew(settings *options.Settings, ds ipfsDatastore.Batching, blockstore blockstore.Blockstore) (interfaces.Host, error) {
64+
provideWorker, err := executor.NewDefaultExecutorContext(settings.Context, 4)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
if err := provideWorker.Start(); err != nil {
70+
return nil, err
71+
}
72+
73+
newHost := &hostImpl{
74+
settings: settings,
75+
76+
ds: ds,
77+
blockstore: blockstore,
78+
79+
provideWorker: provideWorker,
80+
}
81+
6182
sourceMultiAddrIP4, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", settings.Port))
6283
if err != nil {
6384
return nil, err
@@ -75,7 +96,7 @@ func HostNew(settings *options.Settings, ds ipfsDatastore.Batching, blockstore b
7596
if settings.RelayOnly {
7697
opts = append(opts, libp2p.EnableRelay(libp2pCircuit.OptHop))
7798
} else {
78-
opts = append(opts, libp2p.EnableRelay(libp2pCircuit.OptDiscovery))
99+
opts = append(opts, libp2p.EnableRelay(libp2pCircuit.OptDiscovery), libp2p.EnableAutoRelay())
79100
}
80101

81102
id, ok := settings.Identity.(*identity.Libp2pIdentity)
@@ -85,58 +106,42 @@ func HostNew(settings *options.Settings, ds ipfsDatastore.Batching, blockstore b
85106

86107
opts = append(opts, libp2p.Identity(id.GetLibp2pPrivateKey()))
87108

88-
p2pHost, err := libp2p.New(
89-
settings.Context,
90-
opts...,
91-
)
92-
if err != nil {
93-
return nil, err
94-
}
95-
96-
return HostNewWithP2P(settings, p2pHost, ds, blockstore)
97-
}
109+
dhtCreator := func(h host.Host) (routing.PeerRouting, error) {
110+
// Configure peer discovery and key validator
111+
dhtValidator := libp2pDhtOptions.NamespacedValidator(
112+
"crabfs",
113+
DHTNamespaceValidatorNew(settings.Context, newHost.GetSwarmPublicKey),
114+
)
115+
dhtPKValidator := libp2pDhtOptions.NamespacedValidator(
116+
"crabfs_pk",
117+
DHTNamespacePKValidatorNew(),
118+
)
119+
120+
dht, err := libp2pDht.New(settings.Context, h, dhtValidator, dhtPKValidator, libp2pDhtOptions.Datastore(ds))
121+
if err != nil {
122+
return nil, err
123+
}
98124

99-
// HostNewWithP2P creates a new host with an underlying p2p host
100-
func HostNewWithP2P(settings *options.Settings, p2pHost libp2pHost.Host, ds ipfsDatastore.Batching, blockstore blockstore.Blockstore) (interfaces.Host, error) {
101-
provideWorker, err := executor.NewDefaultExecutorContext(settings.Context, 4)
102-
if err != nil {
103-
return nil, err
104-
}
125+
if err = dht.Bootstrap(settings.Context); err != nil {
126+
return nil, err
127+
}
105128

106-
if err := provideWorker.Start(); err != nil {
107-
return nil, err
129+
newHost.dht = dht
130+
return dht, nil
108131
}
109132

110-
newHost := &hostImpl{
111-
settings: settings,
112-
113-
ds: ds,
114-
blockstore: blockstore,
115-
116-
provideWorker: provideWorker,
117-
}
133+
opts = append(opts, libp2p.Routing(dhtCreator))
118134

119-
// Configure peer discovery and key validator
120-
dhtValidator := libp2pDhtOptions.NamespacedValidator(
121-
"crabfs",
122-
DHTNamespaceValidatorNew(settings.Context, newHost.GetSwarmPublicKey),
123-
)
124-
dhtPKValidator := libp2pDhtOptions.NamespacedValidator(
125-
"crabfs_pk",
126-
DHTNamespacePKValidatorNew(),
135+
p2pHost, err := libp2p.New(
136+
settings.Context,
137+
opts...,
127138
)
128-
dht, err := libp2pDht.New(settings.Context, p2pHost, dhtValidator, dhtPKValidator, libp2pDhtOptions.Datastore(ds))
129139
if err != nil {
130140
return nil, err
131141
}
132142

133-
newHost.dht = dht
134-
135-
if err = dht.Bootstrap(settings.Context); err != nil {
136-
return nil, err
137-
}
138-
139-
newHost.p2pHost = libp2pRoutedHost.Wrap(p2pHost, dht)
143+
newHost.p2pHost = libp2pRoutedHost.Wrap(p2pHost, newHost.dht)
144+
// newHost.p2pHost = p2pHost
140145

141146
newHost.p2pHost.SetStreamHandler(ProtocolV1, newHost.handleStreamV1)
142147

@@ -620,6 +625,15 @@ func (host *hostImpl) FindProviders(ctx context.Context, blockMeta *pb.BlockMeta
620625
}
621626

622627
func (host *hostImpl) CreateBlockStream(ctx context.Context, blockMeta *pb.BlockMetadata, peer *libp2pPeerstore.PeerInfo) (io.Reader, error) {
628+
circuitAddr, err := multiaddr.NewMultiaddr("/p2p-circuit/p2p/" + peer.ID.Pretty())
629+
if err != nil {
630+
return nil, err
631+
}
632+
633+
// Add the relay addr circuit to this peer
634+
host.p2pHost.Peerstore().AddAddr(peer.ID, circuitAddr, libp2pPeerstore.TempAddrTTL)
635+
636+
host.p2pHost.Network().(*libp2pSwarm.Swarm).Backoff().Clear(peer.ID)
623637
stream, err := host.p2pHost.NewStream(ctx, peer.ID, ProtocolV1)
624638
if err != nil {
625639
return nil, err

relay.go

+4-36
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ package crabfs
22

33
import (
44
"context"
5-
"fmt"
6-
"strings"
75

86
"github.com/runletapp/crabfs/identity"
97

10-
"github.com/libp2p/go-libp2p"
11-
libp2pCircuit "github.com/libp2p/go-libp2p-circuit"
128
libp2pHost "github.com/libp2p/go-libp2p-core/host"
13-
"github.com/multiformats/go-multiaddr"
149
"github.com/runletapp/crabfs/interfaces"
1510
"github.com/runletapp/crabfs/options"
1611
)
@@ -27,42 +22,20 @@ type Relay struct {
2722

2823
// RelayNew creates a new relay instance
2924
func RelayNew(ctx context.Context, port uint, bootstrapPeers []string, id identity.Identity) (*Relay, error) {
30-
relayHost, err := libp2p.New(
31-
ctx,
32-
libp2p.EnableRelay(libp2pCircuit.OptHop),
33-
)
34-
if err != nil {
35-
return nil, err
36-
}
37-
38-
relayAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", relayHost.ID().Pretty()))
39-
if err != nil {
40-
return nil, err
41-
}
42-
43-
addrs := bootstrapPeers
44-
for _, addr := range relayHost.Addrs() {
45-
if strings.HasPrefix(addr.String(), "/ip4/127") || strings.HasPrefix(addr.String(), "/ip6/::1") {
46-
addrs = append(addrs, addr.Encapsulate(relayAddr).String())
47-
}
48-
}
49-
5025
host, err := New(
5126
options.Port(port),
5227
options.RelayOnly(true),
53-
options.BootstrapPeers(addrs),
28+
options.BootstrapPeers(bootstrapPeers),
5429
options.Identity(id),
5530
)
5631
if err != nil {
57-
relayHost.Close()
5832
return nil, err
5933
}
6034

6135
relay := &Relay{
62-
ctx: ctx,
63-
port: port,
64-
host: host,
65-
p2pRelayHost: relayHost,
36+
ctx: ctx,
37+
port: port,
38+
host: host,
6639
}
6740

6841
return relay, nil
@@ -86,8 +59,3 @@ func (relay *Relay) GetAddrs() []string {
8659
func (relay *Relay) GetHostID() string {
8760
return relay.host.GetID()
8861
}
89-
90-
// GetRelayID returns the id of this p2p relay
91-
func (relay *Relay) GetRelayID() string {
92-
return relay.p2pRelayHost.ID().Pretty()
93-
}

0 commit comments

Comments
 (0)