Skip to content

Commit 6e952bd

Browse files
feat(bau): check-chain script is bit more flexible
1 parent 15971c8 commit 6e952bd

File tree

2 files changed

+121
-13
lines changed

2 files changed

+121
-13
lines changed

cardano-cli.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
#!/usr/bin/env bash
22

3+
# Parse --cardano-network argument if present, otherwise use env var
4+
NETWORK_ARG=""
5+
NEW_ARGS=()
6+
while [[ $# -gt 0 ]]; do
7+
case "$1" in
8+
--cardano-network)
9+
NETWORK_ARG="$2"
10+
shift 2
11+
;;
12+
*)
13+
NEW_ARGS+=("$1")
14+
shift
15+
;;
16+
esac
17+
done
18+
19+
if [ -n "$NETWORK_ARG" ]; then
20+
CARDANO_NETWORK="$NETWORK_ARG"
21+
fi
22+
23+
if [ -z "$CARDANO_NETWORK" ]; then
24+
echo "Error: CARDANO_NETWORK not set. Please provide --cardano-network <network> or set the CARDANO_NETWORK environment variable."
25+
exit 1
26+
fi
27+
328
if [ "$CARDANO_NETWORK" = "preview" ]; then
429
CARDANO_NODE_NETWORK_ID=2
530
elif [ "$CARDANO_NETWORK" = "mainnet" ]; then
@@ -13,4 +38,4 @@ fi
1338

1439
# Run in same container as cardano-node.
1540
# docker exec doesn't support `--env` params so wrapped in an `sh`.
16-
docker exec -t cardano-node sh -c "CARDANO_NODE_NETWORK_ID=${CARDANO_NODE_NETWORK_ID} /usr/local/bin/cardano-cli $*"
41+
docker exec -t cardano-node sh -c "CARDANO_NODE_NETWORK_ID=${CARDANO_NODE_NETWORK_ID} /usr/local/bin/cardano-cli ${NEW_ARGS[*]}"

check-chain.sh

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,87 @@
22
set -euo pipefail
33

44
# ─── Configuration ─────────────────────────────────────────────────────────────
5-
RPC_URL="http://127.0.0.1:9944" # your node’s JSON-RPC HTTP endpoint
6-
RPC_URL="https://rpc.qanet.dev.midnight.network/"
5+
# Accept parameters for RPC_URL and all env vars not set within this file.
6+
usage() {
7+
echo "Usage: $0 [--rpc-url URL] [--postgres-password PASSWORD] [--postgres-port PORT] [--postgres-user USER] [--postgres-db DB] [--cardano-network NETWORK]"
8+
echo ""
9+
echo "All parameters are optional. If not provided, the script will use the corresponding environment variable."
10+
echo "If neither is set, the script will exit with an error."
11+
exit 1
12+
}
13+
14+
# Default values from environment
15+
RPC_URL="${RPC_URL:-http://127.0.0.1:9933}" # your node’s JSON-RPC HTTP endpoint
16+
# RPC_URL="https://rpc.qanet.dev.midnight.network/"
17+
18+
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-}"
19+
POSTGRES_PORT="${POSTGRES_PORT:-}"
20+
POSTGRES_USER="${POSTGRES_USER:-}"
21+
POSTGRES_DB="${POSTGRES_DB:-}"
22+
CARDANO_NETWORK="${CARDANO_NETWORK:-}"
23+
24+
# Parse arguments
25+
while [[ $# -gt 0 ]]; do
26+
case "$1" in
27+
--rpc-url)
28+
RPC_URL="$2"
29+
shift 2
30+
;;
31+
--postgres-password)
32+
POSTGRES_PASSWORD="$2"
33+
shift 2
34+
;;
35+
--postgres-port)
36+
POSTGRES_PORT="$2"
37+
shift 2
38+
;;
39+
--postgres-user)
40+
POSTGRES_USER="$2"
41+
shift 2
42+
;;
43+
--postgres-db)
44+
POSTGRES_DB="$2"
45+
shift 2
46+
;;
47+
--cardano-network)
48+
CARDANO_NETWORK="$2"
49+
shift 2
50+
;;
51+
-h|--help)
52+
usage
53+
;;
54+
*)
55+
echo "Unknown argument: $1"
56+
usage
57+
;;
58+
esac
59+
done
60+
61+
# Validate required variables (report all missing at once)
62+
missing_vars=()
63+
[[ -z "$RPC_URL" ]] && missing_vars+=("RPC_URL")
64+
[[ -z "$POSTGRES_PASSWORD" ]] && missing_vars+=("POSTGRES_PASSWORD")
65+
[[ -z "$POSTGRES_PORT" ]] && missing_vars+=("POSTGRES_PORT")
66+
[[ -z "$POSTGRES_USER" ]] && missing_vars+=("POSTGRES_USER")
67+
[[ -z "$POSTGRES_DB" ]] && missing_vars+=("POSTGRES_DB")
68+
[[ -z "$CARDANO_NETWORK" ]] && missing_vars+=("CARDANO_NETWORK")
69+
70+
if (( ${#missing_vars[@]} )); then
71+
echo "❌ ERROR: The following required arguments are missing. You can provide them as command-line arguments:"
72+
for var in "${missing_vars[@]}"; do
73+
case "$var" in
74+
RPC_URL) echo " --rpc-url <value>" ;;
75+
POSTGRES_PASSWORD) echo " --postgres-password <value>" ;;
76+
POSTGRES_PORT) echo " --postgres-port <value>" ;;
77+
POSTGRES_USER) echo " --postgres-user <value>" ;;
78+
POSTGRES_DB) echo " --postgres-db <value>" ;;
79+
CARDANO_NETWORK) echo " --cardano-network <value>" ;;
80+
*) echo " $var" ;;
81+
esac
82+
done
83+
exit 1
84+
exit 1
85+
fi
786

