regtest_helper.sh 14 KB

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