diff --git a/local.json b/local.json index f840a01..c2020a6 100644 --- a/local.json +++ b/local.json @@ -17,11 +17,11 @@ }, "para": { - "acala": { + "bifrost": { "nodes": [ { "name": "alice", - "node-type": "collatoral" + "node-type": "collator" }, { "name": "bob", diff --git a/main.star b/main.star index 1df8d7e..33724e5 100644 --- a/main.star +++ b/main.star @@ -5,12 +5,10 @@ def run(plan, args): plan.upload_files(src = "./parachain/static_files/configs", name = "configs") service_details = {"relaychains": {}, "parachains": {}} if args["chain-type"] == "local": - service_details["relaychains"] = relay_chain.start_relay_chains_local(plan, args) + relay_chain_details = relay_chain.start_relay_chains_local(plan, args) + service_details["relaychains"] = relay_chain_details - image = "bifrostnetwork/bifrost:bifrost-v0.9.66" - binary = "/usr/local/bin/bifrost" - chain_name = "bifrost-local" - parachain.create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, para_id = 200) + parachain.start_nodes(plan, args, relay_chain_details["relay_service_alice"].ip_address) else: relay_chain.start_relay_chain(plan, args) diff --git a/package_io/build-spec.star b/package_io/build-spec.star index e39ae17..e7b325a 100644 --- a/package_io/build-spec.star +++ b/package_io/build-spec.star @@ -5,20 +5,24 @@ def create_service_for_build_spec(plan, service_name, image, build_file): if build_file != None: files["/build"] = build_file - plan.add_service( + service = plan.add_service( name = service_name, config = ServiceConfig( image = image, files = files, - entrypoint = ["bin/sh"], + entrypoint = ["/bin/sh"], ), ) + return service + def create_edit_and_build_spec(plan, service_name, image, chain_name, command, build_file): - create_service_for_build_spec(plan, service_name, image, build_file) + service = create_service_for_build_spec(plan, service_name, image, build_file) plan.exec(service_name = service_name, recipe = command) plan.store_service_files(service_name = service_name, src = "/tmp/{0}.json".format(chain_name), name = service_name) - plan.stop_service(service_name) + plan.remove_service(service_name) + + return service diff --git a/package_io/constant.star b/package_io/constant.star index c0b0784..89ea0fa 100644 --- a/package_io/constant.star +++ b/package_io/constant.star @@ -2,3 +2,5 @@ PLAIN_BUILD_SPEC = "plain-build-spec" RAW_BUILD_SPEC = "raw-build-spec" EDIT_BUILD_SPEC = "edit-build-spec" CURL_JQ_IMAGE = "badouralix/curl-jq" +NODE_IMAGE = "node:21.1" +PARA_SLOT_REGISTER_SERVICE_NAME = "para-slot-registration" diff --git a/parachain/build-spec.star b/parachain/build-spec.star new file mode 100644 index 0000000..43f1882 --- /dev/null +++ b/parachain/build-spec.star @@ -0,0 +1,55 @@ +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): + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "{0} build-spec --chain={1} --disable-default-bootnode > /tmp/{2}.json".format(binary, chain_base, chain_name), + ]) + build_spec.create_edit_and_build_spec(plan, constant.PLAIN_BUILD_SPEC, image, chain_name, command, build_file = None) + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "sed -e 's/\"parachainId\": *[0-9]\\+/\"parachainId\": {0}/' -e 's/\"para_id\": [0-9]*,/\"para_id\": {0},/' -e 's/\"paraId\": [0-9]*,/\"paraId\": {0},/' /build/{1}.json > /tmp/{1}.json".format(para_id, chain_name), + ]) + build_spec.create_edit_and_build_spec(plan, constant.EDIT_BUILD_SPEC, constant.CURL_JQ_IMAGE, chain_name, command, constant.PLAIN_BUILD_SPEC) + raw_service = create_raw_build_spec_genisis_state_genisis_wasm(plan, binary, image, chain_name, constant.EDIT_BUILD_SPEC) + + return raw_service + +def create_raw_build_spec_genisis_state_genisis_wasm(plan, binary, image, chain_name, build_file): + raw_service = build_spec.create_service_for_build_spec(plan, constant.RAW_BUILD_SPEC, image, build_file) + + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "{0} build-spec --chain=/build/{1}.json --raw --disable-default-bootnode > /tmp/{1}-raw.json".format(binary, chain_name), + ]) + plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "{0} export-genesis-wasm --chain=/tmp/{1}-raw.json > /tmp/{1}-genesis-wasm".format(binary, chain_name), + ]) + plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "{0} export-genesis-state --chain=/tmp/{1}-raw.json > /tmp/{1}-genesis-state".format(binary, chain_name), + ]) + plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + + command = ExecRecipe(command = [ + "bin/sh", + "-c", + "cp /build/{0}.json /tmp/{0}.json".format(chain_name), + ]) + plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + + plan.store_service_files(service_name = constant.RAW_BUILD_SPEC, src = "/tmp/*", name = constant.RAW_BUILD_SPEC) + plan.stop_service(constant.RAW_BUILD_SPEC) + + return raw_service diff --git a/parachain/acala.star b/parachain/local-setup/acala.star similarity index 100% rename from parachain/acala.star rename to parachain/local-setup/acala.star diff --git a/parachain/ajuna.star b/parachain/local-setup/ajuna.star similarity index 100% rename from parachain/ajuna.star rename to parachain/local-setup/ajuna.star diff --git a/parachain/altair.star b/parachain/local-setup/altair.star similarity index 100% rename from parachain/altair.star rename to parachain/local-setup/altair.star diff --git a/parachain/bifrost.star b/parachain/local-setup/bifrost.star similarity index 100% rename from parachain/bifrost.star rename to parachain/local-setup/bifrost.star diff --git a/parachain/centrifuge.star b/parachain/local-setup/centrifuge.star similarity index 100% rename from parachain/centrifuge.star rename to parachain/local-setup/centrifuge.star diff --git a/parachain/clover.star b/parachain/local-setup/clover.star similarity index 100% rename from parachain/clover.star rename to parachain/local-setup/clover.star diff --git a/parachain/encointer.star b/parachain/local-setup/encointer.star similarity index 100% rename from parachain/encointer.star rename to parachain/local-setup/encointer.star diff --git a/parachain/frequency.star b/parachain/local-setup/frequency.star similarity index 100% rename from parachain/frequency.star rename to parachain/local-setup/frequency.star diff --git a/parachain/integritee.star b/parachain/local-setup/integritee.star similarity index 100% rename from parachain/integritee.star rename to parachain/local-setup/integritee.star diff --git a/parachain/interlay.star b/parachain/local-setup/interlay.star similarity index 100% rename from parachain/interlay.star rename to parachain/local-setup/interlay.star diff --git a/parachain/karura.star b/parachain/local-setup/karura.star similarity index 100% rename from parachain/karura.star rename to parachain/local-setup/karura.star diff --git a/parachain/khala.star b/parachain/local-setup/khala.star similarity index 100% rename from parachain/khala.star rename to parachain/local-setup/khala.star diff --git a/parachain/kilt.star b/parachain/local-setup/kilt.star similarity index 100% rename from parachain/kilt.star rename to parachain/local-setup/kilt.star diff --git a/parachain/kintsungi.star b/parachain/local-setup/kintsungi.star similarity index 100% rename from parachain/kintsungi.star rename to parachain/local-setup/kintsungi.star diff --git a/parachain/kylin.star b/parachain/local-setup/kylin.star similarity index 100% rename from parachain/kylin.star rename to parachain/local-setup/kylin.star diff --git a/parachain/litentry.star b/parachain/local-setup/litentry.star similarity index 100% rename from parachain/litentry.star rename to parachain/local-setup/litentry.star diff --git a/parachain/mangata.star b/parachain/local-setup/mangata.star similarity index 100% rename from parachain/mangata.star rename to parachain/local-setup/mangata.star diff --git a/parachain/manta.star b/parachain/local-setup/manta.star similarity index 100% rename from parachain/manta.star rename to parachain/local-setup/manta.star diff --git a/parachain/moonbeam.star b/parachain/local-setup/moonbeam.star similarity index 100% rename from parachain/moonbeam.star rename to parachain/local-setup/moonbeam.star diff --git a/parachain/moonsama.star b/parachain/local-setup/moonsama.star similarity index 100% rename from parachain/moonsama.star rename to parachain/local-setup/moonsama.star diff --git a/parachain/nodle.star b/parachain/local-setup/nodle.star similarity index 100% rename from parachain/nodle.star rename to parachain/local-setup/nodle.star diff --git a/parachain/parallel.star b/parachain/local-setup/parallel.star similarity index 100% rename from parachain/parallel.star rename to parachain/local-setup/parallel.star diff --git a/parachain/pendulum.star b/parachain/local-setup/pendulum.star similarity index 100% rename from parachain/pendulum.star rename to parachain/local-setup/pendulum.star diff --git a/parachain/polkadex.star b/parachain/local-setup/polkadex.star similarity index 100% rename from parachain/polkadex.star rename to parachain/local-setup/polkadex.star diff --git a/parachain/robonomics.star b/parachain/local-setup/robonomics.star similarity index 100% rename from parachain/robonomics.star rename to parachain/local-setup/robonomics.star diff --git a/parachain/subsocial.star b/parachain/local-setup/subsocial.star similarity index 100% rename from parachain/subsocial.star rename to parachain/local-setup/subsocial.star diff --git a/parachain/subzero.star b/parachain/local-setup/subzero.star similarity index 100% rename from parachain/subzero.star rename to parachain/local-setup/subzero.star diff --git a/parachain/turing.star b/parachain/local-setup/turing.star similarity index 100% rename from parachain/turing.star rename to parachain/local-setup/turing.star diff --git a/parachain/zeitgeist.star b/parachain/local-setup/zeitgeist.star similarity index 100% rename from parachain/zeitgeist.star rename to parachain/local-setup/zeitgeist.star diff --git a/parachain/parachain.star b/parachain/parachain.star index 8408bbe..59726b3 100644 --- a/parachain/parachain.star +++ b/parachain/parachain.star @@ -1,51 +1,47 @@ -build_spec = import_module("../package_io/build-spec.star") +build_spec = import_module("./build-spec.star") +register_para_slot = import_module("./register-para-id.star") constant = import_module("../package_io/constant.star") +parachain_list = import_module("./static_files/images.star") -def create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, para_id): - command = ExecRecipe(command = [ - "bin/sh", - "-c", - "{0} build-spec --chain={1} --disable-default-bootnode > /tmp/{1}.json".format(binary, chain_name), - ]) - build_spec.create_edit_and_build_spec(plan, constant.PLAIN_BUILD_SPEC, image, chain_name, command, build_file = None) - command = ExecRecipe(command = [ - "bin/sh", - "-c", - "sed -e 's/\"parachainId\": *[0-9]\\+/\"parachainId\": {0}/' -e 's/\"para_id\": [0-9]*,/\"para_id\": {0},/' -e 's/\"paraId\": [0-9]*,/\"paraId\": {0},/' /build/{1}.json > /tmp/{1}.json".format(para_id, chain_name), - ]) - build_spec.create_edit_and_build_spec(plan, constant.EDIT_BUILD_SPEC, constant.CURL_JQ_IMAGE, chain_name, command, constant.PLAIN_BUILD_SPEC) - create_raw_build_spec_genisis_state_genisis_wasm(plan, binary, image, chain_name, constant.EDIT_BUILD_SPEC) +def spawn_parachain(plan, chain_name, image, command, build_file): + files = { + "/app": "configs", + } + if build_file != None: + files["/build"] = build_file -def create_raw_build_spec_genisis_state_genisis_wasm(plan, binary, image, chain_name, build_file): - build_spec.create_service_for_build_spec(plan, constant.RAW_BUILD_SPEC, image, build_file) + parachain_node = plan.add_service( + name = "start-{}-node".format(chain_name), + config = ServiceConfig( + image = image, + files = files, + ports = { + "ws": PortSpec(9944, transport_protocol = "TCP"), + }, + entrypoint = command, + ), + ) - command = ExecRecipe(command = [ - "bin/sh", - "-c", - "{0} build-spec --chain=/build/{1}.json --raw --disable-default-bootnode > /tmp/{1}-raw.json".format(binary, chain_name), - ]) - plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + return parachain_node - command = ExecRecipe(command = [ - "bin/sh", - "-c", - "{0} export-genesis-wasm --chain=/tmp/{1}-raw.json > /tmp/{1}-genesis-wasm".format(binary, chain_name), - ]) - plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) +def start_local_parachain_node(plan, parachain, para_id): + parachain_details = parachain_list.parachain_images[parachain] + image = parachain_details["image"] + binary = parachain_details["entrypoint"] + chain_base = parachain_details["base"][0] + chain_name = parachain + raw_service = build_spec.create_parachain_build_spec_with_para_id(plan, image, binary, chain_name, chain_base, para_id) - command = ExecRecipe(command = [ - "bin/sh", + exec_comexec_commandmand = [ + "bin/bash", "-c", - "{0} export-genesis-state --chain=/tmp/{1}-raw.json > /tmp/{1}-genesis-state".format(binary, chain_name), - ]) - plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + "{0} --chain=/build/{1}-raw.json --ws-external --rpc-external --rpc-cors=all --name={1} --collator --rpc-methods=unsafe --force-authoring --execution=wasm --alice -- --chain=/app/raw-rococo-local.json --execution=wasm".format(binary, chain_name), + ] - command = ExecRecipe(command = [ - "bin/sh", - "-c", - "cp /build/{0}.json /tmp/{0}.json".format(chain_name), - ]) - plan.exec(service_name = constant.RAW_BUILD_SPEC, recipe = command) + spawn_parachain(plan, chain_name, image, exec_comexec_commandmand, build_file = raw_service.name) - plan.store_service_files(service_name = constant.RAW_BUILD_SPEC, src = "/tmp/*", name = constant.RAW_BUILD_SPEC) - plan.stop_service(constant.RAW_BUILD_SPEC) +def start_nodes(plan, args, relay_chain_ip): + parachains = args["para"] + for parachain in parachains: + para_id = register_para_slot.register_para_id(plan, relay_chain_ip) + start_local_parachain_node(plan, parachain, para_id) diff --git a/parachain/register-para-id.star b/parachain/register-para-id.star new file mode 100644 index 0000000..bd7b6e7 --- /dev/null +++ b/parachain/register-para-id.star @@ -0,0 +1,11 @@ +build_spec = import_module("../package_io/build-spec.star") +constant = import_module("../package_io/constant.star") + +def register_para_id(plan, alice_ip): + plan.upload_files(src = "./static_files/javascript", name = "javascript") + test = build_spec.create_service_for_build_spec(plan, constant.PARA_SLOT_REGISTER_SERVICE_NAME, constant.NODE_IMAGE, "javascript") + plan.exec(service_name = test.name, recipe = ExecRecipe(command = ["/bin/sh", "-c", "cd /build && npm i "])) + plan.exec(service_name = test.name, recipe = ExecRecipe(command = ["/bin/sh", "-c", "cd /build && node register ws://{0}:9944 //Alice ".format(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"] diff --git a/parachain/static_files/images.star b/parachain/static_files/images.star index 3c7a57c..67b0a45 100644 --- a/parachain/static_files/images.star +++ b/parachain/static_files/images.star @@ -8,7 +8,7 @@ parachain_images = { "ajuna": { "image": "ajuna/parachain-ajuna:latest", "entrypoint": "/usr/local/bin/ajuna", - "base": ["local","testnet","mainnet"] + "base": ["dev","testnet","mainnet"] }, "bifrost": { diff --git a/parachain/static_files/javascript/package.json b/parachain/static_files/javascript/package.json new file mode 100644 index 0000000..edad65e --- /dev/null +++ b/parachain/static_files/javascript/package.json @@ -0,0 +1,15 @@ +{ + "name": "kurtosis-polkadot", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "register": "node register.js" + }, + "keywords": [], + "dependencies": { + "@polkadot/api": "^7.6.1", + "@polkadot/util": "^8.7.1", + "@polkadot/util-crypto": "^8.3.3" + } +} diff --git a/parachain/static_files/javascript/register.js b/parachain/static_files/javascript/register.js new file mode 100644 index 0000000..bea2234 --- /dev/null +++ b/parachain/static_files/javascript/register.js @@ -0,0 +1,49 @@ +const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api"); +const fs = require("fs"); + +const run = async () => { + try { + const endpoint = process.argv[2]; + const seed = process.argv[3]; + + const wsProvider = new WsProvider(endpoint); + + const api = await ApiPromise.create({ + provider: wsProvider, + }); + const keyring = new Keyring({ type: "sr25519" }); + const nextParaId = await ( + await api.query.registrar.nextFreeParaId() + ).toString(); + console.log("para_id: ", nextParaId); + const alice = keyring.addFromUri(seed); + + await reservePara(alice, api, nextParaId); + + const jsonData = parseInt(nextParaId); + fs.writeFile("/tmp/para.json", nextParaId, (writeErr) => { + if (writeErr) { + console.error("Error writing to the file:", writeErr); + } else { + console.log("File has been replaced."); + } + }); + } catch (error) { + console.log("error:", error); + } +}; + +const reservePara = async (account, api, paraID) => { + const nonce = Number((await api.query.system.account(account.address)).nonce); + // Reserve Parachain ID + let signAndSend = await api.tx.registrar + .reserve() + .signAndSend(account, { nonce: nonce }, async ({ status }) => { + if (status.isFinalized) { + signAndSend(); + process.exit(); + } + }); +}; + +run(); diff --git a/relaychain/relay-chain.star b/relaychain/relay-chain.star index 4c825d5..fd56f02 100644 --- a/relaychain/relay-chain.star +++ b/relaychain/relay-chain.star @@ -33,7 +33,7 @@ def start_relay_chains_local(plan, args): return relay_detail def start_relay_chain_local(plan, name, port): - exec_command = ["bin/sh", "-c", "polkadot --base-path=/data --chain=/app/rococo-local.json --validator --ws-external --rpc-external --rpc-cors=all --name=alice --{0} --rpc-methods=unsafe --execution=wasm".format(name)] + exec_command = ["bin/sh", "-c", "polkadot --base-path=/data --chain=/app/raw-rococo-local.json --validator --ws-external --rpc-external --rpc-cors=all --name=alice --{0} --rpc-methods=unsafe --execution=wasm".format(name)] service_details = plan.add_service( name = "polkadot-{0}".format(name), config = ServiceConfig(