Skip to content

Commit

Permalink
Mint: Add LndRest and regtest tests (#359)
Browse files Browse the repository at this point in the history
* update

* working

* test with lnd

* update action

* cache poetry

* add lndrest

* enable regtest

* add regtests.yml

* poetry version

* add helpers

* save

* run legend regtest fork

* actually start

* use bash

* give rights

* remove cache?

* change order

* tests succeed with lndrestwallet

* check if wallet is set

* settings for regtest

* fix fakewallet test

* remove wacky balance check

* adjust permissions

* try with sudo

* adjust example

* remove eclair
  • Loading branch information
callebtc authored Nov 15, 2023
1 parent 286f850 commit 45d3059
Show file tree
Hide file tree
Showing 21 changed files with 816 additions and 63 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ MINT_DERIVATION_PATH="0/0/0/0"
MINT_DATABASE=data/mint

# Lightning
# Supported: LNbitsWallet, FakeWallet
# Supported: LndRestWallet, LNbitsWallet, FakeWallet
MINT_LIGHTNING_BACKEND=LNbitsWallet

# for use with LNbitsWallet
MINT_LNBITS_ENDPOINT=https://legend.lnbits.com
MINT_LNBITS_KEY=yourkeyasdasdasd

# LndRestWallet
MINT_LND_REST_ENDPOINT=https://127.0.0.1:8086
MINT_LND_REST_CERT="/home/lnd/.lnd/tls.cert"
MINT_LND_REST_MACAROON="/home/lnd/.lnd/data/chain/bitcoin/regtest/admin.macaroon"

# fee to reserve in percent of the amount
LIGHTNING_FEE_PERCENT=1.0
# minimum fee to reserve
Expand Down
27 changes: 27 additions & 0 deletions .github/actions/prepare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: prepare

inputs:
python-version:
description: "Python Version"
required: true
default: "3.10"
poetry-version:
description: "Poetry Version"
default: "1.5.1"

runs:
using: "composite"
steps:
- name: Set up Poetry ${{ inputs.poetry-version }}
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ inputs.poetry-version }}
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}
cache: "poetry"
- name: Install dependencies
run: |
poetry install --extras pgsql
shell: bash
34 changes: 17 additions & 17 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
name: checks

on: [push, pull_request]
on:
workflow_call:
inputs:
python-version:
default: "3.10.4"
type: string
poetry-version:
default: "1.5.1"
type: string

jobs:
formatting:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10.4"]
poetry-version: ["1.5.1"]
steps:
- uses: actions/checkout@v2
- name: Set up Poetry ${{ matrix.poetry-version }}
- name: Set up Poetry ${{ inputs.poetry-version }}
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Set up Python ${{ matrix.python-version }}
poetry-version: ${{ inputs.poetry-version }}
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ inputs.python-version }}
cache: "poetry"
- name: Install packages
run: poetry install
- name: Check black
run: make black-check
mypy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10.4"]
poetry-version: ["1.5.1"]
steps:
- uses: actions/checkout@v2
- name: Set up Poetry ${{ matrix.poetry-version }}
- name: Set up Poetry ${{ inputs.poetry-version }}
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Set up Python ${{ matrix.python-version }}
poetry-version: ${{ inputs.poetry-version }}
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ inputs.python-version }}
cache: "poetry"
- name: Install packages
run: poetry install
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Nutshell CI
on:
push:
branches:
- main
pull_request:

jobs:
checks:
uses: ./.github/workflows/checks.yml
tests:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.9", "3.10"]
poetry-version: ["1.5.1"]
# db-url: ["", "postgres://cashu:cashu@localhost:5432/test"] # TODO: Postgres test not working
db-url: [""]
backend-wallet-class: ["FakeWallet"]
uses: ./.github/workflows/tests.yml
with:
python-version: ${{ matrix.python-version }}
poetry-version: ${{ matrix.poetry-version }}
regtest:
uses: ./.github/workflows/regtest.yml
strategy:
matrix:
python-version: ["3.10"]
poetry-version: ["1.5.1"]
backend-wallet-class: ["LndRestWallet", "LNbitsWallet"]
with:
python-version: ${{ matrix.python-version }}
backend-wallet-class: ${{ matrix.backend-wallet-class }}
74 changes: 74 additions & 0 deletions .github/workflows/regtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: regtest

on:
workflow_call:
inputs:
python-version:
default: "3.10.4"
type: string
poetry-version:
default: "1.5.1"
type: string
os-version:
default: "ubuntu-latest"
type: string
db-url:
default: ""
type: string
backend-wallet-class:
required: true
type: string

jobs:
regtest:
runs-on: ${{ inputs.os-version }}
timeout-minutes: 10
steps:
- uses: actions/checkout@v3

- uses: ./.github/actions/prepare
with:
python-version: ${{ inputs.python-version }}
poetry-version: ${{ inputs.poetry-version }}

