Skip to content

Commit

Permalink
Run tests on regtest
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Mar 6, 2025
1 parent 840404a commit 8ce332e
Show file tree
Hide file tree
Showing 27 changed files with 910 additions and 522 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Generate cache key
run: echo "${{ matrix.rust }}" | tee .cache_key
- name: cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ jobs:
run: make cargo-test
- name: Run wasm-pack test
run: make wasm-test

regtest-test:
name: Run regtest tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Set up Docker
uses: docker/setup-buildx-action@v2
- name: Set up Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Set default toolchain
run: rustup default nightly
- name: Set profile
run: rustup set profile minimal
- name: Add wasm target
run: rustup target add wasm32-unknown-unknown
- name: Install wasm-pack
run: cargo install wasm-pack
- name: Start regtest environment
working-directory: regtest
run: sh start.sh
- name: Run cargo regtest tests
run: make cargo-regtest-test
- name: Run WASM regtest tests
run: make wasm-regtest-test
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "regtest/boltz"]
path = regtest/boltz
url = https://github.com/BoltzExchange/regtest
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ secp256k1-zkp = { git = "https://github.com/danielgranhao/rust-secp256k1-zkp.git

[dev-dependencies]
futures-util = "0.3.31"
serial_test = "3.2.0"

[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dev-dependencies]
bitcoind = { version = "0.36.0", features = ["25_0"] }
Expand All @@ -65,9 +66,14 @@ tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies]
wasm-bindgen-test = "0.3.50"
wasm-bindgen = "0.2.100"
gloo-timers = { version = "0.3.0", features = ["futures"] }
futures = "0.3.31"

[features]
default = ["esplora"]
lnurl = ["dep:lnurl-rs"]
esplora = []
electrum = ["dep:electrum-client"]
# Feature to enable tests that require previous initialization of a regtest environment
regtest = []
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ifeq ($(UNAME), Darwin)
CLANG_PREFIX += AR=$(shell brew --prefix llvm)/bin/llvm-ar CC=$(shell brew --prefix llvm)/bin/clang
endif

LND_MACAROON_HEX=$(shell xxd -p regtest/boltz/data/lnd1/data/chain/bitcoin/regtest/admin.macaroon | tr -d '\n')
BITCOIND_COOKIE=$(shell cat regtest/boltz/data/bitcoind/regtest/.cookie)
REGTEST_PREFIX = LND_MACAROON_HEX=$(LND_MACAROON_HEX) BITCOIND_COOKIE=$(BITCOIND_COOKIE)

init:
cargo install wasm-pack

Expand All @@ -19,20 +23,36 @@ clippy: cargo-clippy wasm-clippy

test: cargo-test wasm-test

regtest-test: cargo-regtest-test wasm-regtest-test

cargo-clippy:
cargo clippy --all-targets --all-features -- -D warnings

cargo-test:
cargo test -- --nocapture
cargo test --features "esplora, electrum, lnurl" -- --nocapture

cargo-regtest-test:
$(REGTEST_PREFIX) cargo test regtest --features "electrum, regtest" -- --nocapture

wasm-clippy:
$(CLANG_PREFIX) cargo clippy --target=wasm32-unknown-unknown --all-features -- -D warnings

BROWSER ?= firefox

wasm-test:
$(CLANG_PREFIX) wasm-pack test --headless --firefox
$(CLANG_PREFIX) wasm-pack test --headless --$(BROWSER)

wasm-test-chrome:
$(CLANG_PREFIX) wasm-pack test --headless --chrome
BROWSER=chrome $(MAKE) wasm-test

wasm-test-safari:
$(CLANG_PREFIX) wasm-pack test --headless --safari
BROWSER=safari $(MAKE) wasm-test

wasm-regtest-test:
$(CLANG_PREFIX) $(REGTEST_PREFIX) WASM_BINDGEN_TEST_TIMEOUT=500 wasm-pack test --headless --$(BROWSER) --features regtest -- regtest

wasm-regtest-test-chrome:
BROWSER=chrome $(MAKE) wasm-regtest-test

wasm-regtest-test-safari:
BROWSER=safari $(MAKE) wasm-regtest-test
1 change: 1 addition & 0 deletions regtest/boltz
Submodule boltz added at 0379ce
17 changes: 17 additions & 0 deletions regtest/proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.10-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY ssl_proxy.py .

