From 9096b162a306c716538b7bb7f59de01b5558475b Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Thu, 11 Apr 2024 21:18:03 +0800 Subject: [PATCH] update connect boot (#108) --- core/node.go | 34 ++++++++++++++++------------------ examples/sub/sub.go | 22 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/core/node.go b/core/node.go index e9516ee..bd3cae9 100644 --- a/core/node.go +++ b/core/node.go @@ -75,11 +75,11 @@ type P2P interface { // GetDirs returns the data directory structure of the host GetDirs() DataDirs - // GetBootstraps returns a list of host bootstraps - GetBootstraps() []string + // GetBootnode returns bootnode + GetBootnode() string - // SetBootstraps updates the host's bootstrap list - SetBootstraps(bootstrap []string) + // SetBootnode updates the host's boot node + SetBootnode(bootnode string) // GetHost() host.Host @@ -216,7 +216,7 @@ type PeerNode struct { rendezvousVersion string protocolPrefix string enableRecv bool - bootnodes []string + bootnode string dhtable *dht.IpfsDHT *protocols } @@ -320,12 +320,11 @@ func NewPeerNode(ctx context.Context, cfg *config.Config) (*PeerNode, error) { protocolVersion: cfg.ProtocolPrefix + p2pProtocolVer, dhtProtocolVersion: cfg.ProtocolPrefix + dhtProtocolVer, rendezvousVersion: cfg.ProtocolPrefix + rendezvous, - bootnodes: boots, enableRecv: true, protocols: NewProtocol(), } - peer_node.dhtable, err = NewDHT(ctx, bhost, cfg.BucketSize, cfg.Version, boots, cfg.ProtocolPrefix, peer_node.dhtProtocolVersion) + peer_node.dhtable, peer_node.bootnode, err = NewDHT(ctx, bhost, cfg.BucketSize, cfg.Version, boots, cfg.ProtocolPrefix, peer_node.dhtProtocolVersion) if err != nil { return nil, fmt.Errorf("[NewDHT] %v", err) } @@ -443,12 +442,12 @@ func (n *PeerNode) GetRendezvousVersion() string { return n.rendezvousVersion } -func (n *PeerNode) GetBootstraps() []string { - return n.bootnodes +func (n *PeerNode) GetBootnode() string { + return n.bootnode } -func (n *PeerNode) SetBootstraps(bootstrap []string) { - n.bootnodes = bootstrap +func (n *PeerNode) SetBootnode(bootnode string) { + n.bootnode = bootnode } func (n *PeerNode) GetHost() host.Host { @@ -636,7 +635,7 @@ func (n *PeerNode) initProtocol(protocolPrefix string) { n.ReadDataStatProtocol = n.NewReadDataStatProtocol() } -func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, boot_nodes []string, protocolPrefix, dhtProtocol string) (*dht.IpfsDHT, error) { +func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, boot_nodes []string, protocolPrefix, dhtProtocol string) (*dht.IpfsDHT, string, error) { var options []dht.Option options = append(options, dht.ProtocolPrefix(protocol.ID(protocolPrefix)), @@ -655,11 +654,11 @@ func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, bo // inhibiting future peer discovery. kademliaDHT, err := dht.New(ctx, h, options...) if err != nil { - return nil, err + return nil, "", err } if err = kademliaDHT.Bootstrap(ctx); err != nil { - return nil, err + return nil, "", err } for _, peerAddr := range boot_nodes { @@ -672,13 +671,12 @@ func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, bo continue } err = h.Connect(ctx, *peerinfo) - if err != nil { - out.Err(fmt.Sprintf("Failed to connect to boot node: %s", peerinfo.ID.String())) - } else { + if err == nil { out.Ok(fmt.Sprintf("Connect to the boot node: %s", peerinfo.ID.String())) + return kademliaDHT, bootstrapAddr.String(), nil } } - return kademliaDHT, nil + return kademliaDHT, "", fmt.Errorf("failed to connect to all boot nodes") } func buildPrimaryResourceManager() (network.ResourceManager, error) { diff --git a/examples/sub/sub.go b/examples/sub/sub.go index 62fe309..7261c16 100644 --- a/examples/sub/sub.go +++ b/examples/sub/sub.go @@ -14,6 +14,7 @@ import ( "log" "os" "strconv" + "strings" "time" p2pgo "github.com/CESSProject/p2p-go" @@ -33,6 +34,8 @@ type Nnode struct { *core.PeerNode } +var room string + func main() { ctx := context.Background() port, err := strconv.Atoi(os.Args[1]) @@ -47,7 +50,7 @@ func main() { p2pgo.Workspace("/home/test/sub"), p2pgo.BucketSize(100), p2pgo.BootPeers([]string{ - "/ip4/127.0.0.1/tcp/3328/p2p/12D3KooWCQBqdQGJj1FUkt7FYdmLhwAxQ1sXC1TSn985MKg6nTbn", + "_dnsaddr.boot-bucket-devnet.cess.cloud", }), p2pgo.ProtocolPrefix(config.TestnetProtocolPrefix), ) @@ -66,16 +69,27 @@ func main() { fmt.Println(peer_node.GetRendezvousVersion()) fmt.Println(peer_node.GetDhtProtocolVersion()) fmt.Println(peer_node.GetProtocolVersion()) + fmt.Println(peer_node.GetBootnode()) - go Discover(ctx, peer_node.GetHost(), peer_node.GetDHTable(), peer_node.GetRendezvousVersion()) + //go Discover(ctx, peer_node.GetHost(), peer_node.GetDHTable(), peer_node.GetRendezvousVersion()) // setup local mDNS discovery if err := setupDiscovery(peer_node.GetHost()); err != nil { panic(err) } + if strings.Contains(peer_node.GetBootnode(), "12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S") { + room = fmt.Sprintf("%s-12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S", core.NetworkRoom) + } else if strings.Contains(peer_node.GetBootnode(), "12D3KooWRm2sQg65y2ZgCUksLsjWmKbBtZ4HRRsGLxbN76XTtC8T") { + room = fmt.Sprintf("%s-12D3KooWRm2sQg65y2ZgCUksLsjWmKbBtZ4HRRsGLxbN76XTtC8T", core.NetworkRoom) + } else if strings.Contains(peer_node.GetBootnode(), "12D3KooWEGeAp1MvvUrBYQtb31FE1LPg7aHsd1LtTXn6cerZTBBd") { + room = fmt.Sprintf("%s-12D3KooWEGeAp1MvvUrBYQtb31FE1LPg7aHsd1LtTXn6cerZTBBd", core.NetworkRoom) + } else { + panic("Failed to connect to boot node") + } + // join the pubsub topic called librum - topic, err := gossipSub.Join(core.NetworkRoom) + topic, err := gossipSub.Join(room) if err != nil { panic(err) } @@ -166,6 +180,6 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { // This lets us automatically discover peers on the same LAN and connect to them. func setupDiscovery(h host.Host) error { // setup mDNS discovery to find local peers - s := mdns.NewMdnsService(h, core.NetworkRoom, &discoveryNotifee{h: h}) + s := mdns.NewMdnsService(h, room, &discoveryNotifee{h: h}) return s.Start() }