From 15248c132359c23c042ceb2c1155b1715def12f4 Mon Sep 17 00:00:00 2001 From: Shanith K K Date: Wed, 27 Mar 2024 11:52:37 +0530 Subject: [PATCH] chore: update the javascript for editing the plain build spec --- parachain/build-spec.star | 46 +++--------- .../javascript/edit_parachain_plain.js | 71 +++++++++++-------- 2 files changed, 49 insertions(+), 68 deletions(-) diff --git a/parachain/build-spec.star b/parachain/build-spec.star index 09d4a72..1a63f7a 100644 --- a/parachain/build-spec.star +++ b/parachain/build-spec.star @@ -1,39 +1,6 @@ build_spec = import_module("../package_io/build-spec.star") constant = import_module("../package_io/constant.star") -# def create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, chain_base, para_id, sudo_key, collators_keys): -# files = { -# "/app": "configs", -# } - -# plan.run_sh( -# run = "{0} build-spec --chain={1} --disable-default-bootnode > /tmp/{2}.json".format(binary, chain_base, chain_name), -# image = image, -# files = files, -# store = [StoreSpec(src = "/tmp/{0}.json".format(chain_name), name = chain_name + "plain")], -# ) - -# files = { -# "/app": "configs", -# "/build": chain_name + "plain", -# "/javascript": "javascript", -# } - - -# run_command = "cd /javascript && npm i && node edit_parachain_plain.js /build/{0}.json {1} \"{2}\" \'{3}\'".format(chain_name, para_id, sudo_key, collators_keys) -# plan.print(run_command) -# plan.run_sh( -# run = run_command, -# image = constant.NODE_IMAGE, -# files = files, -# store = [StoreSpec(src = "/build/{0}.json".format(chain_name), name = chain_name + "edit")], -# ) - - -# raw_service = create_raw_build_spec_genisis_state_genisis_wasm_for_parachain(plan, binary, image, chain_name, ) - -# return raw_service - def create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, chain_base, para_id, sudo_key, collators_keys): files = { "/app": "configs", @@ -49,19 +16,22 @@ def create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, ch files = { "/app": "configs", "/build": chain_name + "plain", + "/javascript": "javascript", } + + run_command = "cd /javascript && npm i && node edit_parachain_plain.js /build/{0}.json {1} \"{2}\" \'{3}\'".format(chain_name, para_id, sudo_key, collators_keys) + plan.print(run_command) plan.run_sh( - run = "sed -e 's/\"parachainId\": *[0-9]\\+/\"parachainId\": {0}/' -e 's/\"para_id\": [0-9]*,/\"para_id\": {0},/' -e 's/\"paraId\": [0-9]*,/\"paraId\": {0},/' -e 's/\"parachain_id\": [0-9]*,/\"parachain_id\": {0},/' /build/{1}.json > /tmp/{1}.json".format(para_id, chain_name), - image = constant.CURL_JQ_IMAGE, + run = run_command, + image = constant.NODE_IMAGE, files = files, - store = [StoreSpec(src = "/tmp/{0}.json".format(chain_name), name = chain_name + "edit")], + store = [StoreSpec(src = "/build/{0}.json".format(chain_name), name = chain_name + "edit")], ) - raw_service = create_raw_build_spec_genisis_state_genisis_wasm_for_parachain(plan, binary, image, chain_name) + raw_service = create_raw_build_spec_genisis_state_genisis_wasm_for_parachain(plan, binary, image, chain_name, ) return raw_service - def create_raw_build_spec_genisis_state_genisis_wasm_for_parachain(plan, binary, image, chain_name): files = { "/app": "configs", diff --git a/parachain/static_files/javascript/edit_parachain_plain.js b/parachain/static_files/javascript/edit_parachain_plain.js index 9a667a4..08afcc5 100644 --- a/parachain/static_files/javascript/edit_parachain_plain.js +++ b/parachain/static_files/javascript/edit_parachain_plain.js @@ -1,68 +1,79 @@ const fs = require("fs"); -const { Keyring } = require('@polkadot/keyring'); -const { encodeAddress, cryptoWaitReady } = require('@polkadot/util-crypto'); +const { Keyring } = require("@polkadot/keyring"); +const { encodeAddress, cryptoWaitReady } = require("@polkadot/util-crypto"); -async function updateParachainSpec(paraSpecFile, paraId, newSudoKeyPhrase, initialCollatorsPhrase) { +async function updateParachainSpec( + paraSpecFile, + paraId, + newSudoKeyPhrase, + initialCollatorsPhrase +) { try { const rawdata = fs.readFileSync(paraSpecFile); const chainSpec = JSON.parse(rawdata); const collators = JSON.parse(initialCollatorsPhrase); - - chainSpec.para_id = paraId; + + if ("para_id" in chainSpec) { + chainSpec.para_id = paraId; + } + + if ("paraId" in chainSpec) { + chainSpec.paraId = paraId; + } + + if ("parachain_id" in chainSpec) { + chainSpec.parachain_id = paraId; + } + chainSpec.genesis.runtime.parachainInfo.parachainId = paraId; - await cryptoWaitReady(); - const keyring = new Keyring({ type: 'sr25519' }); - - let newSudoKey = ''; // Declaring newSudoKey as let instead of const + const keyring = new Keyring({ type: "sr25519" }); + + let newSudoKey = ""; // Declaring newSudoKey as let instead of const if (newSudoKeyPhrase.length > 0) { const newSudoAccount = keyring.addFromUri(newSudoKeyPhrase); newSudoKey = newSudoAccount.address; chainSpec.genesis.runtime.sudo.key = newSudoKey; - - const SESSION_KEYS = [ - newSudoKey, - newSudoKey, - { aura: newSudoKey } - ]; + + const SESSION_KEYS = [newSudoKey, newSudoKey, { aura: newSudoKey }]; chainSpec.genesis.runtime.session.keys.push(SESSION_KEYS); - + const BALANCE = [ newSudoKey, - chainSpec.genesis.runtime.balances.balances[0][1] + chainSpec.genesis.runtime.balances.balances[0][1], ]; chainSpec.genesis.runtime.balances.balances.push(BALANCE); console.log("changed sudo key:", newSudoKey); } if (initialCollatorsPhrase.length > 0) { - collators.forEach(collatorPhrase => { + collators.forEach((collatorPhrase) => { var collatorAccount = keyring.addFromUri(collatorPhrase); var collator = collatorAccount.address; //adding this condition to prevent adding duplicate keys - if(collatorPhrase!=newSudoKeyPhrase){ + if (collatorPhrase != newSudoKeyPhrase) { console.log("updating collator:", collator); - const sessionKey = [ - collator, - collator, - { aura: collator } - ]; + const sessionKey = [collator, collator, { aura: collator }]; chainSpec.genesis.runtime.session.keys.push(sessionKey); const balance = [ collator, - chainSpec.genesis.runtime.balances.balances[0][1] + chainSpec.genesis.runtime.balances.balances[0][1], ]; chainSpec.genesis.runtime.balances.balances.push(balance); - - chainSpec.genesis.runtime.collatorSelection.invulnerables.push(collator); - } else{ - chainSpec.genesis.runtime.collatorSelection.invulnerables.push(collator); + + chainSpec.genesis.runtime.collatorSelection.invulnerables.push( + collator + ); + } else { + chainSpec.genesis.runtime.collatorSelection.invulnerables.push( + collator + ); } }); - } + } fs.writeFileSync(paraSpecFile, JSON.stringify(chainSpec, null, 2));