Skip to content

Commit

Permalink
Merge pull request fedimint#6184 from joschisan/lnv2_api_cleanup_2
Browse files Browse the repository at this point in the history
cleanup: simplify lnv2 client api
  • Loading branch information
joschisan authored Oct 19, 2024
2 parents fdba64a + b474762 commit 5afc685
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 58 deletions.
18 changes: 15 additions & 3 deletions modules/fedimint-lnv2-client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::Serialize;
use serde_json::Value;

use crate::api::LnFederationApi;
use crate::LightningClientModule;
use crate::{Bolt11InvoiceDescription, LightningClientModule};

#[derive(Parser, Serialize)]
enum Opts {
Expand Down Expand Up @@ -62,9 +62,21 @@ pub(crate) async fn handle_cli_command(
let opts = Opts::parse_from(iter::once(&ffi::OsString::from("lnv2")).chain(args.iter()));

let value = match opts {
Opts::Send { gateway, invoice } => json(lightning.send(invoice, gateway).await?),
Opts::Send { gateway, invoice } => {
json(lightning.send(invoice, gateway, Value::Null).await?)
}
Opts::AwaitSend { operation_id } => json(lightning.await_send(operation_id).await?),
Opts::Receive { gateway, amount } => json(lightning.receive(amount, gateway).await?),
Opts::Receive { amount, gateway } => json(
lightning
.receive(
amount,
3600,
Bolt11InvoiceDescription::Direct(String::new()),
gateway,
Value::Null,
)
.await?,
),
Opts::AwaitReceive { operation_id } => json(lightning.await_receive(operation_id).await?),
Opts::Gateway(gateway_opts) => match gateway_opts {
GatewayOpts::Select { invoice } => json(lightning.select_gateway(invoice).await?.0),
Expand Down
51 changes: 7 additions & 44 deletions modules/fedimint-lnv2-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ pub const EXPIRATION_DELTA_LIMIT_DEFAULT: u64 = 1008;
/// A two hour buffer in case either the client or gateway go offline
pub const CONTRACT_CONFIRMATION_BUFFER: u64 = 12;

/// Default expiration time for lightning invoices
pub const INVOICE_EXPIRATION_SECONDS_DEFAULT: u32 = 24 * 60 * 60;

#[cfg_attr(doc, aquamarine::aquamarine)]
/// The high-level state of sending a payment over lightning.
///
Expand Down Expand Up @@ -520,25 +517,6 @@ impl LightningClientModule {
&self,
invoice: Bolt11Invoice,
gateway: Option<SafeUrl>,
) -> Result<OperationId, SendPaymentError> {
self.send_custom(
invoice,
PaymentFee::SEND_FEE_LIMIT_DEFAULT,
EXPIRATION_DELTA_LIMIT_DEFAULT,
gateway,
Value::Null,
)
.await
}

/// Pay an invoice. For testing you can optionally specify a gateway to
/// route with, otherwise a gateway will be selected automatically.
pub async fn send_custom(
&self,
invoice: Bolt11Invoice,
payment_fee_limit: PaymentFee,
expiration_delta_limit: u64,
gateway: Option<SafeUrl>,
custom_meta: Value,
) -> Result<OperationId, SendPaymentError> {
let amount = invoice
Expand Down Expand Up @@ -580,11 +558,11 @@ impl LightningClientModule {

let (send_fee, expiration_delta) = routing_info.send_parameters(&invoice);

if !send_fee.le(&payment_fee_limit) {
if !send_fee.le(&PaymentFee::SEND_FEE_LIMIT_DEFAULT) {
return Err(SendPaymentError::PaymentFeeExceedsLimit(send_fee));
}

if expiration_delta_limit < expiration_delta {
if EXPIRATION_DELTA_LIMIT_DEFAULT < expiration_delta {
return Err(SendPaymentError::ExpirationDeltaExceedsLimit(
expiration_delta,
));
Expand Down Expand Up @@ -768,27 +746,11 @@ impl LightningClientModule {
/// Request an invoice. For testing you can optionally specify a gateway to
/// generate the invoice, otherwise a gateway will be selected
/// automatically.
pub async fn receive(&self, amount: Amount, gateway: Option<SafeUrl>) -> ReceiveResult {
self.receive_custom(
amount,
INVOICE_EXPIRATION_SECONDS_DEFAULT,
Bolt11InvoiceDescription::Direct(String::new()),
PaymentFee::RECEIVE_FEE_LIMIT_DEFAULT,
gateway,
Value::Null,
)
.await
}

/// Request an invoice. For testing you can optionally specify a gateway to
/// generate the invoice, otherwise a gateway will be selected
/// automatically.
pub async fn receive_custom(
pub async fn receive(
&self,
amount: Amount,
expiry_secs: u32,
description: Bolt11InvoiceDescription,
payment_fee_limit: PaymentFee,
gateway: Option<SafeUrl>,
custom_meta: Value,
) -> Result<(Bolt11Invoice, OperationId), ReceiveError> {
Expand All @@ -798,7 +760,6 @@ impl LightningClientModule {
amount,
expiry_secs,
description,
payment_fee_limit,
gateway,
)
.await?;
Expand All @@ -820,7 +781,6 @@ impl LightningClientModule {
amount: Amount,
expiry_secs: u32,
description: Bolt11InvoiceDescription,
payment_fee_limit: PaymentFee,
gateway: Option<SafeUrl>,
) -> Result<(SafeUrl, IncomingContract, Bolt11Invoice), ReceiveError> {
let (ephemeral_tweak, ephemeral_pk) = generate_ephemeral_tweak(recipient_static_pk);
Expand All @@ -847,7 +807,10 @@ impl LightningClientModule {
.map_err(ReceiveError::FailedToSelectGateway)?,
};

if !routing_info.receive_fee.le(&payment_fee_limit) {
if !routing_info
.receive_fee
.le(&PaymentFee::RECEIVE_FEE_LIMIT_DEFAULT)
{
return Err(ReceiveError::PaymentFeeExceedsLimit(
routing_info.receive_fee,
));
Expand Down
29 changes: 18 additions & 11 deletions modules/fedimint-lnv2-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use fedimint_dummy_common::config::DummyGenParams;
use fedimint_dummy_server::DummyInit;
use fedimint_lnv2_client::{
Bolt11InvoiceDescription, LightningClientInit, LightningClientModule,
LightningClientStateMachines, LightningOperationMeta, PaymentFee, ReceiveState,
SendPaymentError, SendState, CONTRACT_CONFIRMATION_BUFFER, EXPIRATION_DELTA_LIMIT_DEFAULT,
LightningClientStateMachines, LightningOperationMeta, ReceiveState, SendPaymentError,
SendState, CONTRACT_CONFIRMATION_BUFFER, EXPIRATION_DELTA_LIMIT_DEFAULT,
};
use fedimint_lnv2_common::config::LightningGenParams;
use fedimint_lnv2_common::{LightningInput, LightningInputV0, OutgoingWitness};
Expand Down Expand Up @@ -55,13 +55,13 @@ async fn can_pay_external_invoice_exactly_once() -> anyhow::Result<()> {

let operation_id = client
.get_first_module::<LightningClientModule>()?
.send(invoice.clone(), Some(gateway_api.clone()))
.send(invoice.clone(), Some(gateway_api.clone()), Value::Null)
.await?;

assert_eq!(
client
.get_first_module::<LightningClientModule>()?
.send(invoice.clone(), Some(gateway_api.clone()))
.send(invoice.clone(), Some(gateway_api.clone()), Value::Null)
.await,
Err(SendPaymentError::PendingPreviousPayment(operation_id)),
);
Expand All @@ -79,7 +79,7 @@ async fn can_pay_external_invoice_exactly_once() -> anyhow::Result<()> {
assert_eq!(
client
.get_first_module::<LightningClientModule>()?
.send(invoice, Some(gateway_api))
.send(invoice, Some(gateway_api), Value::Null)
.await,
Err(SendPaymentError::SuccessfulPreviousPayment(operation_id)),
);
Expand All @@ -103,7 +103,11 @@ async fn refund_failed_payment() -> anyhow::Result<()> {

let op = client
.get_first_module::<LightningClientModule>()?
.send(mock::unpayable_invoice(), Some(mock::gateway()))
.send(
mock::unpayable_invoice(),
Some(mock::gateway()),
Value::Null,
)
.await?;

let mut sub = client
Expand Down Expand Up @@ -136,7 +140,7 @@ async fn unilateral_refund_of_outgoing_contracts() -> anyhow::Result<()> {

let op = client
.get_first_module::<LightningClientModule>()?
.send(mock::crash_invoice(), Some(mock::gateway()))
.send(mock::crash_invoice(), Some(mock::gateway()), Value::Null)
.await?;

let mut sub = client
Expand Down Expand Up @@ -175,7 +179,7 @@ async fn claiming_outgoing_contract_triggers_success() -> anyhow::Result<()> {

let op = client
.get_first_module::<LightningClientModule>()?
.send(mock::crash_invoice(), Some(mock::gateway()))
.send(mock::crash_invoice(), Some(mock::gateway()), Value::Null)
.await?;

let mut sub = client
Expand Down Expand Up @@ -236,11 +240,10 @@ async fn receive_operation_expires() -> anyhow::Result<()> {

let op = client
.get_first_module::<LightningClientModule>()?
.receive_custom(
.receive(
Amount::from_sats(1000),
5, // receive operation expires in 5 seconds
Bolt11InvoiceDescription::Direct(String::new()),
PaymentFee::RECEIVE_FEE_LIMIT_DEFAULT,
Some(mock::gateway()),
Value::Null,
)
Expand Down Expand Up @@ -268,7 +271,11 @@ async fn rejects_wrong_network_invoice() -> anyhow::Result<()> {
assert_eq!(
client
.get_first_module::<LightningClientModule>()?
.send(mock::signet_bolt_11_invoice(), Some(mock::gateway()))
.send(
mock::signet_bolt_11_invoice(),
Some(mock::gateway()),
Value::Null
)
.await
.expect_err("send did not fail due to incorrect Currency"),
SendPaymentError::WrongCurrency {
Expand Down

0 comments on commit 5afc685

Please sign in to comment.