mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Create node's P2P registration file (#8745)
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -23,6 +23,7 @@ go_library(
|
||||
"//beacon-chain/forkchoice/protoarray:go_default_library",
|
||||
"//beacon-chain/gateway:go_default_library",
|
||||
"//beacon-chain/interop-cold-start:go_default_library",
|
||||
"//beacon-chain/node/registration:go_default_library",
|
||||
"//beacon-chain/operations/attestations:go_default_library",
|
||||
"//beacon-chain/operations/slashings:go_default_library",
|
||||
"//beacon-chain/operations/voluntaryexits:go_default_library",
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/gateway"
|
||||
interopcoldstart "github.com/prysmaticlabs/prysm/beacon-chain/interop-cold-start"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/node/registration"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/slashings"
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/operations/voluntaryexits"
|
||||
@@ -324,38 +325,17 @@ func readbootNodes(fileName string) ([]string, error) {
|
||||
}
|
||||
|
||||
func (b *BeaconNode) registerP2P(cliCtx *cli.Context) error {
|
||||
// Bootnode ENR may be a filepath to a YAML file
|
||||
bootnodesTemp := params.BeaconNetworkConfig().BootstrapNodes // actual CLI values
|
||||
bootnodeAddrs := make([]string, 0) // dest of final list of nodes
|
||||
for _, addr := range bootnodesTemp {
|
||||
if filepath.Ext(addr) == ".yaml" {
|
||||
fileNodes, err := readbootNodes(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bootnodeAddrs = append(bootnodeAddrs, fileNodes...)
|
||||
} else {
|
||||
bootnodeAddrs = append(bootnodeAddrs, addr)
|
||||
}
|
||||
}
|
||||
|
||||
datadir := cliCtx.String(cmd.DataDirFlag.Name)
|
||||
if datadir == "" {
|
||||
datadir = cmd.DefaultDataDir()
|
||||
if datadir == "" {
|
||||
log.Fatal(
|
||||
"Could not determine your system's HOME path, please specify a --datadir you wish " +
|
||||
"to use for your chain data",
|
||||
)
|
||||
}
|
||||
bootstrapNodeAddrs, dataDir, err := registration.P2PPreregistration(cliCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc, err := p2p.NewService(b.ctx, &p2p.Config{
|
||||
NoDiscovery: cliCtx.Bool(cmd.NoDiscovery.Name),
|
||||
StaticPeers: sliceutil.SplitCommaSeparated(cliCtx.StringSlice(cmd.StaticPeers.Name)),
|
||||
BootstrapNodeAddr: bootnodeAddrs,
|
||||
BootstrapNodeAddr: bootstrapNodeAddrs,
|
||||
RelayNodeAddr: cliCtx.String(cmd.RelayNode.Name),
|
||||
DataDir: datadir,
|
||||
DataDir: dataDir,
|
||||
LocalIP: cliCtx.String(cmd.P2PIP.Name),
|
||||
HostAddress: cliCtx.String(cmd.P2PHost.Name),
|
||||
HostDNS: cliCtx.String(cmd.P2PHostDNS.Name),
|
||||
|
||||
28
beacon-chain/node/registration/BUILD.bazel
Normal file
28
beacon-chain/node/registration/BUILD.bazel
Normal file
@@ -0,0 +1,28 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_test")
|
||||
load("@prysm//tools/go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["p2p.go"],
|
||||
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/node/registration",
|
||||
visibility = ["//beacon-chain/node:__subpackages__"],
|
||||
deps = [
|
||||
"//shared/cmd:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"@com_github_urfave_cli_v2//:go_default_library",
|
||||
"@in_gopkg_yaml_v2//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["p2p_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//shared/cmd:go_default_library",
|
||||
"//shared/params:go_default_library",
|
||||
"//shared/testutil/assert:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_urfave_cli_v2//:go_default_library",
|
||||
],
|
||||
)
|
||||
56
beacon-chain/node/registration/p2p.go
Normal file
56
beacon-chain/node/registration/p2p.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package registration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/cmd"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// P2PPreregistration prepares data for p2p.Service's registration.
|
||||
func P2PPreregistration(cliCtx *cli.Context) (bootstrapNodeAddrs []string, dataDir string, err error) {
|
||||
// Bootnode ENR may be a filepath to a YAML file
|
||||
bootnodesTemp := params.BeaconNetworkConfig().BootstrapNodes // actual CLI values
|
||||
bootstrapNodeAddrs = make([]string, 0) // dest of final list of nodes
|
||||
for _, addr := range bootnodesTemp {
|
||||
if filepath.Ext(addr) == ".yaml" {
|
||||
fileNodes, err := readbootNodes(addr)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
bootstrapNodeAddrs = append(bootstrapNodeAddrs, fileNodes...)
|
||||
} else {
|
||||
bootstrapNodeAddrs = append(bootstrapNodeAddrs, addr)
|
||||
}
|
||||
}
|
||||
|
||||
dataDir = cliCtx.String(cmd.DataDirFlag.Name)
|
||||
if dataDir == "" {
|
||||
dataDir = cmd.DefaultDataDir()
|
||||
if dataDir == "" {
|
||||
log.Fatal(
|
||||
"Could not determine your system's HOME path, please specify a --datadir you wish " +
|
||||
"to use for your chain data",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func readbootNodes(fileName string) ([]string, error) {
|
||||
fileContent, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
listNodes := make([]string, 0)
|
||||
err = yaml.Unmarshal(fileContent, &listNodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return listNodes, nil
|
||||
}
|
||||
68
beacon-chain/node/registration/p2p_test.go
Normal file
68
beacon-chain/node/registration/p2p_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package registration
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/cmd"
|
||||
"github.com/prysmaticlabs/prysm/shared/params"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func TestP2PPreregistration_DefaultDataDir(t *testing.T) {
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, "", "")
|
||||
ctx := cli.NewContext(&app, set, nil)
|
||||
|
||||
_, dataDir, err := P2PPreregistration(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, cmd.DefaultDataDir(), dataDir)
|
||||
}
|
||||
|
||||
func TestP2PPreregistration(t *testing.T) {
|
||||
sampleNode := "- enr:-TESTNODE"
|
||||
testDataDir := "testDataDir"
|
||||
|
||||
file, err := ioutil.TempFile(t.TempDir(), "bootstrapFile*.yaml")
|
||||
require.NoError(t, err)
|
||||
err = ioutil.WriteFile(file.Name(), []byte(sampleNode), 0644)
|
||||
require.NoError(t, err, "Error in WriteFile call")
|
||||
params.SetupTestConfigCleanup(t)
|
||||
config := params.BeaconNetworkConfig()
|
||||
config.BootstrapNodes = []string{file.Name()}
|
||||
params.OverrideBeaconNetworkConfig(config)
|
||||
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, testDataDir, "")
|
||||
ctx := cli.NewContext(&app, set, nil)
|
||||
|
||||
bootstrapNodeAddrs, dataDir, err := P2PPreregistration(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(bootstrapNodeAddrs))
|
||||
assert.Equal(t, sampleNode[2:], bootstrapNodeAddrs[0])
|
||||
assert.Equal(t, testDataDir, dataDir)
|
||||
}
|
||||
|
||||
func TestBootStrapNodeFile(t *testing.T) {
|
||||
file, err := ioutil.TempFile(t.TempDir(), "bootstrapFile")
|
||||
require.NoError(t, err)
|
||||
|
||||
sampleNode0 := "- enr:-Ku4QMKVC_MowDsmEa20d5uGjrChI0h8_KsKXDmgVQbIbngZV0i" +
|
||||
"dV6_RL7fEtZGo-kTNZ5o7_EJI_vCPJ6scrhwX0Z4Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD" +
|
||||
"1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQJxCnE6v_x2ekgY_uo" +
|
||||
"E1rtwzvGy40mq9eD66XfHPBWgIIN1ZHCCD6A"
|
||||
sampleNode1 := "- enr:-TESTNODE2"
|
||||
sampleNode2 := "- enr:-TESTNODE3"
|
||||
err = ioutil.WriteFile(file.Name(), []byte(sampleNode0+"\n"+sampleNode1+"\n"+sampleNode2), 0644)
|
||||
require.NoError(t, err, "Error in WriteFile call")
|
||||
nodeList, err := readbootNodes(file.Name())
|
||||
require.NoError(t, err, "Error in readbootNodes call")
|
||||
assert.Equal(t, sampleNode0[2:], nodeList[0], "Unexpected nodes")
|
||||
assert.Equal(t, sampleNode1[2:], nodeList[1], "Unexpected nodes")
|
||||
assert.Equal(t, sampleNode2[2:], nodeList[2], "Unexpected nodes")
|
||||
}
|
||||
Reference in New Issue
Block a user