Skip to content

Commit 0fe99e2

Browse files
committed
set node config to target fuji if network id is 5
1 parent 194a7b5 commit 0fe99e2

File tree

5 files changed

+86
-56
lines changed

5 files changed

+86
-56
lines changed

cmd/control/control.go

+16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ var (
107107
networkID uint32
108108
force bool
109109
inPlace bool
110+
fuji bool
110111
)
111112

112113
func setLogs() error {
@@ -191,6 +192,12 @@ func newStartCommand() *cobra.Command {
191192
constants.DefaultNumNodes,
192193
"number of nodes of the network",
193194
)
195+
cmd.PersistentFlags().Uint32Var(
196+
&numNodes,
197+
"num-nodes",
198+
constants.DefaultNumNodes,
199+
"number of nodes of the network",
200+
)
194201
cmd.PersistentFlags().StringVar(
195202
&pluginDir,
196203
"plugin-dir",
@@ -257,6 +264,12 @@ func newStartCommand() *cobra.Command {
257264
false,
258265
"true to assign dynamic ports",
259266
)
267+
cmd.PersistentFlags().BoolVar(
268+
&fuji,
269+
"fuji",
270+
false,
271+
"true to set all nodes to join fuji network",
272+
)
260273
return cmd
261274
}
262275

@@ -267,6 +280,9 @@ func startFunc(*cobra.Command, []string) error {
267280
}
268281
defer cli.Close()
269282

283+
if fuji {
284+
networkID = 5
285+
}
270286
opts := []client.OpOption{
271287
client.WithNumNodes(numNodes),
272288
client.WithPluginDir(pluginDir),

local/helpers.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/ava-labs/avalanche-network-runner/network/node"
1414
"github.com/ava-labs/avalanchego/config"
15-
"github.com/ava-labs/avalanchego/utils/constants"
1615
"github.com/ava-labs/avalanchego/utils/logging"
1716
)
1817

@@ -37,7 +36,7 @@ func getFreePort() (uint16, error) {
3736

3837
// writeFiles writes the files a node needs on startup.
3938
// It returns flags used to point to those files.
40-
func writeFiles(networkID uint32, genesis []byte, nodeRootDir string, nodeConfig *node.Config) (map[string]string, error) {
39+
func writeFiles(genesis []byte, nodeRootDir string, nodeConfig *node.Config) (map[string]string, error) {
4140
type file struct {
4241
pathKey string
4342
flagValue string
@@ -68,7 +67,7 @@ func writeFiles(networkID uint32, genesis []byte, nodeRootDir string, nodeConfig
6867
contents: decodedStakingSigningKey,
6968
},
7069
}
71-
if networkID != constants.LocalID {
70+
if len(genesis) > 0 {
7271
files = append(files, file{
7372
flagValue: filepath.Join(nodeRootDir, configsPath, genesisFileName),
7473
path: filepath.Join(nodeRootDir, configsPath, genesisFileName),
@@ -80,6 +79,7 @@ func writeFiles(networkID uint32, genesis []byte, nodeRootDir string, nodeConfig
8079
for _, f := range files {
8180
if f.flagValue != "" {
8281
flags[f.pathKey] = f.flagValue
82+
8383
}
8484
if err := createFileAndWrite(f.path, f.contents); err != nil {
8585
return nil, fmt.Errorf("couldn't write file at %q: %w", f.path, err)

local/network.go

+62-48
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func NewDefaultNetwork(
273273
redirectStdout bool,
274274
redirectStderr bool,
275275
) (network.Network, error) {
276-
config, err := NewDefaultConfig(binaryPath)
276+
config, err := NewDefaultConfig(binaryPath, constants.DefaultNetworkID)
277277
if err != nil {
278278
return nil, err
279279
}
@@ -324,7 +324,9 @@ func loadDefaultNetworkFiles() (map[string]interface{}, []byte, []*utils.NodeKey
324324
}
325325

326326
// NewDefaultConfigNNodes creates a new default network config, with an arbitrary number of nodes
327-
func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config, error) {
327+
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
328330
flags, cChainConfig, nodeKeys, err := loadDefaultNetworkFiles()
329331
if err != nil {
330332
return network.Config{}, err
@@ -344,42 +346,54 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
344346
port := constants.FirstAPIPort
345347
for _, keys := range nodeKeys {
346348
encodedKeys := utils.EncodeNodeKeys(keys)
347-
nodeConfigs = append(nodeConfigs, node.Config{
349+
nodeConfig := node.Config{
348350
StakingKey: encodedKeys.StakingKey,
349351
StakingCert: encodedKeys.StakingCert,
350352
StakingSigningKey: encodedKeys.BlsKey,
351353
Flags: map[string]interface{}{
352354
config.HTTPPortKey: port,
353355
config.StakingPortKey: port + 1,
354356
},
355-
IsBeacon: true,
356-
})
357+
}
358+
if !isPublic {
359+
nodeConfig.IsBeacon = true
360+
} else {
361+
nodeConfig.Flags[config.PartialSyncPrimaryNetworkKey] = true
362+
}
363+
nodeConfigs = append(nodeConfigs, nodeConfig)
357364
port += 2
358365
}
359-
if int(numNodes) == 1 {
366+
if int(numNodes) == 1 && !isPublic {
360367
flags[config.SybilProtectionEnabledKey] = false
361368
}
362-
genesis, err := utils.GenerateGenesis(constants.DefaultNetworkID, nodeKeys)
363-
if err != nil {
364-
return network.Config{}, err
369+
if networkID == 0 {
370+
networkID = constants.DefaultNetworkID
365371
}
366-
return network.Config{
367-
NetworkID: constants.DefaultNetworkID,
368-
Flags: flags,
369-
Genesis: string(genesis),
370-
NodeConfigs: nodeConfigs,
371-
BinaryPath: binaryPath,
372-
ChainConfigFiles: map[string]string{
373-
"C": string(cChainConfig),
374-
},
372+
cfg := network.Config{
373+
NetworkID: networkID,
374+
Flags: flags,
375+
NodeConfigs: nodeConfigs,
376+
BinaryPath: binaryPath,
377+
ChainConfigFiles: map[string]string{},
375378
UpgradeConfigFiles: map[string]string{},
376379
SubnetConfigFiles: map[string]string{},
377-
}, nil
380+
}
381+
if isCustom {
382+
genesis, err := utils.GenerateGenesis(networkID, nodeKeys)
383+
if err != nil {
384+
return network.Config{}, err
385+
}
386+
cfg.Genesis = string(genesis)
387+
cfg.ChainConfigFiles = map[string]string{
388+
"C": string(cChainConfig),
389+
}
390+
}
391+
return cfg, nil
378392
}
379393

380394
// NewDefaultConfig creates a new default network config
381-
func NewDefaultConfig(binaryPath string) (network.Config, error) {
382-
return NewDefaultConfigNNodes(binaryPath, constants.DefaultNumNodes)
395+
func NewDefaultConfig(binaryPath string, networkID uint32) (network.Config, error) {
396+
return NewDefaultConfigNNodes(binaryPath, constants.DefaultNumNodes, networkID)
383397
}
384398

385399
func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Config) error {
@@ -388,25 +402,22 @@ func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Co
388402
}
389403
ln.log.Info("creating network", zap.Int("node-num", len(networkConfig.NodeConfigs)))
390404

391-
ln.genesis = []byte(networkConfig.Genesis)
392-
393-
// Set network ID
394-
var err error
395-
ln.networkID, err = utils.NetworkIDFromGenesis(ln.genesis)
396-
if err != nil {
397-
return err
398-
}
399-
if networkConfig.NetworkID != 0 && networkConfig.NetworkID != ln.networkID {
400-
ln.networkID = networkConfig.NetworkID
401-
genesis, err := utils.SetGenesisNetworkID(ln.genesis, ln.networkID)
405+
ln.networkID = networkConfig.NetworkID
406+
if len(networkConfig.Genesis) != 0 {
407+
ln.genesis = []byte(networkConfig.Genesis)
408+
genesisNetworkID, err := utils.NetworkIDFromGenesis(ln.genesis)
402409
if err != nil {
403-
return fmt.Errorf("couldn't set network ID to genesis: %w", err)
410+
return err
411+
}
412+
if ln.networkID == 0 {
413+
ln.networkID = genesisNetworkID
414+
} else if ln.networkID != genesisNetworkID {
415+
genesis, err := utils.SetGenesisNetworkID(ln.genesis, ln.networkID)
416+
if err != nil {
417+
return fmt.Errorf("couldn't set network ID to genesis: %w", err)
418+
}
419+
ln.genesis = genesis
404420
}
405-
ln.genesis = genesis
406-
}
407-
switch ln.networkID {
408-
case avagoconstants.TestnetID, avagoconstants.MainnetID:
409-
return errors.New("network ID can't be mainnet or testnet")
410421
}
411422

412423
// save node defaults
@@ -1101,15 +1112,18 @@ func (ln *localNetwork) buildArgs(
11011112

11021113
// Flags for AvalancheGo
11031114
flags := map[string]string{
1104-
config.NetworkNameKey: fmt.Sprintf("%d", ln.networkID),
1105-
config.DataDirKey: dataDir,
1106-
config.DBPathKey: dbDir,
1107-
config.LogsDirKey: logsDir,
1108-
config.PublicIPKey: publicIP,
1109-
config.HTTPPortKey: fmt.Sprintf("%d", apiPort),
1110-
config.StakingPortKey: fmt.Sprintf("%d", p2pPort),
1111-
config.BootstrapIPsKey: ln.bootstraps.IPsArg(),
1112-
config.BootstrapIDsKey: ln.bootstraps.IDsArg(),
1115+
config.NetworkNameKey: fmt.Sprintf("%d", ln.networkID),
1116+
config.DataDirKey: dataDir,
1117+
config.DBPathKey: dbDir,
1118+
config.LogsDirKey: logsDir,
1119+
config.PublicIPKey: publicIP,
1120+
config.HTTPPortKey: fmt.Sprintf("%d", apiPort),
1121+
config.StakingPortKey: fmt.Sprintf("%d", p2pPort),
1122+
}
1123+
isPublic := ln.networkID == avagoconstants.FujiID || ln.networkID == avagoconstants.MainnetID
1124+
if !isPublic {
1125+
flags[config.BootstrapIPsKey] = ln.bootstraps.IPsArg()
1126+
flags[config.BootstrapIDsKey] = ln.bootstraps.IDsArg()
11131127
}
11141128

11151129
insideContainer, err := utils.IsInsideDockerContainer()
@@ -1124,7 +1138,7 @@ func (ln *localNetwork) buildArgs(
11241138

11251139
// Write staking key/cert etc. to disk so the new node can use them,
11261140
// and get flag that point the node to those files
1127-
fileFlags, err := writeFiles(ln.networkID, ln.genesis, dataDir, nodeConfig)
1141+
fileFlags, err := writeFiles(ln.genesis, dataDir, nodeConfig)
11281142
if err != nil {
11291143
return buildArgsReturn{}, err
11301144
}

network/config.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ type Config struct {
5858

5959
// Validate returns an error if this config is invalid
6060
func (c *Config) Validate() error {
61-
if len(c.Genesis) == 0 {
61+
isPublic := c.NetworkID == constants.FujiID || c.NetworkID == constants.MainnetID
62+
isCustom := !isPublic && c.NetworkID != constants.LocalID
63+
if isCustom && len(c.Genesis) == 0 {
6264
return errors.New("no genesis given")
6365
}
6466

@@ -77,7 +79,7 @@ func (c *Config) Validate() error {
7779
someNodeIsBeacon = true
7880
}
7981
}
80-
if len(c.NodeConfigs) > 0 && !someNodeIsBeacon {
82+
if !isPublic && len(c.NodeConfigs) > 0 && !someNodeIsBeacon {
8183
return errors.New("beacon nodes not given")
8284
}
8385
return nil

server/network.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func newLocalNetwork(opts localNetworkOptions) (*localNetwork, error) {
146146
// TODO document.
147147
// Assumes [lc.lock] is held.
148148
func (lc *localNetwork) createConfig() error {
149-
cfg, err := local.NewDefaultConfigNNodes(lc.options.execPath, lc.options.numNodes)
149+
cfg, err := local.NewDefaultConfigNNodes(lc.options.execPath, lc.options.numNodes, lc.options.networkID)
150150
if err != nil {
151151
return err
152152
}
@@ -168,8 +168,6 @@ func (lc *localNetwork) createConfig() error {
168168
cfg.Flags[config.PluginDirKey] = lc.pluginDir
169169
}
170170

171-
cfg.NetworkID = lc.options.networkID
172-
173171
for k, v := range lc.options.chainConfigs {
174172
ov, ok := cfg.ChainConfigFiles[k]
175173
if ok {

0 commit comments

Comments
 (0)