Skip to content

Commit da99346

Browse files
committed
refactor
1 parent 0fe99e2 commit da99346

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

local/network.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ava-labs/avalanchego/network/peer"
2929
"github.com/ava-labs/avalanchego/staking"
3030
"github.com/ava-labs/avalanchego/utils/beacon"
31-
avagoconstants "github.com/ava-labs/avalanchego/utils/constants"
3231
"github.com/ava-labs/avalanchego/utils/crypto/bls"
3332
"github.com/ava-labs/avalanchego/utils/logging"
3433
"github.com/ava-labs/avalanchego/utils/set"
@@ -325,8 +324,6 @@ func loadDefaultNetworkFiles() (map[string]interface{}, []byte, []*utils.NodeKey
325324

326325
// NewDefaultConfigNNodes creates a new default network config, with an arbitrary number of nodes
327326
func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32) (network.Config, error) {
328-
isPublic := networkID == avagoconstants.FujiID || networkID == avagoconstants.MainnetID
329-
isCustom := !isPublic && networkID != avagoconstants.LocalID
330327
flags, cChainConfig, nodeKeys, err := loadDefaultNetworkFiles()
331328
if err != nil {
332329
return network.Config{}, err
@@ -355,15 +352,15 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32
355352
config.StakingPortKey: port + 1,
356353
},
357354
}
358-
if !isPublic {
355+
if !utils.IsPublicNetwork(networkID) {
359356
nodeConfig.IsBeacon = true
360357
} else {
361358
nodeConfig.Flags[config.PartialSyncPrimaryNetworkKey] = true
362359
}
363360
nodeConfigs = append(nodeConfigs, nodeConfig)
364361
port += 2
365362
}
366-
if int(numNodes) == 1 && !isPublic {
363+
if int(numNodes) == 1 && !utils.IsPublicNetwork(networkID) {
367364
flags[config.SybilProtectionEnabledKey] = false
368365
}
369366
if networkID == 0 {
@@ -378,7 +375,7 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32, networkID uint32
378375
UpgradeConfigFiles: map[string]string{},
379376
SubnetConfigFiles: map[string]string{},
380377
}
381-
if isCustom {
378+
if utils.IsCustomNetwork(networkID) {
382379
genesis, err := utils.GenerateGenesis(networkID, nodeKeys)
383380
if err != nil {
384381
return network.Config{}, err
@@ -1120,8 +1117,7 @@ func (ln *localNetwork) buildArgs(
11201117
config.HTTPPortKey: fmt.Sprintf("%d", apiPort),
11211118
config.StakingPortKey: fmt.Sprintf("%d", p2pPort),
11221119
}
1123-
isPublic := ln.networkID == avagoconstants.FujiID || ln.networkID == avagoconstants.MainnetID
1124-
if !isPublic {
1120+
if !utils.IsPublicNetwork(ln.networkID) {
11251121
flags[config.BootstrapIPsKey] = ln.bootstraps.IPsArg()
11261122
flags[config.BootstrapIDsKey] = ln.bootstraps.IDsArg()
11271123
}

network/config.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/ava-labs/avalanche-network-runner/network/node"
12+
"github.com/ava-labs/avalanche-network-runner/utils"
1213
"github.com/ava-labs/avalanchego/genesis"
1314
"github.com/ava-labs/avalanchego/ids"
1415
"github.com/ava-labs/avalanchego/utils/constants"
@@ -58,9 +59,7 @@ type Config struct {
5859

5960
// Validate returns an error if this config is invalid
6061
func (c *Config) Validate() error {
61-
isPublic := c.NetworkID == constants.FujiID || c.NetworkID == constants.MainnetID
62-
isCustom := !isPublic && c.NetworkID != constants.LocalID
63-
if isCustom && len(c.Genesis) == 0 {
62+
if utils.IsCustomNetwork(c.NetworkID) && len(c.Genesis) == 0 {
6463
return errors.New("no genesis given")
6564
}
6665

@@ -79,7 +78,7 @@ func (c *Config) Validate() error {
7978
someNodeIsBeacon = true
8079
}
8180
}
82-
if !isPublic && len(c.NodeConfigs) > 0 && !someNodeIsBeacon {
81+
if !utils.IsPublicNetwork(c.NetworkID) && len(c.NodeConfigs) > 0 && !someNodeIsBeacon {
8382
return errors.New("beacon nodes not given")
8483
}
8584
return nil

utils/utils.go

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/ava-labs/avalanche-network-runner/ux"
1313
"github.com/ava-labs/avalanchego/ids"
1414
"github.com/ava-labs/avalanchego/staking"
15+
"github.com/ava-labs/avalanchego/utils/constants"
1516
"github.com/ava-labs/avalanchego/utils/logging"
1617
)
1718

@@ -163,3 +164,11 @@ func PathExists(path string) (bool, error) {
163164
}
164165
return true, nil
165166
}
167+
168+
func IsPublicNetwork(networkID uint32) bool {
169+
return networkID == constants.FujiID || networkID == constants.MainnetID
170+
}
171+
172+
func IsCustomNetwork(networkID uint32) bool {
173+
return !IsPublicNetwork(networkID) && networkID != constants.LocalID
174+
}

0 commit comments

Comments
 (0)