@@ -10,14 +10,11 @@ import (
1010 "time"
1111
1212 libp2p "github.com/libp2p/go-libp2p"
13- dht "github.com/libp2p/go-libp2p-kad-dht"
1413 "github.com/libp2p/go-libp2p/core/crypto"
1514 "github.com/libp2p/go-libp2p/core/host"
1615 "github.com/libp2p/go-libp2p/core/network"
1716 "github.com/libp2p/go-libp2p/core/peer"
1817 "github.com/libp2p/go-libp2p/core/protocol"
19- discovery_routing "github.com/libp2p/go-libp2p/p2p/discovery/routing"
20- discovery_util "github.com/libp2p/go-libp2p/p2p/discovery/util"
2118 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
2219 "github.com/libp2p/go-libp2p/p2p/net/connmgr"
2320 "github.com/libp2p/go-libp2p/p2p/protocol/ping"
@@ -49,7 +46,6 @@ type Message struct {
4946
5047// Communication use p2p to broadcast messages among all the TSS nodes
5148type Communication struct {
52- rendezvous string // based on group
5349 bootstrapPeers []maddr.Multiaddr
5450 logger zerolog.Logger
5551 listenAddr maddr.Multiaddr
@@ -63,11 +59,11 @@ type Communication struct {
6359 externalAddr maddr.Multiaddr
6460 streamMgr * StreamMgr
6561 whitelistedPeers []peer.ID
62+ discovery * PeerDiscovery
6663}
6764
6865// NewCommunication create a new instance of Communication
6966func NewCommunication (
70- rendezvous string ,
7167 bootstrapPeers []maddr.Multiaddr ,
7268 port int ,
7369 externalIP string ,
@@ -85,7 +81,7 @@ func NewCommunication(
8581 }
8682 }
8783 return & Communication {
88- rendezvous : rendezvous ,
84+
8985 bootstrapPeers : bootstrapPeers ,
9086 logger : log .With ().Str ("module" , "communication" ).Logger (),
9187 listenAddr : addr ,
@@ -252,7 +248,7 @@ func (c *Communication) bootStrapConnectivityCheck() error {
252248}
253249
254250func (c * Communication ) startChannel (privKeyBytes []byte ) error {
255- ctx := context . Background ( )
251+ c . logger . Info (). Msgf ( "No DHT enabled; use private gossip instead." )
256252 p2pPriKey , err := crypto .UnmarshalSecp256k1PrivateKey (privKeyBytes )
257253 if err != nil {
258254 c .logger .Error ().Msgf ("error is %f" , err )
@@ -310,6 +306,7 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
310306 libp2p .ResourceManager (m ),
311307 libp2p .ConnectionManager (cmgr ),
312308 libp2p .ConnectionGater (NewWhitelistConnectionGater (c .whitelistedPeers , c .logger )),
309+ libp2p .DisableRelay (),
313310 )
314311 if err != nil {
315312 return fmt .Errorf ("fail to create p2p host: %w" , err )
@@ -321,14 +318,6 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
321318 // client because we want each peer to maintain its own local copy of the
322319 // DHT, so that the bootstrapping node of the DHT can go down without
323320 // inhibiting future peer discovery.
324- kademliaDHT , err := dht .New (ctx , h , dht .Mode (dht .ModeServer ))
325- if err != nil {
326- return fmt .Errorf ("fail to create DHT: %w" , err )
327- }
328- c .logger .Debug ().Msg ("Bootstrapping the DHT" )
329- if err = kademliaDHT .Bootstrap (ctx ); err != nil {
330- return fmt .Errorf ("fail to bootstrap DHT: %w" , err )
331- }
332321
333322 var connectionErr error
334323 for i := 0 ; i < 5 ; i ++ {
@@ -343,30 +332,28 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
343332 return fmt .Errorf ("fail to connect to bootstrap peer: %w" , connectionErr )
344333 }
345334
346- // We use a rendezvous point "meet me here" to announce our location.
347- // This is like telling your friends to meet you at the Eiffel Tower.
348- routingDiscovery := discovery_routing .NewRoutingDiscovery (kademliaDHT )
349- discovery_util .Advertise (ctx , routingDiscovery , c .rendezvous )
350-
351- // Create a goroutine to shut down the DHT after 5 minutes
352- go func () {
353- select {
354- case <- time .After (5 * time .Minute ):
355- c .logger .Info ().Msg ("Closing Kademlia DHT after 5 minutes" )
356- if err := kademliaDHT .Close (); err != nil {
357- c .logger .Error ().Err (err ).Msg ("Failed to close Kademlia DHT" )
358- }
359- case <- ctx .Done ():
360- c .logger .Info ().Msg ("Context done, not waiting for 5 minutes to close DHT" )
361- }
362- }()
363-
364335 err = c .bootStrapConnectivityCheck ()
365336 if err != nil {
366337 return err
367338 }
368339
369340 c .logger .Info ().Msg ("Successfully announced!" )
341+
342+ c .logger .Info ().Msg ("Start peer discovery/gossip..." )
343+ //c.bootstrapPeers
344+ bootstrapPeerAddrInfos := make ([]peer.AddrInfo , 0 , len (c .bootstrapPeers ))
345+ for _ , addr := range c .bootstrapPeers {
346+ peerInfo , err := peer .AddrInfoFromP2pAddr (addr )
347+ if err != nil {
348+ c .logger .Error ().Err (err ).Msgf ("fail to convert multiaddr to peer info: %s" , addr )
349+ continue
350+ }
351+ bootstrapPeerAddrInfos = append (bootstrapPeerAddrInfos , * peerInfo )
352+ }
353+ discovery := NewPeerDiscovery (c .host , bootstrapPeerAddrInfos )
354+ c .discovery = discovery
355+ discovery .Start (context .Background ())
356+
370357 return nil
371358}
372359
@@ -435,6 +422,9 @@ func (c *Communication) Start(priKeyBytes []byte) error {
435422
436423// Stop communication
437424func (c * Communication ) Stop () error {
425+ if c .discovery != nil {
426+ c .discovery .Stop ()
427+ }
438428 // we need to stop the handler and the p2p services firstly, then terminate the our communication threads
439429 if err := c .host .Close (); err != nil {
440430 c .logger .Err (err ).Msg ("fail to close host network" )
0 commit comments