Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Move BOLT11 JIT params to payment metadata by tnull · Pull Request #899 · lightningdevkit/ldk-node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
# Pending

## Compatibility Notes
- Pending JIT-channel payments created before upgrading may fail after upgrade because the
prior LSPS2 fee-limit state stored in `PaymentKind::Bolt11Jit` is not migrated.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.

Expand DownExpand Up@@ -419,4 +425,3 @@ integrated LDK and BDK-based wallets.

**Note:** This release is still considered experimental, should not be run in
production, and no compatibility guarantees are given until the release of 0.1.

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,18 +40,18 @@ default = []
#lightning-macros = { version = "0.2.0" }
#lightning-dns-resolver = { version = "0.3.0" }

lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a" }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["tokio"] }
lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["rest-client", "rpc-client", "tokio"] }
lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["esplora-async-https", "time", "electrum-rustls-ring"] }
lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std"] }
lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }
lightning-dns-resolver = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182" }

bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]}
Expand DownExpand Up@@ -81,13 +81,13 @@ async-trait = { version = "0.1", default-features = false }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
bitcoin-payment-instructions = { git = "https://github.com/jkczyz/bitcoin-payment-instructions", rev = "c7d7ea849fcbe8cad385c93f8057cd68ecb994b7" }
bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment-instructions", rev = "ed8657dee284f987b6791cd291d0f0f18811ee76" }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "10608650d455f4d535cbac73921be329d814854a", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "090e09f3992694040d3a55c1c798b3a92fb77182", features = ["std", "_test_utils"] }
rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] }
proptest = "1.0.0"
regex = "1.5.6"
Expand Down
157 changes: 116 additions & 41 deletions src/event.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,7 @@ use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand DownExpand Up@@ -580,6 +581,36 @@ where
}
}

fn fail_claimable_payment(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have been independent extraction refactor. Might have been easier to spot that it now no longer sets hash: Some(Some(payment_hash)).

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually good catch. Dropping that wasn't intentional in this PR and while it's not a behavioral change right now, it might be in the future when we implement BOLT12<>LSPS2. So added it back defensively for now, even if it's technically not necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd be rewarded with a separate extraction commit for this discovery, but no 😅

&self, payment_id: PaymentId, payment_hash: &PaymentHash,
) -> Result<(), ReplayEvent> {
self.channel_manager.fail_htlc_backwards(payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(*payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
Err(ReplayEvent())
},
}
}

fn lsps2_max_total_opening_fee_msat(payment_metadata: &[u8], amount_msat: u64) -> Option<u64> {
let metadata = PaymentMetadata::read(&mut &payment_metadata[..]).ok()?;
let lsps2_parameters = metadata.lsps2_parameters?;
lsps2_parameters.max_total_opening_fee_msat.or_else(|| {
lsps2_parameters.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)
})
})
}

pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> {
match event {
LdkEvent::FundingGenerationReady {
Expand DownExpand Up@@ -693,7 +724,8 @@ where
..
} => {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
let payment_info = self.payment_store.get(&payment_id);
if let Some(info) = payment_info.as_ref() {
if info.direction == PaymentDirection::Outbound {
log_info!(
self.logger,
Expand All@@ -716,14 +748,13 @@ where
}

if info.status == PaymentStatus::Succeeded
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
|| matches!(&info.kind, PaymentKind::Spontaneous { .. })
{
let stored_preimage = match info.kind {
let stored_preimage = match &info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. }
| PaymentKind::Bolt12Offer { preimage, .. }
| PaymentKind::Bolt12Refund { preimage, .. }
| PaymentKind::Spontaneous { preimage, .. } => preimage,
| PaymentKind::Spontaneous { preimage, .. } => *preimage,
_ => None,
};

Expand DownExpand Up@@ -758,22 +789,28 @@ where
},
};
}
}

let max_total_opening_fee_msat = match info.kind {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
lsp_fee_limits.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)
},
_ => 0,
if counterparty_skimmed_fee_msat > 0 {
let max_total_opening_fee_msat = match &purpose {
PaymentPurpose::Bolt11InvoicePayment { .. } => onion_fields
.as_ref()
.and_then(|fields| fields.payment_metadata.as_ref())
.and_then(|metadata| {
Self::lsps2_max_total_opening_fee_msat(metadata, amount_msat)
}),
_ => None,
};

let Some(max_total_opening_fee_msat) = max_total_opening_fee_msat else {
log_info!(
self.logger,
"Refusing inbound payment with hash {} as the counterparty withheld {}msat without valid BOLT11 LSPS2 payment metadata",
hex_utils::to_string(&payment_hash.0),
counterparty_skimmed_fee_msat,
);
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
};

if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
Expand All@@ -784,26 +821,13 @@ where
counterparty_skimmed_fee_msat,
max_total_opening_fee_msat,
);
self.channel_manager.fail_htlc_backwards(&payment_hash);

let update = PaymentDetailsUpdate {
hash: Some(Some(payment_hash)),
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(update) {
Ok(_) => return Ok(()),
Err(e) => {
log_error!(self.logger, "Failed to access payment store: {}", e);
return Err(ReplayEvent());
},
};
self.fail_claimable_payment(payment_id, &payment_hash)?;
return Ok(());
}

// If the LSP skimmed anything, update our stored payment.
if counterparty_skimmed_fee_msat > 0 {
match info.kind {
PaymentKind::Bolt11Jit { .. } => {
if let Some(info) = payment_info.as_ref() {
match &info.kind {
PaymentKind::Bolt11 { .. } => {
let update = PaymentDetailsUpdate {
counterparty_skimmed_fee_msat: Some(Some(counterparty_skimmed_fee_msat)),
..PaymentDetailsUpdate::new(payment_id)
Expand All@@ -816,16 +840,17 @@ where
},
};
}
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for BOLT11 payments."),
}
}
}

if let Some(info) = payment_info {
// If this is known by the store but ChannelManager doesn't know the preimage,
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
// user interaction.
match info.kind {
PaymentKind::Bolt11 { preimage, .. }
| PaymentKind::Bolt11Jit { preimage, .. } => {
PaymentKind::Bolt11 { preimage, .. } => {
if purpose.preimage().is_none() {
debug_assert!(
preimage.is_none(),
Expand DownExpand Up@@ -1890,8 +1915,58 @@ mod tests {

use super::*;
use crate::io::test_utils::InMemoryStore;
use crate::payment::store::LSPS2Parameters;
use crate::types::DynStoreWrapper;

#[test]
fn lsps2_payment_metadata_decodes_total_fee_limit() {
let metadata = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: Some(42_000),
max_proportional_opening_fee_ppm_msat: None,
}),
};

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata.encode(),
100_000
),
Some(42_000)
);
}

#[test]
fn lsps2_payment_metadata_missing_or_malformed_limit_is_rejected() {
let empty_metadata = PaymentMetadata { lsps2_parameters: None }.encode();
let metadata_without_fee_limit = PaymentMetadata {
lsps2_parameters: Some(LSPS2Parameters {
max_total_opening_fee_msat: None,
max_proportional_opening_fee_ppm_msat: None,
}),
}
.encode();

assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&empty_metadata,
100_000
),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(&[0xff], 100_000),
None
);
assert_eq!(
EventHandler::<Arc<TestLogger>>::lsps2_max_total_opening_fee_msat(
&metadata_without_fee_limit,
100_000
),
None
);
}

#[tokio::test]
async fn event_queue_persistence() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
Expand Down
Loading
Loading