Skip to content

Commit e457a48

Browse files
authored
Merge pull request lightningdevkit#4167 from TheBlueMatt/2025-10-upgrade-scid-timeout
Fix SCID removal when an HTLC is pending from 0.1 before upgrading
2 parents c218818 + 2c836b3 commit e457a48

File tree

12 files changed

+560
-103
lines changed

12 files changed

+560
-103
lines changed

lightning-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ lightning-types = { path = "../lightning-types", features = ["_test_utils"] }
1515
lightning-invoice = { path = "../lightning-invoice", default-features = false }
1616
lightning-macros = { path = "../lightning-macros" }
1717
lightning = { path = "../lightning", features = ["_test_utils"] }
18-
lightning_0_1 = { package = "lightning", version = "0.1.1", features = ["_test_utils"] }
18+
lightning_0_1 = { package = "lightning", version = "0.1.7", features = ["_test_utils"] }
1919
lightning_0_0_125 = { package = "lightning", version = "0.0.125", features = ["_test_utils"] }
2020

2121
bitcoin = { version = "0.32.2", default-features = false }

lightning-tests/src/upgrade_downgrade_tests.rs

Lines changed: 202 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
//! Tests which test upgrading from previous versions of LDK or downgrading to previous versions of
1111
//! LDK.
1212
13+
use lightning_0_1::commitment_signed_dance as commitment_signed_dance_0_1;
1314
use lightning_0_1::events::ClosureReason as ClosureReason_0_1;
15+
use lightning_0_1::expect_pending_htlcs_forwardable_ignore as expect_pending_htlcs_forwardable_ignore_0_1;
1416
use lightning_0_1::get_monitor as get_monitor_0_1;
17+
use lightning_0_1::ln::channelmanager::PaymentId as PaymentId_0_1;
18+
use lightning_0_1::ln::channelmanager::RecipientOnionFields as RecipientOnionFields_0_1;
1519
use lightning_0_1::ln::functional_test_utils as lightning_0_1_utils;
20+
use lightning_0_1::ln::msgs::ChannelMessageHandler as _;
21+
use lightning_0_1::routing::router as router_0_1;
1622
use lightning_0_1::util::ser::Writeable as _;
1723

1824
use lightning_0_0_125::chain::ChannelMonitorUpdateStatus as ChannelMonitorUpdateStatus_0_0_125;
@@ -29,16 +35,23 @@ use lightning_0_0_125::ln::msgs::ChannelMessageHandler as _;
2935
use lightning_0_0_125::routing::router as router_0_0_125;
3036
use lightning_0_0_125::util::ser::Writeable as _;
3137

32-
use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
33-
use lightning::events::{ClosureReason, Event};
38+
use lightning::chain::channelmonitor::{ANTI_REORG_DELAY, HTLC_FAIL_BACK_BUFFER};
39+
use lightning::events::bump_transaction::sync::WalletSourceSync;
40+
use lightning::events::{ClosureReason, Event, HTLCHandlingFailureType};
3441
use lightning::ln::functional_test_utils::*;
42+
use lightning::ln::funding::SpliceContribution;
43+
use lightning::ln::msgs::BaseMessageHandler as _;
44+
use lightning::ln::msgs::ChannelMessageHandler as _;
45+
use lightning::ln::msgs::MessageSendEvent;
46+
use lightning::ln::splicing_tests::*;
47+
use lightning::ln::types::ChannelId;
3548
use lightning::sign::OutputSpender;
3649

37-
use lightning_types::payment::PaymentPreimage;
50+
use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
3851

39-
use bitcoin::opcodes;
4052
use bitcoin::script::Builder;
4153
use bitcoin::secp256k1::Secp256k1;
54+
use bitcoin::{opcodes, Amount, TxOut};
4255

4356
use std::sync::Arc;
4457

