Skip to content

Commit

Permalink
Merge pull request #2 from BoltzExchange/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Aug 21, 2024
2 parents 3ff0ef1 + 67b7437 commit 95b545d
Show file tree
Hide file tree
Showing 32 changed files with 2,073 additions and 121 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
strategy:
matrix:
platform: [ ubuntu-latest ]
python-version: [ '3.10' ]
rust-version: [ stable, nightly ]

runs-on: ${{ matrix.platform }}
Expand Down Expand Up @@ -35,3 +36,30 @@ jobs:

- name: Clippy
run: cargo clippy

- name: Unit tests
run: cargo test

- name: Install Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: abatilo/actions-poetry@v3

- name: Install Python dependencies
run: make python-install

- name: Python lint
run: make python-lint

- name: Python formatting
run: poetry run ruff format --check
working-directory: ./tests

- name: Regtest startup
run: make regtest-start

- name: Integration tests
run: make integration-tests
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ target/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

tests/hold/protos/*
!tests/hold/protos/__init__.py
**/__pycache__
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "regtest"]
path = regtest
url = https://github.com/BoltzExchange/regtest
78 changes: 78 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ bitcoin = { version = "0.30.2", features = ["rand-std"] }
secp256k1 = "0.27.0"
cln-rpc = "0.1.9"
hex = "0.4.3"
tokio-util = "0.7.11"

[build-dependencies]
built = { version = "0.7.4", features = ["git2"] }
tonic-build = "0.12.1"

[dev-dependencies]
mockall = "0.13.0"
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
build:
cargo build

build-release:
cargo build --release

python-install:
cd tests && poetry install

python-lint:
cd tests && poetry run ruff check

python-format:
cd tests && poetry run ruff format

python-protos:
cd tests && poetry run python -m grpc_tools.protoc -I ../protos \
--python_out=hold/protos \
--pyi_out=hold/protos \
--grpc_python_out=hold/protos \
../protos/hold.proto

regtest-start:
git submodule init
git submodule update
chmod -R 777 regtest 2> /dev/null || true
cd regtest && COMPOSE_PROFILES=ci ./start.sh
mkdir regtest/data/cln2/plugins
cp target/debug/hold regtest/data/cln2/plugins/
docker exec boltz-cln-2 lightning-cli --regtest plugin stop /root/hold.sh
rm -rf regtest/data/cln2/regtest/hold/
docker exec boltz-cln-2 lightning-cli --regtest plugin start /root/.lightning/plugins/hold

sleep 1
docker exec boltz-cln-2 chmod 777 -R /root/.lightning/regtest/hold

make python-protos

regtest-stop:
cd regtest && ./stop.sh

db-start:
docker run --name hold-db --rm -e POSTGRES_DB=hold -e POSTGRES_USER=hold -e POSTGRES_PASSWORD=hold \
-d -p 5433:5432 postgres:14-alpine

db-stop:
docker stop hold-db

integration-tests:
cd tests && poetry run pytest
1 change: 0 additions & 1 deletion db-start.sh

This file was deleted.

1 change: 0 additions & 1 deletion db-stop.sh

This file was deleted.

