Skip to content

Commit 2c836b3

Browse files
committed
Add an upgrade test of splicing after upgrading from 0.1
1 parent d12c6a3 commit 2c836b3

File tree

5 files changed

+221
-15
lines changed

5 files changed

+221
-15
lines changed

lightning-tests/Cargo.toml

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

2020
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: 1 addition & 1 deletion
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)]

lightning/src/ln/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ mod reorg_tests;
120120
#[cfg(test)]
121121
#[allow(unused_mut)]
122122
mod shutdown_tests;
123-
#[cfg(test)]
124-
mod splicing_tests;
123+
#[cfg(any(feature = "_test_utils", test))]
124+
pub mod splicing_tests;
125125
#[cfg(any(test, feature = "_externalize_tests"))]
126126
#[allow(unused_mut)]
127127
pub mod update_fee_tests;

lightning/src/ln/splicing_tests.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
#![cfg_attr(not(test), allow(unused_imports))]
11+
1012
use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;
1113
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS};
1214
use crate::chain::transaction::OutPoint;
@@ -68,7 +70,7 @@ fn test_v1_splice_in_negative_insufficient_inputs() {
6870
}
6971
}
7072

71-
fn negotiate_splice_tx<'a, 'b, 'c, 'd>(
73+
pub fn negotiate_splice_tx<'a, 'b, 'c, 'd>(
7274
initiator: &'a Node<'b, 'c, 'd>, acceptor: &'a Node<'b, 'c, 'd>, channel_id: ChannelId,
7375
initiator_contribution: SpliceContribution,
7476
) -> msgs::CommitmentSigned {
@@ -83,7 +85,7 @@ fn negotiate_splice_tx<'a, 'b, 'c, 'd>(
8385
)
8486
}
8587

86-
fn complete_splice_handshake<'a, 'b, 'c, 'd>(
88+
pub fn complete_splice_handshake<'a, 'b, 'c, 'd>(
8789
initiator: &'a Node<'b, 'c, 'd>, acceptor: &'a Node<'b, 'c, 'd>, channel_id: ChannelId,
8890
initiator_contribution: SpliceContribution,
8991
) -> ScriptBuf {
@@ -120,7 +122,7 @@ fn complete_splice_handshake<'a, 'b, 'c, 'd>(
120122
new_funding_script
121123
}
122124

123-
fn complete_interactive_funding_negotiation<'a, 'b, 'c, 'd>(
125+
pub fn complete_interactive_funding_negotiation<'a, 'b, 'c, 'd>(
124126
initiator: &'a Node<'b, 'c, 'd>, acceptor: &'a Node<'b, 'c, 'd>, channel_id: ChannelId,
125127
initiator_contribution: SpliceContribution, new_funding_script: ScriptBuf,
126128
) -> msgs::CommitmentSigned {
@@ -209,7 +211,7 @@ fn complete_interactive_funding_negotiation<'a, 'b, 'c, 'd>(
209211
}
210212
}
211213

212-
fn sign_interactive_funding_tx<'a, 'b, 'c, 'd>(
214+
pub fn sign_interactive_funding_tx<'a, 'b, 'c, 'd>(
213215
initiator: &'a Node<'b, 'c, 'd>, acceptor: &'a Node<'b, 'c, 'd>,
214216
initial_commit_sig_for_acceptor: msgs::CommitmentSigned, is_0conf: bool,
215217
) -> (Transaction, Option<(msgs::SpliceLocked, PublicKey)>) {
@@ -277,7 +279,7 @@ fn sign_interactive_funding_tx<'a, 'b, 'c, 'd>(
277279
(tx, splice_locked)
278280
}
279281

280-
fn splice_channel<'a, 'b, 'c, 'd>(
282+
pub fn splice_channel<'a, 'b, 'c, 'd>(
281283
initiator: &'a Node<'b, 'c, 'd>, acceptor: &'a Node<'b, 'c, 'd>, channel_id: ChannelId,
282284
initiator_contribution: SpliceContribution,
283285
) -> Transaction {
@@ -304,7 +306,7 @@ fn splice_channel<'a, 'b, 'c, 'd>(
304306
splice_tx
305307
}
306308

307-
fn lock_splice_after_blocks<'a, 'b, 'c, 'd>(
309+
pub fn lock_splice_after_blocks<'a, 'b, 'c, 'd>(
308310
node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, num_blocks: u32,
309311
) {
310312
connect_blocks(node_a, num_blocks);
@@ -316,7 +318,7 @@ fn lock_splice_after_blocks<'a, 'b, 'c, 'd>(
316318
lock_splice(node_a, node_b, &splice_locked_for_node_b, false);
317319
}
318320

319-
fn lock_splice<'a, 'b, 'c, 'd>(
321+
pub fn lock_splice<'a, 'b, 'c, 'd>(
320322
node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>,
321323
splice_locked_for_node_b: &msgs::SpliceLocked, is_0conf: bool,
322324
) {
@@ -387,6 +389,7 @@ fn test_splice_state_reset_on_disconnect() {
387389
do_test_splice_state_reset_on_disconnect(true);
388390
}
389391

392+
#[cfg(test)]
390393
fn do_test_splice_state_reset_on_disconnect(reload: bool) {
391394
// Tests that we're able to forget our pending splice state after a disconnect such that we can
392395
// retry later.
@@ -714,6 +717,7 @@ fn test_splice_out() {
714717
let _ = send_payment(&nodes[0], &[&nodes[1]], htlc_limit_msat);
715718
}
716719

720+
#[cfg(test)]
717721
#[derive(PartialEq)]
718722
enum SpliceStatus {
719723
Unconfirmed,
@@ -731,6 +735,7 @@ fn test_splice_commitment_broadcast() {
731735
do_test_splice_commitment_broadcast(SpliceStatus::Locked, true);
732736
}
733737

738+
#[cfg(test)]
734739
fn do_test_splice_commitment_broadcast(splice_status: SpliceStatus, claim_htlcs: bool) {
735740
// Tests that we're able to enforce HTLCs onchain during the different stages of a splice.
736741
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -926,6 +931,7 @@ fn test_splice_reestablish() {
926931
do_test_splice_reestablish(true, true);
927932
}
928933

934+
#[cfg(test)]
929935
fn do_test_splice_reestablish(reload: bool, async_monitor_update: bool) {
930936
// Test that we're able to reestablish the channel succesfully throughout the lifecycle of a splice.
931937
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -1188,6 +1194,7 @@ fn test_propose_splice_while_disconnected() {
11881194
do_test_propose_splice_while_disconnected(true, true);
11891195
}
11901196

1197+
#[cfg(test)]
11911198
fn do_test_propose_splice_while_disconnected(reload: bool, use_0conf: bool) {
11921199
// Test that both nodes are able to propose a splice while the counterparty is disconnected, and
11931200
// whoever doesn't go first due to the quiescence tie-breaker, will retry their splice after the
@@ -1804,6 +1811,7 @@ fn fail_quiescent_action_on_channel_close() {
18041811
check_added_monitors(&nodes[0], 1);
18051812
}
18061813

1814+
#[cfg(test)]
18071815
fn do_test_splice_with_inflight_htlc_forward_and_resolution(expire_scid_pre_forward: bool) {
18081816
// Test that we are still able to forward and resolve HTLCs while the original SCIDs contained
18091817
// in the onion packets have now changed due channel splices becoming locked.

0 commit comments

Comments
 (0)