WIP experimenting with discovery

This commit is contained in:
2024-10-08 18:49:05 +02:00
parent 9fce13c6db
commit 0bb7ce12bd
3 changed files with 21 additions and 30 deletions

View File

@@ -20,8 +20,7 @@ import (
"github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/network"
//"github.com/libp2p/go-libp2p/core/peerstore"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
discovery "github.com/libp2p/go-libp2p/p2p/discovery/routing"
ds "github.com/ipfs/go-datastore"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/google/uuid"
@@ -511,7 +510,7 @@ func PrintDBContent(ctx context.Context, store *badger.Datastore) {
func (s *StorageHandler) ListNamespaces() []string {
var result []string
for k, _ := range s.Namespaces {
for k := range s.Namespaces {
result = append(result, k)
}
@@ -555,11 +554,12 @@ func CreateNamespace(ID string, storageHandler *StorageHandler) (*Namespace, err
fmt.Printf("Creating Namespace %s\n", ID)
err := storageHandler.PubSub.RegisterTopicValidator(
ID, //== topicName
func(ctx context.Context, _ peer.ID, msg *pubsub.Message) bool {
func(ctx context.Context, id peer.ID, msg *pubsub.Message) bool {
fmt.Printf("PubSubmsg TOPIC: %s, PEER: %s\n", ID, id)
signer := msg.GetFrom()
trusted := IsTrustedPeer(ctx, signer, ID, storageHandler.Config)
if !trusted {
logger.Debug("discarded pubsub message from non trusted source %s ", signer)
fmt.Printf("discarded pubsub message from non trusted source %s\n", signer)
}
return trusted
},
@@ -634,8 +634,8 @@ func initDHT(ctx context.Context, h host.Host) *dht.IpfsDHT {
}
func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
routingDiscovery := drouting.NewRoutingDiscovery(dht)
dutil.Advertise(ctx, routingDiscovery, topicNameFlag)
routingDiscovery := discovery.NewRoutingDiscovery(dht)
routingDiscovery.Advertise(ctx, topicNameFlag)
// Look for others who have announced and attempt to connect to them
anyConnected := false
@@ -648,29 +648,21 @@ func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
panic(err)
}
for peer := range peerChan {
if peer.ID == h.ID() {
if peer.ID == h.ID() || len(peer.Addrs) == 0 {
continue // No self connection
}
fmt.Printf("Trying to connect to a peer with id %s", peer.ID.String())
if h.Network().Connectedness(peer.ID) != network.Connected {
_, err = h.Network().DialPeer(ctx, peer.ID)
if err != nil {
fmt.Println("Connected to:", peer.ID)
continue
}
fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
if h.Network().Connectedness(peer.ID) == network.Connected {
continue
}
fmt.Printf("Connecting to peer with id %s", peer.ID.String())
err := h.Connect(ctx, peer)
if err != nil {
fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
} else {
fmt.Println("Connected to:", peer.ID)
}
//fmt.Printf("Trying to connect to a peer with id %s", peer.ID.String())
//err := h.Connect(ctx, peer)
//if err != nil {
// fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
//} else {
// fmt.Println("Connected to:", peer.ID)
// anyConnected = true
//}
}
}
fmt.Println("Peer discovery complete")