@@ -299,3 +312,188 @@ fn test_0_1_legacy_remote_key_derivation() {
299312
panic!("Wrong event");
300313
}
301314
}
315+
316+
fn do_test_0_1_htlc_forward_after_splice(fail_htlc: bool) {
317+
// Test what happens if an HTLC set to be forwarded in 0.1 is forwarded after the inbound
318+
// channel is spliced. In the initial splice code, this could have led to a dangling HTLC if
319+
// the HTLC is failed as the backwards-failure would use the channel's original SCID which is
320+
// no longer valid.
321+
// In some later splice code, this also failed because the `KeysManager` would have tried to
322+
// rotate the `to_remote` key, which we aren't able to do in the splicing protocol.
323+
let (node_a_ser, node_b_ser, node_c_ser, mon_a_1_ser, mon_b_1_ser, mon_b_2_ser, mon_c_1_ser);
324+
let (node_a_id, node_b_id, node_c_id);
325+
let (chan_id_bytes_a, chan_id_bytes_b);
326+
let (payment_secret_bytes, payment_hash_bytes, payment_preimage_bytes);
327+
let (node_a_blocks, node_b_blocks, node_c_blocks);
328+
329+
const EXTRA_BLOCKS_BEFORE_FAIL: u32 = 145;
330+
331+
{
332+
let chanmon_cfgs = lightning_0_1_utils::create_chanmon_cfgs(3);
333+
let node_cfgs = lightning_0_1_utils::create_node_cfgs(3, &chanmon_cfgs);
334+
let node_chanmgrs =
335+
lightning_0_1_utils::create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
336+
let nodes = lightning_0_1_utils::create_network(3, &node_cfgs, &node_chanmgrs);
337+
338+
node_a_id = nodes[0].node.get_our_node_id();
339+
node_b_id = nodes[1].node.get_our_node_id();
340+
node_c_id = nodes[2].node.get_our_node_id();
341+
let chan_id_a = lightning_0_1_utils::create_announced_chan_between_nodes_with_value(
342+
&nodes, 0, 1, 10_000_000, 0,
343+
)
344+
.2;
345+
chan_id_bytes_a = chan_id_a.0;
346+
347+
let chan_id_b = lightning_0_1_utils::create_announced_chan_between_nodes_with_value(
348+
&nodes, 1, 2, 50_000, 0,
349+
)
350+
.2;
351+
chan_id_bytes_b = chan_id_b.0;
352+
353+
// Ensure all nodes are at the same initial height.
354+
let node_max_height = nodes.iter().map(|node| node.best_block_info().1).max().unwrap();
355+
for node in &nodes {
356+
let blocks_to_mine = node_max_height - node.best_block_info().1;
357+
if blocks_to_mine > 0 {
358+
lightning_0_1_utils::connect_blocks(node, blocks_to_mine);
359+
}
360+
}
361+
362+
let (preimage, hash, secret) =
363+
lightning_0_1_utils::get_payment_preimage_hash(&nodes[2], Some(1_000_000), None);
364+
payment_preimage_bytes = preimage.0;
365+
payment_hash_bytes = hash.0;
366+
payment_secret_bytes = secret.0;
367+
368+
let pay_params = router_0_1::PaymentParameters::from_node_id(
369+
node_c_id,
370+
lightning_0_1_utils::TEST_FINAL_CLTV,
371+
)
372+
.with_bolt11_features(nodes[2].node.bolt11_invoice_features())
373+
.unwrap();
374+
375+
let route_params =
376+
router_0_1::RouteParameters::from_payment_params_and_value(pay_params, 1_000_000);
377+
let mut route = lightning_0_1_utils::get_route(&nodes[0], &route_params).unwrap();
378+
route.paths[0].hops[1].cltv_expiry_delta =
379+
EXTRA_BLOCKS_BEFORE_FAIL + HTLC_FAIL_BACK_BUFFER + 1;
380+
if fail_htlc {
381+
// Pay more than the channel's value (and probably not enough fee)
382+
route.paths[0].hops[1].fee_msat = 50_000_000;
383+
}
384+
385+
let onion = RecipientOnionFields_0_1::secret_only(secret);
386+
let id = PaymentId_0_1(hash.0);
387+
nodes[0].node.send_payment_with_route(route, hash, onion, id).unwrap();
388+
389+
lightning_0_1_utils::check_added_monitors(&nodes[0], 1);
390+
let send_event = lightning_0_1_utils::SendEvent::from_node(&nodes[0]);
391+
392+
nodes[1].node.handle_update_add_htlc(node_a_id, &send_event.msgs[0]);
393+
commitment_signed_dance_0_1!(nodes[1], nodes[0], send_event.commitment_msg, false);
394+
expect_pending_htlcs_forwardable_ignore_0_1!(nodes[1]);
395+
396+
// We now have an HTLC pending in node B's forwarding queue with the original channel's
397+
// SCID as the source.
398+
// We now upgrade to 0.2 and splice before forwarding that HTLC...
399+
node_a_ser = nodes[0].node.encode();
400+
node_b_ser = nodes[1].node.encode();
401+
node_c_ser = nodes[2].node.encode();
402+
mon_a_1_ser = get_monitor_0_1!(nodes[0], chan_id_a).encode();
403+
mon_b_1_ser = get_monitor_0_1!(nodes[1], chan_id_a).encode();
404+
mon_b_2_ser = get_monitor_0_1!(nodes[1], chan_id_b).encode();
405+
mon_c_1_ser = get_monitor_0_1!(nodes[2], chan_id_b).encode();
406+
407+
node_a_blocks = Arc::clone(&nodes[0].blocks);
408+
node_b_blocks = Arc::clone(&nodes[1].blocks);
409+
node_c_blocks = Arc::clone(&nodes[2].blocks);
410+
}
411+
412+
// Create a dummy node to reload over with the 0.1 state
413+
let mut chanmon_cfgs = create_chanmon_cfgs(3);
414+
415+
// Our TestChannelSigner will fail as we're jumping ahead, so disable its state-based checks
416+
chanmon_cfgs[0].keys_manager.disable_all_state_policy_checks = true;
417+
chanmon_cfgs[1].keys_manager.disable_all_state_policy_checks = true;
418+
chanmon_cfgs[2].keys_manager.disable_all_state_policy_checks = true;
419+
420+
chanmon_cfgs[0].tx_broadcaster.blocks = node_a_blocks;
421+
chanmon_cfgs[1].tx_broadcaster.blocks = node_b_blocks;
422+
chanmon_cfgs[2].tx_broadcaster.blocks = node_c_blocks;
423+
424+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
425+
let (persister_a, persister_b, persister_c, chain_mon_a, chain_mon_b, chain_mon_c);
426+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
427+
let (node_a, node_b, node_c);
428+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
429+
430+
let config = test_default_channel_config();
431+
let a_mons = &[&mon_a_1_ser[..]];
432+
reload_node!(nodes[0], config.clone(), &node_a_ser, a_mons, persister_a, chain_mon_a, node_a);
433+
let b_mons = &[&mon_b_1_ser[..], &mon_b_2_ser[..]];
434+
reload_node!(nodes[1], config.clone(), &node_b_ser, b_mons, persister_b, chain_mon_b, node_b);
435+
let c_mons = &[&mon_c_1_ser[..]];
436+
reload_node!(nodes[2], config, &node_c_ser, c_mons, persister_c, chain_mon_c, node_c);
437+
438+
reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
439+
let mut reconnect_b_c_args = ReconnectArgs::new(&nodes[1], &nodes[2]);
440+
reconnect_b_c_args.send_channel_ready = (true, true);
441+
reconnect_b_c_args.send_announcement_sigs = (true, true);
442+
reconnect_nodes(reconnect_b_c_args);
443+
444+
let contribution = SpliceContribution::SpliceOut {
445+
outputs: vec![TxOut {
446+
value: Amount::from_sat(1_000),
447+
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
448+
}],
449+
};
450+
let splice_tx = splice_channel(&nodes[0], &nodes[1], ChannelId(chan_id_bytes_a), contribution);
451+
for node in nodes.iter() {
452+
mine_transaction(node, &splice_tx);
453+
connect_blocks(node, ANTI_REORG_DELAY - 1);
454+
}
455+
456+
let splice_locked = get_event_msg!(nodes[0], MessageSendEvent::SendSpliceLocked, node_b_id);
457+
lock_splice(&nodes[0], &nodes[1], &splice_locked, false);
458+
459+
for node in nodes.iter() {
460+
connect_blocks(node, EXTRA_BLOCKS_BEFORE_FAIL - ANTI_REORG_DELAY);
461+
}
462+
463+
// Now release the HTLC to be failed back to node A
464+
nodes[1].node.process_pending_htlc_forwards();
465+
466+
let pay_secret = PaymentSecret(payment_secret_bytes);
467+
let pay_hash = PaymentHash(payment_hash_bytes);
468+
let pay_preimage = PaymentPreimage(payment_preimage_bytes);
469+
470+
if fail_htlc {
471+
let failure = HTLCHandlingFailureType::Forward {
472+
node_id: Some(node_c_id),
473+
channel_id: ChannelId(chan_id_bytes_b),
474+
};
475+
expect_and_process_pending_htlcs_and_htlc_handling_failed(&nodes[1], &[failure]);
476+
check_added_monitors(&nodes[1], 1);
477+
478+
let updates = get_htlc_update_msgs(&nodes[1], &node_a_id);
479+
nodes[0].node.handle_update_fail_htlc(node_b_id, &updates.update_fail_htlcs[0]);
480+
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
481+
let conditions = PaymentFailedConditions::new();
482+
expect_payment_failed_conditions(&nodes[0], pay_hash, false, conditions);
483+
} else {
484+
check_added_monitors(&nodes[1], 1);
485+
let forward_event = SendEvent::from_node(&nodes[1]);
486+
nodes[2].node.handle_update_add_htlc(node_b_id, &forward_event.msgs[0]);
487+
commitment_signed_dance!(nodes[2], nodes[1], forward_event.commitment_msg, false);
488+
489+
expect_and_process_pending_htlcs(&nodes[2], false);
490+
expect_payment_claimable!(nodes[2], pay_hash, pay_secret, 1_000_000);
491+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], pay_preimage);
492+
}
493+
}
494+
495+
#[test]
496+
fn test_0_1_htlc_forward_after_splice() {
497+
do_test_0_1_htlc_forward_after_splice(true);
498+
do_test_0_1_htlc_forward_after_splice(false);
499+
}

