Skip to content

Commit

Permalink
Move lsp_fee_limits to PaymentKind::Bolt11Jit variant
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 8, 2024
1 parent 1e74787 commit d01fc0d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
2 changes: 1 addition & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ interface ClosureReason {
interface PaymentKind {
Onchain();
Bolt11();
Bolt11Jit(LSPFeeLimits? lsp_fee_limits);
Bolt12();
Spontaneous();
};
Expand Down Expand Up @@ -250,7 +251,6 @@ dictionary PaymentDetails {
u64? amount_msat;
PaymentDirection direction;
PaymentStatus status;
LSPFeeLimits? lsp_fee_limits;
};

[NonExhaustive]
Expand Down
27 changes: 16 additions & 11 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,23 @@ where
return;
}

let max_total_opening_fee_msat = info
.lsp_fee_limits
.and_then(|l| {
l.max_total_opening_fee_msat.or_else(|| {
l.max_proportional_opening_fee_ppm_msat.and_then(|max_prop_fee| {
// If it's a variable amount payment, compute the actual fee.
compute_opening_fee(amount_msat, 0, max_prop_fee)
let max_total_opening_fee_msat = match info.kind {
Some(PaymentKind::Bolt11Jit { lsp_fee_limits }) => {
lsp_fee_limits
.and_then(|l| {
l.max_total_opening_fee_msat.or_else(|| {
l.max_proportional_opening_fee_ppm_msat.and_then(
|max_prop_fee| {
// If it's a variable amount payment, compute the actual fee.
compute_opening_fee(amount_msat, 0, max_prop_fee)
},
)
})
})
})
})
.unwrap_or(0);
.unwrap_or(0)
},
_ => 0,
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
log_info!(
Expand Down Expand Up @@ -558,7 +564,6 @@ where
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Succeeded,
lsp_fee_limits: None,
};

match self.payment_store.insert(payment) {
Expand Down
9 changes: 2 additions & 7 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl Bolt11Payment {
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
lsp_fee_limits: None,
};
self.payment_store.insert(payment)?;

Expand All @@ -129,7 +128,6 @@ impl Bolt11Payment {
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
lsp_fee_limits: None,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -218,7 +216,6 @@ impl Bolt11Payment {
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
lsp_fee_limits: None,
};
self.payment_store.insert(payment)?;

Expand All @@ -238,7 +235,6 @@ impl Bolt11Payment {
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
lsp_fee_limits: None,
};
self.payment_store.insert(payment)?;

Expand Down Expand Up @@ -299,7 +295,6 @@ impl Bolt11Payment {
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
lsp_fee_limits: None,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -418,15 +413,15 @@ impl Bolt11Payment {
max_total_opening_fee_msat: lsp_total_opening_fee,
max_proportional_opening_fee_ppm_msat: lsp_prop_opening_fee,
});
let kind = Some(PaymentKind::Bolt11Jit { lsp_fee_limits });
let payment = PaymentDetails {
kind: Some(PaymentKind::Bolt11),
kind,
hash: payment_hash,
preimage: None,
secret: Some(invoice.payment_secret().clone()),
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
lsp_fee_limits,
};

self.payment_store.insert(payment)?;
Expand Down
38 changes: 21 additions & 17 deletions src/payment/payment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,11 @@ pub struct PaymentDetails {
pub direction: PaymentDirection,
/// The status of the payment.
pub status: PaymentStatus,
/// Limits applying to how much fee we allow an LSP to deduct from the payment amount.
///
/// This is only `Some` for payments received via a JIT-channel, in which case the first
/// inbound payment will pay for the LSP's channel opening fees.
///
/// See [`LdkChannelConfig::accept_underpaying_htlcs`] for more information.
///
/// [`LdkChannelConfig::accept_underpaying_htlcs`]: lightning::util::config::ChannelConfig::accept_underpaying_htlcs
pub lsp_fee_limits: Option<LSPFeeLimits>,
}

impl_writeable_tlv_based!(PaymentDetails, {
(0, hash, required),
(1, lsp_fee_limits, option),
// 1 briefly used to be lsp_fee_limits, could probably be reused at some point in the future.
(2, preimage, required),
(3, kind, option),
(4, secret, required),
Expand Down Expand Up @@ -97,6 +88,22 @@ pub enum PaymentKind {
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
// TODO: Bolt11 { invoice: Option<Bolt11Invoice> },
Bolt11,
/// A [BOLT 11] payment intended to open an [LSPS 2] just-in-time channel.
///
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
/// [LSPS 2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
// TODO: Bolt11Jit { invoice: Option<Bolt11Invoice> },
Bolt11Jit {
/// Limits applying to how much fee we allow an LSP to deduct from the payment amount.
///
/// This is only `Some` for payments received via a JIT-channel, in which case the first
/// inbound payment will pay for the LSP's channel opening fees.
///
/// See [`LdkChannelConfig::accept_underpaying_htlcs`] for more information.
///
/// [`LdkChannelConfig::accept_underpaying_htlcs`]: lightning::util::config::ChannelConfig::accept_underpaying_htlcs
lsp_fee_limits: Option<LSPFeeLimits>,
},
/// A spontaneous ("keysend") payment.
Spontaneous,
}
Expand All @@ -106,7 +113,10 @@ impl_writeable_tlv_based_enum!(PaymentKind,
(2, Bolt11) => {
// TODO: (0, invoice, option),
},
(6, Spontaneous) => {};
(4, Bolt11Jit) => {
(1, lsp_fee_limits, option),
},
(8, Spontaneous) => {};
);

/// Limits applying to how much fee we allow an LSP to deduct from the payment amount.
Expand Down Expand Up @@ -137,7 +147,6 @@ pub(crate) struct PaymentDetailsUpdate {
pub amount_msat: Option<Option<u64>>,
pub direction: Option<PaymentDirection>,
pub status: Option<PaymentStatus>,
pub lsp_fee_limits: Option<Option<LSPFeeLimits>>,
}

impl PaymentDetailsUpdate {
Expand All @@ -149,7 +158,6 @@ impl PaymentDetailsUpdate {
amount_msat: None,
direction: None,
status: None,
lsp_fee_limits: None,
}
}
}
Expand Down Expand Up @@ -230,10 +238,6 @@ where
payment.status = status;
}

if let Some(lsp_fee_limits) = update.lsp_fee_limits {
payment.lsp_fee_limits = lsp_fee_limits
}

self.persist_info(&update.hash, payment)?;
updated = true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl SpontaneousPayment {
status: PaymentStatus::Pending,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
lsp_fee_limits: None,
};
self.payment_store.insert(payment)?;

Expand All @@ -104,7 +103,6 @@ impl SpontaneousPayment {
status: PaymentStatus::Failed,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
lsp_fee_limits: None,
};

self.payment_store.insert(payment)?;
Expand Down
4 changes: 3 additions & 1 deletion src/uniffi_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub use crate::payment::payment_store::{LSPFeeLimits, PaymentDirection, PaymentStatus, PaymentKind};
pub use crate::payment::payment_store::{
LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus,
};

pub use lightning::events::{ClosureReason, PaymentFailureReason};
pub use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
Expand Down

0 comments on commit d01fc0d

Please sign in to comment.