Skip to content

Commit

Permalink
Add option to isolate env using forkId/protocolId in chain-spec (#…
Browse files Browse the repository at this point in the history
…1975)

* Add option to isolate env using forkId/protocolId in chain-spec

* docs and typo
  • Loading branch information
pepoviola authored Mar 4, 2025
1 parent ce5331d commit 73c779a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/network-definition-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The network config can be provided both in `json` or `toml` format and each sect
- `node_spawn_timeout`: (Number, default per provider) timeout to spawn pod/process.
- `local_ip`: (String, default "127.0.0.1") ip used for expose local services (rpc/metrics/monitors).
- `node_verifier`: (String, "None" or "Metric"), Allow to manage how we verify node readiness or disable (None). The default value is `Metric`.
- `isolate_env`: (boolean, default falce), If true, zombienet will add a random suffix to `protocolId` (in the chain-spec) and will add the `forkId` field with the same value of the `protocolId` (plus the suffix). This will _isolate_ the network since the prefix used in p2p protocols will not match with other networks (mostly used to run multiple networks in native provider).

## `relaychain`

Expand Down
3 changes: 3 additions & 0 deletions javascript/packages/orchestrator/src/configTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export interface Settings {
// Allow to manage how we verify node readiness or disable (None)
// Metric: query prometheus startProcess metric
node_verifier?: "None" | "Metric";
// If true zombienet will add a random sufix to `protocolId` and use the same `protocolId`sufix
// as `forkId` to prevent other nodes to connect to this chain.
isolate_env?: boolean;
}

export interface GlobalVolume {
Expand Down
15 changes: 15 additions & 0 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
addParachainToGenesis,
customizePlainRelayChain,
readAndParseChainSpec,
writeChainSpec,
} from "./chainSpec";
import {
generateBootnodeSpec,
Expand Down Expand Up @@ -236,6 +237,10 @@ export async function start(
// see https://github.com/paritytech/substrate/pull/13384
await setSubstrateCliArgsVersion(networkSpec, client);

const random_suffix_to_isolate = networkSpec.settings.isolate_env
? generateNamespace(2)
: null;

// create or copy relay chain spec
await setupChainSpec(
namespace,
Expand Down Expand Up @@ -265,6 +270,7 @@ export async function start(
chainName,
parachain,
relayChainSpecIsRaw,
random_suffix_to_isolate,
);
};

Expand Down Expand Up @@ -309,6 +315,15 @@ export async function start(
await fs.promises.copyFile(chainSpecFullPathPlain, chainSpecFullPath);
}

// make chain unique if is set
if (random_suffix_to_isolate) {
// customize forkId/protocolId to make chain uniq
const chainSpecContent = readAndParseChainSpec(chainSpecFullPath);
chainSpecContent.forkId = `${chainSpecContent.protocolId}${random_suffix_to_isolate}`;
chainSpecContent.protocolId = `${chainSpecContent.protocolId}${random_suffix_to_isolate}`;
writeChainSpec(chainSpecFullPath, chainSpecContent);
}

// ensure chain raw is ok
try {
const chainSpecContent = readAndParseChainSpec(chainSpecFullPathPlain);
Expand Down
9 changes: 9 additions & 0 deletions javascript/packages/orchestrator/src/paras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function generateParachainFiles(
relayChainName: string,
parachain: Parachain,
relayChainSpecIsRaw: boolean,
random_sufix_to_isolate: string | null = null,
): Promise<void> {
const [
addAuraAuthority,
Expand Down Expand Up @@ -191,6 +192,14 @@ export async function generateParachainFiles(
const paraSpecRaw = readAndParseChainSpec(chainSpecFullPath);
if (paraSpecRaw.para_id) paraSpecRaw.para_id = parachain.id;
if (paraSpecRaw.paraId) paraSpecRaw.paraId = parachain.id;

// make chain unique if is set
if (random_sufix_to_isolate) {
// customize forkId/protocolId to make chain uniq
paraSpecRaw.forkId = `${paraSpecRaw.protocolId}${random_sufix_to_isolate}`;
paraSpecRaw.protocolId = `${paraSpecRaw.protocolId}${random_sufix_to_isolate}`;
}

writeChainSpec(chainSpecFullPath, paraSpecRaw);
} catch (e: any) {
if (e.code !== "ERR_FS_FILE_TOO_LARGE") throw e;
Expand Down

0 comments on commit 73c779a

Please sign in to comment.