From a10c62b0cc3c20c00119bbc36100372d2d95707b Mon Sep 17 00:00:00 2001 From: Felipe Madero Date: Tue, 7 Nov 2023 10:28:16 -0300 Subject: [PATCH] lint --- client/client.go | 4 ++-- cmd/control/control.go | 9 +++------ local/network.go | 14 ++++++++++---- network/config.go | 3 +++ utils/constants/constants.go | 5 +---- utils/utils.go | 7 ++++--- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/client/client.go b/client/client.go index b0b69bcc..c14fe5ed 100644 --- a/client/client.go +++ b/client/client.go @@ -472,9 +472,9 @@ func WithGlobalNodeConfig(nodeConfig string) OpOption { } } -func WithNetworkID(networkId uint32) OpOption { +func WithNetworkID(networkID uint32) OpOption { return func(op *Op) { - op.networkID = networkId + op.networkID = networkID } } diff --git a/cmd/control/control.go b/cmd/control/control.go index c3d06fed..15f3b473 100644 --- a/cmd/control/control.go +++ b/cmd/control/control.go @@ -104,7 +104,7 @@ var ( subnetConfigs string reassignPortsIfUsed bool dynamicPorts bool - networkId uint32 + networkID uint32 ) func setLogs() error { @@ -178,7 +178,7 @@ func newStartCommand() *cobra.Command { "avalanchego binary path", ) cmd.PersistentFlags().Uint32Var( - &networkId, + &networkID, "network-id", 0, "network id to assign to the network", @@ -272,10 +272,7 @@ func startFunc(*cobra.Command, []string) error { client.WithRootDataDir(rootDataDir), client.WithReassignPortsIfUsed(reassignPortsIfUsed), client.WithDynamicPorts(dynamicPorts), - } - - if networkId != 0 { - opts = append(opts, client.WithNetworkID(networkId)) + client.WithNetworkID(networkID), } if globalNodeConfig != "" { diff --git a/local/network.go b/local/network.go index 802e1e63..a9905b3a 100644 --- a/local/network.go +++ b/local/network.go @@ -28,6 +28,7 @@ import ( "github.com/ava-labs/avalanchego/network/peer" "github.com/ava-labs/avalanchego/staking" "github.com/ava-labs/avalanchego/utils/beacon" + avagoconstants "github.com/ava-labs/avalanchego/utils/constants" "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/ips" "github.com/ava-labs/avalanchego/utils/logging" @@ -445,13 +446,18 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co ln.genesis = []byte(networkConfig.Genesis) // Set network ID - ln.networkID = constants.DefaultNetworkID + var err error + ln.networkID, err = utils.NetworkIDFromGenesis(ln.genesis) + if err != nil { + return err + } if networkConfig.NetworkID != 0 { - if networkConfig.NetworkID < 11 { - return fmt.Errorf("network IDs < 11 are reserved ones") - } ln.networkID = networkConfig.NetworkID } + switch ln.networkID { + case avagoconstants.TestnetID, avagoconstants.MainnetID: + return errors.New("network ID can't be mainnet or testnet") + } genesis, err := utils.SetGenesisNetworkID(ln.genesis, ln.networkID) if err != nil { return fmt.Errorf("couldn't set network ID to genesis: %w", err) diff --git a/network/config.go b/network/config.go index 3526ad98..5342c81e 100644 --- a/network/config.go +++ b/network/config.go @@ -89,6 +89,9 @@ func (c *Config) Validate() error { if err != nil { return fmt.Errorf("couldn't get network ID from genesis: %w", err) } + if c.NetworkID != 0 { + networkID = c.NetworkID + } var someNodeIsBeacon bool for i, nodeConfig := range c.NodeConfigs { diff --git a/utils/constants/constants.go b/utils/constants/constants.go index 6e7b55bb..32fd930a 100644 --- a/utils/constants/constants.go +++ b/utils/constants/constants.go @@ -12,9 +12,6 @@ const ( DefaultExecPathEnvVar = "AVALANCHEGO_EXEC_PATH" DefaultPluginDirEnvVar = "AVALANCHEGO_PLUGIN_PATH" LocalGenesisFile = "genesis.json" - DefaultNetworkID = uint32(1337) ) -var ( - LocalConfigDir = filepath.Join("local", "default") -) +var LocalConfigDir = filepath.Join("local", "default") diff --git a/utils/utils.go b/utils/utils.go index 473b1854..f527afc0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -21,9 +21,10 @@ const ( ) var ( - ErrEmptyExecPath = errors.New("avalanche exec is not defined") - ErrNotExists = errors.New("avalanche exec not exists") - ErrNotExistsPlugin = errors.New("plugin exec not exists") + ErrEmptyExecPath = errors.New("avalanche exec is not defined") + ErrNotExists = errors.New("avalanche exec not exists") + ErrNotExistsPlugin = errors.New("plugin exec not exists") + ErrorNoNetworkIDKey = fmt.Errorf("couldn't find key %q in genesis", genesisNetworkIDKey) ) func ToNodeID(stakingKey, stakingCert []byte) (ids.NodeID, error) {