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

@@ -16,11 +16,10 @@ import (
) )
var ( var (
topicNameFlag = flag.String("topicName", "akdjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd2", "name of topic to join") topicNameFlag = flag.String("topicName", "pentapass", "name of topic to join")
dbPath = flag.String("db", "./db", "db file path") dbPath = flag.String("db", "./db", "db file path")
nameSpace = flag.String("namespace", "crdt", "namespace") nameSpace = flag.String("namespace", "crdt", "namespace")
logger = logging.Logger("globaldb") logger = logging.Logger("globaldb")
// topicName = "globaldb-example"
// netTopic = "globaldb-example-net" // netTopic = "globaldb-example-net"
// config = "globaldb-example" // config = "globaldb-example"
) )
@@ -58,7 +57,7 @@ func main() {
panic(err) panic(err)
} }
//topic, err := ps.Join(*topicNameFlag) //_, err = ps.Join(*topicNameFlag)
//if err != nil { //if err != nil {
// panic(err) // panic(err)
//} //}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" d2Version="v0.6.5" preserveAspectRatio="xMinYMin meet" viewBox="0 0 1984 3328"><svg id="d2-svg" class="d2-1224808988" width="1984" height="3328" viewBox="-91 -121 1984 3328"><rect x="-91.000000" y="-121.000000" width="1984.000000" height="3328.000000" rx="0.000000" class=" fill-N7" stroke-width="0" /><style type="text/css"><![CDATA[ <?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" d2Version="v0.6.6" preserveAspectRatio="xMinYMin meet" viewBox="0 0 1984 3328"><svg id="d2-svg" class="d2-1224808988" width="1984" height="3328" viewBox="-91 -121 1984 3328"><rect x="-91.000000" y="-121.000000" width="1984.000000" height="3328.000000" rx="0.000000" class=" fill-N7" stroke-width="0" /><style type="text/css"><![CDATA[
.d2-1224808988 .text { .d2-1224808988 .text {
font-family: "d2-1224808988-font-regular"; font-family: "d2-1224808988-font-regular";
} }

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

View File

@@ -20,8 +20,7 @@ import (
"github.com/libp2p/go-libp2p/core/control" "github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/network"
//"github.com/libp2p/go-libp2p/core/peerstore" //"github.com/libp2p/go-libp2p/core/peerstore"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing" discovery "github.com/libp2p/go-libp2p/p2p/discovery/routing"
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
ds "github.com/ipfs/go-datastore" ds "github.com/ipfs/go-datastore"
ipfslite "github.com/hsanjuan/ipfs-lite" ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/google/uuid" "github.com/google/uuid"
@@ -511,7 +510,7 @@ func PrintDBContent(ctx context.Context, store *badger.Datastore) {
func (s *StorageHandler) ListNamespaces() []string { func (s *StorageHandler) ListNamespaces() []string {
var result []string var result []string
for k, _ := range s.Namespaces { for k := range s.Namespaces {
result = append(result, k) result = append(result, k)
} }
@@ -555,11 +554,12 @@ func CreateNamespace(ID string, storageHandler *StorageHandler) (*Namespace, err
fmt.Printf("Creating Namespace %s\n", ID) fmt.Printf("Creating Namespace %s\n", ID)
err := storageHandler.PubSub.RegisterTopicValidator( err := storageHandler.PubSub.RegisterTopicValidator(
ID, //== topicName 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() signer := msg.GetFrom()
trusted := IsTrustedPeer(ctx, signer, ID, storageHandler.Config) trusted := IsTrustedPeer(ctx, signer, ID, storageHandler.Config)
if !trusted { 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 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) { func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
routingDiscovery := drouting.NewRoutingDiscovery(dht) routingDiscovery := discovery.NewRoutingDiscovery(dht)
dutil.Advertise(ctx, routingDiscovery, topicNameFlag) routingDiscovery.Advertise(ctx, topicNameFlag)
// Look for others who have announced and attempt to connect to them // Look for others who have announced and attempt to connect to them
anyConnected := false anyConnected := false
@@ -648,29 +648,21 @@ func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
panic(err) panic(err)
} }
for peer := range peerChan { for peer := range peerChan {
if peer.ID == h.ID() { if peer.ID == h.ID() || len(peer.Addrs) == 0 {
continue // No self connection continue // No self connection
} }
if h.Network().Connectedness(peer.ID) == network.Connected {
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 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) 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") fmt.Println("Peer discovery complete")