887
echo Get genesis utxo from running midnight node...
988
GET_PC_PARAMS="curl -s -H \"Content-Type:application/json\" \
@@ -12,11 +91,10 @@ GET_PC_PARAMS="curl -s -H \"Content-Type:application/json\" \
1291
GENESIS_UTXO=$(eval "$GET_PC_PARAMS" | jq -r '.result.genesis_utxo')
1392
echo "GENESIS_UTXO=$GENESIS_UTXO"
1493

15-
1694
export GENESIS_TX_HASH="${GENESIS_UTXO%%#*}"
1795
export GENESIS_COIN_INDEX="${GENESIS_UTXO##*#}"
18-
echo Check coin has been spent according to cardano...
19-
QUERY_UTXO_CMD="./cardano-cli.sh query utxo --tx-in $GENESIS_UTXO --output-json 2>/dev/null"
96+
echo Check if coin has been spent according to cardano...
97+
QUERY_UTXO_CMD="./cardano-cli.sh query utxo --tx-in $GENESIS_UTXO --output-json --cardano-network $CARDANO_NETWORK"
2098
genesis_utxo_json=$(eval "$QUERY_UTXO_CMD");
2199

22100
if [ "$genesis_utxo_json" = "{}" ] ; then
@@ -25,7 +103,15 @@ else
25103
echo "❌ As part of chain registration process Genesis UTXO should have been spent but $genesis_utxo_json"
26104
fi
27105

28-
# Assume that db-sync is synced.
106+
# DB-sync should be synced!
107+
tip_json=$(docker exec cardano-node cardano-cli query tip --socket-path /ipc/node.socket --testnet-magic 2)
108+
sync_progress=$(echo "$tip_json" | jq -r '.syncProgress')
109+
if [[ "$sync_progress" != "100.00" ]]; then
110+
echo "❌ ERROR: DB-sync is not fully synced. syncProgress=$sync_progress"
111+
exit 1
112+
else
113+
echo "✅ Cardano-node is at the latest tip, synced."
114+
fi
29115

30116
# Find spent UTXO:
31117
export PGPASSWORD=$POSTGRES_PASSWORD
@@ -55,12 +141,11 @@ else
55141
# can show url to tx hash https://preview.cexplorer.io/tx/1bb8027cb698c4b4c829396ac6eabac2ad46d80744c7a6822298dd76633c4f4f
56142
fi
57143

58-
59-
EPOCH_CMD="./cardano-cli.sh query tip 2>/dev/null"
144+
echo "Querying Cardano chain tip for current epoch..."
145+
EPOCH_CMD="./cardano-cli.sh query tip --cardano-network $CARDANO_NETWORK 2>/dev/null"
60146
tip=$(eval "$EPOCH_CMD");
61147
CURRENT_EPOCH=$(echo "$tip" | jq -r '.epoch')
62-
echo "epoc=$CURRENT_EPOCH"
63-
148+
echo "Current Cardano epoch: $CURRENT_EPOCH"
64149

65150
# curl -s -H "Content-Type:application/json" -d '{"jsonrpc":"2.0","method":"sidechain_getStatus","params":[],"id":1}' https://rpc.qanet.dev.midnight.network/
66151

@@ -143,8 +228,6 @@ done
143228
# midnight_apiVersions
144229
# midnight_ledgerVersion
145230

146-
147-
148231
# Registrations status for epoch 929:
149232
# running external command: /midnight-node registration-status --mainchain-pub-key 0x1f4bf447da2b78482b2656f7f504b321c9f0b8712faabbd0de7c47ab13d9cd4a --mc-epoch-number 929 --chain chain-spec.json --base-path /tmp/.tmpii6vGf
150233
# command output: Error: Input("ChainSpec Parse error: Error opening spec file `chain-spec.json`: No such file or directory (os error 2)")

0 commit comments

Comments
 (0)