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: implement script for automating the obtaining of paraId slot #39

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
4 changes: 2 additions & 2 deletions local.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
},

"para": {
"acala": {
"bifrost": {
"nodes": [
{
"name": "alice",
"node-type": "collatoral"
"node-type": "collator"
},
{
"name": "bob",
Expand Down
8 changes: 3 additions & 5 deletions main.star
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions package_io/build-spec.star
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions package_io/constant.star
Original file line number Diff line number Diff line change
Expand Up @@ -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"
55 changes: 55 additions & 0 deletions parachain/build-spec.star
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 38 additions & 42 deletions parachain/parachain.star
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions parachain/register-para-id.star
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion parachain/static_files/images.star
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 15 additions & 0 deletions parachain/static_files/javascript/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
49 changes: 49 additions & 0 deletions parachain/static_files/javascript/register.js
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion relaychain/relay-chain.star
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down