Skip to content

Commit

Permalink
test: gRPC integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Aug 21, 2024
1 parent 8368516 commit 9ddaf0c
Show file tree
Hide file tree
Showing 14 changed files with 594 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ target/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

tests/hold/protos/*
!tests/hold/protos/__init__.py
**/__pycache__
25 changes: 18 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ python-lint:
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
chmod -R 777 regtest 2> /dev/null || true
cd regtest && COMPOSE_PROFILES=ci ./start.sh
cd ..
mkdir regtest/data/cln1/plugins
cp target/debug/hold regtest/data/cln1/plugins/
docker exec boltz-cln-1 lightning-cli --regtest plugin stop /root/hold.sh
rm -rf regtest/data/cln1/regtest/hold/
docker exec boltz-cln-1 lightning-cli --regtest plugin start /root/.lightning/plugins/hold
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
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
16 changes: 15 additions & 1 deletion src/grpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use tonic::transport::ServerTlsConfig;
pub struct Server<T, E> {
host: String,
port: i64,
is_regtest: bool,

directory: PathBuf,
cancellation_token: CancellationToken,

Expand All @@ -28,9 +30,11 @@ where
T: InvoiceHelper + Sync + Send + Clone + 'static,
E: InvoiceEncoder + Sync + Send + Clone + 'static,
{
#[allow(clippy::too_many_arguments)]
pub fn new(
host: &str,
port: i64,
is_regtest: bool,
cancellation_token: CancellationToken,
directory: PathBuf,
invoice_helper: T,
Expand All @@ -42,14 +46,23 @@ where
settler,
encoder,
directory,
is_regtest,
invoice_helper,
cancellation_token,
host: host.to_string(),
}
}

pub async fn start(&self) -> Result<()> {
let socket_addr = SocketAddr::new(IpAddr::from_str(self.host.as_str())?, self.port as u16);
// Always listen to all interfaces on regtest
let socket_addr = SocketAddr::new(
IpAddr::from_str(if !self.is_regtest {
self.host.as_str()
} else {
"0.0.0.0"
})?,
self.port as u16,
);
info!("Starting gRPC server on: {}", socket_addr);

let (identity, ca) = load_certificates(self.directory.clone())?;
Expand Down Expand Up @@ -201,6 +214,7 @@ mod test {
let server = Server::new(
"127.0.0.1",
port,
false,
token.clone(),
certs_dir.clone(),
make_mock_invoice_helper(),
Expand Down
30 changes: 30 additions & 0 deletions src/grpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,36 @@ where

let mut state_rx = self.settler.state_rx();

match self
.invoice_helper
.get_by_payment_hash(&params.payment_hash)
{
Ok(res) => {
if let Some(res) = res {
if let Ok(state) = InvoiceState::try_from(res.invoice.state.as_str()) {
if let Err(err) = tx
.send(Ok(TrackResponse {
state: transform_invoice_state(state),
}))
.await
{
error!("Could not send invoice state update: {}", err);
return Err(Status::new(
Code::Internal,
format!("could not send initial invoice state: {}", err),
));
}
}
}
}
Err(err) => {
return Err(Status::new(
Code::Internal,
format!("could not fetch invoice state from database: {}", err),
));
}
};

tokio::spawn(async move {
loop {
match state_rx.recv().await {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async fn main() -> Result<()> {
let grpc_server = grpc::server::Server::new(
&grpc_host,
grpc_port,
config.network == "regtest",
cancellation_token.clone(),
std::env::current_dir()?.join(utils::built_info::PKG_NAME),
invoice_helper,
Expand Down
4 changes: 3 additions & 1 deletion src/settler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct Settler<T> {
pending_htlcs: Arc<Mutex<HashMap<Vec<u8>, Vec<PendingHtlc>>>>,
}

// TODO: only allow valid state transitions

impl<T> Settler<T>
where
T: InvoiceHelper + Sync + Send + Clone,
Expand Down Expand Up @@ -90,7 +92,7 @@ where
self.invoice_helper
.set_invoice_state(invoice.id, InvoiceState::Accepted)?;
let _ = self.state_tx.send(StateUpdate {
state: InvoiceState::Paid,
state: InvoiceState::Accepted,
bolt11: invoice.bolt11.clone(),
payment_hash: invoice.payment_hash.clone(),
});
Expand Down
Empty file added tests/hold/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/hold/protos/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))
Loading

0 comments on commit 9ddaf0c

Please sign in to comment.