@@ -273,7 +273,7 @@ func NewDefaultNetwork(
273
273
redirectStdout bool ,
274
274
redirectStderr bool ,
275
275
) (network.Network , error ) {
276
- config , err := NewDefaultConfig (binaryPath )
276
+ config , err := NewDefaultConfig (binaryPath , constants . DefaultNetworkID )
277
277
if err != nil {
278
278
return nil , err
279
279
}
@@ -324,7 +324,9 @@ func loadDefaultNetworkFiles() (map[string]interface{}, []byte, []*utils.NodeKey
324
324
}
325
325
326
326
// 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
328
330
flags , cChainConfig , nodeKeys , err := loadDefaultNetworkFiles ()
329
331
if err != nil {
330
332
return network.Config {}, err
@@ -344,42 +346,54 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
344
346
port := constants .FirstAPIPort
345
347
for _ , keys := range nodeKeys {
346
348
encodedKeys := utils .EncodeNodeKeys (keys )
347
- nodeConfigs = append ( nodeConfigs , node.Config {
349
+ nodeConfig := node.Config {
348
350
StakingKey : encodedKeys .StakingKey ,
349
351
StakingCert : encodedKeys .StakingCert ,
350
352
StakingSigningKey : encodedKeys .BlsKey ,
351
353
Flags : map [string ]interface {}{
352
354
config .HTTPPortKey : port ,
353
355
config .StakingPortKey : port + 1 ,
354
356
},
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 )
357
364
port += 2
358
365
}
359
- if int (numNodes ) == 1 {
366
+ if int (numNodes ) == 1 && ! isPublic {
360
367
flags [config .SybilProtectionEnabledKey ] = false
361
368
}
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
365
371
}
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 {},
375
378
UpgradeConfigFiles : map [string ]string {},
376
379
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
378
392
}
379
393
380
394
// 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 )
383
397
}
384
398
385
399
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
388
402
}
389
403
ln .log .Info ("creating network" , zap .Int ("node-num" , len (networkConfig .NodeConfigs )))
390
404
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 )
402
409
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
404
420
}
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" )
410
421
}
411
422
412
423
// save node defaults
@@ -1101,15 +1112,18 @@ func (ln *localNetwork) buildArgs(
1101
1112
1102
1113
// Flags for AvalancheGo
1103
1114
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 ()
1113
1127
}
1114
1128
1115
1129
insideContainer , err := utils .IsInsideDockerContainer ()
@@ -1124,7 +1138,7 @@ func (ln *localNetwork) buildArgs(
1124
1138
1125
1139
// Write staking key/cert etc. to disk so the new node can use them,
1126
1140
// 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 )
1128
1142
if err != nil {
1129
1143
return buildArgsReturn {}, err
1130
1144
}
0 commit comments