MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / bootstrapDHT

Function bootstrapDHT

IceFireDB-SQLProxy/pkg/p2p/p2p.go:373–416  ·  view source on GitHub ↗

A function that bootstraps a given Kademlia DHT to satisfy the IPFS router interface and connects to all the bootstrap peers provided by libp2p

(ctx context.Context, nodehost host.Host, kaddht *dht.IpfsDHT)

Source from the content-addressed store, hash-verified

371// A function that bootstraps a given Kademlia DHT to satisfy the IPFS router
372// interface and connects to all the bootstrap peers provided by libp2p
373func bootstrapDHT(ctx context.Context, nodehost host.Host, kaddht *dht.IpfsDHT) {
374 // Bootstrap the DHT to satisfy the IPFS Router interface
375 if err := kaddht.Bootstrap(ctx); err != nil {
376 logrus.WithFields(logrus.Fields{
377 "error": err.Error(),
378 }).Fatalln("Failed to Bootstrap the Kademlia!")
379 }
380
381 // Trace log
382 logrus.Traceln("Set the Kademlia DHT into Bootstrap Mode.")
383
384 // Declare a WaitGroup
385 var wg sync.WaitGroup
386 // Declare counters for the number of bootstrap peers
387 var connectedbootpeers int32
388 var totalbootpeers int32
389
390 // Iterate over the default bootstrap peers provided by libp2p
391 for _, peeraddr := range dht.DefaultBootstrapPeers {
392 // Retrieve the peer address information
393 peerinfo, _ := peer.AddrInfoFromP2pAddr(peeraddr)
394
395 // Incremenent waitgroup counter
396 wg.Add(1)
397 totalbootpeers++
398 // Start a goroutine to connect to each bootstrap peer
399 go func() {
400 // Defer the waitgroup decrement
401 defer wg.Done()
402 // Attempt to connect to the bootstrap peer
403 if err := nodehost.Connect(ctx, *peerinfo); err == nil {
404 // Increment the connected bootstrap peer count
405 atomic.AddInt32(&connectedbootpeers, 1)
406 // log.Println("Connected bootstrap peer success.", peerinfo.ID, peerinfo)
407 }
408 }()
409 }
410
411 // Wait for the waitgroup to complete
412 wg.Wait()
413
414 // Log the number of bootstrap peers connected
415 logrus.Debugf("Connected to %d out of %d Bootstrap Peers.", connectedbootpeers, totalbootpeers)
416}
417
418// A function that connects the given host to all peers received from a
419// channel of peer address information. Meant to be started as a go routine.

Callers 1

NewP2PFunction · 0.70

Calls 2

DoneMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected