Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add netenv #109

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type P2P interface {
// GetBootnode returns bootnode
GetBootnode() string

// GetNetEnv returns network env
GetNetEnv() string

// SetBootnode updates the host's boot node
SetBootnode(bootnode string)

Expand Down Expand Up @@ -217,6 +220,7 @@ type PeerNode struct {
protocolPrefix string
enableRecv bool
bootnode string
netenv string
dhtable *dht.IpfsDHT
*protocols
}
Expand Down Expand Up @@ -324,7 +328,7 @@ func NewPeerNode(ctx context.Context, cfg *config.Config) (*PeerNode, error) {
protocols: NewProtocol(),
}

peer_node.dhtable, peer_node.bootnode, err = NewDHT(ctx, bhost, cfg.BucketSize, cfg.Version, boots, cfg.ProtocolPrefix, peer_node.dhtProtocolVersion)
peer_node.dhtable, peer_node.bootnode, peer_node.netenv, 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 @@ -462,6 +466,10 @@ func (n *PeerNode) GetDirs() DataDirs {
return n.dir
}

func (n *PeerNode) GetNetEnv() string {
return n.netenv
}

func (n *PeerNode) EnableRecv() {
n.enableRecv = true
}
Expand Down Expand Up @@ -635,7 +643,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, string, error) {
func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, boot_nodes []string, protocolPrefix, dhtProtocol string) (*dht.IpfsDHT, string, string, error) {
var options []dht.Option
options = append(options,
dht.ProtocolPrefix(protocol.ID(protocolPrefix)),
Expand All @@ -654,13 +662,14 @@ 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
}

netenv := ""
for _, peerAddr := range boot_nodes {
bootstrapAddr, err := ma.NewMultiaddr(peerAddr)
if err != nil {
Expand All @@ -673,10 +682,23 @@ func NewDHT(ctx context.Context, h host.Host, bucketsize int, version string, bo
err = h.Connect(ctx, *peerinfo)
if err == nil {
out.Ok(fmt.Sprintf("Connect to the boot node: %s", peerinfo.ID.String()))
return kademliaDHT, bootstrapAddr.String(), nil
switch peerinfo.ID.String() {
case "12D3KooWS8a18xoBzwkmUsgGBctNo6QCr6XCpUDR946mTBBUTe83",
"12D3KooWDWeiiqbpNGAqA5QbDTdKgTtwX8LCShWkTpcyxpRf2jA9",
"12D3KooWNcTWWuUWKhjTVDF1xZ38yCoHXoF4aDjnbjsNpeVwj33U":
netenv = "testnet"
case "12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S",
"12D3KooWEGeAp1MvvUrBYQtb31FE1LPg7aHsd1LtTXn6cerZTBBd",
"12D3KooWRm2sQg65y2ZgCUksLsjWmKbBtZ4HRRsGLxbN76XTtC8T":
netenv = "devnet"
default:
netenv = "mainnet"
}

return kademliaDHT, bootstrapAddr.String(), netenv, nil
}
}
return kademliaDHT, "", fmt.Errorf("failed to connect to all boot nodes")
return kademliaDHT, "", netenv, fmt.Errorf("failed to connect to all boot nodes")
}

func buildPrimaryResourceManager() (network.ResourceManager, error) {
Expand Down
Loading