Skip to content

Commit

Permalink
Revert "chore: update the javascript for editing the plain build spec"
Browse files Browse the repository at this point in the history
This reverts commit 15248c1.
  • Loading branch information
shanithkk committed Mar 27, 2024
1 parent 15248c1 commit 71b943f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 49 deletions.
46 changes: 38 additions & 8 deletions parachain/build-spec.star
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
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",
Expand All @@ -16,22 +49,19 @@ 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 = run_command,
image = constant.NODE_IMAGE,
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,
files = files,
store = [StoreSpec(src = "/build/{0}.json".format(chain_name), name = chain_name + "edit")],
store = [StoreSpec(src = "/tmp/{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",
Expand Down
71 changes: 30 additions & 41 deletions parachain/static_files/javascript/edit_parachain_plain.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,68 @@
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);

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.para_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));

Expand Down

0 comments on commit 71b943f

Please sign in to comment.