Skip to content

Instantly share code, notes, and snippets.

@woss
Last active March 3, 2023 18:07
Show Gist options
  • Save woss/516609f19d9126e936fa172a3b9544cd to your computer and use it in GitHub Desktop.
Save woss/516609f19d9126e936fa172a3b9544cd to your computer and use it in GitHub Desktop.
Use docker to generate spec for the parachain and create the needed files. This script expects OCI has the CMD responding with the binary. You can also use it to generate relaychain specs
#!/usr/bin/env bash
#########################################################################################################################
## Make it excutable
## chmod +x create.sh
##
## Parachain example
## ./create.sh --chain local --image registry.gitlab.com/anagolay/anagolay/idiyanale:3861d9b3 --parachain 4222
##
## Relaychain example
## ./create.sh --chain rococo-local --image parity/polkadot:v0.9.38 --relay
#########################################################################################################################
##### this part taken from https://stackoverflow.com/a/20816534/2764898
SCRIPTNAME="${0##*/}"
OUTPUT_DIR="./parachain_out"
warn() {
printf '%s \n' "$1"
}
iscmd() {
command -v "$@" >&-
}
checkdeps() {
local -i not_found
for cmd; do
iscmd "$cmd" || {
warn $"$cmd is not found"
((not_found++))
}
done
((not_found == 0)) || {
warn $"Install dependencies listed above to use $SCRIPTNAME"
exit 1
}
}
### deps check
checkdeps docker docker-compose jq wget
#######
# Convenience functions.
usage_error() {
echo >&2 "$(basename "$0"): $1"
exit 2
}
function show_help() {
USAGE="Usage: ${CMD:=${0##*/}} [(-h|--help)] [--chain=TEXT] [--parachain=NUMBER] [--image=TEXT]"
echo "Anagolay Collator and RelayChain Helper Script"
echo ""
printf "%s\n" "$USAGE"
echo ""
echo "Relaychain params, all mandatory"
echo " --relay - this will not require --parachain since it is expected to build relaychain spec"
echo ""
echo "Parachain params, all mandatory"
echo " --chain - chain spec name. Possible values: local, testnet, ..."
echo " --image - OCI name. If provided in short form it will use docker hub"
echo ""
echo "Optional params"
echo " --parachain - prachain ID. You obtain this through reserving it, or sudo-ing the local relay chain"
echo ""
echo "Common flags"
echo " -h | --help - Shows this help"
echo ""
exit 0
}
assert_argument() { test "$1" != "$EOL" || usage_error "$2 requires an argument"; }
# shamelessly taken and modified from https://stackoverflow.com/a/62616466/2764898
# One loop, nothing more.
if [ "$#" != 0 ]; then
EOL=$(printf '\1\3\3\7')
set -- "$@" "$EOL"
while [ "$1" != "$EOL" ]; do
opt="$1"
shift
case "$opt" in
# Your options go here.
-h | --help)
show_help
;;
--chain)
assert_argument "$1" "$opt"
chain="$1"
shift
;;
--image)
assert_argument "$1" "$opt"
image="$1"
shift
;;
--parachain)
assert_argument "$1" "$opt"
parachain="$1"
shift
;;
# THIS PART IS TO GENERATE RELAYCHAIN SPEC
--relay) isRelay='true' ;;
# Arguments processing. You may remove any unneeded line after the 1st.
--*=*) set -- "${opt%%=*}" "${opt#*=}" "$@" ;; # convert '--name=arg' to '--name' 'arg'
--) while [ "$1" != "$EOL" ]; do
set -- "$@" "$1"
shift
done ;; # process remaining arguments as positional
-*) usage_error "unknown option: '$opt'" ;; # catch misspelled options
*) usage_error "this should NEVER happen ($opt)" ;; # sanity test for previous patterns
esac
done
shift # $EOL
fi
if [ -n "${parachain}" ] && [ -n "${isRelay}" ]; then
echo "❗ You cannnot have --parachain and --relay together. pick one!"
exit 1
fi
function create_spec() {
mkdir -p $OUTPUT_DIR
# if has --relay flag
if [ "${isRelay}" == "true" ]; then
specfileName="relay-chain-spec-$chain.json"
else
specfileName="chain-spec-$parachain.json"
fi
if test -f "$OUTPUT_DIR/$specfileName"; then
echo " "
echo "================================================"
echo " Spec exist."
echo " Open it and modify it according to your needs."
echo " Then come back here and continue."
echo "================================================"
echo " "
else
echo "Generating the spec file in $OUTPUT_DIR/$specfileName"
cmd="docker run --rm $image build-spec --chain=$chain --disable-default-bootnode > $OUTPUT_DIR/$specfileName"
echo "$cmd"
docker run --rm "$image" build-spec --chain="$chain" --disable-default-bootnode >"$OUTPUT_DIR/$specfileName"
echo " "
echo "================================================"
echo " Spec generated."
echo " Open it and modify it according to your needs."
echo " Then come back here and continue."
echo " DO NOT RUN THIS COMAND AGAIN YOU WILL LOSE ALL YOUR WORK"
echo "================================================"
echo " "
fi
}
function create_raw() {
echo "Generating RAW spec, verification code and genesis state."
# if has --relay flag
if [ "${isRelay}" == "true" ]; then
specfileName="relay-chain-spec-$chain.json"
rawName="relay-chain-raw-$chain.json"
else
specfileName="chain-spec-$parachain.json"
rawName="chain-raw-$parachain.json"
wasmFilename="para-$parachain-wasm.txt"
stateFilename="para-$parachain-state.txt"
fi
echo " build-spec --raw"
docker run -v "$(pwd)/$OUTPUT_DIR":/app --rm "$image" build-spec --raw --chain=/app/"$specfileName" --disable-default-bootnode >"$OUTPUT_DIR/$rawName"
if [ ! "${isRelay}" == "true" ]; then
echo " export-genesis-wasm"
docker run -v "$(pwd)/$OUTPUT_DIR":/app --rm "$image" export-genesis-wasm --chain=/app/"$rawName" >"$OUTPUT_DIR/$wasmFilename"
echo " verificationCode: $OUTPUT_DIR/$wasmFilename"
echo " export-genesis-state"
docker run -v "$(pwd)/$OUTPUT_DIR":/app --rm "$image" export-genesis-state --chain=/app/"$rawName" >"$OUTPUT_DIR/$stateFilename"
echo " genesisState: $OUTPUT_DIR/$stateFilename"
fi
## exit here because of the bash select will not exit
exit 0
}
if [[ -n "${chain}" ]]; then
if [[ -z "${image}" ]]; then
echo "image is not provided. Please add --image flag with value [--image anagolay/idiyanale:221121]"
exit 1
fi
if [ ! "${isRelay}" == "true" ]; then
if [[ -z "${parachain}" ]]; then
echo "parachain ID is not provided. Please add --parachain flag with value [--parachain 4222]"
exit 1
fi
fi
create_spec
echo "I have updated spec file, please generate raw genesis."
select yn in "Yes" "No"; do
case $yn in
Yes) create_raw ;;
No) exit ;;
esac
done
fi
@woss
Copy link
Author

