Skip to content

Commit

Permalink
Use para id in specs instead of flag (#157)
Browse files Browse the repository at this point in the history
* Updates polkadot dependency

* add log

* remove parachain id flag and chain flag

* cleanup

* apply feedback

Co-authored-by: Crystalin <[email protected]>
  • Loading branch information
joelamouche and Crystalin authored Dec 13, 2021
1 parent ba23b5b commit 1098183
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 51 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ npm i polkadot-launch -g
## Binary Files

To use polkadot-launch, you need to have binary files for a `polkadot` relay chain and a
`polkadot-collator`.
`polkadot-collator` in the bin folder.

You can generate these files by cloning the `rococo-v1` branch of these projects and building them
with the specific flags below:
You can generate these files by cloning the `rococo-v1` branch of these projects in the same root as the polkadot-launch repo
and building them with the specific flags below:

```bash
git clone https://github.com/paritytech/polkadot
cd polkadot
cargo build --release
cp ./target/release/polkadot ../polkadot-launch/bin/polkadot-relaychain
```

and
Expand All @@ -34,6 +35,7 @@ and
git clone https://github.com/paritytech/cumulus
cd cumulus
cargo build --release -p polkadot-collator
cp ./target/release/polkadot-collator ../polkadot-launch/bin/polkadot-collator
```

## Use
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "yarn build && node dist/cli.js",
"lint": "prettier -v && prettier --check .",
"lint:write": "prettier --write .",
"para-test": "mocha -r ts-node/register 'test/tests/**/test-*.ts'"
"para-test": "mocha -r ts-node/register 'test/tests/**/test-*.ts'",
"para-test-no-ci": "mocha -r ts-node/register 'test/tests-no-ci/**/test-*.ts'"
},
"dependencies": {
"@polkadot/api": "^6.10.3",
Expand Down
21 changes: 6 additions & 15 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export async function run(config_dir: string, rawConfig: LaunchConfig) {

// Then launch each parachain
for (const parachain of config.parachains) {
const { id, resolvedId, balance, chain } = parachain;
const { id, resolvedId, balance } = parachain;

const bin = resolve(config_dir, parachain.bin);
if (!fs.existsSync(bin)) {
console.error("Parachain binary does not exist: ", bin);
Expand All @@ -137,13 +138,11 @@ export async function run(config_dir: string, rawConfig: LaunchConfig) {
`Starting a Collator for parachain ${resolvedId}: ${account}, Collator port : ${port} wsPort : ${wsPort} rpcPort : ${rpcPort}`
);
const skip_id_arg = !id;
await startCollator(bin, resolvedId, wsPort, rpcPort, port, {
await startCollator(bin, wsPort, rpcPort, port, {
name,
chain,
spec,
flags,
basePath,
skip_id_arg,
onlyOneParachainNode: config.parachains.length === 1,
});
}
Expand Down Expand Up @@ -186,7 +185,6 @@ export async function run(config_dir: string, rawConfig: LaunchConfig) {

interface GenesisParachain {
isSimple: boolean;
id?: string;
resolvedId: string;
chain?: string;
bin: string;
Expand All @@ -210,7 +208,7 @@ async function addParachainsToGenesis(
let paras = x.concat(y);

for (const parachain of paras) {
const { isSimple, id, resolvedId, chain } = parachain;
const { resolvedId, chain } = parachain;
const bin = resolve(config_dir, parachain.bin);
if (!fs.existsSync(bin)) {
console.error("Parachain binary does not exist: ", bin);
Expand All @@ -222,15 +220,8 @@ async function addParachainsToGenesis(
let genesisState: string;
let genesisWasm: string;
try {
if (isSimple) {
// adder-collator does not support `--parachain-id` for export-genesis-state (and it is
// not necessary for it anyway), so we don't pass it here.
genesisState = await exportGenesisState(bin);
genesisWasm = await exportGenesisWasm(bin);
} else {
genesisState = await exportGenesisState(bin, id, chain);
genesisWasm = await exportGenesisWasm(bin, chain);
}
genesisState = await exportGenesisState(bin, chain);
genesisWasm = await exportGenesisWasm(bin);
} catch (err) {
console.error(err);
process.exit(1);
Expand Down
28 changes: 3 additions & 25 deletions src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ export async function exportGenesisWasm(
chain?: string
): Promise<string> {
let args = ["export-genesis-wasm"];
if (chain) {
args.push("--chain=" + chain);
}

// wasm files are typically large and `exec` requires us to supply the maximum buffer size in
// advance. Hopefully, this generous limit will be enough.
Expand All @@ -162,13 +159,10 @@ export async function exportGenesisWasm(
/// Export the genesis state aka genesis head.
export async function exportGenesisState(
bin: string,
id?: string,
chain?: string
): Promise<string> {
let args = ["export-genesis-state"];
if (id) {
args.push("--parachain-id=" + id);
}

if (chain) {
args.push("--chain=" + chain);
}
Expand All @@ -186,7 +180,6 @@ export async function exportGenesisState(
// Start a collator node for a parachain.
export function startCollator(
bin: string,
id: string,
wsPort: number,
rpcPort: number | undefined,
port: number,
Expand All @@ -195,15 +188,7 @@ export function startCollator(
return new Promise<void>(function (resolve) {
// TODO: Make DB directory configurable rather than just `tmp`
let args = ["--ws-port=" + wsPort, "--port=" + port];
const {
basePath,
name,
skip_id_arg,
onlyOneParachainNode,
chain,
flags,
spec,
} = options;
const { basePath, name, onlyOneParachainNode, flags, spec } = options;

if (rpcPort) {
args.push("--rpc-port=" + rpcPort);
Expand All @@ -221,18 +206,11 @@ export function startCollator(
args.push(`--${name.toLowerCase()}`);
console.log(`Added --${name.toLowerCase()}`);
}
if (!skip_id_arg) {
args.push("--parachain-id=" + id);
console.log(`Added --parachain-id=${id}`);
}

if (onlyOneParachainNode) {
args.push("--force-authoring");
console.log(`Added --force-authoring`);
}
if (chain) {
args.push("--chain=" + chain);
console.log(`Added --chain=${chain}`);
}

let flags_collator = null;
let flags_parachain = null;
Expand Down
1 change: 1 addition & 0 deletions src/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export async function addGenesisParachain(
paras.push(new_para);

let data = JSON.stringify(chainSpec, null, 2);

fs.writeFileSync(spec, data);
console.log(` ✓ Added Genesis Parachain ${para_id}`);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export interface CollatorOptions {
name?: string;
chain?: string;
spec?: string;
flags?: string[];
basePath?: string;
skip_id_arg?: boolean;
onlyOneParachainNode?: boolean;
}

Expand Down
4 changes: 2 additions & 2 deletions test/test-utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const DISPLAY_LOG = process.env.PARACHAIN_LOG || false;
export const PARACHAIN_LOG = process.env.PARACHAIN_LOG || "info";

export const BINARY_PATH =
process.env.BINARY_PATH || `../bin/polkadot-collator-mac`;
process.env.BINARY_PATH || `../bin/polkadot-collator`;
export const RELAY_BINARY_PATH =
process.env.RELAY_BINARY_PATH || `../bin/polkadot-relaychain-mac`;
process.env.RELAY_BINARY_PATH || `../bin/polkadot-relaychain`;
export const SPAWNING_TIME = 20000;
export const ETHAPI_CMD = process.env.ETHAPI_CMD || "";

Expand Down
3 changes: 0 additions & 3 deletions test/test-utils/para-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { killAll, run } from "../../src"; //"polkadot-launch";
import {
BINARY_PATH,
RELAY_BINARY_PATH,
DISPLAY_LOG,
SPAWNING_TIME,
RELAY_CHAIN_NODE_NAMES,
} from "./constants";
const debug = require("debug")("test:para-node");
Expand Down Expand Up @@ -156,7 +154,6 @@ export async function startParachainNodes(options: ParachainOptions): Promise<{
bin: BINARY_PATH,
id: (1000 * (i + 1)).toString(),
balance: "1000000000000000000000",
// chain: options.chain, this is not supported by cumulus
nodes: [
{
port: ports[i * 2 + numberOfParachains + 1].p2pPort,
Expand Down
34 changes: 34 additions & 0 deletions test/tests-no-ci/test-balance-genesis-2-parachains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Keyring from "@polkadot/keyring";
import { expect } from "chai";

import { describeParachain } from "../test-utils/setup-para-tests";

describeParachain(
"Balance genesis",
{ chain: "rococo-local", numberOfParachains: 2 },
(context) => {
it("should be accessible through web3", async function () {
const keyring = new Keyring({ type: "sr25519" });
const aliceRelay = keyring.addFromUri("//Alice");

//check parachain id
expect(
(
(await context._polkadotApiRelaychains[0].query.paras.parachains()) as any
)[0].toString()
).to.eq("1000");
expect(
(
(await context._polkadotApiRelaychains[0].query.paras.parachains()) as any
)[1].toString()
).to.eq("2000");
expect(
(
await context.polkadotApiParaone.query.system.account(
aliceRelay.addressRaw
)
).data.free.toHuman()
).to.eq("1,152,921,504,606,846,976");
});
}
);
8 changes: 8 additions & 0 deletions test/tests/test-balance-genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ describeParachain("Balance genesis", { chain: "rococo-local" }, (context) => {
it("should be accessible through web3", async function () {
const keyring = new Keyring({ type: "sr25519" });
const aliceRelay = keyring.addFromUri("//Alice");

//check parachain id
expect(
(
(await context._polkadotApiRelaychains[0].query.paras.parachains()) as any
)[0].toString()
).to.eq("1000");
// check genesis balance
expect(
(
await context.polkadotApiParaone.query.system.account(
Expand Down

0 comments on commit 1098183

Please sign in to comment.