Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create script for automating the registration of parachain with the relay chain #43

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parachain/parachain.star
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ def start_nodes(plan, args, relay_chain_ip):
for parachain in parachains:
para_id = register_para_slot.register_para_id(plan, relay_chain_ip)
start_local_parachain_node(plan, parachain, para_id)
register_para_slot.onboard_genesis_state_and_wasm(plan, para_id, parachain)
17 changes: 17 additions & 0 deletions parachain/register-para-id.star
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ def register_para_id(plan, alice_ip):
para_id = plan.exec(service_name = test.name, recipe = ExecRecipe(command = ["/bin/sh", "-c", "cat /tmp/para.json | tr -d '\n\r'"]))

return para_id["output"]

def onboard_genesis_state_and_wasm(plan, para_id, chain_name):
service = plan.add_service(
name = "upload-genesis-file",
config = ServiceConfig(
image = constant.NODE_IMAGE,
files = {
"/app": "configs",
"/build": constant.RAW_BUILD_SPEC,
"/javascript": "javascript",
},
entrypoint = ["/bin/sh"],
),
)

plan.exec(service_name = service.name, recipe = ExecRecipe(command = ["/bin/sh", "-c", "cd /javascript && npm i "]))
plan.exec(service_name = service.name, recipe = ExecRecipe(command = ["/bin/sh", "-c", "cd /javascript && node onboard ws://{0}:9944 //Alice {1} /build/{2}-genesis-state /build/{2}-genesis-wasm".format(alice_ip, para_id, chain_name)]))
69 changes: 69 additions & 0 deletions parachain/static_files/javascript/onboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api");
const fs = require("fs");

const run = async () => {
try {
console.log("Parsing Args ...");
// 0 & 1 are command context
const endpoint = process.argv[2];
const seed = process.argv[3];
const id = process.argv[4];
const header = process.argv[5];
const wasmFile = process.argv[6];

const wsProvider = new WsProvider(endpoint);

const api = await ApiPromise.create({
provider: wsProvider,
});

const keyring = new Keyring({ type: "sr25519" });
const alice = keyring.addFromUri(seed);

let wasm;
try {
wasm = fs.readFileSync(wasmFile, "utf8");
} catch (err) {
console.error(err);
throw err;
}

let paraGenesisArgs = {
genesis_head: header,
validation_code: wasm,
parachain: true,
};

let genesis = api.createType("ParaGenesisArgs", paraGenesisArgs);

const nonce = Number((await api.query.system.account(alice.address)).nonce);

console.log(
`--- Submitting extrinsic to register parachain ${id}. (nonce: ${nonce}) ---`
);
const sudoCall = await api.tx.sudo
.sudo(api.tx.parasSudoWrapper.sudoScheduleParaInitialize(id, genesis))
.signAndSend(alice, { nonce: nonce, era: 0 }, (result) => {
console.log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
console.log(
`Transaction included at blockHash ${result.status.asInBlock}`
);
console.log("Waiting for finalization...");
} else if (result.status.isFinalized) {
console.log(
`Transaction finalized at blockHash ${result.status.asFinalized}`
);
sudoCall();
process.exit();
} else if (result.isError) {
console.log(`Transaction Error`);
process.exit();
}
});
} catch (error) {
console.log("error:", error);
}
};

run();
3 changes: 2 additions & 1 deletion parachain/static_files/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"register": "node register.js"
"register": "node register.js",
"onboard": "node onboard.js"
},
"keywords": [],
"dependencies": {
Expand Down