-
Notifications
You must be signed in to change notification settings - Fork 91
/
configGenerator.ts
837 lines (749 loc) · 26.9 KB
/
configGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import fs from "fs";
import path, { resolve } from "path";
import {
decorators,
getRandomPort,
getSha256,
validateImageUrl,
} from "@zombienet/utils";
import {
ARGS_TO_REMOVE,
DEFAULT_ADDER_COLLATOR_BIN,
DEFAULT_BALANCE,
DEFAULT_CHAIN,
DEFAULT_CHAIN_SPEC_COMMAND,
DEFAULT_COLLATOR_IMAGE,
DEFAULT_COMMAND,
DEFAULT_CUMULUS_COLLATOR_BIN,
DEFAULT_GLOBAL_TIMEOUT,
DEFAULT_IMAGE,
DEFAULT_KEYSTORE_KEY_TYPES,
DEFAULT_MAX_NOMINATIONS,
DEFAULT_PORTS,
DEFAULT_PROMETHEUS_PREFIX,
DEFAULT_WASM_GENERATE_SUBCOMMAND,
DEV_ACCOUNTS,
GENESIS_STATE_FILENAME,
GENESIS_WASM_FILENAME,
UNDYING_COLLATOR_BIN,
ZOMBIE_WRAPPER,
} from "./constants";
import { generateKeyForNode } from "./keys";
import { CHAIN, decorate, whichChain } from "./chain-decorators";
import { ComputedNetwork, LaunchConfig, ParachainConfig } from "./configTypes";
import {
NodeConfig,
Override,
Parachain,
envVars,
Node,
ZombieRole,
} from "./sharedTypes";
const debug = require("debug")("zombie::config-manager");
// get the path of the zombie wrapper
export const zombieWrapperPath = resolve(__dirname, `../${ZOMBIE_WRAPPER}`);
const DEFAULT_ENV: envVars[] = [
{ name: "COLORBT_SHOW_HIDDEN", value: "1" },
{ name: "RUST_BACKTRACE", value: "FULL" },
];
const isIterable = (obj: any) => {
// checks for null and undefined
if (obj == null || typeof obj == "string") {
return false;
}
return typeof obj[Symbol.iterator] === "function";
};
const configurationFileChecks = (config: LaunchConfig): void => {
if ((config as any).hrmpChannels) {
throw new Error(
"'hrmpChannels' value the given configuration file is deprecated; Please use 'hrmp_channels' instead;",
);
}
validateImageUrl(config?.relaychain?.default_image || DEFAULT_IMAGE);
if (
config?.relaychain?.node_groups &&
isIterable(config?.relaychain?.node_groups)
)
for (const nodeGroup of config?.relaychain?.node_groups || []) {
validateImageUrl(
nodeGroup?.image || config?.relaychain.default_image || DEFAULT_IMAGE,
);
}
if (config?.parachains && isIterable(config?.parachains))
for (const parachain of config.parachains) {
if (parachain?.collator_groups && isIterable(parachain?.collator_groups))
for (const collatorGroup of parachain?.collator_groups || []) {
validateImageUrl(
collatorGroup?.image ||
config?.relaychain?.default_image ||
DEFAULT_COLLATOR_IMAGE,
);
}
if (parachain?.collators && isIterable(parachain?.collators))
for (const collatorConfig of parachain?.collators || []) {
validateImageUrl(collatorConfig?.image || DEFAULT_COLLATOR_IMAGE);
}
}
};
export async function generateNetworkSpec(
config: LaunchConfig,
): Promise<ComputedNetwork> {
let globalOverrides: Override[] = [];
if (config.relaychain.default_overrides) {
globalOverrides = await Promise.all(
config.relaychain.default_overrides.map(async (override: Override) => {
const valid_local_path = await getLocalOverridePath(
config.configBasePath,
override.local_path,
);
return {
local_path: valid_local_path,
remote_name: override.remote_name,
};
}),
);
}
const networkSpec: any = {
configBasePath: config.configBasePath,
relaychain: {
defaultImage: config.relaychain.default_image || DEFAULT_IMAGE,
defaultCommand: config.relaychain.default_command || DEFAULT_COMMAND,
defaultArgs: config.relaychain.default_args || [],
defaultKeystoreKeyTypes:
config.relaychain.default_keystore_key_types ||
DEFAULT_KEYSTORE_KEY_TYPES,
randomNominatorsCount: config.relaychain?.random_nominators_count || 0,
maxNominations:
config.relaychain?.max_nominations || DEFAULT_MAX_NOMINATIONS,
nodes: [],
chain: config.relaychain.chain || DEFAULT_CHAIN,
overrides: globalOverrides,
defaultResources: config.relaychain.default_resources,
defaultPrometheusPrefix:
config.relaychain.default_prometheus_prefix ||
DEFAULT_PROMETHEUS_PREFIX,
delayNetworkSettings:
config.relaychain.default_delay_network_settings ||
config.settings?.global_delay_network_global_settings,
},
parachains: [],
};
// check all imageURLs for validity
// TODO: These checks should be against all config items that needs check
configurationFileChecks(config);
if (config.relaychain.genesis)
networkSpec.relaychain.genesis = config.relaychain.genesis;
const chainName = config.relaychain.chain || DEFAULT_CHAIN;
if (config.relaychain.default_db_snapshot)
networkSpec.relaychain.defaultDbSnapshot =
config.relaychain.default_db_snapshot;
// settings
networkSpec.settings = {
timeout: DEFAULT_GLOBAL_TIMEOUT,
enable_tracing: true,
node_verifier: "Metric",
...(config.settings ? config.settings : {}),
};
// default provider
if (!networkSpec.settings.provider)
networkSpec.settings.provider = "kubernetes";
// if we don't have a path to the chain-spec leave undefined to create
if (config.relaychain.chain_spec_path) {
const chainSpecPath = resolve(
process.cwd(),
config.relaychain.chain_spec_path,
);
if (!fs.existsSync(chainSpecPath)) {
console.error(
decorators.red(
`Genesis spec provided does not exist: ${chainSpecPath}`,
),
);
process.exit(1);
} else {
networkSpec.relaychain.chainSpecPath = chainSpecPath;
}
}
// even if we have a chain_spec_path we need to set
// the command to generate the raw version
networkSpec.relaychain.chainSpecCommand = config.relaychain.chain_spec_command
? config.relaychain.chain_spec_command
: DEFAULT_CHAIN_SPEC_COMMAND.replace(
"{{DEFAULT_COMMAND}}",
networkSpec.relaychain.defaultCommand,
);
const relayChainBootnodes: string[] = [];
for (const node of config.relaychain.nodes || []) {
const nodeSetup = await getNodeFromConfig(
networkSpec,
node,
relayChainBootnodes,
globalOverrides,
node.name, // group of 1
);
networkSpec.relaychain.nodes.push(nodeSetup);
}
for (const nodeGroup of config.relaychain.node_groups || []) {
for (let i = 0; i < (nodeGroup.count as number); i++) {
const node: NodeConfig = {
// Replace whitespaces with dashes for node names
// see https://github.com/paritytech/zombienet/issues/1659
name: `${nodeGroup.name.replaceAll(" ", "-")}-${i}`,
image: nodeGroup.image || networkSpec.relaychain.defaultImage,
command: nodeGroup.command,
args: sanitizeArgs(nodeGroup.args || []),
validator: true, // groups are always validators
invulnerable: false,
balance: DEFAULT_BALANCE,
env: nodeGroup.env,
overrides: nodeGroup.overrides,
resources:
nodeGroup.resources || networkSpec.relaychain.defaultResources,
db_snapshot: nodeGroup.db_snapshot,
prometheus_prefix:
nodeGroup.prometheus_prefix ||
networkSpec.relaychain.defaultPrometheusPrefix,
substrate_cli_args_version:
nodeGroup.substrate_cli_args_version ||
networkSpec.relaychain.default_substrate_cli_args_version,
};
const nodeSetup = await getNodeFromConfig(
networkSpec,
node,
relayChainBootnodes,
globalOverrides,
nodeGroup.name,
);
networkSpec.relaychain.nodes.push(nodeSetup);
}
}
if (networkSpec.relaychain.nodes.length < 1) {
throw new Error("No NODE defined in config, please review.");
}
if (config.parachains && config.parachains.length) {
for (const parachain of config.parachains) {
const para: CHAIN = whichChain(
parachain.chain || parachain.chain_spec_path || "",
parachain.force_decorator,
);
let computedStatePath,
computedStateCommand,
computedWasmPath,
computedWasmCommand;
const bootnodes = relayChainBootnodes;
// parachain_relaychain
const paraChainName =
(parachain.chain ? parachain.chain + "_" : "") + chainName;
// IF is defined use that value
// else check if the command is one off undying/adder otherwise true
const isCumulusBased =
parachain.cumulus_based !== undefined
? parachain.cumulus_based
: ![DEFAULT_ADDER_COLLATOR_BIN, UNDYING_COLLATOR_BIN].includes(
getFirstCollatorCommand(parachain),
);
// collator could by defined in groups or
// just using one collator definition
const collators = [];
const collatorConfigs = parachain.collator ? [parachain.collator] : [];
if (parachain.collators) collatorConfigs.push(...parachain.collators);
for (const collatorConfig of collatorConfigs) {
collators.push(
await getCollatorNodeFromConfig(
networkSpec,
collatorConfig,
parachain,
paraChainName,
para,
bootnodes,
isCumulusBased,
collatorConfig.name, // group of 1
),
);
}
for (const collatorGroup of parachain.collator_groups || []) {
for (let i = 0; i < (collatorGroup.count as number); i++) {
const node: NodeConfig = {
// Replace whitespaces with dashes for node names
// see https://github.com/paritytech/zombienet/issues/1659
name: `${collatorGroup.name.replaceAll(" ", "-")}-${i}`,
image: collatorGroup.image || DEFAULT_COLLATOR_IMAGE,
command: collatorGroup.command || DEFAULT_CUMULUS_COLLATOR_BIN,
args: sanitizeArgs(collatorGroup.args || [], { "listen-addr": 2 }),
validator: true, // groups are always validators
invulnerable: false,
balance: DEFAULT_BALANCE,
env: collatorGroup.env,
overrides: collatorGroup.overrides,
resources:
collatorGroup.resources ||
networkSpec.relaychain.defaultResources,
};
if (collatorGroup.substrate_cli_args_version)
node.substrate_cli_args_version =
collatorGroup.substrate_cli_args_version;
collators.push(
await getCollatorNodeFromConfig(
networkSpec,
node,
parachain,
paraChainName,
para,
bootnodes,
isCumulusBased,
collatorGroup.name,
),
);
}
}
// use the first collator for state/wasm generation
const firstCollator = collators[0];
if (!firstCollator)
throw new Error(
`No Collator defined for parachain ${parachain.id}, please review.`,
);
const collatorBinary = firstCollator.commandWithArgs
? firstCollator.commandWithArgs.split(" ")[0]
: firstCollator.command || DEFAULT_CUMULUS_COLLATOR_BIN;
// `test-parachain` doesn't support output file yet
const exportCmdSupportOutputFile =
isCumulusBased && collatorBinary !== "test-parachain";
if (parachain.genesis_state_path) {
const genesisStatePath = resolve(
process.cwd(),
parachain.genesis_state_path,
);
if (!fs.existsSync(genesisStatePath)) {
console.error(
decorators.red(
`Genesis state file path provided does not exist: ${genesisStatePath}`,
),
);
process.exit(1);
} else {
computedStatePath = genesisStatePath;
}
} else {
// NOTE: the subcommand to execute will be set later based on `substrateCliArgsVersion`.
computedStateCommand = parachain.genesis_state_generator
? parachain.genesis_state_generator
: `${collatorBinary} {{GENESIS_GENERATE_SUBCOMMAND}}`;
// TODO: we should remove this conditional ones
// https://github.com/paritytech/polkadot-sdk/pull/2375 and
// https://github.com/paritytech/polkadot-sdk/pull/2370
// and the jobs tha use a previous version of the collators reach those fixes.
computedStateCommand += `${
exportCmdSupportOutputFile ? "" : " >"
} {{CLIENT_REMOTE_DIR}}/${GENESIS_STATE_FILENAME}`;
}
if (parachain.genesis_wasm_path) {
const genesisWasmPath = resolve(
process.cwd(),
parachain.genesis_wasm_path,
);
if (!fs.existsSync(genesisWasmPath)) {
console.error(
decorators.red(
`Genesis state file path provided does not exist: ${genesisWasmPath}`,
),
);
process.exit(1);
} else {
computedWasmPath = genesisWasmPath;
}
} else {
computedWasmCommand = parachain.genesis_wasm_generator
? parachain.genesis_wasm_generator
: `${collatorBinary} ${DEFAULT_WASM_GENERATE_SUBCOMMAND}`;
// TODO: we should remove this conditional ones
// https://github.com/paritytech/polkadot-sdk/pull/2375 and
// https://github.com/paritytech/polkadot-sdk/pull/2370
// and the jobs tha use a previous version of the collators reach those fixes.
computedWasmCommand += `${
exportCmdSupportOutputFile ? "" : " >"
} {{CLIENT_REMOTE_DIR}}/${GENESIS_WASM_FILENAME}`;
}
let parachainSetup: Parachain = {
id: parachain.id,
name: getUniqueName(parachain.id.toString()),
para,
cumulusBased: isCumulusBased,
addToGenesis:
parachain.add_to_genesis === undefined
? true
: parachain.add_to_genesis, // add by default
registerPara:
parachain.register_para === undefined
? true
: parachain.register_para, // register by default
onboardAsParachain:
parachain.onboard_as_parachain !== undefined
? parachain.onboard_as_parachain
: true, // onboard by default
collators,
};
if (parachain.chain) parachainSetup.chain = parachain.chain;
// if we don't have a path to the chain-spec leave undefined to create
if (parachain.chain_spec_path) {
const chainSpecPath = resolve(process.cwd(), parachain.chain_spec_path);
if (!fs.existsSync(chainSpecPath)) {
console.error(
decorators.red(
`Chain spec provided for parachain id: ${parachain.id} does not exist: ${chainSpecPath}`,
),
);
process.exit(1);
} else {
parachainSetup.chainSpecPath = chainSpecPath;
}
}
// even if we have a chain_spec_path we need to set
// the command to generate the raw version
parachainSetup.chainSpecCommand = parachain.chain_spec_command
? parachain.chain_spec_command
: `${collatorBinary} build-spec ${
parachain.chain ? "--chain {{chainName}}" : ""
} --disable-default-bootnode`;
parachainSetup = {
...parachainSetup,
...(parachain.balance ? { balance: parachain.balance } : {}),
...(computedWasmPath ? { genesisWasmPath: computedWasmPath } : {}),
...(computedWasmCommand
? { genesisWasmGenerator: computedWasmCommand }
: {}),
...(computedStatePath ? { genesisStatePath: computedStatePath } : {}),
...(computedStateCommand
? { genesisStateGenerator: computedStateCommand }
: {}),
...(parachain.genesis ? { genesis: parachain.genesis } : {}),
};
networkSpec.parachains.push(parachainSetup);
}
}
networkSpec.types = config.types ? config.types : {};
if (config.hrmp_channels) networkSpec.hrmp_channels = config.hrmp_channels;
return networkSpec as ComputedNetwork;
}
// TODO: move this fn to other module.
export async function generateBootnodeSpec(
config: ComputedNetwork,
): Promise<Node> {
const provider = config.settings.provider;
const ports = await getPorts(provider, {});
const externalPorts = await getExternalPorts(provider, ports, {});
const nodeSetup: Node = {
name: "bootnode",
key: "0000000000000000000000000000000000000000000000000000000000000001",
command: config.relaychain.defaultCommand || DEFAULT_COMMAND,
image: config.relaychain.defaultImage || DEFAULT_IMAGE,
chain: config.relaychain.chain,
validator: false,
invulnerable: false,
args: ["--rpc-external", "--listen-addr", "/ip4/0.0.0.0/tcp/30333/ws"],
env: [],
bootnodes: [],
telemetryUrl: "",
prometheus: true, // --prometheus-external
overrides: [],
zombieRole: ZombieRole.BootNode,
imagePullPolicy: config.settings.image_pull_policy || "Always",
...ports,
externalPorts,
};
return nodeSetup;
}
interface UsedNames {
[properyName: string]: number;
}
const mUsedNames: UsedNames = {};
export function getUniqueName(name: string): string {
// Transform whitespaces to dashes in name, since
// whitespaces in names are not supported
// see https://github.com/paritytech/zombienet/issues/1659
name = name.replaceAll(" ", "-");
let uniqueName;
if (!mUsedNames[name]) {
mUsedNames[name] = 1;
uniqueName = name;
} else {
uniqueName = `${name}-${mUsedNames[name]}`;
mUsedNames[name] += 1;
}
return uniqueName;
}
async function getLocalOverridePath(
configBasePath: string,
definedLocalPath: string,
): Promise<string> {
// let check if local_path is full or relative
let local_real_path = definedLocalPath;
if (!fs.existsSync(definedLocalPath)) {
// check relative to config
local_real_path = path.join(configBasePath, definedLocalPath);
if (!fs.existsSync(local_real_path))
throw new Error(
"Invalid override config, only fullpaths or relative paths (from the config) are allowed",
);
}
return local_real_path;
}
async function getCollatorNodeFromConfig(
networkSpec: any,
collatorConfig: NodeConfig,
parachain: ParachainConfig,
chain: string, // relay-chain
para: CHAIN,
bootnodes: string[], // parachain bootnodes
cumulusBased: boolean,
group?: string,
): Promise<Node> {
let args: string[] = [];
const env = collatorConfig.env
? DEFAULT_ENV.concat(collatorConfig.env)
: DEFAULT_ENV;
const collatorBinary = collatorConfig.command_with_args
? collatorConfig.command_with_args.split(" ")[0]
: collatorConfig.command || DEFAULT_CUMULUS_COLLATOR_BIN;
const collatorName = getUniqueName(collatorConfig.name || "collator");
const [decoratedKeysGenerator] = decorate(para, [generateKeyForNode]);
const accountsForNode = await decoratedKeysGenerator(collatorName);
const provider = networkSpec.settings.provider;
const ports = await getPorts(provider, collatorConfig);
const externalPorts = await getExternalPorts(provider, ports, collatorConfig);
// IFF the collator have explicit set the validator field we use that value,
// if not we set by default cumulus collators as `validators`, this implies that we will
// run those with this flag `--collator`.
const isValidator =
collatorConfig.validator !== undefined
? collatorConfig.validator
: cumulusBased
? true
: false;
if (collatorConfig.args)
args = args.concat(
sanitizeArgs(
collatorConfig.args,
{ "listen-addr": 2 },
{ nodeName: collatorName, isValidator },
),
);
const node: Node = {
name: collatorName,
key: getSha256(collatorName),
accounts: accountsForNode,
validator: isValidator,
invulnerable: collatorConfig.invulnerable,
balance: collatorConfig.balance,
image: collatorConfig.image || DEFAULT_COLLATOR_IMAGE,
command: collatorBinary,
commandWithArgs: collatorConfig.command_with_args,
keystoreKeyTypes:
collatorConfig.keystore_key_types || DEFAULT_KEYSTORE_KEY_TYPES,
args: args || [],
chain,
bootnodes,
env,
telemetryUrl: "",
prometheus: prometheusExternal(networkSpec),
overrides: [],
zombieRole: cumulusBased ? ZombieRole.CumulusCollator : ZombieRole.Collator,
parachainId: parachain.id,
dbSnapshot: collatorConfig.db_snapshot,
imagePullPolicy: networkSpec.settings.image_pull_policy || "Always",
...ports,
externalPorts,
p2pCertHash: collatorConfig.p2p_cert_hash,
prometheusPrefix:
parachain.prometheus_prefix ||
networkSpec.relaychain.defaultPrometheusPrefix,
delayNetworkSettings:
collatorConfig.delay_network_settings ||
parachain.delayNetworkSettings ||
networkSpec.settings.delayNetworkSettings,
};
if (group) node.group = group;
return node;
}
async function getNodeFromConfig(
networkSpec: any,
node: NodeConfig,
relayChainBootnodes: string[],
globalOverrides: Override[],
group?: string,
): Promise<Node> {
const command = node.command
? node.command
: networkSpec.relaychain.defaultCommand;
const image = node.image || networkSpec.relaychain.defaultImage;
let args: string[] = sanitizeArgs(networkSpec.relaychain.defaultArgs || []);
const env = node.env ? DEFAULT_ENV.concat(node.env) : DEFAULT_ENV;
let nodeOverrides: Override[] = [];
if (node.overrides) {
nodeOverrides = await Promise.all(
node.overrides.map(async (override) => {
const valid_local_path = await getLocalOverridePath(
networkSpec.configBasePath,
override.local_path,
);
return {
local_path: valid_local_path,
remote_name: override.remote_name,
};
}),
);
}
// by default nodes are validators except for those
// set explicit to not be validators.
const isValidator = node.validator !== false;
const nodeName = getUniqueName(node.name);
const accountsForNode = await generateKeyForNode(nodeName);
const provider = networkSpec.settings.provider;
const ports = await getPorts(provider, node);
const externalPorts = await getExternalPorts(provider, ports, node);
if (node.args)
args = args.concat(sanitizeArgs(node.args, {}, { nodeName, isValidator }));
const uniqueArgs = [...new Set(args)];
// build node Setup
const nodeSetup: Node = {
name: nodeName,
key: getSha256(nodeName),
accounts: accountsForNode,
command: command || DEFAULT_COMMAND,
commandWithArgs: node.command_with_args,
image: image || DEFAULT_IMAGE,
chain: networkSpec.relaychain.chain,
validator: isValidator,
invulnerable: node.invulnerable,
balance: node.balance || DEFAULT_BALANCE,
args: uniqueArgs,
keystoreKeyTypes:
node.keystore_key_types ||
networkSpec.relaychain.defaultKeystoreKeyTypes ||
DEFAULT_KEYSTORE_KEY_TYPES,
env,
bootnodes: relayChainBootnodes,
telemetryUrl: networkSpec.settings?.telemetry
? "ws://telemetry:8000/submit 0"
: "",
telemetry: networkSpec.settings?.telemetry ? true : false,
prometheus: prometheusExternal(networkSpec),
overrides: [...globalOverrides, ...nodeOverrides],
addToBootnodes: node.add_to_bootnodes ? true : false,
resources: node.resources || networkSpec.relaychain.defaultResources,
zombieRole: ZombieRole.Node,
imagePullPolicy: networkSpec.settings.image_pull_policy || "Always",
...ports,
externalPorts,
p2pCertHash: node.p2p_cert_hash,
prometheusPrefix:
node.prometheus_prefix || networkSpec.relaychain.defaultPrometheusPrefix,
delayNetworkSettings:
node.delay_network_settings ||
networkSpec.relaychain.delayNetworkSettings,
};
if (group) nodeSetup.group = group;
const dbSnapshot = node.db_snapshot
? node.db_snapshot
: networkSpec.relaychain.defaultDbSnapshot || null;
if (dbSnapshot) nodeSetup.dbSnapshot = dbSnapshot;
if (
node.substrate_cli_args_version ||
networkSpec.default_substrate_cli_args_version
)
nodeSetup.substrateCliArgsVersion =
node.substrate_cli_args_version ||
networkSpec.default_substrate_cli_args_version;
return nodeSetup;
}
function sanitizeArgs(
args: string[],
extraArgsToRemove: { [key: string]: number } = {},
context?: {
nodeName: string;
isValidator: boolean;
},
): string[] {
// Do NOT filter any argument to the internal full-node of the collator
const augmentedArgsToRemove = { ...ARGS_TO_REMOVE, ...extraArgsToRemove };
let removeNext = false;
const separatorIndex = args.indexOf("--");
const filteredArgs = args
.slice(0, separatorIndex >= 0 ? separatorIndex : args.length)
.filter((arg) => {
if (removeNext) {
removeNext = false;
return false;
}
const argParsed = arg === "-d" ? "d" : arg.replace(/--/g, "");
if (augmentedArgsToRemove[argParsed]) {
// Don't sanitize `--<dev_account>` flags
// IFF the node name is one of the dev accounts and is set to be a validator
// see: https://github.com/paritytech/zombienet/issues/1448
if (
context &&
argParsed === context.nodeName &&
context.isValidator &&
DEV_ACCOUNTS.includes(argParsed)
)
return true;
if (augmentedArgsToRemove[argParsed] === 2) removeNext = true;
return false;
} else {
return true;
}
});
// put the internal full-node args again
if (separatorIndex >= 0) {
filteredArgs.push(...args.slice(separatorIndex));
}
return filteredArgs;
}
async function getPorts(provider: string, nodeSetup: any): Promise<any> {
let ports = DEFAULT_PORTS;
if (provider === "native") {
ports = {
p2pPort: nodeSetup.p2p_port || (await getRandomPort()),
wsPort: nodeSetup.ws_port || (await getRandomPort()),
rpcPort: nodeSetup.rpc_port || (await getRandomPort()),
prometheusPort: nodeSetup.prometheus_port || (await getRandomPort()),
};
}
return ports;
}
async function getExternalPorts(
provider: string,
processPorts: any,
nodeSetup: any,
): Promise<any> {
if (provider === "native") return processPorts;
const ports = {
p2pPort: nodeSetup.p2p_port || (await getRandomPort()),
wsPort: nodeSetup.ws_port || (await getRandomPort()),
rpcPort: nodeSetup.rpc_port || (await getRandomPort()),
prometheusPort: nodeSetup.prometheus_port || (await getRandomPort()),
};
return ports;
}
// enable --prometheus-external by default
// TODO: fix the `any` to an actual interface
const prometheusExternal = (networkSpec: ComputedNetwork): boolean => {
return networkSpec.settings?.prometheus !== undefined
? networkSpec.settings.prometheus
: true;
};
export function getFirstCollatorCommand(parachain: ParachainConfig): string {
let cmd;
if (parachain.collator) {
cmd = parachain.collator.command_with_args || parachain.collator.command;
} else if (parachain.collators?.length) {
cmd =
parachain.collators[0].command_with_args ||
parachain.collators[0].command;
} else if (parachain.collator_groups?.length) {
cmd = parachain.collator_groups[0].command;
}
cmd = cmd || DEFAULT_CUMULUS_COLLATOR_BIN; // no command defined we use the default polkadot-parachain.
debug(`cmd is ${cmd}`);
cmd = cmd.split(" ")[0];
return cmd.split("/").pop()!;
}