From 1fe55add3f52d9843eeaae5325f463517a069fb4 Mon Sep 17 00:00:00 2001 From: gonzalezzfelipe Date: Tue, 19 Nov 2024 10:58:44 -0300 Subject: [PATCH 1/2] chore: Add prepare online node script --- crates/rpc/src/bin/open_head.rs | 12 +- crates/rpc/src/model/cluster/node.rs | 5 +- scripts/README.md | 44 +++++++ scripts/prepare_online_node.sh | 184 +++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 scripts/README.md create mode 100755 scripts/prepare_online_node.sh diff --git a/crates/rpc/src/bin/open_head.rs b/crates/rpc/src/bin/open_head.rs index 1730a4e..bef1ba6 100644 --- a/crates/rpc/src/bin/open_head.rs +++ b/crates/rpc/src/bin/open_head.rs @@ -89,7 +89,7 @@ async fn main() { .try_into() .expect("Failed to get party verification key from file"); - println!("Building init transaction..."); + debug!("Building init transaction..."); let participant_hash = match Address::from_bech32(args.participant.as_str()) .expect("Failed to parse bech32 participant address") @@ -125,7 +125,7 @@ async fn main() { )) .expect("Failed to build tx"); - println!("Tx bytes: {}", hex::encode(built_init_tx.tx_bytes.clone())); + debug!("Tx bytes: {}", hex::encode(built_init_tx.tx_bytes.clone())); let built_init_tx = built_init_tx .sign(admin_key.clone().into()) @@ -141,8 +141,8 @@ async fn main() { .await .expect("Failed to submit init tx"); - println!("Submitted init tx: {}", init_tx_id); - println!("Committing funds..."); + debug!("Submitted init tx: {}", init_tx_id); + debug!("Committing funds..."); let commit_inputs: Vec<(InputWrapper, OutputWrapper)> = join_all(args.commit_inputs.into_iter().map(|input| async { let input: InputWrapper = input.try_into().expect("Failed to parse commit input. Please make sure it uses the following format: {tx_hash}#{index}"); @@ -196,7 +196,7 @@ async fn main() { .sign(admin_key.into()) .expect("Failed to sign commit tx"); - println!( + debug!( "Signed commit tx: {}", hex::encode(built_commit_tx.tx_bytes.clone()) ); @@ -205,5 +205,5 @@ async fn main() { .submit_transaction(built_commit_tx) .await .expect("Failed to submit commit tx"); - println!("Submitted commit tx: {}", commit_tx_id); + debug!("Submitted commit tx: {}", commit_tx_id); } diff --git a/crates/rpc/src/model/cluster/node.rs b/crates/rpc/src/model/cluster/node.rs index 369a168..69dc4a1 100644 --- a/crates/rpc/src/model/cluster/node.rs +++ b/crates/rpc/src/model/cluster/node.rs @@ -1,9 +1,6 @@ use anyhow::{anyhow, Context, Result}; use hex::FromHex; -use pallas::{ - crypto::key::ed25519::SecretKey, - ledger::addresses::{Address, Network}, -}; +use pallas::{crypto::key::ed25519::SecretKey, ledger::addresses::Network}; use reqwest::Url; use serde::{Deserialize, Serialize}; use serde_json::Value; diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..75dc112 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,44 @@ +# Scripts + +## CRD + +The `crd.sh` is there to update the CRD of the HydraDoom resource on the +bootstrapping folder. It dumps the definition generated on the Rust code into +YAML format, that is later formatted into K8s by the `tfk8s` bin. + + +## Prepare online node + +To prepare an online node you must have: + +* Permission to list and upload into `hydradoomsnapshots` bucket. +* `cardano-cli` in your PATH. +* `hydra-node` in your PATH. +* `admin.sk` and `admin.vk` files. +* A Blockfrost API key. +* A Cardano node-socket (`dmtrctl ports tunnel`). + +To run the script: + +```sh +./prepare_online_node.sh \ + --admin-signing-key-file PATH_TO_ADMIN_SK \ + --admin-verification-key-file PATH_TO_ADMIN_VK \ + --blockfrost-key YOUR_BF_API_KEY \ + --node-id NODE_ID \ + --protocol-parameters ../playbook/doom-dev/protocol-parameters.json \ + --cardano-node-socket PATH_TO_YOUR_SOCKET +``` + +The script will: + +1. Query the network to get available UTxOs to use as seed and commit inputs. +2. Generate a hydra key pair to use. +3. Query the network to get current tip. +4. Run `open-head` binary with the corresponding parameters. +5. Start the hydra-node with persistence activated. This process will run until + it is externally killed. The user is supposed to `ctrl-c` once the node has + acknowledged the transaction and the head is open. +6. After the hydra-node process is terminated, a tar file with the keys and + persistence is uploaded to S3. You should see a `node.yml` with the + parameters needed to create this resource on K8s. diff --git a/scripts/prepare_online_node.sh b/scripts/prepare_online_node.sh new file mode 100755 index 0000000..3dad615 --- /dev/null +++ b/scripts/prepare_online_node.sh @@ -0,0 +1,184 @@ +#!/bin/sh + +set -e +# Function to display help message +function show_help() { + echo "Usage: $0 " + echo " --admin-signing-key-file \\" + echo " --admin-verification-key-file \\" + echo " --blockfrost-key \\" + echo " --node-id \\" + echo " --protocol-parameters \\" + echo " --cardano-node-socket " + echo + echo "This expects for you to have cardano-cli and hydra-node in your PATH." + echo + echo "Arguments:" + echo " --admin-signing-key-file Path to the admin signing key file." + echo " --admin-verification-key-file Path to the admin verification key file." + echo " --blockfrost-key Blockfrost API key." + echo " --node-id ID to set to the node being prepared." + echo " --protocol-parameters Protocol parameters file." + echo " --cardano-node-socket Path to socket." + echo + echo "Options:" + echo " --help Display this help message" +} + +# If no arguments are provided, display help +if [ "$#" -eq 0 ]; then + show_help + exit 1 +fi + +# Check for --help flag +if [ "$1" == "--help" ]; then + show_help + exit 0 +fi + +# Check if exactly 12 arguments are provided +if [ "$#" -ne 12 ]; then + echo "Error: Incorrect number of arguments." + show_help + exit 1 +fi + +# Check AWS privileges to upload snapshot +if ! aws s3 ls s3://hydradoomsnapshots/ > /dev/null 2>&1; then + echo "Error: Unable to access S3 bucket. Please check your AWS permissions." + exit 1 +fi + +# Check hydra-node binary is on path +if ! hydra-node --help > /dev/null 2>&1; then + echo "Error: hydra-node not found on PATH." + exit 1 +fi + +# Check cardano-cli is on path +if ! cardano-cli --help > /dev/null 2>&1; then + echo "Error: cardano-cli not found on PATH." + exit 1 +fi + +# Parse arguments +while [ "$#" -gt 0 ]; do + case "$1" in + --admin-signing-key-file) + ADMIN_SIGNING_KEY_FILE="$2" + shift 2 + ;; + --admin-verification-key-file) + ADMIN_VERIFICATION_KEY_FILE="$2" + shift 2 + ;; + --blockfrost-key) + BLOCKFROST_KEY="$2" + shift 2 + ;; + --node-id) + NODE_ID="$2" + shift 2 + ;; + --protocol-parameters) + PROTOCOL_PARAMETERS="$2" + shift 2 + ;; + --cardano-node-socket) + CARDANO_NODE_SOCKET="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + show_help + exit 1 + ;; + esac +done + +echo "---" +echo "" +echo "Getting UTxOs to generate init and commit..." +echo "" +echo "---" +ADDRESS=$(cardano-cli address build --verification-key-file "$ADMIN_VERIFICATION_KEY_FILE" --testnet-magic 1) +SEED_INPUT=$(cardano-cli conway query utxo --address $ADDRESS --output-json --testnet-magic 1 --socket-path $CARDANO_NODE_SOCKET | jq -r 'to_entries[0].key') +COMMIT_INPUT=$(cardano-cli conway query utxo --address $ADDRESS --output-json --testnet-magic 1 --socket-path $CARDANO_NODE_SOCKET | jq -r 'to_entries[1].key') + +echo "* Seed input: $SEED_INPUT" +echo "* Commit input: $COMMIT_INPUT" + +# Generate key pair. +mkdir keys +hydra-node gen-hydra-key --output-file keys/hydra + +# Get current SLOT.HASH +START_POINT=$(cardano-cli query tip --socket-path socket --testnet-magic 1 | jq -r '"\(.slot).\(.hash)"') + +echo "---" +echo "" +echo "Opening head..." +echo "" +echo "---" +# Open head +cargo run --bin open-head -- \ + --network-id 0 \ + --seed-input $SEED_INPUT \ + --participant $ADDRESS \ + --party-verification-file keys/hydra.vk \ + --cardano-key-file $ADMIN_SIGNING_KEY_FILE \ + --blockfrost-key $BLOCKFROST_KEY \ + --commit-inputs $COMMIT_INPUT + +# Start hydra-node in the background. +echo "---" +echo "" +echo "Running hydra-node with persistence. You should terminate this process when the head is already opened." +echo "" +echo "---" + +set +e +hydra-node \ + --node-id $NODE_ID \ + --persistence-dir persistence \ + --cardano-signing-key $ADMIN_SIGNING_KEY_FILE \ + --hydra-signing-key keys/hydra.sk \ + --hydra-scripts-tx-id 03f8deb122fbbd98af8eb58ef56feda37728ec957d39586b78198a0cf624412a \ + --ledger-protocol-parameters $PROTOCOL_PARAMETERS \ + --testnet-magic 1 \ + --node-socket $CARDANO_NODE_SOCKET \ + --api-port 4001 \ + --host 0.0.0.0 \ + --api-host 0.0.0.0 \ + --port 5001 \ + --start-chain-from $START_POINT +set -e + +echo "---" +echo "" +echo "Uploading tar..." +echo "" +echo "---" +tar -czvf "$NODE_ID.tar.gz" persistence keys +aws s3 cp "$NODE_ID.tar.gz" s3://hydradoomsnapshots/ + +echo "To run online node, apply the following:" +echo "---" +FILE=$(cat < node.yml + +rm -rf persistence keys From e54fd42f2d21265bd633d6ea4651ffcefe36c84c Mon Sep 17 00:00:00 2001 From: gonzalezzfelipe Date: Wed, 20 Nov 2024 13:44:31 -0300 Subject: [PATCH 2/2] Minor changes for script --- scripts/prepare_online_node.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/prepare_online_node.sh b/scripts/prepare_online_node.sh index 3dad615..3aac2ec 100755 --- a/scripts/prepare_online_node.sh +++ b/scripts/prepare_online_node.sh @@ -104,7 +104,7 @@ echo "" echo "---" ADDRESS=$(cardano-cli address build --verification-key-file "$ADMIN_VERIFICATION_KEY_FILE" --testnet-magic 1) SEED_INPUT=$(cardano-cli conway query utxo --address $ADDRESS --output-json --testnet-magic 1 --socket-path $CARDANO_NODE_SOCKET | jq -r 'to_entries[0].key') -COMMIT_INPUT=$(cardano-cli conway query utxo --address $ADDRESS --output-json --testnet-magic 1 --socket-path $CARDANO_NODE_SOCKET | jq -r 'to_entries[1].key') +COMMIT_INPUT=$(cardano-cli conway query utxo --address $ADDRESS --output-json --testnet-magic 1 --socket-path $CARDANO_NODE_SOCKET | jq -r 'to_entries[-1].key') echo "* Seed input: $SEED_INPUT" echo "* Commit input: $COMMIT_INPUT" @@ -179,6 +179,6 @@ spec: EOF ) printf "%s\n" "$FILE" -printf "%s\n" "$FILE" > node.yml +printf "%s\n" "$FILE" > "$NODE_ID.yml" rm -rf persistence keys