prysm-pulse/beacon-chain/node/registration/p2p.go
Manu NALEPA 65f71b3a48
P2P: Simplify code (#13719)
* `subscribeStaticWithSubnets`: Fix docstring.

* `buildOptions`: Avoid `options` mutations.

* `dv5Cfg`: Avoid mutation.

* `RefreshENR`: Use default for all but Phase0.

* `udp4`, `udp6`: Create enum.

* `p2p.Config`: `BootstrapNodeAddr`==> `BootstrapNodeAddrs`.

* `p2p.Config`: `Discv5BootStrapAddr` ==> `Discv5BootStrapAddrs`.

* `TestScorers_BadResponses_Score`: Improve.

* `BeaconNode`: Avoid mutation.

* `TestStore_TrustedPeers`: Remove blankline.

* Remove blank identifiers.

* `privKey`: Keep the majority of code with low indentation.

* `P2PPreregistration`: Return error instead of fatal log.

* `parseBootStrapAddrs` => `ParseBootStrapAddrs` (export)

* `p2p.Config`: Remove `BootstrapNodeAddrs`.

* `NewService`: Avoid mutation when possible.

* `Service`: Remove blank identifier.

* `buildOptions`: Avoid `log.Fatalf` (make deepsource happy).

* `registerGRPCGateway`: Use `net.JoinHostPort` (make deepsource happy).

* `registerBuilderService`: Make deepsource happy.

* `scorers`: Add `NoLock` suffix (make deepsource happy).

* `scorerr`: Add some `NoLock`suffixes (making deepsource happy).

* `discovery_test.go`. Remove init.

Rationale:
`rand.Seed` is deprecated: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator.

This makes deepsource happy as well.

* `createListener`: Reduce cyclomatic complexity (make deepsource happy).

* `startDB`: Reduce cyclomatic complexity (make deepsource happy).

* `main`: Log a FATAL on error.

This way, the error message is very readable.
Before this commit, the error message is the less readable
message in the logs.

* `New`: Reduce cyclomatic complexity (make deepsource happy).

* `main`: Avoid `App` mutation, and make deepsource happy.

* Update beacon-chain/node/node.go

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>

* `bootnodes` ==> `BootNodes` (Fix PR comment).

* Remove duplicate `configureFastSSZHashingAlgorithm` since already done in `configureBeacon`. (Fix PR comment)

* Add `TestCreateLocalNode`. (PR comment fix.)

* `startModules` ==> `startBaseServices (Fix PR comment).

* `buildOptions` return errors consistently.

* `New`: Change ordering.

---------

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>
2024-03-15 11:08:19 +00:00

61 lines
1.6 KiB
Go

package registration
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/cmd"
"github.com/prysmaticlabs/prysm/v5/config/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 == "" {
err = errors.Errorf(
"Could not determine your system's HOME path, please specify a --%s you wish to use for your chain data",
cmd.DataDirFlag.Name,
)
}
}
return
}
func readbootNodes(fileName string) ([]string, error) {
fileContent, err := os.ReadFile(fileName) // #nosec G304
if err != nil {
return nil, err
}
listNodes := make([]string, 0)
err = yaml.UnmarshalStrict(fileContent, &listNodes)
if err != nil {
if _, ok := err.(*yaml.TypeError); !ok {
return nil, err
} else {
log.WithError(err).Error("There were some issues parsing the bootnodes from a yaml file.")
}
}
return listNodes, nil
}