regtest_helper.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #!/usr/bin/env bash
  2. # Helper script for interacting with CDK regtest environment
  3. # Run this after starting interactive_regtest_mprocs.sh
  4. # Check for environment state file first, then environment variable
  5. ENV_FILE="/tmp/cdk_regtest_env"
  6. if [ -f "$ENV_FILE" ]; then
  7. source "$ENV_FILE"
  8. elif [ ! -z "$CDK_ITESTS_DIR" ]; then
  9. # Environment variable is set, create state file for other sessions
  10. echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE"
  11. echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE"
  12. echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE"
  13. echo "export CDK_MINTD_PID=\"$CDK_MINTD_PID\"" >> "$ENV_FILE"
  14. echo "export CDK_MINTD_LND_PID=\"$CDK_MINTD_LND_PID\"" >> "$ENV_FILE"
  15. echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE"
  16. else
  17. echo "❌ CDK regtest environment not found!"
  18. echo "Please run './misc/interactive_regtest_mprocs.sh' or 'just regtest' first"
  19. exit 1
  20. fi
  21. # Validate that the environment is actually running
  22. if [ -z "$CDK_ITESTS_DIR" ] || [ ! -d "$CDK_ITESTS_DIR" ]; then
  23. echo "❌ CDK regtest environment not found or directory missing!"
  24. echo "Please run './misc/interactive_regtest_mprocs.sh' or 'just regtest' first"
  25. [ -f "$ENV_FILE" ] && rm "$ENV_FILE" # Clean up stale state file
  26. exit 1
  27. fi
  28. show_help() {
  29. echo "CDK Regtest Environment Helper"
  30. echo "============================="
  31. echo
  32. echo "Lightning Node Commands:"
  33. echo " ln-cln1 <command> - Execute command on CLN node 1"
  34. echo " ln-cln2 <command> - Execute command on CLN node 2"
  35. echo " ln-lnd1 <command> - Execute command on LND node 1"
  36. echo " ln-lnd2 <command> - Execute command on LND node 2"
  37. echo
  38. echo "Bitcoin Commands:"
  39. echo " btc <command> - Execute bitcoin-cli command"
  40. echo " btc-mine [blocks] - Mine blocks (default: 10)"
  41. echo
  42. echo "CDK Mint Commands:"
  43. echo " mint-info - Show mint information"
  44. echo " mint-test - Run integration tests"
  45. echo " restart-mints - Stop, recompile, and restart both mints (log mode)"
  46. echo
  47. echo "Environment Commands:"
  48. echo " show-env - Show environment variables"
  49. echo " show-logs - Show recent mint logs"
  50. echo " show-status - Show status of all components"
  51. echo " logs - Start mprocs TUI (adapts to current mode)"
  52. echo
  53. echo "Environment Modes:"
  54. echo " just regtest - Log tailing mode (mints auto-start, logs to files)"
  55. echo " just regtest-mprocs - Direct management (mprocs controls mint processes)"
  56. echo
  57. echo "Examples:"
  58. echo " $0 ln-cln1 getinfo"
  59. echo " $0 ln-lnd1 getinfo"
  60. echo " $0 btc getblockcount"
  61. echo " $0 btc-mine 5"
  62. echo " $0 mint-info"
  63. echo " $0 restart-mints # Only works in log tailing mode"
  64. echo " $0 logs # Start mprocs viewer"
  65. }
  66. # Bitcoin commands
  67. btc_command() {
  68. bitcoin-cli -regtest -rpcuser=testuser -rpcpassword=testpass -rpcport=18443 "$@"
  69. }
  70. btc_mine() {
  71. local blocks=${1:-10}
  72. local address=$(btc_command getnewaddress)
  73. btc_command generatetoaddress "$blocks" "$address"
  74. echo "Mined $blocks blocks"
  75. }
  76. # CLN commands
  77. cln_command() {
  78. local node=$1
  79. shift
  80. lightning-cli --rpc-file="$CDK_ITESTS_DIR/cln/$node/regtest/lightning-rpc" "$@"
  81. }
  82. # LND commands
  83. lnd_command() {
  84. local node=$1
  85. shift
  86. local port
  87. case $node in
  88. "one") port=10009 ;;
  89. "two") port=10010 ;;
  90. *) echo "Unknown LND node: $node"; return 1 ;;
  91. esac
  92. lncli --rpcserver=localhost:$port \
  93. --tlscertpath="$CDK_ITESTS_DIR/lnd/$node/tls.cert" \
  94. --macaroonpath="$CDK_ITESTS_DIR/lnd/$node/data/chain/bitcoin/regtest/admin.macaroon" \
  95. "$@"
  96. }
  97. # Mint commands
  98. mint_info() {
  99. echo "CLN Mint (Port 8085):"
  100. curl -s "$CDK_TEST_MINT_URL/v1/info" | jq . 2>/dev/null || curl -s "$CDK_TEST_MINT_URL/v1/info"
  101. echo
  102. echo "LND Mint (Port 8087):"
  103. curl -s "$CDK_TEST_MINT_URL_2/v1/info" | jq . 2>/dev/null || curl -s "$CDK_TEST_MINT_URL_2/v1/info"
  104. }
  105. mint_test() {
  106. echo "Running integration tests..."
  107. cargo test -p cdk-integration-tests
  108. }
  109. # Environment info
  110. show_env() {
  111. echo "CDK Regtest Environment Variables:"
  112. echo "================================="
  113. echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR"
  114. echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL"
  115. echo "CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2"
  116. echo "CDK_MINTD_PID=$CDK_MINTD_PID"
  117. echo "CDK_MINTD_LND_PID=$CDK_MINTD_LND_PID"
  118. echo "CDK_REGTEST_PID=$CDK_REGTEST_PID"
  119. }
  120. show_logs() {
  121. echo "=== Recent CLN Mint Logs ==="
  122. if [ -f "$CDK_ITESTS_DIR/cln_mint/mintd.log" ]; then
  123. tail -10 "$CDK_ITESTS_DIR/cln_mint/mintd.log"
  124. else
  125. echo "Log file not found"
  126. fi
  127. echo
  128. echo "=== Recent LND Mint Logs ==="
  129. if [ -f "$CDK_ITESTS_DIR/lnd_mint/mintd.log" ]; then
  130. tail -10 "$CDK_ITESTS_DIR/lnd_mint/mintd.log"
  131. else
  132. echo "Log file not found"
  133. fi
  134. }
  135. start_mprocs() {
  136. echo "Starting mprocs log viewer..."
  137. if ! command -v mprocs >/dev/null 2>&1; then
  138. echo "❌ mprocs not found! Please install it with:"
  139. echo " cargo install mprocs"
  140. echo " or your package manager"
  141. return 1
  142. fi
  143. # Check if we have the direct process management config
  144. DIRECT_MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs.yaml"
  145. FALLBACK_MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs_fallback.yaml"
  146. if [ -f "$DIRECT_MPROCS_CONFIG" ]; then
  147. echo "Using direct process management mode..."
  148. echo "In mprocs: 's' to start, 'k' to kill, 'r' to restart processes"
  149. cd "$CDK_ITESTS_DIR"
  150. mprocs --config "$DIRECT_MPROCS_CONFIG"
  151. return
  152. fi
  153. # Create fallback mprocs configuration for log tailing
  154. cat > "$FALLBACK_MPROCS_CONFIG" << EOF
  155. procs:
  156. cln-mint:
  157. shell: "touch $CDK_ITESTS_DIR/cln_mint/mintd.log && tail -f $CDK_ITESTS_DIR/cln_mint/mintd.log"
  158. autostart: true
  159. lnd-mint:
  160. shell: "touch $CDK_ITESTS_DIR/lnd_mint/mintd.log && tail -f $CDK_ITESTS_DIR/lnd_mint/mintd.log"
  161. autostart: true
  162. bitcoind:
  163. shell: "touch $CDK_ITESTS_DIR/bitcoin/regtest/debug.log && tail -f $CDK_ITESTS_DIR/bitcoin/regtest/debug.log"
  164. autostart: true
  165. cln-one:
  166. shell: "while [ ! -f $CDK_ITESTS_DIR/cln/one/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/one/regtest/log"
  167. autostart: true
  168. cln-two:
  169. shell: "while [ ! -f $CDK_ITESTS_DIR/cln/two/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/two/regtest/log"
  170. autostart: true
  171. lnd-one:
  172. shell: "while [ ! -f $CDK_ITESTS_DIR/lnd/one/logs/bitcoin/regtest/lnd.log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/lnd/one/logs/bitcoin/regtest/lnd.log"
  173. autostart: true
  174. lnd-two:
  175. shell: "while [ ! -f $CDK_ITESTS_DIR/lnd/two/logs/bitcoin/regtest/lnd.log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/lnd/two/logs/bitcoin/regtest/lnd.log"
  176. autostart: true
  177. settings:
  178. mouse_scroll_speed: 3
  179. proc_list_width: 20
  180. hide_keymap_window: false
  181. EOF
  182. echo "Using log tailing mode..."
  183. echo "Use 'q' to quit the log viewer"
  184. cd "$CDK_ITESTS_DIR"
  185. mprocs --config "$FALLBACK_MPROCS_CONFIG"
  186. }
  187. show_status() {
  188. echo "CDK Regtest Environment Status:"
  189. echo "==============================="
  190. # Check processes
  191. echo "Processes:"
  192. if [ ! -z "$CDK_REGTEST_PID" ] && kill -0 $CDK_REGTEST_PID 2>/dev/null; then
  193. echo " ✓ Regtest network (PID: $CDK_REGTEST_PID)"
  194. else
  195. echo " ❌ Regtest network"
  196. fi
  197. if [ ! -z "$CDK_MINTD_PID" ] && kill -0 $CDK_MINTD_PID 2>/dev/null; then
  198. echo " ✓ CLN Mint (PID: $CDK_MINTD_PID)"
  199. else
  200. echo " ❌ CLN Mint"
  201. fi
  202. if [ ! -z "$CDK_MINTD_LND_PID" ] && kill -0 $CDK_MINTD_LND_PID 2>/dev/null; then
  203. echo " ✓ LND Mint (PID: $CDK_MINTD_LND_PID)"
  204. else
  205. echo " ❌ LND Mint"
  206. fi
  207. echo
  208. echo "Network connectivity:"
  209. if curl -s "$CDK_TEST_MINT_URL/v1/info" >/dev/null 2>&1; then
  210. echo " ✓ CLN Mint responding"
  211. else
  212. echo " ❌ CLN Mint not responding"
  213. fi
  214. if curl -s "$CDK_TEST_MINT_URL_2/v1/info" >/dev/null 2>&1; then
  215. echo " ✓ LND Mint responding"
  216. else
  217. echo " ❌ LND Mint not responding"
  218. fi
  219. }
  220. restart_mints() {
  221. echo "==============================="
  222. echo "Restarting CDK Mints"
  223. echo "==============================="
  224. # Stop existing mints
  225. echo "Stopping existing mints..."
  226. if [ ! -z "$CDK_MINTD_PID" ] && kill -0 $CDK_MINTD_PID 2>/dev/null; then
  227. echo " Stopping CLN Mint (PID: $CDK_MINTD_PID)"
  228. kill -2 $CDK_MINTD_PID
  229. wait $CDK_MINTD_PID 2>/dev/null || true
  230. fi
  231. if [ ! -z "$CDK_MINTD_LND_PID" ] && kill -0 $CDK_MINTD_LND_PID 2>/dev/null; then
  232. echo " Stopping LND Mint (PID: $CDK_MINTD_LND_PID)"
  233. kill -2 $CDK_MINTD_LND_PID
  234. wait $CDK_MINTD_LND_PID 2>/dev/null || true
  235. fi
  236. # Recompile
  237. echo "Recompiling cdk-mintd..."
  238. if ! cargo build --bin cdk-mintd; then
  239. echo "❌ Compilation failed"
  240. return 1
  241. fi
  242. echo "✓ Compilation successful"
  243. # Restart CLN mint
  244. echo "Starting CLN Mint..."
  245. export CDK_MINTD_CLN_RPC_PATH="$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc"
  246. export CDK_MINTD_URL="http://127.0.0.1:8085"
  247. export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/cln_mint"
  248. export CDK_MINTD_LISTEN_HOST="127.0.0.1"
  249. export CDK_MINTD_LISTEN_PORT=8085
  250. export CDK_MINTD_LN_BACKEND="cln"
  251. export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal"
  252. export RUST_BACKTRACE=1
  253. cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 &
  254. NEW_CLN_PID=$!
  255. # Wait for CLN mint to be ready
  256. echo "Waiting for CLN mint to start..."
  257. local start_time=$(date +%s)
  258. while true; do
  259. local current_time=$(date +%s)
  260. local elapsed_time=$((current_time - start_time))
  261. if [ $elapsed_time -ge 30 ]; then
  262. echo "❌ Timeout waiting for CLN mint"
  263. return 1
  264. fi
  265. if curl -s "http://127.0.0.1:8085/v1/info" >/dev/null 2>&1; then
  266. echo "✓ CLN Mint ready"
  267. break
  268. fi
  269. sleep 1
  270. done
  271. # Restart LND mint
  272. echo "Starting LND Mint..."
  273. export CDK_MINTD_LND_ADDRESS="https://localhost:10010"
  274. export CDK_MINTD_LND_CERT_FILE="$CDK_ITESTS_DIR/lnd/two/tls.cert"
  275. export CDK_MINTD_LND_MACAROON_FILE="$CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon"
  276. export CDK_MINTD_URL="http://127.0.0.1:8087"
  277. export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/lnd_mint"
  278. export CDK_MINTD_LISTEN_HOST="127.0.0.1"
  279. export CDK_MINTD_LISTEN_PORT=8087
  280. export CDK_MINTD_LN_BACKEND="lnd"
  281. export CDK_MINTD_MNEMONIC="cattle gold bind busy sound reduce tone addict baby spend february strategy"
  282. cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 &
  283. NEW_LND_PID=$!
  284. # Wait for LND mint to be ready
  285. echo "Waiting for LND mint to start..."
  286. start_time=$(date +%s)
  287. while true; do
  288. current_time=$(date +%s)
  289. elapsed_time=$((current_time - start_time))
  290. if [ $elapsed_time -ge 30 ]; then
  291. echo "❌ Timeout waiting for LND mint"
  292. return 1
  293. fi
  294. if curl -s "http://127.0.0.1:8087/v1/info" >/dev/null 2>&1; then
  295. echo "✓ LND Mint ready"
  296. break
  297. fi
  298. sleep 1
  299. done
  300. # Update PIDs in state file
  301. CDK_MINTD_PID=$NEW_CLN_PID
  302. CDK_MINTD_LND_PID=$NEW_LND_PID
  303. # Update state file
  304. echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE"
  305. echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE"
  306. echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE"
  307. echo "export CDK_MINTD_PID=\"$CDK_MINTD_PID\"" >> "$ENV_FILE"
  308. echo "export CDK_MINTD_LND_PID=\"$CDK_MINTD_LND_PID\"" >> "$ENV_FILE"
  309. echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE"
  310. echo
  311. echo "✅ Mints restarted successfully!"
  312. echo " CLN Mint: http://127.0.0.1:8085 (PID: $CDK_MINTD_PID)"
  313. echo " LND Mint: http://127.0.0.1:8087 (PID: $CDK_MINTD_LND_PID)"
  314. echo "==============================="
  315. }
  316. # Main command dispatcher
  317. case "$1" in
  318. "ln-cln1")
  319. shift
  320. cln_command "one" "$@"
  321. ;;
  322. "ln-cln2")
  323. shift
  324. cln_command "two" "$@"
  325. ;;
  326. "ln-lnd1")
  327. shift
  328. lnd_command "one" "$@"
  329. ;;
  330. "ln-lnd2")
  331. shift
  332. lnd_command "two" "$@"
  333. ;;
  334. "btc")
  335. shift
  336. btc_command "$@"
  337. ;;
  338. "btc-mine")
  339. shift
  340. btc_mine "$@"
  341. ;;
  342. "mint-info")
  343. mint_info
  344. ;;
  345. "mint-test")
  346. mint_test
  347. ;;
  348. "restart-mints")
  349. restart_mints
  350. ;;
  351. "show-env")
  352. show_env
  353. ;;
  354. "show-logs")
  355. show_logs
  356. ;;
  357. "show-status")
  358. show_status
  359. ;;
  360. "logs")
  361. start_mprocs
  362. ;;
  363. "help"|"-h"|"--help"|"")
  364. show_help
  365. ;;
  366. *)
  367. echo "Unknown command: $1"
  368. echo "Run '$0 help' for available commands"
  369. exit 1
  370. ;;
  371. esac