QOL: Utilize configured scoring metrics and bootstrap peers to better flesh out mesh

This commit is contained in:
Cassandra Heart 2023-10-02 00:40:29 -05:00
parent 289c56e9df
commit 3c35244cea
No known key found for this signature in database
GPG Key ID: 6352152859385958

View File

@ -1,6 +1,7 @@
package p2p package p2p
import ( import (
"bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
@ -237,8 +238,27 @@ func initDHT(
logger *zap.Logger, logger *zap.Logger,
h host.Host, h host.Host,
) *dht.IpfsDHT { ) *dht.IpfsDHT {
isBootstrapPeer := false
for _, peerAddr := range p2pConfig.BootstrapPeers {
peerinfo, err := peer.AddrInfoFromString(peerAddr)
if err != nil {
panic(err)
}
if bytes.Equal([]byte(peerinfo.ID), []byte(h.ID())) {
isBootstrapPeer = true
break
}
}
logger.Info("establishing dht") logger.Info("establishing dht")
kademliaDHT, err := dht.New(ctx, h) var kademliaDHT *dht.IpfsDHT
var err error
if isBootstrapPeer {
kademliaDHT, err = dht.New(ctx, h, dht.Mode(dht.ModeServer))
} else {
kademliaDHT, err = dht.New(ctx, h, dht.Mode(dht.ModeAuto))
}
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -249,7 +269,10 @@ func initDHT(
reconnect := func() { reconnect := func() {
var wg sync.WaitGroup var wg sync.WaitGroup
logger.Info("connecting to bootstrap", zap.String("peer_id", h.ID().String())) logger.Info(
"connecting to bootstrap",
zap.String("peer_id", h.ID().String()),
)
defaultBootstrapPeers := p2pConfig.BootstrapPeers defaultBootstrapPeers := p2pConfig.BootstrapPeers
@ -264,6 +287,10 @@ func initDHT(
if err := h.Connect(ctx, *peerinfo); err != nil { if err := h.Connect(ctx, *peerinfo); err != nil {
logger.Warn("error while connecting to dht peer", zap.Error(err)) logger.Warn("error while connecting to dht peer", zap.Error(err))
} }
logger.Info(
"connected to peer",
zap.String("peer_id", peerinfo.ID.String()),
)
}() }()
} }
wg.Wait() wg.Wait()
@ -327,43 +354,40 @@ func discoverPeers(
) { ) {
logger.Info("initiating peer discovery") logger.Info("initiating peer discovery")
discover := func(peerCount int) { discover := func() {
for peerCount < p2pConfig.MinPeers { peerChan, err := routingDiscovery.FindPeers(ctx, string(BITMASK_ALL))
peerChan, err := routingDiscovery.FindPeers(ctx, string(BITMASK_ALL)) if err != nil {
if err != nil { logger.Error("could not find peers", zap.Error(err))
logger.Error("could not find peers", zap.Error(err)) }
for peer := range peerChan {
if peer.ID == h.ID() {
continue
} }
for peer := range peerChan { logger.Info("found peer", zap.String("peer_id", peer.ID.Pretty()))
if peer.ID == h.ID() { err := h.Connect(ctx, peer)
continue if err != nil {
} logger.Warn(
"error while connecting to blossomsub peer",
logger.Info("found peer", zap.String("peer_id", peer.ID.Pretty())) zap.String("peer_id", peer.ID.Pretty()),
err := h.Connect(ctx, peer) zap.Error(err),
if err != nil { )
logger.Warn( } else {
"error while connecting to blossomsub peer", logger.Info(
zap.String("peer_id", peer.ID.Pretty()), "connected to peer",
zap.Error(err), zap.String("peer_id", peer.ID.Pretty()),
) )
} else {
logger.Info(
"connected to peer",
zap.String("peer_id", peer.ID.Pretty()),
)
peerCount++
}
} }
} }
} }
discover(0) discover()
go func() { go func() {
for { for {
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
discover(len(h.Network().Peers())) discover()
} }
}() }()