- name: Setup Regtest
run: |
git clone https://github.com/callebtc/cashu-regtest-enviroment.git regtest
cd regtest
chmod -R 777 .
bash ./start.sh
- name: Create fake admin
if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
run: docker exec cashu-lnbits-1 poetry run python tools/create_fake_admin.py

- name: Run Tests
env:
WALLET_NAME: test_wallet
MINT_HOST: localhost
MINT_PORT: 3337
MINT_DATABASE: ${{ inputs.db-url }}
TOR: false
MINT_LIGHTNING_BACKEND: ${{ inputs.backend-wallet-class }}
MINT_LNBITS_ENDPOINT: http://localhost:5001
MINT_LNBITS_KEY: d08a3313322a4514af75d488bcc27eee
MINT_LND_REST_ENDPOINT: https://localhost:8081/
MINT_LND_REST_CERT: ./regtest/data/lnd-3/tls.cert
MINT_LND_REST_MACAROON: ./regtest/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
# LND_GRPC_ENDPOINT: localhost
# LND_GRPC_PORT: 10009
# LND_GRPC_CERT: docker/data/lnd-3/tls.cert
# LND_GRPC_MACAROON: docker/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
# CORELIGHTNING_RPC: ./docker/data/clightning-1/regtest/lightning-rpc
# CORELIGHTNING_REST_URL: https://localhost:3001
# CORELIGHTNING_REST_MACAROON: ./docker/data/clightning-2-rest/access.macaroon
# CORELIGHTNING_REST_CERT: ./docker/data/clightning-2-rest/certificate.pem
run: |
sudo chmod -R 777 .
make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
44 changes: 20 additions & 24 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
name: tests

on: [push, pull_request]
on:
workflow_call:
inputs:
python-version:
default: "3.10.4"
type: string
poetry-version:
default: "1.5.1"
type: string
db-url:
default: ""
type: string
os:
default: "ubuntu-latest"
type: string

jobs:
poetry:
runs-on: ${{ matrix.os }}
runs-on: ${{ inputs.os }}
services:
postgres:
image: postgres:latest
Expand All @@ -19,34 +33,16 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.10.4"]
poetry-version: ["1.5.1"]
# db-url: ["", "postgres://cashu:cashu@localhost:5432/test"] # TODO: Postgres test not working
db-url: [""]
steps:
- name: Checkout repository and submodules
uses: actions/checkout@v2
- uses: ./.github/actions/prepare
with:
submodules: recursive
- name: Set up Poetry ${{ matrix.poetry-version }}
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install dependencies
run: |
poetry install --extras pgsql
shell: bash
python-version: ${{ inputs.python-version }}
poetry-version: ${{ inputs.poetry-version }}
- name: Run tests
env:
LIGHTNING: false
MINT_LIGHTNING_BACKEND: FakeWallet
WALLET_NAME: test_wallet
MINT_HOST: localhost
MINT_PORT: 3337
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ package:
python setup.py sdist bdist_wheel

test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest tests --cov-report xml --cov cashu

test-lndrest:
PYTHONUNBUFFERED=1 \
DEBUG=true \
MINT_LIGHTNING_BACKEND=LndRestWallet \
MINT_LND_REST_ENDPOINT=https://localhost:8081/ \
MINT_LND_REST_CERT=../cashu-regtest-enviroment/data/lnd-3/tls.cert \
MINT_LND_REST_MACAROON=../cashu-regtest-enviroment/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon \
poetry run pytest tests/test_cli.py --cov-report xml --cov cashu

install:
make clean
python setup.py sdist bdist_wheel
Expand Down
11 changes: 10 additions & 1 deletion cashu/core/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from pathlib import Path
from typing import List
from typing import List, Optional

from environs import Env # type: ignore
from pydantic import BaseSettings, Extra, Field
Expand Down Expand Up @@ -99,8 +99,17 @@ class WalletSettings(CashuSettings):
locktime_delta_seconds: int = Field(default=86400) # 1 day


class LndRestFundingSource(MintSettings):
mint_lnd_rest_endpoint: Optional[str] = Field(default=None)
mint_lnd_rest_cert: Optional[str] = Field(default=None)
mint_lnd_rest_macaroon: Optional[str] = Field(default=None)
mint_lnd_rest_admin_macaroon: Optional[str] = Field(default=None)
mint_lnd_rest_invoice_macaroon: Optional[str] = Field(default=None)


class Settings(
EnvSettings,
LndRestFundingSource,
MintSettings,
MintInformation,
WalletSettings,
Expand Down
5 changes: 5 additions & 0 deletions cashu/lightning/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# type: ignore
from ..core.settings import settings
from .fake import FakeWallet # noqa: F401
from .lnbits import LNbitsWallet # noqa: F401
from .lndrest import LndRestWallet # noqa: F401

if settings.mint_lightning_backend is None:
raise Exception("MINT_LIGHTNING_BACKEND not configured")
Loading

0 comments on commit 45d3059

Please sign in to comment.