lightning/src/chain/channelmonitor.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub const ARCHIVAL_DELAY_BLOCKS: u32 = 4032;
327327
/// (2) is the same, but with an additional buffer to avoid accepting an HTLC which is immediately
328328
/// in a race condition between the user connecting a block (which would fail it) and the user
329329
/// providing us the preimage (which would claim it).
330-
pub(crate) const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS;
330+
pub const HTLC_FAIL_BACK_BUFFER: u32 = CLTV_CLAIM_BUFFER + LATENCY_GRACE_PERIOD_BLOCKS;
331331

332332
// Deprecated, use [`HolderCommitment`] or [`HolderCommitmentTransaction`].
333333
#[derive(Clone, PartialEq, Eq)]
@@ -4118,6 +4118,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41184118
self.funding.prev_holder_commitment_tx.clone(),
41194119
);
41204120

4121+
// It's possible that no commitment updates happened during the lifecycle of the pending
4122+
// splice's `FundingScope` that was promoted. If so, our `prev_holder_htlc_data` is
4123+
// now irrelevant, since there's no valid previous commitment that exists for the current
4124+
// funding transaction that could be broadcast.
4125+
if self.funding.prev_holder_commitment_tx.is_none() {
4126+
self.prev_holder_htlc_data.take();
4127+
}
4128+
41214129
let no_further_updates_allowed = self.no_further_updates_allowed();
41224130

