diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f2f3cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +stage0-spec.yml +stage0-deployment +stage1-spec.yml +stage1-deployment + +stage1-genesis +output diff --git a/stack-orchestrator/stacks/fixturenet-laconicd/README.md b/stack-orchestrator/stacks/fixturenet-laconicd/README.md index d5c879a..b613b97 100644 --- a/stack-orchestrator/stacks/fixturenet-laconicd/README.md +++ b/stack-orchestrator/stacks/fixturenet-laconicd/README.md @@ -82,7 +82,7 @@ Instructions for running a laconicd fixturenet along with registry CLI and conso cp fixturenet-laconicd-deployment/deployment.yml laconic-console-deployment/deployment.yml ``` -* (Optional) Copy over the genesis file (`.json`) containing the onboarding module state with funded participants to data directory in deployment (`fixturenet-laconicd-deployment/data/genesis-config`): +* (Optional) Copy over an existing genesis file (`.json`) to data directory in deployment (`fixturenet-laconicd-deployment/data/genesis-config`): ```bash # Example @@ -97,6 +97,7 @@ Instructions for running a laconicd fixturenet along with registry CLI and conso # Optional # Set to true to enable adding participants functionality of the onboarding module + # (default: false) ONBOARDING_ENABLED= ``` diff --git a/stack-orchestrator/stacks/fixturenet-laconicd/scripts/fetch-account-holdings.sh b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/fetch-account-holdings.sh new file mode 100755 index 0000000..3a90a3b --- /dev/null +++ b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/fetch-account-holdings.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Exit on error +set -e + +# Note: Needs to be run in the go-nitro repository +# Example usage: +# In go-nitro +# /home/user/fixturenet-laconicd-stack/stack-orchestrator/stacks/fixturenet-laconicd/scripts/fetch-account-holdings.sh eth-account-holdings.json + +# Check if output file is provided +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +OUTPUT_FILE=$1 + +# Run the nitro-rpc-client command and process the output with jq +npm exec -c "nitro-rpc-client get-all-ledger-channels -p 4006" | jq -s '[.[] | {ethereum_address: .Balance.Them, balance: .Balance.TheirBalance}]' > "$OUTPUT_FILE" + +echo "Ethereum account holdings exported to $OUTPUT_FILE" diff --git a/stack-orchestrator/stacks/fixturenet-laconicd/scripts/generate-stage1-genesis.sh b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/generate-stage1-genesis.sh new file mode 100755 index 0000000..15f3fc2 --- /dev/null +++ b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/generate-stage1-genesis.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Exit on error +set -e + +# Prerequisite: a json file with ETH accounts holdings + +# Check if output file is provided +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +STAGE0_DEPLOYMENT_DIR="$1" +ETH_ACCOUNT_HOLDINGS_FILE="$2" + +STAGE1_GENESIS_DIR=stage1-genesis + +# Create a target directory +mkdir -p $STAGE1_GENESIS_DIR + +# Export onboarding module state from stage0 laconicd chain +onboarding_state_file="$STAGE1_GENESIS_DIR/stage0-onboarding-state.json" +docker run -it \ + -v ${STAGE0_DEPLOYMENT_DIR}/data/laconicd-data:/root/stage0-deployment/.laconicd \ + cerc/laconic2d:local bash -c "laconicd export --home /root/stage0-deployment/.laconicd" \ + | jq .app_state.onboarding > "$onboarding_state_file" + +eth_account_holdings=$(cat "$ETH_ACCOUNT_HOLDINGS_FILE") +stage0_participants=$(cat "$onboarding_state_file" | jq .participants) + +# For each participant, using ETH account holdings, figure out balance allocation for stage1 +# TODO Check what happens if participant is missing +stage1_allocations_file="$STAGE1_GENESIS_DIR/stage1-allocations.json" +jq -n --argjson holdings "$eth_account_holdings" --argjson participants "$stage0_participants" ' +[ + $holdings[] | + . as $holding | + $participants[] | + select((.ethereum_address | ascii_downcase) == ($holding.ethereum_address | ascii_downcase)) | + { + cosmos_address: .cosmos_address, + balance: $holding.balance + } +]' > $stage1_allocations_file + +# Run a script with cerc/laconic2d:local to generate the genesis file +# with onboarding module state and given allocations +docker run -it \ + -v ./stage1-genesis:/root/.laconicd \ + -v ./scripts:/scripts \ + cerc/laconic2d:local bash -c "/scripts/genesis.sh" + +# Copy over the genesis file to output folder +OUTPUT_DIR=output +mkdir -p $OUTPUT_DIR +cp ./stage1-genesis/config/genesis.json $OUTPUT_DIR/genesis.json +echo "Genesis file for stage1 written to $OUTPUT_DIR/genesis.json" + +# Clean up +echo "Please remove the temporary data directory: sudo rm -rf stage1-genesis" diff --git a/stack-orchestrator/stacks/fixturenet-laconicd/scripts/genesis.sh b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/genesis.sh new file mode 100755 index 0000000..554a4ea --- /dev/null +++ b/stack-orchestrator/stacks/fixturenet-laconicd/scripts/genesis.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Exit on error +set -e + +# Note: Needs to be run in a docker container with image cerc/laconic2d:local + +CHAINID="laconic_9000-1" +STAGE1_MONIKER=localtestnet-stage-1 +NODE_HOME="/root/.laconicd" + +onboarding_state_file="$NODE_HOME/stage0-onboarding-state.json" +stage1_allocations_file="$NODE_HOME/stage1-allocations.json" +stage1_genesis_file="$NODE_HOME/config/genesis.json" + +laconicd init $STAGE1_MONIKER --chain-id $CHAINID --default-denom photon + +# Update onboarding module state with participants from stage0 +participants=$(jq -c '.participants' $onboarding_state_file) +jq --argjson p "$participants" '.app_state.onboarding.participants = $p' "$stage1_genesis_file" > tmp.$$.json && mv tmp.$$.json "$stage1_genesis_file" + +# Read and assign allocations +jq -c '.[]' "$stage1_allocations_file" | while IFS= read -r line; do + cosmos_address=$(jq -r '.cosmos_address' <<< "$line") + balance=$(jq -r '.balance' <<< "$line") + + # Add a genesis account for participant's laconic address with required balance + laconicd genesis add-genesis-account $cosmos_address ${balance}photon +done + +# Ensure that resulting genesis file is valid +laconicd genesis validate