@@ -26,13 +26,16 @@ import (
26
26
blockstore "github.com/ipfs/go-ipfs-blockstore"
27
27
"github.com/libp2p/go-libp2p"
28
28
libp2pCircuit "github.com/libp2p/go-libp2p-circuit"
29
+ "github.com/libp2p/go-libp2p-core/host"
29
30
libp2pHost "github.com/libp2p/go-libp2p-core/host"
30
31
libp2pNet "github.com/libp2p/go-libp2p-core/network"
31
32
libp2pRouting "github.com/libp2p/go-libp2p-core/routing"
32
33
discovery "github.com/libp2p/go-libp2p-discovery"
33
34
libp2pDht "github.com/libp2p/go-libp2p-kad-dht"
34
35
libp2pDhtOptions "github.com/libp2p/go-libp2p-kad-dht/opts"
35
36
libp2pPeerstore "github.com/libp2p/go-libp2p-peerstore"
37
+ routing "github.com/libp2p/go-libp2p-routing"
38
+ libp2pSwarm "github.com/libp2p/go-libp2p-swarm"
36
39
libp2pRoutedHost "github.com/libp2p/go-libp2p/p2p/host/routed"
37
40
)
38
41
@@ -58,6 +61,24 @@ type hostImpl struct {
58
61
59
62
// HostNew creates a new host
60
63
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
+
61
82
sourceMultiAddrIP4 , err := multiaddr .NewMultiaddr (fmt .Sprintf ("/ip4/0.0.0.0/tcp/%d" , settings .Port ))
62
83
if err != nil {
63
84
return nil , err
@@ -75,7 +96,7 @@ func HostNew(settings *options.Settings, ds ipfsDatastore.Batching, blockstore b
75
96
if settings .RelayOnly {
76
97
opts = append (opts , libp2p .EnableRelay (libp2pCircuit .OptHop ))
77
98
} else {
78
- opts = append (opts , libp2p .EnableRelay (libp2pCircuit .OptDiscovery ))
99
+ opts = append (opts , libp2p .EnableRelay (libp2pCircuit .OptDiscovery ), libp2p . EnableAutoRelay () )
79
100
}
80
101
81
102
id , ok := settings .Identity .(* identity.Libp2pIdentity )
@@ -85,58 +106,42 @@ func HostNew(settings *options.Settings, ds ipfsDatastore.Batching, blockstore b
85
106
86
107
opts = append (opts , libp2p .Identity (id .GetLibp2pPrivateKey ()))
87
108
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
+ }
98
124
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
+ }
105
128
106
- if err := provideWorker . Start (); err != nil {
107
- return nil , err
129
+ newHost . dht = dht
130
+ return dht , nil
108
131
}
109
132
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 ))
118
134
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 ... ,
127
138
)
128
- dht , err := libp2pDht .New (settings .Context , p2pHost , dhtValidator , dhtPKValidator , libp2pDhtOptions .Datastore (ds ))
129
139
if err != nil {
130
140
return nil , err
131
141
}
132
142
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
140
145
141
146
newHost .p2pHost .SetStreamHandler (ProtocolV1 , newHost .handleStreamV1 )
142
147
@@ -620,6 +625,15 @@ func (host *hostImpl) FindProviders(ctx context.Context, blockMeta *pb.BlockMeta
620
625
}
621
626
622
627
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 )
623
637
stream , err := host .p2pHost .NewStream (ctx , peer .ID , ProtocolV1 )
624
638
if err != nil {
625
639
return nil , err
0 commit comments