From 42c899b664ad2ff5a6fc9000164d2d9bee852012 Mon Sep 17 00:00:00 2001 From: abhiyana Date: Mon, 12 Feb 2024 22:54:14 +0530 Subject: [PATCH] refactor: update script to update parachain spec --- main.star | 6 +-- parachain/build-spec.star | 1 + parachain/parachain.star | 5 ++- .../javascript/edit_parachain_plain.js | 43 ++++++++++++------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/main.star b/main.star index 3cd7b0c..58c0ea3 100644 --- a/main.star +++ b/main.star @@ -8,7 +8,7 @@ utils = import_module("./package_io/utils.star") constant = import_module("./package_io/constant.star") parachain_without_registration = import_module("./parachain/parachain_without_registration.star") -def run(plan, chain_type = "localnet", relaychain = None, parachains = None, explorer = False, without_registration = False, sudo_key=None): +def run(plan, chain_type = "localnet", relaychain = None, parachains = None, explorer = False, without_registration = False): """ Main function to run the Polkadot relay and parachain setup. @@ -31,10 +31,10 @@ def run(plan, chain_type = "localnet", relaychain = None, parachains = None, exp Returns: service_details (json): Service details containing information about relay chains, parachains, and Prometheus. """ - service_details = run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer, without_registration, sudo_key) + service_details = run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer, without_registration) return service_details -def run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer, without_registration, sudo_key): +def run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer, without_registration): """ Main function to run the Polkadot relay and parachain setup. diff --git a/parachain/build-spec.star b/parachain/build-spec.star index d1d153c..fbfdfdf 100644 --- a/parachain/build-spec.star +++ b/parachain/build-spec.star @@ -48,3 +48,4 @@ def create_raw_build_spec_genisis_state_genisis_wasm_for_parachain(plan, binary, ) return chain_name + "raw" + \ No newline at end of file diff --git a/parachain/parachain.star b/parachain/parachain.star index 107923d..3005d56 100644 --- a/parachain/parachain.star +++ b/parachain/parachain.star @@ -25,9 +25,9 @@ def start_local_parachain_node(plan, chain_type, parachain, para_id): sudo_key = "" if len(parachain["sudo_key"]) != 0: - sudo_key = parachain["sudo_key"]["public_key"] + sudo_key = parachain["sudo_key"]["private_phrase"] - public_keys = [node.get("key", {}).get("public_key", "") for node in parachain["nodes"] if node.get("key")] + public_keys = [node.get("key", {}).get("private_phrase", "") for node in parachain["nodes"] if node.get("key")] collators_keys = "[" + ", ".join(["\"{}\"".format(key) for key in public_keys]) + "]" raw_service = build_spec.create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, chain_base, para_id, sudo_key, collators_keys) @@ -97,6 +97,7 @@ def start_local_parachain_node(plan, chain_type, parachain, para_id): if len(node["key"]) != 0: insert_keys(plan, "aura", node["key"]["private_phrase"], parachain_detail["endpoint"]) + return parachain_final def start_nodes(plan, chain_type, parachains, relay_chain_ip): diff --git a/parachain/static_files/javascript/edit_parachain_plain.js b/parachain/static_files/javascript/edit_parachain_plain.js index fe45bdf..9a667a4 100644 --- a/parachain/static_files/javascript/edit_parachain_plain.js +++ b/parachain/static_files/javascript/edit_parachain_plain.js @@ -1,37 +1,48 @@ const fs = require("fs"); +const { Keyring } = require('@polkadot/keyring'); +const { encodeAddress, cryptoWaitReady } = require('@polkadot/util-crypto'); -function updateParachainSpec(PARA_SPEC_FILE, PARA_ID, NEW_SUDO_KEY, INITIAL_COLLATORS) { +async function updateParachainSpec(paraSpecFile, paraId, newSudoKeyPhrase, initialCollatorsPhrase) { try { - const rawdata = fs.readFileSync(PARA_SPEC_FILE); + const rawdata = fs.readFileSync(paraSpecFile); const chainSpec = JSON.parse(rawdata); - const collators = JSON.parse(INITIAL_COLLATORS); + const collators = JSON.parse(initialCollatorsPhrase); - chainSpec.para_id = PARA_ID; - chainSpec.genesis.runtime.parachainInfo.parachainId = PARA_ID; + chainSpec.para_id = paraId; + chainSpec.genesis.runtime.parachainInfo.parachainId = paraId; - if (NEW_SUDO_KEY.length > 0) { - chainSpec.genesis.runtime.sudo.key = NEW_SUDO_KEY; + await cryptoWaitReady(); + 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 = [ - NEW_SUDO_KEY, - NEW_SUDO_KEY, - { aura: NEW_SUDO_KEY } + newSudoKey, + newSudoKey, + { aura: newSudoKey } ]; chainSpec.genesis.runtime.session.keys.push(SESSION_KEYS); const BALANCE = [ - NEW_SUDO_KEY, + newSudoKey, chainSpec.genesis.runtime.balances.balances[0][1] ]; chainSpec.genesis.runtime.balances.balances.push(BALANCE); - console.log("changed sudo key:", NEW_SUDO_KEY); + console.log("changed sudo key:", newSudoKey); } - if (collators.length > 0) { - collators.forEach(collator => { + if (initialCollatorsPhrase.length > 0) { + collators.forEach(collatorPhrase => { + var collatorAccount = keyring.addFromUri(collatorPhrase); + var collator = collatorAccount.address; //adding this condition to prevent adding duplicate keys - if(collator!=NEW_SUDO_KEY){ + if(collatorPhrase!=newSudoKeyPhrase){ console.log("updating collator:", collator); const sessionKey = [ collator, @@ -53,7 +64,7 @@ function updateParachainSpec(PARA_SPEC_FILE, PARA_ID, NEW_SUDO_KEY, INITIAL_COLL }); } - fs.writeFileSync(PARA_SPEC_FILE, JSON.stringify(chainSpec, null, 2)); + fs.writeFileSync(paraSpecFile, JSON.stringify(chainSpec, null, 2)); console.log("✓ Updated sudo key and session keys in parachain spec"); } catch (error) {