LNURL Balancer Testing Environment
This directory contains the Docker-based regtest environment for testing Switchgear with Lightning Network nodes, plus the switchgear-testing Rust crate for accessing test credentials and port allocation.
Architecture
The regtest environment provides:
- Bitcoin Core (regtest mode) - Local Bitcoin network for testing
- Core Lightning (CLN) - Lightning Network node implementation
- LND - Lightning Network Daemon implementation
- PostgreSQL - Database for testing
- MySQL - Alternative database for testing
- Setup Container - Automated initialization and credential extraction
- Testing Crate - Rust library for reading Lightning node credentials and dynamic port allocation
Quick Start
-
Start the environment:
-
Run integration tests:
Components
Docker Services
- bitcoin (port 18443) - Bitcoin Core in regtest mode
- cln (ports 9735, 9736) - Core Lightning node with gRPC
- lnd (ports 8080, 9734, 10009) - LND node with REST and gRPC APIs
- postgres (port 5432) - PostgreSQL database
- mysql (port 3306) - MySQL database
- setup - Initialization container that:
- Generates initial Bitcoin blocks
- Funds Lightning node wallets
- Creates bidirectional payment channels
- Extracts credentials and external addresses to shared volume
Credential Management
The setup container automatically extracts Lightning node credentials and external addresses to a mounted directory (./shared-credentials/credentials) containing:
CLN Credentials:
cln*/node_id- Node public key (hex-encoded)cln*/address.txt- External gRPC address (host:port format)cln*/ca.pem- CA certificatecln*/client.pem- Client certificatecln*/client-key.pem- Client private keycln*/access.rune- Access rune for authentication
LND Credentials:
lnd*/node_id- Node public key (hex-encoded)lnd*/address.txt- External gRPC address (host:port format)lnd*/tls.cert- TLS certificatelnd*/admin.macaroon- Admin authentication macaroon (binary)
Note: Multiple nodes of each type may be present with numbered suffixes (e.g., cln1, cln2, lnd1, lnd2)
Testing Crate (switchgear-testing)
The testing crate provides a Rust library for accessing Lightning node credentials and dynamic port allocation for integration tests.
API
The crate exposes functionality for credential management and port allocation:
Credentials API
use ;
// Get all backends
let backends = get_backends?;
// Match on node types
for backend in backends
Port Allocation API
use PortAllocator;
use PathBuf;
// Find an available port (with file-based locking to prevent conflicts)
let ports_path = from;
let port = find_available_port?;
Credential Types
The crate uses strongly-typed credential structures:
All credentials are provided as file paths for maximum flexibility and security.
Data Structures
The RegTestLnNode enum provides convenience methods:
public_key()- Get the node's public keyaddress()- Get the node's addresskind()- Get the node type ("cln" or "lnd")
Environment Configuration
Set LNURL_BALANCER_CREDENTIALS_PATH to specify the credentials directory:
To skip integration tests:
Integration Testing
Integration tests use the testing crate to access credentials:
use get_backends;
// Get all available backends
let backends = get_backends?;
// Filter by type if needed
let cln_nodes: = backends.iter
.filter
.collect;
Test Environment Requirements:
- Set
LNURL_BALANCER_CREDENTIALS_PATHenvironment variable - Or set
LNURL_SKIP_INTEGRATION_TESTS=trueto skip integration tests
Configuration
Environment Variables
LNURL_BALANCER_CREDENTIALS_PATH- Path to credentials directory (required for tests)- Example:
/Users/user/dev/lnurl-balancer/testing/shared-credentials/credentials - Used by
get_backends()to locate credential files - Must be set when running integration tests
- Example:
LNURL_SKIP_INTEGRATION_TESTS- Set totrueto skip integration tests- Useful for CI/CD environments without Docker
- Returns empty backend list when set
Ports
18443- Bitcoin Core RPC18444- Bitcoin P2P8080- LND REST API9734- LND P2P9735- CLN P2P9736- CLN gRPC (external address used in tests)10009- LND gRPC (external address used in tests)28332-28333- Bitcoin Core ZMQ5432- PostgreSQL3306- MySQL
External addresses for Lightning nodes are automatically configured:
- CLN:
127.0.0.1:9736 - LND:
127.0.0.1:10009
Troubleshooting
Setup Issues
Check the setup container logs:
Common issues:
- Wallet creation failures - Usually resolved by restarting:
docker-compose down && docker-compose up -d - Channel creation timeouts - Lightning nodes may need more time to sync
- Block generation errors - Bitcoin Core may need time to start
Integration Test Issues
- Credentials not found - Ensure setup completed successfully and check
./shared-credentials/credentials/ - Connection failures - Verify Docker containers are running:
docker-compose ps - Permission errors - Check file permissions in credentials directory
- Address parsing errors - Verify
address.txtfiles contain validhost:portformat - Missing testing crate - Ensure
switchgear-testingis added as a dev-dependency - Port conflicts - The PortAllocator uses file-based locking to prevent conflicts
- Environment not set - Remember to set
LNURL_BALANCER_CREDENTIALS_PATHorLNURL_SKIP_INTEGRATION_TESTS
Clean Restart
To completely reset the environment:
Development Workflow
- Start environment:
docker-compose up -d - Wait for setup:
docker-compose logs -f setup - Set credentials path:
export LNURL_BALANCER_CREDENTIALS_PATH=$(pwd)/shared-credentials/credentials - Run tests:
cargo test --release - Iterate: Make changes and re-run tests
- Clean up:
docker-compose down(ordown -vfor full reset)