1 change: 1 addition & 0 deletions regtest
Submodule regtest added at dd1b4f
4 changes: 3 additions & 1 deletion src/commands/cancel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::commands::structs::{parse_args, FromArr, ParamsError};
use crate::database::helpers::invoice_helper::InvoiceHelper;
use crate::encoder::InvoiceEncoder;
use crate::State;
use cln_plugin::Plugin;
use serde::{Deserialize, Serialize};
Expand All @@ -25,9 +26,10 @@ impl FromArr for CancelRequest {
#[derive(Debug, Serialize)]
struct CancelResponse {}

pub async fn cancel<T>(plugin: Plugin<State<T>>, args: Value) -> anyhow::Result<Value>
pub async fn cancel<T, E>(plugin: Plugin<State<T, E>>, args: Value) -> anyhow::Result<Value>
where
T: InvoiceHelper + Sync + Send + Clone,
E: InvoiceEncoder + Sync + Send + Clone,
{
let params = parse_args::<CancelRequest>(args)?;
let payment_hash = hex::decode(params.payment_hash)?;
Expand Down
15 changes: 7 additions & 8 deletions src/commands/invoice.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::commands::structs::{parse_args, FromArr, ParamsError};
use crate::database::helpers::invoice_helper::InvoiceHelper;
use crate::database::model::{InvoiceInsertable, InvoiceState};
use crate::encoder::InvoiceBuilder;
use crate::encoder::{InvoiceBuilder, InvoiceEncoder};
use crate::State;
use anyhow::Result;
use cln_plugin::Plugin;
use log::info;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Debug;
Expand Down Expand Up @@ -34,9 +33,10 @@ struct InvoiceResponse {
bolt11: String,
}

pub async fn invoice<T>(plugin: Plugin<State<T>>, args: Value) -> Result<Value>
pub async fn invoice<T, E>(plugin: Plugin<State<T, E>>, args: Value) -> Result<Value>
where
T: InvoiceHelper + Sync + Send + Clone,
E: InvoiceEncoder + Sync + Send + Clone,
{
let params = parse_args::<InvoiceRequest>(args)?;
let payment_hash = hex::decode(params.payment_hash)?;
Expand All @@ -51,11 +51,10 @@ where
payment_hash: payment_hash.clone(),
state: InvoiceState::Unpaid.into(),
})?;
info!(
"Added hold invoice {} for {}msat",
hex::encode(payment_hash),
params.amount
);
plugin
.state()
.settler
.new_invoice(invoice.clone(), payment_hash, params.amount);

Ok(serde_json::to_value(&InvoiceResponse { bolt11: invoice })?)
}
4 changes: 3 additions & 1 deletion src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::structs::{parse_args, FromArr, ParamsError};
use crate::database::helpers::invoice_helper::InvoiceHelper;
use crate::database::model::{HoldInvoice, Htlc};
use crate::encoder::InvoiceEncoder;
use crate::State;
use cln_plugin::Plugin;
use lightning_invoice::Bolt11Invoice;
Expand Down Expand Up @@ -64,9 +65,10 @@ struct ListInvoicesResponse {
holdinvoices: Vec<PrettyHoldInvoice>,
}

pub async fn list_invoices<T>(plugin: Plugin<State<T>>, args: Value) -> anyhow::Result<Value>
pub async fn list_invoices<T, E>(plugin: Plugin<State<T, E>>, args: Value) -> anyhow::Result<Value>
where
T: InvoiceHelper + Sync + Send + Clone,
E: InvoiceEncoder + Sync + Send + Clone,
{
let params = parse_args::<ListInvoicesRequest>(args)?;
if params.bolt11.is_some() && params.payment_hash.is_some() {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/settle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::commands::structs::{parse_args, FromArr, ParamsError};
use crate::database::helpers::invoice_helper::InvoiceHelper;
use crate::encoder::InvoiceEncoder;
use crate::State;
use bitcoin::hashes::{sha256, Hash};
use cln_plugin::Plugin;
Expand All @@ -26,9 +27,10 @@ impl FromArr for SettleRequest {
#[derive(Debug, Serialize)]
struct SettleResponse {}

pub async fn settle<T>(plugin: Plugin<State<T>>, args: Value) -> anyhow::Result<Value>
pub async fn settle<T, E>(plugin: Plugin<State<T, E>>, args: Value) -> anyhow::Result<Value>
where
T: InvoiceHelper + Sync + Send + Clone,
E: InvoiceEncoder + Sync + Send + Clone,
{
let params = parse_args::<SettleRequest>(args)?;
let preimage = hex::decode(params.preimage)?;
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cln_plugin::options;
pub const OPTION_DATABASE: options::DefaultStringConfigOption =
options::ConfigOption::new_str_with_default(
"hold-database",
"sqlite://./hold/hold.sqlite",
"sqlite://./hold/hold.sqlite3",
"hold database",
);

Expand Down
3 changes: 2 additions & 1 deletion src/database/helpers/invoice_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl InvoiceHelper for InvoiceHelperDatabase {

let invoices = invoices::dsl::invoices
.select(Invoice::as_select())
.order_by(invoices::dsl::id)
.load(&mut con)?;
let htlcs = Htlc::belonging_to(&invoices)
.select(Htlc::as_select())
Expand All @@ -95,7 +96,7 @@ impl InvoiceHelper for InvoiceHelperDatabase {
let invoices = invoices::dsl::invoices
.select(Invoice::as_select())
.filter(invoices::dsl::id.ge(index_start))
.order_by(invoices::dsl::id.desc())
.order_by(invoices::dsl::id)
.limit(limit as i64)
.load(&mut con)?;
let htlcs = Htlc::belonging_to(&invoices)
Expand Down
Loading

0 comments on commit 95b545d

Please sign in to comment.