-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht_discovery.go
45 lines (39 loc) · 1.13 KB
/
dht_discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package node
import (
"context"
"log"
"github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multiaddr"
"github.com/pierreleocadie/SecuraChain/internal/config"
"github.com/pierreleocadie/SecuraChain/internal/network"
)
func SetupDHTDiscovery(ctx context.Context, cfg *config.Config, host host.Host, bootstrapNode bool) *network.DHT {
/*
* NETWORK PEER DISCOVERY WITH DHT
*/
// Convert the bootstrap peers from string to multiaddr
var bootstrapPeersMultiaddr []multiaddr.Multiaddr
if !bootstrapNode {
for _, peer := range cfg.BootstrapPeers {
peerMultiaddr, err := multiaddr.NewMultiaddr(peer)
if err != nil {
log.Println("Error converting bootstrap peer to multiaddr : ", err)
return nil
}
bootstrapPeersMultiaddr = append(bootstrapPeersMultiaddr, peerMultiaddr)
}
}
// Initialize DHT in server mode
dhtDiscovery := network.NewDHTDiscovery(
bootstrapNode,
cfg.RendezvousStringFlag,
bootstrapPeersMultiaddr,
cfg.DiscoveryRefreshInterval,
)
// Run DHT
if err := dhtDiscovery.Run(ctx, host); err != nil {
log.Fatalf("Failed to run DHT: %s", err)
return nil
}
return dhtDiscovery
}