From 4028824b1ac3d2f163a20e24004796438243d082 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Tue, 8 Jul 2025 10:48:22 +0100 Subject: [PATCH] feat: mprocs regtest --- MINT_RESTART_FEATURE.md | 86 ++++++ MPROCS_INTEGRATION.md | 147 ++++++++++ MPROCS_MODES.md | 132 +++++++++ REGTEST_QUICKSTART.md | 125 +++++++++ flake.nix | 3 +- justfile | 67 +++++ misc/README.md | 218 +++++++++++++++ misc/interactive_regtest.sh | 330 ++++++++++++++++++++++ misc/interactive_regtest_mprocs.sh | 320 ++++++++++++++++++++++ misc/regtest_helper.sh | 421 +++++++++++++++++++++++++++++ 10 files changed, 1848 insertions(+), 1 deletion(-) create mode 100644 MINT_RESTART_FEATURE.md create mode 100644 MPROCS_INTEGRATION.md create mode 100644 MPROCS_MODES.md create mode 100644 REGTEST_QUICKSTART.md create mode 100644 misc/README.md create mode 100755 misc/interactive_regtest.sh create mode 100755 misc/interactive_regtest_mprocs.sh create mode 100755 misc/regtest_helper.sh diff --git a/MINT_RESTART_FEATURE.md b/MINT_RESTART_FEATURE.md new file mode 100644 index 00000000..0a6180a3 --- /dev/null +++ b/MINT_RESTART_FEATURE.md @@ -0,0 +1,86 @@ +# Mint Restart Feature + +A new command has been added to restart the CDK mints after recompiling, perfect for development workflows when you're making changes to the mint code. + +## Command + +```bash +just restart-mints +``` + +or + +```bash +./misc/regtest_helper.sh restart-mints +``` + +## What It Does + +1. **Stops** both running mints (CLN and LND mints) +2. **Recompiles** the `cdk-mintd` binary with your latest changes +3. **Restarts** both mints with the same configuration +4. **Waits** for both mints to be ready and responding +5. **Updates** the state file with new process IDs + +## Development Workflow + +### Before this feature: +```bash +# Terminal 1: Start environment +just regtest + +# Terminal 2: Make code changes, then manually restart everything +# Ctrl+C in Terminal 1 (stops entire environment including Lightning network) +just regtest # Start everything again (slow) +``` + +### With this feature: +```bash +# Terminal 1: Start environment (once) +just regtest + +# Terminal 2: Make code changes, then quickly restart just the mints +just restart-mints # Fast! Keeps Lightning network running +just mint-test # Test your changes +``` + +## Benefits + +1. **Faster Development Cycle** - No need to restart the entire Lightning network +2. **Preserves Network State** - Bitcoin blockchain, Lightning channels, and node states remain intact +3. **Automatic Recompilation** - No need to manually run `cargo build` +4. **Status Validation** - Ensures mints are responding before completing +5. **State Management** - Updates process IDs for other commands to work correctly + +## Example Output + +``` +=============================== +Restarting CDK Mints +=============================== +Stopping existing mints... + Stopping CLN Mint (PID: 12345) + Stopping LND Mint (PID: 12346) +Recompiling cdk-mintd... +✓ Compilation successful +Starting CLN Mint... +Waiting for CLN mint to start... +✓ CLN Mint ready +Starting LND Mint... +Waiting for LND mint to start... +✓ LND Mint ready + +✅ Mints restarted successfully! + CLN Mint: http://127.0.0.1:8085 (PID: 54321) + LND Mint: http://127.0.0.1:8087 (PID: 54322) +=============================== +``` + +## Use Cases + +- **Testing mint code changes** without restarting the entire regtest environment +- **Debugging mint behavior** with fresh mint instances +- **Performance testing** with clean mint state but preserved Lightning network +- **Integration testing** after mint code modifications + +This feature makes the development experience much smoother when working on CDK mint functionality! diff --git a/MPROCS_INTEGRATION.md b/MPROCS_INTEGRATION.md new file mode 100644 index 00000000..79aee0fe --- /dev/null +++ b/MPROCS_INTEGRATION.md @@ -0,0 +1,147 @@ +# mprocs Integration for CDK Regtest + +The CDK regtest environment now integrates with `mprocs` to provide a beautiful TUI (Terminal User Interface) for monitoring all component logs in real-time. + +## What is mprocs? + +`mprocs` is a TUI for running multiple processes and monitoring their output. Perfect for development environments where you need to watch logs from multiple services simultaneously. + +## Features + +### Automatic Setup +- The regtest script checks for `mprocs` and offers to install it if missing +- Creates a dynamic mprocs configuration with all relevant log files +- Handles missing log files gracefully (waits for them to be created) + +### Components Monitored +- **cln-mint**: CDK mint connected to CLN +- **lnd-mint**: CDK mint connected to LND +- **bitcoind**: Bitcoin regtest node +- **cln-one**: Core Lightning node #1 +- **cln-two**: Core Lightning node #2 +- **lnd-one**: LND node #1 +- **lnd-two**: LND node #2 + +### Key Benefits +- **Real-time log monitoring** for all components +- **Side-by-side view** of related services +- **Easy navigation** between different logs +- **Scrollback** to review history +- **Search functionality** within logs +- **Process management** (start/stop/restart individual processes) + +## Usage + +### Automatic (Recommended) - Log Tailing Mode +```bash +just regtest +# After setup completes, mprocs launches automatically +# Mints start and log to files, mprocs shows log contents +``` + +### Direct Process Management Mode +```bash +just regtest-mprocs +# After setup, mprocs starts with mint processes stopped +# Use 's' key to start individual mints +# Full process control from within mprocs +``` + +### Manual Launch +```bash +# Start environment without mprocs +just regtest + +# In another terminal, launch mprocs +just regtest-logs +``` + +### Commands Available +```bash +just regtest # Starts environment and mprocs (log tailing mode) +just regtest-mprocs # Starts environment with direct process management +just regtest-logs # Manual mprocs launch (adapts to current mode) +``` + +## mprocs Controls + +### Direct Process Management Mode: +- **Arrow keys**: Navigate between processes +- **s**: Start the selected process +- **k**: Kill the selected process +- **r**: Restart the selected process +- **Enter**: Focus on selected process +- **Tab**: Switch between process list and log view +- **?**: Show help +- **q**: Quit mprocs (stops all managed processes) + +### Log Tailing Mode: +- **Arrow keys**: Navigate between processes +- **Enter**: Focus on selected process +- **Tab**: Switch between process list and log view +- **PageUp/PageDown**: Scroll through logs +- **Ctrl+C**: Interrupt current process +- **q**: Quit mprocs (processes continue running) + +## Installation + +If `mprocs` is not installed: +```bash +# Automatic installation prompt when running regtest +just regtest + +# Manual installation +cargo install mprocs + +# Or via package manager (varies by OS) +# Ubuntu/Debian: apt install mprocs +# macOS: brew install mprocs +``` + +## Configuration + +The mprocs configuration is automatically generated at `$CDK_ITESTS_DIR/mprocs.yaml`. It includes: + +- Proper log file paths for all components +- Graceful handling of missing files +- Optimized UI settings for development +- Auto-start for all monitoring processes + +## Development Workflow + +### Before mprocs: +- Start regtest environment +- Open multiple terminals to `tail -f` different log files +- Manually manage multiple windows/panes +- Switch between terminals to see different components + +### With mprocs: +- Start regtest environment → automatic log monitoring +- Single TUI shows all component logs +- Easy navigation between components +- Professional development experience + +## Example View + +``` +┌─Processes─────────┬─Output───────────────────────────────────────┐ +│ ● cln-mint │ 2024-07-08T08:30:12 INFO cdk_mintd: Starting │ +│ ● lnd-mint │ mint server on 127.0.0.1:8085 │ +│ ● bitcoind │ 2024-07-08T08:30:13 INFO: New invoice │ +│ ● cln-one │ received for 1000 sats │ +│ ● cln-two │ 2024-07-08T08:30:14 INFO: Payment │ +│ ● lnd-one │ successful │ +│ ● lnd-two │ │ +│ │ │ +└───────────────────┴──────────────────────────────────────────────┘ +``` + +## Fallback + +If mprocs is not available or fails: +- Environment continues to work normally +- Falls back to simple wait loop +- All `just` commands work as expected +- Logs still accessible via `just regtest-logs` + +This integration makes CDK development much more pleasant by providing professional-grade log monitoring out of the box! 🎉 diff --git a/MPROCS_MODES.md b/MPROCS_MODES.md new file mode 100644 index 00000000..06faea0e --- /dev/null +++ b/MPROCS_MODES.md @@ -0,0 +1,132 @@ +# mprocs Process Management Modes + +The CDK regtest environment now supports two different modes for managing processes with mprocs: + +## Mode 1: Log Tailing (Default) +**Command**: `just regtest` + +### How it works: +- Mints are started by the bash script and run in the background +- Mints write their output to log files (`mintd.log`) +- mprocs uses `tail -f` to follow these log files +- Log files persist even after mprocs exits + +### Pros: +- ✅ Log files are preserved for later analysis +- ✅ Simple setup +- ✅ Works even if mprocs crashes + +### Cons: +- ❌ Cannot restart mints from within mprocs +- ❌ Must use external commands to control mints +- ❌ mprocs shows file contents, not live processes + +## Mode 2: Direct Process Management +**Command**: `just regtest-mprocs` + +### How it works: +- mprocs directly manages the mint processes +- Mints are started/stopped by mprocs itself +- Output goes directly to mprocs (no log files by default) +- Full process control from within mprocs + +### Pros: +- ✅ Start/stop/restart mints directly from mprocs +- ✅ Live process output +- ✅ Better development workflow +- ✅ Process status indicators + +### Cons: +- ❌ Output not saved to files (unless configured) +- ❌ If mprocs crashes, you lose the processes + +## mprocs Controls + +### Direct Process Management Mode: +- **Arrow keys**: Navigate between processes +- **s**: Start the selected process +- **k**: Kill the selected process +- **r**: Restart the selected process +- **Enter**: Focus on a process (see its output) +- **Tab**: Switch between process list and output +- **?**: Show help +- **q**: Quit mprocs (stops all managed processes) + +### Log Tailing Mode: +- **Arrow keys**: Navigate between log sources +- **Enter**: Focus on a log source +- **Tab**: Switch between process list and log view +- **PageUp/PageDown**: Scroll through logs +- **q**: Quit mprocs (processes continue running) + +## Usage Examples + +### Start with Log Tailing (Original Mode) +```bash +just regtest +# Mints start automatically and log to files +# mprocs shows log contents +# Use Ctrl+C or 'q' to exit mprocs +# Processes continue running in background +``` + +### Start with Direct Process Management +```bash +just regtest-mprocs +# Only Lightning network starts automatically +# In mprocs, navigate to "cln-mint" and press 's' to start it +# Navigate to "lnd-mint" and press 's' to start it +# Use 'r' to restart mints after code changes +# Use 'q' to exit and stop all processes +``` + +### Switch Between Modes + +If you started with log tailing mode, you can access the direct management: +```bash +# In another terminal +source /tmp/cdk_regtest_env +just regtest-logs # This will detect the mode and adapt +``` + +## Development Workflow Comparison + +### Traditional (Log Tailing): +1. `just regtest` +2. Make code changes +3. In another terminal: `just restart-mints` +4. Check logs in mprocs + +### Direct Management: +1. `just regtest-mprocs` +2. Press 's' to start mints +3. Make code changes +4. In mprocs: press 'r' on each mint to restart +5. Watch live output directly + +## Technical Details + +### Project Root Handling +The direct process management mode ensures that: +- Startup scripts change to the correct project root directory +- Cargo commands run from where the `Cargo.toml` file is located +- Environment variables are properly set before starting processes + +### File Structure +``` +$CDK_ITESTS_DIR/ +├── start_cln_mint.sh # Sets PROJECT_ROOT and runs cargo from there +├── start_lnd_mint.sh # Sets PROJECT_ROOT and runs cargo from there +└── mprocs.yaml # Points to the startup scripts +``` + +Each startup script: +1. Changes to the project root directory (`cd "$PROJECT_ROOT"`) +2. Sets all required environment variables +3. Executes `cargo run --bin cdk-mintd` from the correct location + +The environment variables and helper commands work the same in both modes: +- `just ln-cln1 getinfo` +- `just btc-mine 5` +- `just mint-info` +- etc. diff --git a/REGTEST_QUICKSTART.md b/REGTEST_QUICKSTART.md new file mode 100644 index 00000000..f02bedf6 --- /dev/null +++ b/REGTEST_QUICKSTART.md @@ -0,0 +1,125 @@ +# CDK Interactive Regtest - Quick Start + +A simple guide to get up and running with the interactive regtest environment. + +## Start Environment + +```bash +# Terminal 1: Start regtest with default sqlite database +just regtest + +# Or with redb database +just regtest redb +``` + +The script will: +1. Check for `mprocs` and offer to install it if missing +2. Set up the regtest environment (Bitcoin + Lightning nodes + CDK mints) +3. Launch `mprocs` showing logs from all components +4. Press 'q' in mprocs to quit and stop the environment + +## Use Lightning Nodes (in any other terminal) + +The `just` commands work from any terminal - they automatically find the running environment. + +### Get Node Information +```bash +just ln-cln1 getinfo # CLN node 1 +just ln-cln2 getinfo # CLN node 2 +just ln-lnd1 getinfo # LND node 1 +just ln-lnd2 getinfo # LND node 2 +``` + +### Create and Pay Invoices +```bash +# Create 1000 sat invoice on CLN +just ln-cln1 invoice 1000 test_label "Test payment" + +# Pay invoice with LND (use the bolt11 from above) +just ln-lnd1 payinvoice lnbcrt10u1... + +# Check balances +just ln-cln1 listfunds +just ln-lnd1 listchannels +``` + +## Bitcoin Operations + +```bash +just btc getblockchaininfo # Blockchain status +just btc getbalance # Wallet balance +just btc-mine 5 # Mine 5 blocks +``` + +## CDK Mint Operations + +```bash +just mint-info # Show both mints' info +just mint-test # Run integration tests +just restart-mints # Stop, recompile, and restart mints +just regtest-status # Check all components +just regtest-logs # Show recent logs +just regtest-mprocs # Start mprocs TUI (if not already running) +``` + +## Stop Environment + +Press `Ctrl+C` in the terminal running `just regtest`. Everything will be cleaned up automatically. + +## Available Endpoints + +- **CLN Mint**: http://127.0.0.1:8085 +- **LND Mint**: http://127.0.0.1:8087 +- **Bitcoin RPC**: 127.0.0.1:18443 (testuser/testpass) + +## Common Workflows + +### Test Lightning Payment Flow +```bash +# Terminal 1 +just regtest + +# Terminal 2 +just ln-cln1 invoice 1000 test "Test payment" +# Copy the bolt11 invoice + +just ln-lnd1 payinvoice +just ln-cln1 listinvoices +just ln-lnd1 listpayments +``` + +### Test CDK Integration +```bash +# Terminal 1 +just regtest + +# Terminal 2 +just mint-test # Run all tests +cargo test -p cdk-integration-tests # Or specific tests +``` + +### Development with CDK CLI +```bash +# Terminal 1 +just regtest + +# Terminal 2 - use environment variables +echo $CDK_TEST_MINT_URL # CLN mint URL +echo $CDK_TEST_MINT_URL_2 # LND mint URL + +# Use with CDK CLI +cargo run --bin cdk-cli -- --mint-url $CDK_TEST_MINT_URL mint-info +``` + +### Development Workflow (Mint Code Changes) +```bash +# Terminal 1: Keep regtest running +just regtest + +# Terminal 2: Make changes to mint code, then +just restart-mints # Recompiles and restarts both mints +just mint-info # Test the changes +just mint-test # Run integration tests +``` + +That's it! The environment provides a full Lightning Network with CDK mints for testing and development. diff --git a/flake.nix b/flake.nix index 311f5307..20def631 100644 --- a/flake.nix +++ b/flake.nix @@ -81,6 +81,7 @@ bitcoind sqlx-cli cargo-outdated + mprocs # Needed for github ci libz @@ -274,7 +275,7 @@ echo "Docker is available at $(which docker)" echo "Docker version: $(docker --version)" ''; - buildInputs = buildInputs ++ [ + buildInputs = buildInputs ++ [ stable_toolchain pkgs.docker-client ]; diff --git a/justfile b/justfile index fbf3b9c1..7c403f97 100644 --- a/justfile +++ b/justfile @@ -153,6 +153,73 @@ nutshell-wallet-itest: #!/usr/bin/env bash ./misc/nutshell_wallet_itest.sh +# Start interactive regtest environment (Bitcoin + 4 LN nodes + 2 CDK mints) +regtest db="sqlite": + #!/usr/bin/env bash + ./misc/interactive_regtest.sh {{db}} + +# Start regtest with direct process management via mprocs (lets you start/stop mints) +regtest-mprocs db="sqlite": + #!/usr/bin/env bash + ./misc/interactive_regtest_mprocs.sh {{db}} + +# Lightning Network Commands (require regtest environment to be running) + +# Get CLN node 1 info +ln-cln1 *ARGS: + #!/usr/bin/env bash + ./misc/regtest_helper.sh ln-cln1 {{ARGS}} + +# Get CLN node 2 info +ln-cln2 *ARGS: + #!/usr/bin/env bash + ./misc/regtest_helper.sh ln-cln2 {{ARGS}} + +# Get LND node 1 info +ln-lnd1 *ARGS: + #!/usr/bin/env bash + ./misc/regtest_helper.sh ln-lnd1 {{ARGS}} + +# Get LND node 2 info +ln-lnd2 *ARGS: + #!/usr/bin/env bash + ./misc/regtest_helper.sh ln-lnd2 {{ARGS}} + +# Bitcoin regtest commands +btc *ARGS: + #!/usr/bin/env bash + ./misc/regtest_helper.sh btc {{ARGS}} + +# Mine blocks in regtest +btc-mine blocks="10": + #!/usr/bin/env bash + ./misc/regtest_helper.sh btc-mine {{blocks}} + +# Show mint information +mint-info: + #!/usr/bin/env bash + ./misc/regtest_helper.sh mint-info + +# Run integration tests against regtest environment +mint-test: + #!/usr/bin/env bash + ./misc/regtest_helper.sh mint-test + +# Restart mints after recompiling (useful for development) +restart-mints: + #!/usr/bin/env bash + ./misc/regtest_helper.sh restart-mints + +# Show regtest environment status +regtest-status: + #!/usr/bin/env bash + ./misc/regtest_helper.sh show-status + +# Show regtest environment logs +regtest-logs: + #!/usr/bin/env bash + ./misc/regtest_helper.sh show-logs + run-examples: cargo r --example p2pk cargo r --example mint-token diff --git a/misc/README.md b/misc/README.md new file mode 100644 index 00000000..51b19c35 --- /dev/null +++ b/misc/README.md @@ -0,0 +1,218 @@ +# CDK Interactive Regtest Environment + +This directory contains scripts for setting up and interacting with a CDK regtest environment for development and testing. + +## Scripts + +### 1. `interactive_regtest.sh` +Sets up a complete regtest environment with: +- Bitcoin regtest node +- 2 CLN (Core Lightning) nodes with channels +- 2 LND nodes with channels +- 2 CDK mint instances (one connected to CLN, one to LND) + +Unlike `itests.sh`, this script keeps the environment running for interactive use and creates a state file (`/tmp/cdk_regtest_env`) that allows other terminal sessions to find and interact with the environment. + +### 2. `regtest_helper.sh` +Helper script providing convenient commands to interact with the running regtest environment. Automatically detects the environment using the state file. + +## Quick Start + +### Using `just` (Recommended) + +1. **Start the regtest environment:** + ```bash + just regtest [database_type] + ``` + - `database_type`: Optional, defaults to "sqlite". Can be "sqlite" or "redb" + - The script will check for `mprocs` and offer to install it if missing + - After setup, it will launch `mprocs` showing logs from all nodes and mints + - Press 'q' in mprocs to quit and stop the environment + +2. **In another terminal, interact with Lightning nodes:** + ```bash + # Get node information + just ln-cln1 getinfo + just ln-lnd1 getinfo + + # Mine some blocks + just btc-mine 5 + + # Check mint status + just mint-info + + # Start mprocs log viewer in another terminal + just regtest-mprocs + + # See all available commands + just --list + ``` + +### Using Scripts Directly + +1. **Start the regtest environment:** + ```bash + ./misc/interactive_regtest.sh [database_type] + ``` + - `database_type`: Optional, defaults to "sqlite". Can be "sqlite" or "redb" + - The script will build necessary binaries and set up the full environment + - Keep this terminal open - the environment runs until you press Ctrl+C + +2. **In another terminal, use the helper script:** + ```bash + ./misc/regtest_helper.sh help + ``` + +## How It Works + +The interactive regtest environment uses a state file (`/tmp/cdk_regtest_env`) to share environment information between terminal sessions: + +1. When you run `just regtest` or `./misc/interactive_regtest.sh`, it creates the state file with all necessary environment variables +2. When you run Lightning node commands in other terminals (e.g., `just ln-cln1 getinfo`), the helper script automatically sources the state file +3. When the environment shuts down (Ctrl+C), it cleans up the state file automatically + +This allows you to use `just` commands from any terminal without needing to export environment variables manually. + +## Environment Details + +When running, the environment provides: + +### Network Endpoints +- **Bitcoin RPC**: `127.0.0.1:18443` (user: `testuser`, pass: `testpass`) +- **CLN Node 1**: Unix socket at `$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc` +- **CLN Node 2**: Unix socket at `$CDK_ITESTS_DIR/cln/two/regtest/lightning-rpc` +- **LND Node 1**: HTTPS on `localhost:10009` +- **LND Node 2**: HTTPS on `localhost:10010` + +### CDK Mints +- **CLN Mint**: `http://127.0.0.1:8085` (connected to CLN node 1) +- **LND Mint**: `http://127.0.0.1:8087` (connected to LND node 2) + +### Environment Variables +The following variables are exported for easy access: +- `CDK_TEST_MINT_URL`: CLN mint URL +- `CDK_TEST_MINT_URL_2`: LND mint URL +- `CDK_ITESTS_DIR`: Temporary directory with all data + +## Usage Examples + +### Using `just` Commands (Recommended) + +```bash +# Start the environment +just regtest + +# In another terminal: +# Get Lightning node info +just ln-cln1 getinfo +just ln-cln2 getinfo +just ln-lnd1 getinfo +just ln-lnd2 getinfo + +# Create and pay invoices +just ln-cln1 invoice 1000 label description +just ln-lnd1 payinvoice + +# Bitcoin operations +just btc getblockchaininfo +just btc-mine 10 +just btc getbalance + +# CDK operations +just mint-info +just mint-test +just restart-mints # Stop, recompile, and restart mints +just regtest-status +just regtest-logs # Show recent logs +just regtest-mprocs # Start mprocs TUI log viewer +``` + +### Using Helper Script Directly + +```bash +# Lightning Node Operations +./misc/regtest_helper.sh ln-cln1 getinfo +./misc/regtest_helper.sh ln-lnd1 getinfo +./misc/regtest_helper.sh ln-cln1 invoice 1000 label description +./misc/regtest_helper.sh ln-lnd1 payinvoice + +# Bitcoin Operations +./misc/regtest_helper.sh btc getblockchaininfo +./misc/regtest_helper.sh btc-mine 10 +./misc/regtest_helper.sh btc getbalance + +# CDK Mint Operations +./misc/regtest_helper.sh mint-info +./misc/regtest_helper.sh mint-test +./misc/regtest_helper.sh restart-mints +./misc/regtest_helper.sh show-status +``` + +### Legacy Examples (for reference) + +### Direct API Access +```bash +# Query mint info directly +curl http://127.0.0.1:8085/v1/info | jq + +# Get mint keysets +curl http://127.0.0.1:8085/v1/keysets | jq +``` + +### Development Workflow +```bash +# Terminal 1: Start environment +just regtest + +# Terminal 2: Development and testing +just ln-cln1 getinfo # Check CLN status +just mint-info # Check mint status +just mint-test # Run integration tests + +# Or use CDK CLI tools directly with the mint URLs +# The environment sets CDK_TEST_MINT_URL and CDK_TEST_MINT_URL_2 +cargo run --bin cdk-cli -- --mint-url $CDK_TEST_MINT_URL mint-info +``` + +## File Locations + +All files are stored in a temporary directory (`$CDK_ITESTS_DIR`): +``` +$CDK_ITESTS_DIR/ +├── bitcoin/ # Bitcoin regtest data +├── cln/ +│ ├── one/ # CLN node 1 data +│ └── two/ # CLN node 2 data +├── lnd/ +│ ├── one/ # LND node 1 data +│ │ ├── tls.cert +│ │ └── data/chain/bitcoin/regtest/admin.macaroon +│ └── two/ # LND node 2 data +│ ├── tls.cert +│ └── data/chain/bitcoin/regtest/admin.macaroon +├── cln_mint/ # CLN mint working directory +│ └── mintd.log +└── lnd_mint/ # LND mint working directory + └── mintd.log +``` + +## Cleanup + +- Press `Ctrl+C` in the terminal running `interactive_regtest.sh` +- All processes will be terminated and the temporary directory will be cleaned up automatically +- No manual cleanup is required + +## Troubleshooting + +### Environment not starting +- Check that ports 8085, 8087, 18443, 19846, 19847, 10009, 10010 are available +- Ensure you have the necessary dependencies (Bitcoin Core, CLN, LND) available +- Check the logs in `$CDK_ITESTS_DIR/cln_mint/mintd.log` and `$CDK_ITESTS_DIR/lnd_mint/mintd.log` + +### Helper script not working +- Ensure the regtest environment is running first +- The `CDK_ITESTS_DIR` environment variable must be set (done automatically by `interactive_regtest.sh`) + +### Connection issues +- Use `./misc/regtest_helper.sh show-status` to check component health +- Check mint logs with `./misc/regtest_helper.sh show-logs` diff --git a/misc/interactive_regtest.sh b/misc/interactive_regtest.sh new file mode 100755 index 00000000..e38b8fb4 --- /dev/null +++ b/misc/interactive_regtest.sh @@ -0,0 +1,330 @@ +#!/usr/bin/env bash + +# Interactive Regtest Environment for CDK +# This script sets up the regtest environment and keeps it running for interaction + +set -e + +# Function to wait for HTTP endpoint +wait_for_endpoint() { + local url=$1 + local timeout=${2:-60} + local start_time=$(date +%s) + + while true; do + local current_time=$(date +%s) + local elapsed_time=$((current_time - start_time)) + + if [ $elapsed_time -ge $timeout ]; then + echo "❌ Timeout waiting for $url" + return 1 + fi + + local http_status=$(curl -o /dev/null -s -w "%{http_code}" "$url" 2>/dev/null || echo "000") + + if [ "$http_status" -eq 200 ]; then + echo "✓ $url is ready" + return 0 + fi + + sleep 2 + done +} + +# Function to perform cleanup +cleanup() { + echo "Cleaning up..." + + # Remove state file for other sessions + rm -f "/tmp/cdk_regtest_env" + + if [ ! -z "$CDK_MINTD_PID" ] && kill -0 $CDK_MINTD_PID 2>/dev/null; then + echo "Killing the cdk mintd (CLN)" + kill -2 $CDK_MINTD_PID + wait $CDK_MINTD_PID + fi + + if [ ! -z "$CDK_MINTD_LND_PID" ] && kill -0 $CDK_MINTD_LND_PID 2>/dev/null; then + echo "Killing the cdk mintd (LND)" + kill -2 $CDK_MINTD_LND_PID + wait $CDK_MINTD_LND_PID + fi + + if [ ! -z "$CDK_REGTEST_PID" ] && kill -0 $CDK_REGTEST_PID 2>/dev/null; then + echo "Killing the cdk regtest" + kill -2 $CDK_REGTEST_PID + wait $CDK_REGTEST_PID + fi + + echo "Environment terminated" + + # Remove the temporary directory + if [ ! -z "$CDK_ITESTS_DIR" ]; then + rm -rf "$CDK_ITESTS_DIR" + echo "Temp directory removed: $CDK_ITESTS_DIR" + fi + + # Unset all environment variables + unset CDK_ITESTS_DIR + unset CDK_ITESTS_MINT_ADDR + unset CDK_ITESTS_MINT_PORT_0 + unset CDK_ITESTS_MINT_PORT_1 + unset CDK_MINTD_DATABASE + unset CDK_TEST_MINT_URL + unset CDK_TEST_MINT_URL_2 + unset CDK_MINTD_URL + unset CDK_MINTD_WORK_DIR + unset CDK_MINTD_LISTEN_HOST + unset CDK_MINTD_LISTEN_PORT + unset CDK_MINTD_LN_BACKEND + unset CDK_MINTD_MNEMONIC + unset CDK_MINTD_CLN_RPC_PATH + unset CDK_MINTD_LND_ADDRESS + unset CDK_MINTD_LND_CERT_FILE + unset CDK_MINTD_LND_MACAROON_FILE + unset CDK_MINTD_PID + unset CDK_MINTD_LND_PID + unset CDK_REGTEST_PID + unset RUST_BACKTRACE + unset CDK_TEST_REGTEST +} + +# Set up trap to call cleanup on script exit +trap cleanup EXIT + +export CDK_TEST_REGTEST=1 + +# Check for mprocs and offer to install if missing +if ! command -v mprocs >/dev/null 2>&1; then + echo "⚠️ mprocs not found - this tool provides a nice TUI for monitoring logs" + echo "Install it with: cargo install mprocs" + echo + read -p "Would you like to install mprocs now? (y/n): " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Installing mprocs..." + cargo install mprocs + if [ $? -eq 0 ]; then + echo "✓ mprocs installed successfully" + else + echo "❌ Failed to install mprocs. You can install it later with: cargo install mprocs" + fi + else + echo "Skipping mprocs installation. The environment will work without it." + fi + echo +fi + +# Parse command line arguments +CDK_MINTD_DATABASE=${1:-"sqlite"} # Default to sqlite if not specified + +# Create a temporary directory +export CDK_ITESTS_DIR=$(mktemp -d) +export CDK_ITESTS_MINT_ADDR="127.0.0.1" +export CDK_ITESTS_MINT_PORT_0=8085 +export CDK_ITESTS_MINT_PORT_1=8087 + +# Check if the temporary directory was created successfully +if [[ ! -d "$CDK_ITESTS_DIR" ]]; then + echo "Failed to create temp directory" + exit 1 +fi + +echo "==============================================" +echo "Starting Interactive CDK Regtest Environment" +echo "==============================================" +echo "Temp directory: $CDK_ITESTS_DIR" +echo "Database type: $CDK_MINTD_DATABASE" +echo + +export CDK_MINTD_DATABASE="$CDK_MINTD_DATABASE" + +# Build the necessary binaries +echo "Building binaries..." +cargo build -p cdk-integration-tests --bin start_regtest + +echo "Starting regtest network (Bitcoin + Lightning nodes)..." +cargo run --bin start_regtest & +export CDK_REGTEST_PID=$! + +# Create named pipe for progress tracking +mkfifo "$CDK_ITESTS_DIR/progress_pipe" +rm -f "$CDK_ITESTS_DIR/signal_received" + +# Start reading from pipe in background +(while read line; do + case "$line" in + "checkpoint1") + echo "✓ Regtest network is ready" + touch "$CDK_ITESTS_DIR/signal_received" + exit 0 + ;; + esac +done < "$CDK_ITESTS_DIR/progress_pipe") & + +# Wait for regtest setup (up to 120 seconds) +echo "Waiting for regtest network to be ready..." +for ((i=0; i<120; i++)); do + if [ -f "$CDK_ITESTS_DIR/signal_received" ]; then + break + fi + sleep 1 +done + +if [ ! -f "$CDK_ITESTS_DIR/signal_received" ]; then + echo "❌ Timeout waiting for regtest network" + exit 1 +fi + +echo +echo "Starting CDK Mint #1 (CLN backend)..." +export CDK_MINTD_CLN_RPC_PATH="$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc" +export CDK_MINTD_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0" +export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/cln_mint" +export CDK_MINTD_LISTEN_HOST=$CDK_ITESTS_MINT_ADDR +export CDK_MINTD_LISTEN_PORT=$CDK_ITESTS_MINT_PORT_0 +export CDK_MINTD_LN_BACKEND="cln" +export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal" +export RUST_BACKTRACE=1 + +mkdir -p "$CDK_MINTD_WORK_DIR" +cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 & +export CDK_MINTD_PID=$! + +# Wait for CLN mint to be ready +echo "Waiting for CLN mint to be ready..." +URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0/v1/info" +wait_for_endpoint "$URL" 60 + +echo +echo "Starting CDK Mint #2 (LND backend)..." +export CDK_MINTD_LND_ADDRESS="https://localhost:10010" +export CDK_MINTD_LND_CERT_FILE="$CDK_ITESTS_DIR/lnd/two/tls.cert" +export CDK_MINTD_LND_MACAROON_FILE="$CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon" + +export CDK_MINTD_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1" +mkdir -p "$CDK_ITESTS_DIR/lnd_mint" +export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/lnd_mint" +export CDK_MINTD_LISTEN_HOST=$CDK_ITESTS_MINT_ADDR +export CDK_MINTD_LISTEN_PORT=$CDK_ITESTS_MINT_PORT_1 +export CDK_MINTD_LN_BACKEND="lnd" +export CDK_MINTD_MNEMONIC="cattle gold bind busy sound reduce tone addict baby spend february strategy" + +cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 & +export CDK_MINTD_LND_PID=$! + +# Wait for LND mint to be ready +echo "Waiting for LND mint to be ready..." +URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1/v1/info" +wait_for_endpoint "$URL" 60 + +# Set environment variables for easy access +export CDK_TEST_MINT_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0" +export CDK_TEST_MINT_URL_2="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1" + +# Create state file for other terminal sessions +ENV_FILE="/tmp/cdk_regtest_env" +echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE" +echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE" +echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE" +echo "export CDK_MINTD_PID=\"$CDK_MINTD_PID\"" >> "$ENV_FILE" +echo "export CDK_MINTD_LND_PID=\"$CDK_MINTD_LND_PID\"" >> "$ENV_FILE" +echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE" + +echo +echo "==============================================" +echo "🎉 CDK Regtest Environment is Ready!" +echo "==============================================" +echo +echo "Network Information:" +echo " • Bitcoin RPC: 127.0.0.1:18443 (user: testuser, pass: testpass)" +echo " • CLN Node 1: $CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc" +echo " • CLN Node 2: $CDK_ITESTS_DIR/cln/two/regtest/lightning-rpc" +echo " • LND Node 1: https://localhost:10009" +echo " • LND Node 2: https://localhost:10010" +echo +echo "CDK Mints:" +echo " • CLN Mint: $CDK_TEST_MINT_URL" +echo " • LND Mint: $CDK_TEST_MINT_URL_2" +echo +echo "Files and Directories:" +echo " • Working Directory: $CDK_ITESTS_DIR" +echo " • CLN Mint Logs: $CDK_ITESTS_DIR/cln_mint/mintd.log" +echo " • LND Mint Logs: $CDK_ITESTS_DIR/lnd_mint/mintd.log" +echo " • LND 1 TLS Cert: $CDK_ITESTS_DIR/lnd/one/tls.cert" +echo " • LND 1 Macaroon: $CDK_ITESTS_DIR/lnd/one/data/chain/bitcoin/regtest/admin.macaroon" +echo " • LND 2 TLS Cert: $CDK_ITESTS_DIR/lnd/two/tls.cert" +echo " • LND 2 Macaroon: $CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon" +echo +echo "Environment Variables (available in other terminals):" +echo " • CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" +echo " • CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" +echo " • CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" +echo +echo "You can now:" +echo " • Use 'just' commands in other terminals: 'just ln-cln1 getinfo'" +echo " • Run integration tests: 'just mint-test' or 'cargo test -p cdk-integration-tests'" +echo " • Use CDK CLI tools with the mint URLs above" +echo " • Interact with Lightning nodes directly" +echo " • Access Bitcoin regtest node" +echo +echo "State File: /tmp/cdk_regtest_env (allows other terminals to find this environment)" +echo +echo "Starting mprocs to monitor logs..." +echo "Press 'q' to quit mprocs and stop the environment" +echo "==============================================" + +# Create mprocs configuration +MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs.yaml" +cat > "$MPROCS_CONFIG" << EOF +procs: + cln-mint: + shell: "touch $CDK_ITESTS_DIR/cln_mint/mintd.log && tail -f $CDK_ITESTS_DIR/cln_mint/mintd.log" + autostart: true + + lnd-mint: + shell: "touch $CDK_ITESTS_DIR/lnd_mint/mintd.log && tail -f $CDK_ITESTS_DIR/lnd_mint/mintd.log" + autostart: true + + bitcoind: + shell: "touch $CDK_ITESTS_DIR/bitcoin/regtest/debug.log && tail -f $CDK_ITESTS_DIR/bitcoin/regtest/debug.log" + autostart: true + + cln-one: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/one/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/one/regtest/log" + autostart: true + + cln-two: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/two/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/two/regtest/log" + autostart: true + + lnd-one: + 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" + autostart: true + + lnd-two: + 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" + autostart: true + +settings: + mouse_scroll_speed: 3 + proc_list_width: 20 + hide_keymap_window: false +EOF + +# Wait a bit for log files to be created +sleep 2 + +# Start mprocs to show all logs +if command -v mprocs >/dev/null 2>&1; then + cd "$CDK_ITESTS_DIR" + mprocs --config "$MPROCS_CONFIG" +else + echo "⚠️ mprocs not found. Install it with: cargo install mprocs" + echo "Falling back to simple wait loop..." + echo "Press Ctrl+C to stop the environment" + # Keep the script running + while true; do + sleep 1 + done +fi diff --git a/misc/interactive_regtest_mprocs.sh b/misc/interactive_regtest_mprocs.sh new file mode 100755 index 00000000..8f486fb6 --- /dev/null +++ b/misc/interactive_regtest_mprocs.sh @@ -0,0 +1,320 @@ +#!/usr/bin/env bash + +# Interactive Regtest Environment for CDK with Direct Process Management +# This script sets up mprocs to manage the mint processes directly + +set -e + +# Function to wait for HTTP endpoint +wait_for_endpoint() { + local url=$1 + local timeout=${2:-60} + local start_time=$(date +%s) + + while true; do + local current_time=$(date +%s) + local elapsed_time=$((current_time - start_time)) + + if [ $elapsed_time -ge $timeout ]; then + echo "❌ Timeout waiting for $url" + return 1 + fi + + local http_status=$(curl -o /dev/null -s -w "%{http_code}" "$url" 2>/dev/null || echo "000") + + if [ "$http_status" -eq 200 ]; then + echo "✓ $url is ready" + return 0 + fi + + sleep 2 + done +} + +# Function to perform cleanup +cleanup() { + echo "Cleaning up..." + + # Remove state file for other sessions + rm -f "/tmp/cdk_regtest_env" + + if [ ! -z "$CDK_REGTEST_PID" ] && kill -0 $CDK_REGTEST_PID 2>/dev/null; then + echo "Killing the cdk regtest" + kill -2 $CDK_REGTEST_PID + wait $CDK_REGTEST_PID + fi + + echo "Environment terminated" + + # Remove the temporary directory + if [ ! -z "$CDK_ITESTS_DIR" ]; then + rm -rf "$CDK_ITESTS_DIR" + echo "Temp directory removed: $CDK_ITESTS_DIR" + fi + + # Unset all environment variables + unset CDK_ITESTS_DIR + unset CDK_ITESTS_MINT_ADDR + unset CDK_ITESTS_MINT_PORT_0 + unset CDK_ITESTS_MINT_PORT_1 + unset CDK_MINTD_DATABASE + unset CDK_TEST_MINT_URL + unset CDK_TEST_MINT_URL_2 + unset CDK_REGTEST_PID + unset RUST_BACKTRACE + unset CDK_TEST_REGTEST +} + +# Set up trap to call cleanup on script exit +trap cleanup EXIT + +export CDK_TEST_REGTEST=1 + +# Check for mprocs and offer to install if missing +if ! command -v mprocs >/dev/null 2>&1; then + echo "⚠️ mprocs not found - this tool is required for direct process management" + echo "Install it with: cargo install mprocs" + echo + read -p "Would you like to install mprocs now? (y/n): " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Installing mprocs..." + cargo install mprocs + if [ $? -eq 0 ]; then + echo "✓ mprocs installed successfully" + else + echo "❌ Failed to install mprocs." + exit 1 + fi + else + echo "❌ mprocs is required for this mode. Exiting." + exit 1 + fi + echo +fi + +# Parse command line arguments +CDK_MINTD_DATABASE=${1:-"sqlite"} # Default to sqlite if not specified + +# Create a temporary directory +export CDK_ITESTS_DIR=$(mktemp -d) +export CDK_ITESTS_MINT_ADDR="127.0.0.1" +export CDK_ITESTS_MINT_PORT_0=8085 +export CDK_ITESTS_MINT_PORT_1=8087 + +# Check if the temporary directory was created successfully +if [[ ! -d "$CDK_ITESTS_DIR" ]]; then + echo "Failed to create temp directory" + exit 1 +fi + +echo "==============================================" +echo "Starting CDK Regtest with Direct Process Management" +echo "==============================================" +echo "Temp directory: $CDK_ITESTS_DIR" +echo "Database type: $CDK_MINTD_DATABASE" +echo + +export CDK_MINTD_DATABASE="$CDK_MINTD_DATABASE" + +# Build the necessary binaries +echo "Building binaries..." +cargo build -p cdk-integration-tests --bin start_regtest +cargo build --bin cdk-mintd + +echo "Starting regtest network (Bitcoin + Lightning nodes)..." +cargo run --bin start_regtest & +export CDK_REGTEST_PID=$! + +# Create named pipe for progress tracking +mkfifo "$CDK_ITESTS_DIR/progress_pipe" +rm -f "$CDK_ITESTS_DIR/signal_received" + +# Start reading from pipe in background +(while read line; do + case "$line" in + "checkpoint1") + echo "✓ Regtest network is ready" + touch "$CDK_ITESTS_DIR/signal_received" + exit 0 + ;; + esac +done < "$CDK_ITESTS_DIR/progress_pipe") & + +# Wait for regtest setup (up to 120 seconds) +echo "Waiting for regtest network to be ready..." +for ((i=0; i<120; i++)); do + if [ -f "$CDK_ITESTS_DIR/signal_received" ]; then + break + fi + sleep 1 +done + +if [ ! -f "$CDK_ITESTS_DIR/signal_received" ]; then + echo "❌ Timeout waiting for regtest network" + exit 1 +fi + +# Create work directories for mints +mkdir -p "$CDK_ITESTS_DIR/cln_mint" +mkdir -p "$CDK_ITESTS_DIR/lnd_mint" + +# Set environment variables for easy access +export CDK_TEST_MINT_URL="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_0" +export CDK_TEST_MINT_URL_2="http://$CDK_ITESTS_MINT_ADDR:$CDK_ITESTS_MINT_PORT_1" + +# Create state file for other terminal sessions +ENV_FILE="/tmp/cdk_regtest_env" +echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE" +echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE" +echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE" +echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE" + +# Get the project root directory (where justfile is located) +PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + +# Create environment setup scripts for mprocs to use +cat > "$CDK_ITESTS_DIR/start_cln_mint.sh" << EOF +#!/usr/bin/env bash +cd "$PROJECT_ROOT" +export CDK_MINTD_CLN_RPC_PATH="$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc" +export CDK_MINTD_URL="http://127.0.0.1:8085" +export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/cln_mint" +export CDK_MINTD_LISTEN_HOST="127.0.0.1" +export CDK_MINTD_LISTEN_PORT=8085 +export CDK_MINTD_LN_BACKEND="cln" +export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal" +export RUST_BACKTRACE=1 +export CDK_MINTD_DATABASE="$CDK_MINTD_DATABASE" + +echo "Starting CLN Mint on port 8085..." +echo "Project root: $PROJECT_ROOT" +echo "Working directory: \$CDK_MINTD_WORK_DIR" +echo "CLN RPC path: \$CDK_MINTD_CLN_RPC_PATH" +echo "Database type: \$CDK_MINTD_DATABASE" +echo "---" + +exec cargo run --bin cdk-mintd +EOF + +cat > "$CDK_ITESTS_DIR/start_lnd_mint.sh" << EOF +#!/usr/bin/env bash +cd "$PROJECT_ROOT" +export CDK_MINTD_LND_ADDRESS="https://localhost:10010" +export CDK_MINTD_LND_CERT_FILE="$CDK_ITESTS_DIR/lnd/two/tls.cert" +export CDK_MINTD_LND_MACAROON_FILE="$CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon" +export CDK_MINTD_URL="http://127.0.0.1:8087" +export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/lnd_mint" +export CDK_MINTD_LISTEN_HOST="127.0.0.1" +export CDK_MINTD_LISTEN_PORT=8087 +export CDK_MINTD_LN_BACKEND="lnd" +export CDK_MINTD_MNEMONIC="cattle gold bind busy sound reduce tone addict baby spend february strategy" +export RUST_BACKTRACE=1 +export CDK_MINTD_DATABASE="$CDK_MINTD_DATABASE" + +echo "Starting LND Mint on port 8087..." +echo "Project root: $PROJECT_ROOT" +echo "Working directory: \$CDK_MINTD_WORK_DIR" +echo "LND address: \$CDK_MINTD_LND_ADDRESS" +echo "Database type: \$CDK_MINTD_DATABASE" +echo "---" + +exec cargo run --bin cdk-mintd +EOF + +# Make scripts executable +chmod +x "$CDK_ITESTS_DIR/start_cln_mint.sh" +chmod +x "$CDK_ITESTS_DIR/start_lnd_mint.sh" + +echo +echo "==============================================" +echo "🎉 CDK Regtest Environment is Ready!" +echo "==============================================" +echo +echo "Network Information:" +echo " • Bitcoin RPC: 127.0.0.1:18443 (user: testuser, pass: testpass)" +echo " • CLN Node 1: $CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc" +echo " • CLN Node 2: $CDK_ITESTS_DIR/cln/two/regtest/lightning-rpc" +echo " • LND Node 1: https://localhost:10009" +echo " • LND Node 2: https://localhost:10010" +echo +echo "CDK Mints (will be managed by mprocs):" +echo " • CLN Mint: $CDK_TEST_MINT_URL" +echo " • LND Mint: $CDK_TEST_MINT_URL_2" +echo +echo "Files and Directories:" +echo " • Working Directory: $CDK_ITESTS_DIR" +echo " • Start Scripts: $CDK_ITESTS_DIR/start_{cln,lnd}_mint.sh" +echo +echo "Environment Variables (available in other terminals):" +echo " • CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" +echo " • CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" +echo " • CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" +echo +echo "Starting mprocs with direct process management..." +echo +echo "In mprocs you can:" +echo " • 's' to start a process" +echo " • 'k' to kill a process" +echo " • 'r' to restart a process" +echo " • 'Enter' to focus on a process" +echo " • 'q' to quit and stop the environment" +echo "==============================================" + +# Wait a moment for everything to settle +sleep 2 + +# Create mprocs configuration with direct process management +MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs.yaml" +cat > "$MPROCS_CONFIG" << EOF +procs: + cln-mint: + shell: "$CDK_ITESTS_DIR/start_cln_mint.sh" + autostart: false + env: + CDK_ITESTS_DIR: "$CDK_ITESTS_DIR" + CDK_MINTD_DATABASE: "$CDK_MINTD_DATABASE" + + lnd-mint: + shell: "$CDK_ITESTS_DIR/start_lnd_mint.sh" + autostart: false + env: + CDK_ITESTS_DIR: "$CDK_ITESTS_DIR" + CDK_MINTD_DATABASE: "$CDK_MINTD_DATABASE" + + bitcoind: + shell: "while [ ! -f $CDK_ITESTS_DIR/bitcoin/regtest/debug.log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/bitcoin/regtest/debug.log" + autostart: true + + cln-one: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/one/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/one/regtest/log" + autostart: true + + cln-two: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/two/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/two/regtest/log" + autostart: true + + lnd-one: + 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" + autostart: true + + lnd-two: + 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" + autostart: true + +settings: + mouse_scroll_speed: 3 + proc_list_width: 20 + hide_keymap_window: false + keymap_procs: + toggle_process: 's' + kill_process: 'k' + restart_process: 'r' + focus_process: 'Enter' + show_keymap: '?' +EOF + +# Start mprocs with direct process management +echo "Starting mprocs..." +cd "$CDK_ITESTS_DIR" +mprocs --config "$MPROCS_CONFIG" diff --git a/misc/regtest_helper.sh b/misc/regtest_helper.sh new file mode 100755 index 00000000..c0442c41 --- /dev/null +++ b/misc/regtest_helper.sh @@ -0,0 +1,421 @@ +#!/usr/bin/env bash + +# Helper script for interacting with CDK regtest environment +# Run this after starting interactive_regtest.sh + +# Check for environment state file first, then environment variable +ENV_FILE="/tmp/cdk_regtest_env" +if [ -f "$ENV_FILE" ]; then + source "$ENV_FILE" +elif [ ! -z "$CDK_ITESTS_DIR" ]; then + # Environment variable is set, create state file for other sessions + echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE" + echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE" + echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE" + echo "export CDK_MINTD_PID=\"$CDK_MINTD_PID\"" >> "$ENV_FILE" + echo "export CDK_MINTD_LND_PID=\"$CDK_MINTD_LND_PID\"" >> "$ENV_FILE" + echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE" +else + echo "❌ CDK regtest environment not found!" + echo "Please run './misc/interactive_regtest.sh' or 'just regtest' first" + exit 1 +fi + +# Validate that the environment is actually running +if [ -z "$CDK_ITESTS_DIR" ] || [ ! -d "$CDK_ITESTS_DIR" ]; then + echo "❌ CDK regtest environment not found or directory missing!" + echo "Please run './misc/interactive_regtest.sh' or 'just regtest' first" + [ -f "$ENV_FILE" ] && rm "$ENV_FILE" # Clean up stale state file + exit 1 +fi + +show_help() { + echo "CDK Regtest Environment Helper" + echo "=============================" + echo + echo "Lightning Node Commands:" + echo " ln-cln1 - Execute command on CLN node 1" + echo " ln-cln2 - Execute command on CLN node 2" + echo " ln-lnd1 - Execute command on LND node 1" + echo " ln-lnd2 - Execute command on LND node 2" + echo + echo "Bitcoin Commands:" + echo " btc - Execute bitcoin-cli command" + echo " btc-mine [blocks] - Mine blocks (default: 10)" + echo + echo "CDK Mint Commands:" + echo " mint-info - Show mint information" + echo " mint-test - Run integration tests" + echo " restart-mints - Stop, recompile, and restart both mints (log mode)" + echo + echo "Environment Commands:" + echo " show-env - Show environment variables" + echo " show-logs - Show recent mint logs" + echo " show-status - Show status of all components" + echo " logs - Start mprocs TUI (adapts to current mode)" + echo + echo "Environment Modes:" + echo " just regtest - Log tailing mode (mints auto-start, logs to files)" + echo " just regtest-mprocs - Direct management (mprocs controls mint processes)" + echo + echo "Examples:" + echo " $0 ln-cln1 getinfo" + echo " $0 ln-lnd1 getinfo" + echo " $0 btc getblockcount" + echo " $0 btc-mine 5" + echo " $0 mint-info" + echo " $0 restart-mints # Only works in log tailing mode" + echo " $0 logs # Start mprocs viewer" +} + +# Bitcoin commands +btc_command() { + bitcoin-cli -regtest -rpcuser=testuser -rpcpassword=testpass -rpcport=18443 "$@" +} + +btc_mine() { + local blocks=${1:-10} + local address=$(btc_command getnewaddress) + btc_command generatetoaddress "$blocks" "$address" + echo "Mined $blocks blocks" +} + +# CLN commands +cln_command() { + local node=$1 + shift + lightning-cli --rpc-file="$CDK_ITESTS_DIR/cln/$node/regtest/lightning-rpc" "$@" +} + +# LND commands +lnd_command() { + local node=$1 + shift + local port + case $node in + "one") port=10009 ;; + "two") port=10010 ;; + *) echo "Unknown LND node: $node"; return 1 ;; + esac + + lncli --rpcserver=localhost:$port \ + --tlscertpath="$CDK_ITESTS_DIR/lnd/$node/tls.cert" \ + --macaroonpath="$CDK_ITESTS_DIR/lnd/$node/data/chain/bitcoin/regtest/admin.macaroon" \ + "$@" +} + +# Mint commands +mint_info() { + echo "CLN Mint (Port 8085):" + curl -s "$CDK_TEST_MINT_URL/v1/info" | jq . 2>/dev/null || curl -s "$CDK_TEST_MINT_URL/v1/info" + echo + echo "LND Mint (Port 8087):" + curl -s "$CDK_TEST_MINT_URL_2/v1/info" | jq . 2>/dev/null || curl -s "$CDK_TEST_MINT_URL_2/v1/info" +} + +mint_test() { + echo "Running integration tests..." + cargo test -p cdk-integration-tests +} + +# Environment info +show_env() { + echo "CDK Regtest Environment Variables:" + echo "=================================" + echo "CDK_ITESTS_DIR=$CDK_ITESTS_DIR" + echo "CDK_TEST_MINT_URL=$CDK_TEST_MINT_URL" + echo "CDK_TEST_MINT_URL_2=$CDK_TEST_MINT_URL_2" + echo "CDK_MINTD_PID=$CDK_MINTD_PID" + echo "CDK_MINTD_LND_PID=$CDK_MINTD_LND_PID" + echo "CDK_REGTEST_PID=$CDK_REGTEST_PID" +} + +show_logs() { + echo "=== Recent CLN Mint Logs ===" + if [ -f "$CDK_ITESTS_DIR/cln_mint/mintd.log" ]; then + tail -10 "$CDK_ITESTS_DIR/cln_mint/mintd.log" + else + echo "Log file not found" + fi + echo + echo "=== Recent LND Mint Logs ===" + if [ -f "$CDK_ITESTS_DIR/lnd_mint/mintd.log" ]; then + tail -10 "$CDK_ITESTS_DIR/lnd_mint/mintd.log" + else + echo "Log file not found" + fi +} + +start_mprocs() { + echo "Starting mprocs log viewer..." + + if ! command -v mprocs >/dev/null 2>&1; then + echo "❌ mprocs not found! Please install it with:" + echo " cargo install mprocs" + echo " or your package manager" + return 1 + fi + + # Check if we have the direct process management config + DIRECT_MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs.yaml" + FALLBACK_MPROCS_CONFIG="$CDK_ITESTS_DIR/mprocs_fallback.yaml" + + if [ -f "$DIRECT_MPROCS_CONFIG" ]; then + echo "Using direct process management mode..." + echo "In mprocs: 's' to start, 'k' to kill, 'r' to restart processes" + cd "$CDK_ITESTS_DIR" + mprocs --config "$DIRECT_MPROCS_CONFIG" + return + fi + + # Create fallback mprocs configuration for log tailing + cat > "$FALLBACK_MPROCS_CONFIG" << EOF +procs: + cln-mint: + shell: "touch $CDK_ITESTS_DIR/cln_mint/mintd.log && tail -f $CDK_ITESTS_DIR/cln_mint/mintd.log" + autostart: true + + lnd-mint: + shell: "touch $CDK_ITESTS_DIR/lnd_mint/mintd.log && tail -f $CDK_ITESTS_DIR/lnd_mint/mintd.log" + autostart: true + + bitcoind: + shell: "touch $CDK_ITESTS_DIR/bitcoin/regtest/debug.log && tail -f $CDK_ITESTS_DIR/bitcoin/regtest/debug.log" + autostart: true + + cln-one: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/one/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/one/regtest/log" + autostart: true + + cln-two: + shell: "while [ ! -f $CDK_ITESTS_DIR/cln/two/regtest/log ]; do sleep 1; done && tail -f $CDK_ITESTS_DIR/cln/two/regtest/log" + autostart: true + + lnd-one: + 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" + autostart: true + + lnd-two: + 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" + autostart: true + +settings: + mouse_scroll_speed: 3 + proc_list_width: 20 + hide_keymap_window: false +EOF + + echo "Using log tailing mode..." + echo "Use 'q' to quit the log viewer" + cd "$CDK_ITESTS_DIR" + mprocs --config "$FALLBACK_MPROCS_CONFIG" +} + +show_status() { + echo "CDK Regtest Environment Status:" + echo "===============================" + + # Check processes + echo "Processes:" + if [ ! -z "$CDK_REGTEST_PID" ] && kill -0 $CDK_REGTEST_PID 2>/dev/null; then + echo " ✓ Regtest network (PID: $CDK_REGTEST_PID)" + else + echo " ❌ Regtest network" + fi + + if [ ! -z "$CDK_MINTD_PID" ] && kill -0 $CDK_MINTD_PID 2>/dev/null; then + echo " ✓ CLN Mint (PID: $CDK_MINTD_PID)" + else + echo " ❌ CLN Mint" + fi + + if [ ! -z "$CDK_MINTD_LND_PID" ] && kill -0 $CDK_MINTD_LND_PID 2>/dev/null; then + echo " ✓ LND Mint (PID: $CDK_MINTD_LND_PID)" + else + echo " ❌ LND Mint" + fi + + echo + echo "Network connectivity:" + if curl -s "$CDK_TEST_MINT_URL/v1/info" >/dev/null 2>&1; then + echo " ✓ CLN Mint responding" + else + echo " ❌ CLN Mint not responding" + fi + + if curl -s "$CDK_TEST_MINT_URL_2/v1/info" >/dev/null 2>&1; then + echo " ✓ LND Mint responding" + else + echo " ❌ LND Mint not responding" + fi +} + +restart_mints() { + echo "===============================" + echo "Restarting CDK Mints" + echo "===============================" + + # Stop existing mints + echo "Stopping existing mints..." + if [ ! -z "$CDK_MINTD_PID" ] && kill -0 $CDK_MINTD_PID 2>/dev/null; then + echo " Stopping CLN Mint (PID: $CDK_MINTD_PID)" + kill -2 $CDK_MINTD_PID + wait $CDK_MINTD_PID 2>/dev/null || true + fi + + if [ ! -z "$CDK_MINTD_LND_PID" ] && kill -0 $CDK_MINTD_LND_PID 2>/dev/null; then + echo " Stopping LND Mint (PID: $CDK_MINTD_LND_PID)" + kill -2 $CDK_MINTD_LND_PID + wait $CDK_MINTD_LND_PID 2>/dev/null || true + fi + + # Recompile + echo "Recompiling cdk-mintd..." + if ! cargo build --bin cdk-mintd; then + echo "❌ Compilation failed" + return 1 + fi + echo "✓ Compilation successful" + + # Restart CLN mint + echo "Starting CLN Mint..." + export CDK_MINTD_CLN_RPC_PATH="$CDK_ITESTS_DIR/cln/one/regtest/lightning-rpc" + export CDK_MINTD_URL="http://127.0.0.1:8085" + export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/cln_mint" + export CDK_MINTD_LISTEN_HOST="127.0.0.1" + export CDK_MINTD_LISTEN_PORT=8085 + export CDK_MINTD_LN_BACKEND="cln" + export CDK_MINTD_MNEMONIC="eye survey guilt napkin crystal cup whisper salt luggage manage unveil loyal" + export RUST_BACKTRACE=1 + + cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 & + NEW_CLN_PID=$! + + # Wait for CLN mint to be ready + echo "Waiting for CLN mint to start..." + local start_time=$(date +%s) + while true; do + local current_time=$(date +%s) + local elapsed_time=$((current_time - start_time)) + + if [ $elapsed_time -ge 30 ]; then + echo "❌ Timeout waiting for CLN mint" + return 1 + fi + + if curl -s "http://127.0.0.1:8085/v1/info" >/dev/null 2>&1; then + echo "✓ CLN Mint ready" + break + fi + sleep 1 + done + + # Restart LND mint + echo "Starting LND Mint..." + export CDK_MINTD_LND_ADDRESS="https://localhost:10010" + export CDK_MINTD_LND_CERT_FILE="$CDK_ITESTS_DIR/lnd/two/tls.cert" + export CDK_MINTD_LND_MACAROON_FILE="$CDK_ITESTS_DIR/lnd/two/data/chain/bitcoin/regtest/admin.macaroon" + export CDK_MINTD_URL="http://127.0.0.1:8087" + export CDK_MINTD_WORK_DIR="$CDK_ITESTS_DIR/lnd_mint" + export CDK_MINTD_LISTEN_HOST="127.0.0.1" + export CDK_MINTD_LISTEN_PORT=8087 + export CDK_MINTD_LN_BACKEND="lnd" + export CDK_MINTD_MNEMONIC="cattle gold bind busy sound reduce tone addict baby spend february strategy" + + cargo run --bin cdk-mintd > "$CDK_MINTD_WORK_DIR/mintd.log" 2>&1 & + NEW_LND_PID=$! + + # Wait for LND mint to be ready + echo "Waiting for LND mint to start..." + start_time=$(date +%s) + while true; do + current_time=$(date +%s) + elapsed_time=$((current_time - start_time)) + + if [ $elapsed_time -ge 30 ]; then + echo "❌ Timeout waiting for LND mint" + return 1 + fi + + if curl -s "http://127.0.0.1:8087/v1/info" >/dev/null 2>&1; then + echo "✓ LND Mint ready" + break + fi + sleep 1 + done + + # Update PIDs in state file + CDK_MINTD_PID=$NEW_CLN_PID + CDK_MINTD_LND_PID=$NEW_LND_PID + + # Update state file + echo "export CDK_ITESTS_DIR=\"$CDK_ITESTS_DIR\"" > "$ENV_FILE" + echo "export CDK_TEST_MINT_URL=\"$CDK_TEST_MINT_URL\"" >> "$ENV_FILE" + echo "export CDK_TEST_MINT_URL_2=\"$CDK_TEST_MINT_URL_2\"" >> "$ENV_FILE" + echo "export CDK_MINTD_PID=\"$CDK_MINTD_PID\"" >> "$ENV_FILE" + echo "export CDK_MINTD_LND_PID=\"$CDK_MINTD_LND_PID\"" >> "$ENV_FILE" + echo "export CDK_REGTEST_PID=\"$CDK_REGTEST_PID\"" >> "$ENV_FILE" + + echo + echo "✅ Mints restarted successfully!" + echo " CLN Mint: http://127.0.0.1:8085 (PID: $CDK_MINTD_PID)" + echo " LND Mint: http://127.0.0.1:8087 (PID: $CDK_MINTD_LND_PID)" + echo "===============================" +} + +# Main command dispatcher +case "$1" in + "ln-cln1") + shift + cln_command "one" "$@" + ;; + "ln-cln2") + shift + cln_command "two" "$@" + ;; + "ln-lnd1") + shift + lnd_command "one" "$@" + ;; + "ln-lnd2") + shift + lnd_command "two" "$@" + ;; + "btc") + shift + btc_command "$@" + ;; + "btc-mine") + shift + btc_mine "$@" + ;; + "mint-info") + mint_info + ;; + "mint-test") + mint_test + ;; + "restart-mints") + restart_mints + ;; + "show-env") + show_env + ;; + "show-logs") + show_logs + ;; + "show-status") + show_status + ;; + "logs") + start_mprocs + ;; + "help"|"-h"|"--help"|"") + show_help + ;; + *) + echo "Unknown command: $1" + echo "Run '$0 help' for available commands" + exit 1 + ;; +esac