woss commented Mar 3, 2023

https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L68-L69 - explain what is it used for

https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L50-L64

function show_help() {
  USAGE="Usage:  ${CMD:=${0##*/}} [(-h|--help)] [--chain=TEXT] [--parachain=NUMBER] [--image=TEXT]"

  echo "Anagolay Collator Helper Script"
  echo ""
  printf "%s\n" "$USAGE"
  echo ""
  echo "  --chain             - relay chain spec name. Possible values: local, testnet, polkadot, kusama, etc."
  echo "  --image             - OCI name. If provided without registry domain it will use docker hub."
  echo "  --parachain         - parachain ID. You obtain this through reserving it or sudo-ing the local relay chain."
  echo ""
  echo "  -h | --help         - Shows this help."
  echo ""
  exit 0
}

also, it would be useful to add a link how to get a parachain ID

the first link you shared --- this code i didn't write, i took it from the link provided above, my shell-scripting knowledge is not that level yet :)

The last link and comment -- this is a script that assumes a person already knows how to get the paraId.

@woss
Copy link
Author

woss commented Mar 3, 2023

Why I still can't suggest my code here lol...

No proper description what and where is it used for. It should run only in a context with a Dockerfile with a polkadot bin.

https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L35 - not cool

https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L177 and https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L181 - excessive double quotes

https://gist.github.com/woss/516609f19d9126e936fa172a3b9544cd#file-create-parachain-spec-sh-L96 - seems to be unreachable.

I fixed only the deletion of history, the double quotes are passing shell lint so o prob there.

@woss
Copy link
Author

woss commented Mar 3, 2023

latest revision brings relaychain specs generation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment