@@ -11,7 +11,9 @@ import (
11
11
"github.com/libp2p/go-libp2p/core/host"
12
12
"github.com/libp2p/go-libp2p/core/peer"
13
13
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
14
+ "github.com/libp2p/go-libp2p/p2p/discovery/util"
14
15
rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
16
+ "github.com/libp2p/go-libp2p/p2p/net/connmgr"
15
17
"github.com/multiformats/go-multiaddr"
16
18
"github.com/pkg/errors"
17
19
"github.com/rs/zerolog/log"
@@ -52,20 +54,20 @@ type P2PNode struct {
52
54
mux sync.Mutex
53
55
host host.Host
54
56
dht * dht.IpfsDHT
57
+ discovery * routing.RoutingDiscovery
55
58
pubSub * pubsub.PubSub
56
59
gossipRooms map [string ]* gossipRoom
57
60
58
61
GossipMessages chan * pubsub.Message
59
62
}
60
63
61
64
type p2pNodeConfig struct {
62
- ListenAddrs []multiaddr.Multiaddr
63
- BootstrapPeers []peer.AddrInfo
64
- PrivKey keys.Libp2pPrivate
65
- Environment env.Environment
66
- IsBootstrapNode bool
67
- DisableTopicDHT bool
68
- DisableRoutingDHT bool
65
+ ListenAddrs []multiaddr.Multiaddr
66
+ BootstrapPeers []peer.AddrInfo
67
+ PrivKey keys.Libp2pPrivate
68
+ Environment env.Environment
69
+ IsBootstrapNode bool
70
+ DiscoveryNamespace string
69
71
}
70
72
71
73
func NewP2PNode (config p2pNodeConfig ) * P2PNode {
@@ -118,6 +120,15 @@ func (p *P2PNode) Run(
118
120
return room .readLoop (ctx , p .GossipMessages )
119
121
})
120
122
}
123
+ runner .Go (func () error {
124
+ log .Info ().Str ("namespace" , p .config .DiscoveryNamespace ).Msg ("starting advertizing discovery node" )
125
+ util .Advertise (ctx , p .discovery , p .config .DiscoveryNamespace )
126
+ <- ctx .Done ()
127
+ return ctx .Err ()
128
+ })
129
+ runner .Go (func () error {
130
+ return findPeers (ctx , p .host , p .discovery , p .config .DiscoveryNamespace )
131
+ })
121
132
return nil
122
133
}
123
134
@@ -141,18 +152,31 @@ func (p *P2PNode) init(ctx context.Context) error {
141
152
if err != nil {
142
153
return err
143
154
}
144
- p2pPubSub , err := createPubSub (ctx , p2pHost , p .config , hashTable )
155
+ discovery := routing .NewRoutingDiscovery (hashTable )
156
+ p2pPubSub , err := createPubSub (ctx , p2pHost , p .config , discovery )
145
157
if err != nil {
146
158
return err
147
159
}
148
160
149
161
p .host = p2pHost
150
162
p .dht = hashTable
163
+ p .discovery = discovery
151
164
p .pubSub = p2pPubSub
152
165
log .Info ().Str ("address" , p .p2pAddress ()).Msg ("created libp2p host" )
153
166
return nil
154
167
}
155
168
169
+ func createConnectionManager () (* connmgr.BasicConnMgr , error ) {
170
+ // TODO: This starts a background goroutine. It works, but it's better to do that later in
171
+ // P2PNode.Run() when we have a proper context.
172
+ m , err := connmgr .NewConnManager (peerLow , peerHigh )
173
+ if err != nil {
174
+ return nil , errors .Wrap (err , "failed to create connection manager" )
175
+ }
176
+
177
+ return m , nil
178
+ }
179
+
156
180
func createHost (
157
181
ctx context.Context ,
158
182
config p2pNodeConfig ,
@@ -166,12 +190,15 @@ func createHost(
166
190
// This was a bug in the check function, reading the wrong config value to check against:
167
191
// https://github.com/libp2p/go-libp2p/issues/2628
168
192
193
+ connectionManager , err := createConnectionManager ()
194
+ if err != nil {
195
+ return nil , nil , err
196
+ }
197
+
169
198
options := []libp2p.Option {
170
199
libp2p .Identity (& config .PrivKey .Key ),
171
200
libp2p .ListenAddrs (config .ListenAddrs ... ),
172
- // libp2p.DefaultTransports,
173
- // libp2p.DefaultSecurity,
174
- // libp2p.ConnectionManager(connectionManager),
201
+ libp2p .ConnectionManager (connectionManager ),
175
202
libp2p .ProtocolVersion (protocolVersion ),
176
203
}
177
204
@@ -192,11 +219,7 @@ func createHost(
192
219
return nil , nil , err
193
220
}
194
221
195
- if config .DisableRoutingDHT {
196
- return p2pHost , nil , err
197
- }
198
-
199
- opts := dhtRoutingOptions (config .Environment , config .BootstrapPeers ... )
222
+ opts := dhtRoutingOptions (& config )
200
223
idht , err := dht .New (ctx , p2pHost , opts ... )
201
224
if err != nil {
202
225
return nil , nil , err
@@ -212,7 +235,7 @@ func createPubSub(
212
235
ctx context.Context ,
213
236
p2pHost host.Host ,
214
237
config p2pNodeConfig ,
215
- hashTable * dht. IpfsDHT ,
238
+ discovery * routing. RoutingDiscovery ,
216
239
) (* pubsub.PubSub , error ) {
217
240
gossipSubParams , peerScoreParams , peerScoreThresholds := makePubSubParams (pubSubParamsOptions {
218
241
isBootstrapNode : config .IsBootstrapNode ,
@@ -222,14 +245,9 @@ func createPubSub(
222
245
pubsubOptions := []pubsub.Option {
223
246
pubsub .WithGossipSubParams (* gossipSubParams ),
224
247
pubsub .WithPeerScore (peerScoreParams , peerScoreThresholds ),
248
+ pubsub .WithDiscovery (discovery ),
225
249
}
226
250
227
- if ! config .DisableTopicDHT {
228
- pubsubOptions = append (
229
- pubsubOptions ,
230
- pubsub .WithDiscovery (routing .NewRoutingDiscovery (hashTable )),
231
- )
232
- }
233
251
if config .IsBootstrapNode {
234
252
// enables the pubsub v1.1 feature to handle discovery and
235
253
// connection management over the PubSub protocol
0 commit comments