@@ -274,164 +274,108 @@ func NewDefaultNetwork(
274
274
// * NodeID-NFBbbJ4qCmNaCzeW7sxErhvWqvEQMnYcN
275
275
// * NodeID-GWPcbFJZFfZreETSoWjPimr846mXEKCtu
276
276
// * NodeID-P7oB2McjBGgW2NXXWVYjV8JEDFoW9xDE5
277
- func loadDefaultNetworkConfig () (network. Config , error ) {
277
+ func loadDefaultNetworkFiles () (map [ string ] interface {}, [] byte , [] * utils. NodeKeys , error ) {
278
278
configsDir , err := fs .Sub (embeddedDefaultNetworkConfigDir , "default" )
279
279
if err != nil {
280
- return network. Config {} , err
280
+ return nil , nil , nil , err
281
281
}
282
282
// network flags
283
283
flagsBytes , err := fs .ReadFile (configsDir , "flags.json" )
284
284
if err != nil {
285
- return network. Config {} , err
285
+ return nil , nil , nil , err
286
286
}
287
287
flags := map [string ]interface {}{}
288
288
if err = json .Unmarshal (flagsBytes , & flags ); err != nil {
289
- return network. Config {} , err
289
+ return nil , nil , nil , err
290
290
}
291
291
// c-chain config
292
292
cChainConfig , err := fs .ReadFile (configsDir , "cchain_config.json" )
293
293
if err != nil {
294
- return network.Config {}, err
295
- }
296
- defaultNetworkConfig := network.Config {
297
- NetworkID : constants .DefaultNetworkID ,
298
- NodeConfigs : make ([]node.Config , constants .DefaultNumNodes ),
299
- Flags : flags ,
300
- ChainConfigFiles : map [string ]string {
301
- "C" : string (cChainConfig ),
302
- },
303
- UpgradeConfigFiles : map [string ]string {},
304
- SubnetConfigFiles : map [string ]string {},
294
+ return nil , nil , nil , err
305
295
}
296
+ nodeKeys := []* utils.NodeKeys {}
306
297
for i := 0 ; i < constants .DefaultNumNodes ; i ++ {
307
298
nodeDir := fmt .Sprintf ("node%d" , i + 1 )
308
299
stakingKey , err := fs .ReadFile (configsDir , filepath .Join (nodeDir , "staking.key" ))
309
300
if err != nil {
310
- return network. Config {} , err
301
+ return nil , nil , nil , err
311
302
}
312
303
stakingCert , err := fs .ReadFile (configsDir , filepath .Join (nodeDir , "staking.crt" ))
313
304
if err != nil {
314
- return network. Config {} , err
305
+ return nil , nil , nil , err
315
306
}
316
307
blsKey , err := fs .ReadFile (configsDir , filepath .Join (nodeDir , "signer.key" ))
317
308
if err != nil {
318
- return network.Config {}, err
319
- }
320
- defaultNetworkConfig .NodeConfigs [i ] = node.Config {
321
- StakingKey : string (stakingKey ),
322
- StakingCert : string (stakingCert ),
323
- StakingSigningKey : base64 .StdEncoding .EncodeToString (blsKey ),
324
- IsBeacon : true ,
309
+ return nil , nil , nil , err
325
310
}
311
+ nodeKeys = append (nodeKeys , & utils.NodeKeys {
312
+ StakingKey : stakingKey ,
313
+ StakingCert : stakingCert ,
314
+ BlsKey : blsKey ,
315
+ })
326
316
}
327
- return defaultNetworkConfig , nil
317
+ return flags , cChainConfig , nodeKeys , nil
328
318
}
329
319
330
320
// NewDefaultConfigNNodes creates a new default network config, with an arbitrary number of nodes
331
321
func NewDefaultConfigNNodes (binaryPath string , numNodes uint32 ) (network.Config , error ) {
332
- netConfig , err := loadDefaultNetworkConfig ()
322
+ flags , cChainConfig , nodeKeys , err := loadDefaultNetworkFiles ()
333
323
if err != nil {
334
- return netConfig , err
324
+ return network. Config {} , err
335
325
}
336
- netConfig .BinaryPath = binaryPath
337
326
if int (numNodes ) > constants .DefaultNumNodes {
338
327
toAdd := int (numNodes ) - constants .DefaultNumNodes
339
- keys , err := generateNNodeKeys (toAdd )
328
+ newNodeKeys , err := utils . GenerateNNodeKeys (toAdd )
340
329
if err != nil {
341
- return netConfig , err
342
- }
343
- for i := 0 ; i < toAdd ; i ++ {
344
- netConfig .NodeConfigs = append (netConfig .NodeConfigs , node.Config {
345
- StakingKey : string (keys [i ].StakingKey ),
346
- StakingCert : string (keys [i ].StakingCert ),
347
- StakingSigningKey : base64 .StdEncoding .EncodeToString (keys [i ].BlsKey ),
348
- IsBeacon : true ,
349
- })
330
+ return network.Config {}, err
350
331
}
332
+ nodeKeys = append (nodeKeys , newNodeKeys ... )
351
333
}
352
334
if int (numNodes ) < constants .DefaultNumNodes {
353
- netConfig . NodeConfigs = netConfig . NodeConfigs [:numNodes ]
335
+ nodeKeys = nodeKeys [:numNodes ]
354
336
}
337
+ nodeConfigs := []node.Config {}
355
338
port := constants .FirstAPIPort
356
- for i := 0 ; i < int (numNodes ); i ++ {
357
- netConfig .NodeConfigs [i ].Flags = map [string ]interface {}{
358
- config .HTTPPortKey : port ,
359
- config .StakingPortKey : port + 1 ,
360
- }
339
+ for _ , keys := range nodeKeys {
340
+ encodedKeys := utils .EncodeNodeKeys (keys )
341
+ nodeConfigs = append (nodeConfigs , node.Config {
342
+ StakingKey : encodedKeys .StakingKey ,
343
+ StakingCert : encodedKeys .StakingCert ,
344
+ StakingSigningKey : encodedKeys .BlsKey ,
345
+ Flags : map [string ]interface {}{
346
+ config .HTTPPortKey : port ,
347
+ config .StakingPortKey : port + 1 ,
348
+ },
349
+ IsBeacon : true ,
350
+ })
361
351
port += 2
362
352
}
363
353
if int (numNodes ) == 1 {
364
- netConfig . Flags [config .SybilProtectionEnabledKey ] = false
354
+ flags [config .SybilProtectionEnabledKey ] = false
365
355
}
366
- genesis , err := utils .GenerateGenesis (netConfig )
356
+ genesis , err := utils .GenerateGenesis (constants . DefaultNetworkID , nodeKeys )
367
357
if err != nil {
368
- return netConfig , err
358
+ return network. Config {} , err
369
359
}
370
- netConfig .Genesis = string (genesis )
371
- return netConfig , nil
360
+ return network.Config {
361
+ NetworkID : constants .DefaultNetworkID ,
362
+ Flags : flags ,
363
+ Genesis : string (genesis ),
364
+ NodeConfigs : nodeConfigs ,
365
+ BinaryPath : binaryPath ,
366
+ ChainConfigFiles : map [string ]string {
367
+ "C" : string (cChainConfig ),
368
+ },
369
+ UpgradeConfigFiles : map [string ]string {},
370
+ SubnetConfigFiles : map [string ]string {},
371
+ }, nil
372
372
}
373
373
374
374
// NewDefaultConfig creates a new default network config
375
375
func NewDefaultConfig (binaryPath string ) (network.Config , error ) {
376
376
return NewDefaultConfigNNodes (binaryPath , constants .DefaultNumNodes )
377
377
}
378
378
379
- type EncodedNodeKeys struct {
380
- StakingKey string
381
- StakingCert string
382
- BlsKey string
383
- }
384
-
385
- type NodeKeys struct {
386
- StakingKey []byte
387
- StakingCert []byte
388
- BlsKey []byte
389
- }
390
-
391
- func encodeNodeKeys (key * NodeKeys ) * EncodedNodeKeys {
392
- return & EncodedNodeKeys {
393
- StakingKey : string (key .StakingKey ),
394
- StakingCert : string (key .StakingCert ),
395
- BlsKey : base64 .StdEncoding .EncodeToString (key .BlsKey ),
396
- }
397
- }
398
-
399
- func generateNodeKeys () (* NodeKeys , error ) {
400
- stakingCert , stakingKey , err := staking .NewCertAndKeyBytes ()
401
- if err != nil {
402
- return nil , fmt .Errorf ("couldn't generate staking Cert/Key: %w" , err )
403
- }
404
- key , err := bls .NewSecretKey ()
405
- if err != nil {
406
- return nil , fmt .Errorf ("couldn't generate new signing key: %w" , err )
407
- }
408
- return & NodeKeys {
409
- StakingKey : stakingKey ,
410
- StakingCert : stakingCert ,
411
- BlsKey : bls .SecretKeyToBytes (key ),
412
- }, nil
413
- }
414
-
415
- func generateNNodeKeys (num int ) ([]* NodeKeys , error ) {
416
- nodesKeys := []* NodeKeys {}
417
- lock := sync.Mutex {}
418
- eg := errgroup.Group {}
419
- for i := 0 ; i < num ; i ++ {
420
- eg .Go (func () error {
421
- keys , err := generateNodeKeys ()
422
- if err != nil {
423
- return err
424
- }
425
- lock .Lock ()
426
- nodesKeys = append (nodesKeys , keys )
427
- lock .Unlock ()
428
- return nil
429
- })
430
- }
431
- err := eg .Wait ()
432
- return nodesKeys , err
433
- }
434
-
435
379
func (ln * localNetwork ) loadConfig (ctx context.Context , networkConfig network.Config ) error {
436
380
if err := networkConfig .Validate (); err != nil {
437
381
return fmt .Errorf ("config failed validation: %w" , err )
0 commit comments