41234131
// The swap above places the previous `FundingScope` into `pending_funding`.
@@ -7001,7 +7009,7 @@ mod tests {
70017009
let funding_outpoint = OutPoint { txid: Txid::all_zeros(), index: u16::MAX };
70027010
let channel_id = ChannelId::v1_from_funding_outpoint(funding_outpoint);
70037011
let channel_parameters = ChannelTransactionParameters {
7004-
holder_pubkeys: keys.new_pubkeys(None, &secp_ctx),
7012+
holder_pubkeys: keys.pubkeys(&secp_ctx),
70057013
holder_selected_contest_delay: 66,
70067014
is_outbound_from_holder: true,
70077015
counterparty_parameters: Some(CounterpartyChannelTransactionParameters {
@@ -7264,7 +7272,7 @@ mod tests {
72647272
let funding_outpoint = OutPoint { txid: Txid::all_zeros(), index: u16::MAX };
72657273
let channel_id = ChannelId::v1_from_funding_outpoint(funding_outpoint);
72667274
let channel_parameters = ChannelTransactionParameters {
7267-
holder_pubkeys: keys.new_pubkeys(None, &secp_ctx),
7275+
holder_pubkeys: keys.pubkeys(&secp_ctx),
72687276
holder_selected_contest_delay: 66,
72697277
is_outbound_from_holder: true,
72707278
counterparty_parameters: Some(CounterpartyChannelTransactionParameters {

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ mod tests {
13391339
// Use non-anchor channels so that HTLC-Timeouts are broadcast immediately instead of sent
13401340
// to the user for external funding.
13411341
let chan_params = ChannelTransactionParameters {
1342-
holder_pubkeys: signer.new_pubkeys(None, &secp_ctx),
1342+
holder_pubkeys: signer.pubkeys(&secp_ctx),
13431343
holder_selected_contest_delay: 66,
13441344
is_outbound_from_holder: true,
13451345
counterparty_parameters: Some(CounterpartyChannelTransactionParameters {

lightning/src/ln/chan_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,11 +1018,11 @@ pub struct ChannelTransactionParameters {
10181018
/// If a channel was funded with transaction A, and later spliced with transaction B, this field
10191019
/// tracks the txid of transaction A.
10201020
///
1021-
/// See [`compute_funding_key_tweak`] and [`ChannelSigner::new_pubkeys`] for more context on how
1021+
/// See [`compute_funding_key_tweak`] and [`ChannelSigner::pubkeys`] for more context on how
10221022
/// this may be used.
10231023
///
10241024
/// [`compute_funding_key_tweak`]: crate::sign::compute_funding_key_tweak
1025-
/// [`ChannelSigner::new_pubkeys`]: crate::sign::ChannelSigner::new_pubkeys
1025+
/// [`ChannelSigner::pubkeys`]: crate::sign::ChannelSigner::pubkeys
10261026
pub splice_parent_funding_txid: Option<Txid>,
10271027
/// This channel's type, as negotiated during channel open. For old objects where this field
10281028
/// wasn't serialized, it will default to static_remote_key at deserialization.
@@ -2245,8 +2245,8 @@ mod tests {
22452245
let counterparty_signer = keys_provider.derive_channel_signer(keys_provider.generate_channel_keys_id(true, 1));
22462246
let per_commitment_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
22472247
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
2248-
let holder_pubkeys = signer.new_pubkeys(None, &secp_ctx);
2249-
let counterparty_pubkeys = counterparty_signer.new_pubkeys(None, &secp_ctx).clone();
2248+
let holder_pubkeys = signer.pubkeys(&secp_ctx);
2249+
let counterparty_pubkeys = counterparty_signer.pubkeys(&secp_ctx).clone();
22502250
let channel_parameters = ChannelTransactionParameters {
22512251
holder_pubkeys: holder_pubkeys.clone(),
22522252
holder_selected_contest_delay: 0,

0 commit comments

Comments
 (0)