EXPOSE 51234

CMD ["gunicorn", "--bind", "0.0.0.0:51234", "--workers", "2", "ssl_proxy:app"]
4 changes: 4 additions & 0 deletions regtest/proxy/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
ssl-proxy:
network_mode: "host"
build: .
5 changes: 5 additions & 0 deletions regtest/proxy/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flask==2.2.5
flask-cors==3.0.10
requests==2.28.2
gunicorn==20.1.0
werkzeug==2.2.3
106 changes: 106 additions & 0 deletions regtest/proxy/ssl_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from flask import Flask, request, json, Response
from flask_cors import CORS
import requests
import logging

app = Flask(__name__)
CORS(app) # Allow all origins

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@app.route('/proxy', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy_request():
target_url = request.headers.get('X-Proxy-URL')

if not target_url:
return Response(
json.dumps({"error": "No target URL provided"}),
status=400,
mimetype='application/json'
)

try:
# Log the incoming request details
logger.info(f"Proxy request to: {target_url}")
logger.info(f"Request method: {request.method}")
logger.info(f"Request headers: {dict(request.headers)}")

# Try to log request body
try:
request_body = request.get_json(silent=True)
logger.info(f"Request body: {request_body}")
except Exception as body_log_error:
logger.error(f"Could not log request body: {body_log_error}")

# Prepare headers, excluding Flask-specific ones
headers = {
key: value for (key, value) in request.headers
if key not in ['Host', 'X-Proxy-URL', 'Content-Length']
}

# Determine the method and make the request
methods = {
'GET': requests.get,
'POST': requests.post,
'PUT': requests.put,
'DELETE': requests.delete
}

method = methods.get(request.method)
if not method:
return Response(
json.dumps({"error": "Unsupported HTTP method"}),
status=405,
mimetype='application/json'
)

# Prepare request arguments
kwargs = {
'url': target_url,
'headers': headers,
'verify': False # Bypass SSL verification
}

# Add data for methods that support it
if request.method in ['POST', 'PUT']:
# Try to parse request data as JSON if possible
request_json = request.get_json(silent=True)
if request_json:
kwargs['json'] = request_json
else:
kwargs['data'] = request.get_data()

# Make the request
try:
response = method(**kwargs)
except Exception as request_error:
logger.error(f"Request error: {request_error}")
return Response(
json.dumps({
"error": "Failed to make request to target service",
"details": str(request_error)
}),
status=500,
mimetype='application/json'
)

return Response(
response.text,
status=response.status_code,
mimetype='application/json'
)

except Exception as e:
logger.error(f"Unexpected error: {e}")
return Response(
json.dumps({"error": str(e)}),
status=500,
mimetype='application/json'
)


if __name__ == '__main__':
app.run(host='0.0.0.0', port=51234)
9 changes: 9 additions & 0 deletions regtest/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -xe

cd proxy
docker compose down
docker compose up --remove-orphans -d

cd ../boltz
./start.sh
8 changes: 8 additions & 0 deletions regtest/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -xe

cd boltz
./stop.sh

cd ../proxy
docker compose down --volumes
6 changes: 6 additions & 0 deletions set_regtest_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
export LND_MACAROON_HEX=$(xxd -p regtest/boltz/data/lnd1/data/chain/bitcoin/regtest/admin.macaroon | tr -d '\n')
export BITCOIND_COOKIE=$(<regtest/boltz/data/bitcoind/regtest/.cookie)

echo "LND_MACAROON_HEX set to: $LND_MACAROON_HEX"
echo "BITCOIND_COOKIE set to: $BITCOIND_COOKIE"
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Error {
Hash(bitcoin::hashes::FromSliceError),
Locktime(String),
Url(url::ParseError),
WebSocket(tokio_tungstenite_wasm::Error),
WebSocket(Box<tokio_tungstenite_wasm::Error>),
Taproot(String),
Musig2(String),
Generic(String),
Expand Down Expand Up @@ -186,7 +186,7 @@ impl From<url::ParseError> for Error {

impl From<tokio_tungstenite_wasm::Error> for Error {
fn from(value: tokio_tungstenite_wasm::Error) -> Self {
Self::WebSocket(value)
Self::WebSocket(value.into())
}
}

Expand Down
Loading

0 comments on commit 8ce332e

Please sign in to comment.