Skip to content

Commit

Permalink
update connect boot (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Apr 11, 2024
1 parent 171f318 commit 9096b16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
34 changes: 16 additions & 18 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -216,7 +216,7 @@ type PeerNode struct {
rendezvousVersion string
protocolPrefix string
enableRecv bool
bootnodes []string
bootnode string
dhtable *dht.IpfsDHT
*protocols
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
22 changes: 18 additions & 4 deletions examples/sub/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"os"
"strconv"
"strings"
"time"

p2pgo "github.com/CESSProject/p2p-go"
Expand All @@ -33,6 +34,8 @@ type Nnode struct {
*core.PeerNode
}

var room string

func main() {
ctx := context.Background()
port, err := strconv.Atoi(os.Args[1])
Expand All @@ -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),
)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
}

0 comments on commit 9096b16

Please sign in to comment.