Skip to content

Commit 70684b1

Browse files
committed
Update tests to test re-claiming of forwarded HTLCs on startup
1 parent 15b919c commit 70684b1

File tree

4 files changed

+216
-43
lines changed

4 files changed

+216
-43
lines changed

lightning/src/chain/channelmonitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::sync::{Mutex, LockTestExt};
6969
/// much smaller than a full [`ChannelMonitor`]. However, for large single commitment transaction
7070
/// updates (e.g. ones during which there are hundreds of HTLCs pending on the commitment
7171
/// transaction), a single update may reach upwards of 1 MiB in serialized size.
72-
#[derive(Clone, PartialEq, Eq)]
72+
#[derive(Clone, Debug, PartialEq, Eq)]
7373
#[must_use]
7474
pub struct ChannelMonitorUpdate {
7575
pub(crate) updates: Vec<ChannelMonitorUpdateStep>,
@@ -490,7 +490,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
490490

491491
);
492492

493-
#[derive(Clone, PartialEq, Eq)]
493+
#[derive(Clone, Debug, PartialEq, Eq)]
494494
pub(crate) enum ChannelMonitorUpdateStep {
495495
LatestHolderCommitmentTXInfo {
496496
commitment_tx: HolderCommitmentTransaction,

lightning/src/ln/chan_utils.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
438438
/// channel basepoints via the new function, or they were obtained via
439439
/// CommitmentTransaction.trust().keys() because we trusted the source of the
440440
/// pre-calculated keys.
441-
#[derive(PartialEq, Eq, Clone)]
441+
#[derive(PartialEq, Eq, Clone, Debug)]
442442
pub struct TxCreationKeys {
443443
/// The broadcaster's per-commitment public key which was used to derive the other keys.
444444
pub per_commitment_point: PublicKey,
@@ -950,7 +950,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
950950
/// Information needed to build and sign a holder's commitment transaction.
951951
///
952952
/// The transaction is only signed once we are ready to broadcast.
953-
#[derive(Clone)]
953+
#[derive(Clone, Debug)]
954954
pub struct HolderCommitmentTransaction {
955955
inner: CommitmentTransaction,
956956
/// Our counterparty's signature for the transaction
@@ -1057,7 +1057,7 @@ impl HolderCommitmentTransaction {
10571057
}
10581058

10591059
/// A pre-built Bitcoin commitment transaction and its txid.
1060-
#[derive(Clone)]
1060+
#[derive(Clone, Debug)]
10611061
pub struct BuiltCommitmentTransaction {
10621062
/// The commitment transaction
10631063
pub transaction: Transaction,
@@ -1228,7 +1228,7 @@ impl<'a> TrustedClosingTransaction<'a> {
12281228
///
12291229
/// This class can be used inside a signer implementation to generate a signature given the relevant
12301230
/// secret key.
1231-
#[derive(Clone)]
1231+
#[derive(Clone, Debug)]
12321232
pub struct CommitmentTransaction {
12331233
commitment_number: u64,
12341234
to_broadcaster_value_sat: u64,

lightning/src/ln/chanmon_update_fail_tests.rs

+207-34
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,7 @@ fn test_blocked_chan_preimage_release() {
30533053
expect_payment_sent(&nodes[2], payment_preimage_2, None, true, true);
30543054
}
30553055

3056-
fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
3056+
fn do_test_inverted_mon_completion_order(with_latest_manager: bool, complete_bc_commitment_dance: bool) {
30573057
// When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
30583058
// channel, we immediately claim the HTLC on the upstream channel, before even doing a
30593059
// `commitment_signed` dance on the downstream channel. This implies that our
@@ -3081,6 +3081,10 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
30813081
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
30823082

30833083
let mon_ab = get_monitor!(nodes[1], chan_id_ab).encode();
3084+
let mut manager_b = Vec::new();
3085+
if !with_latest_manager {
3086+
manager_b = nodes[1].node.encode();
3087+
}
30843088

30853089
nodes[2].node.claim_funds(payment_preimage);
30863090
check_added_monitors(&nodes[2], 1);
@@ -3117,58 +3121,227 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
31173121
}
31183122

31193123
// Now reload node B
3120-
let manager_b = nodes[1].node.encode();
3124+
if with_latest_manager {
3125+
manager_b = nodes[1].node.encode();
3126+
}
31213127

31223128
let mon_bc = get_monitor!(nodes[1], chan_id_bc).encode();
31233129
reload_node!(nodes[1], &manager_b, &[&mon_ab, &mon_bc], persister, new_chain_monitor, nodes_1_deserialized);
31243130

31253131
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
31263132
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
31273133

3128-
// If we used the latest ChannelManager to reload from, we should have both channels still
3129-
// live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3130-
// before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3131-
// When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3132-
// complete after reconnecting to our peers.
3133-
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3134-
nodes[1].node.timer_tick_occurred();
3135-
check_added_monitors(&nodes[1], 1);
3136-
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3134+
if with_latest_manager {
3135+
// If we used the latest ChannelManager to reload from, we should have both channels still
3136+
// live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3137+
// before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3138+
// When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3139+
// complete after reconnecting to our peers.
3140+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3141+
nodes[1].node.timer_tick_occurred();
3142+
check_added_monitors(&nodes[1], 1);
3143+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
31373144

3138-
// Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3139-
// the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3140-
// expect to *not* receive the final RAA ChannelMonitorUpdate.
3141-
if complete_bc_commitment_dance {
3142-
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3145+
// Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3146+
// the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3147+
// expect to *not* receive the final RAA ChannelMonitorUpdate.
3148+
if complete_bc_commitment_dance {
3149+
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3150+
} else {
3151+
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, -2), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3152+
}
3153+
3154+
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3155+
3156+
// (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3157+
// disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3158+
// process.
3159+
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3160+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
3161+
3162+
// When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3163+
// completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3164+
// channel.
3165+
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3166+
check_added_monitors(&nodes[1], 1);
3167+
3168+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3169+
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3170+
3171+
expect_payment_forwarded!(nodes[1], &nodes[0], &nodes[2], Some(1_000), false, false);
31433172
} else {
3144-
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, -2), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3145-
}
3173+
// If the ChannelManager used in the reload was stale, check that the B <-> C channel was
3174+
// closed.
3175+
//
3176+
// Note that this will also process the ChannelMonitorUpdates which were queued up when we
3177+
// reloaded the ChannelManager. This will re-emit the A<->B preimage as well as the B<->C
3178+
// force-closure ChannelMonitorUpdate. Once the A<->B preimage update completes, the claim
3179+
// commitment update will be allowed to go out.
3180+
check_added_monitors(&nodes[1], 0);
3181+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3182+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3183+
check_closed_event(&nodes[1], 1, ClosureReason::OutdatedChannelManager, false);
3184+
check_added_monitors(&nodes[1], 2);
31463185

3147-
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3186+
nodes[1].node.timer_tick_occurred();
3187+
check_added_monitors(&nodes[1], 0);
31483188

3149-
// (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3150-
// disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3151-
// process.
3152-
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3153-
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
3189+
// Don't bother to reconnect B to C - that channel has been closed. We don't need to
3190+
// exchange any messages here even though there's a pending commitment update because the
3191+
// ChannelMonitorUpdate hasn't yet completed.
3192+
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
31543193

3155-
// When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3156-
// completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3157-
// channel.
3158-
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3159-
check_added_monitors(&nodes[1], 1);
3194+
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3195+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
31603196

3161-
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3162-
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3197+
// The ChannelMonitorUpdate which was completed prior to the reconnect only contained the
3198+
// preimage (as it was a replay of the original ChannelMonitorUpdate from before we
3199+
// restarted). When we go to fetch the commitment transaction updates we'll poll the
3200+
// ChannelMonitorUpdate completion, then generate (and complete) a new ChannelMonitorUpdate
3201+
// with the actual commitment transaction, which will allow us to fulfill the HTLC with
3202+
// node A.
3203+
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3204+
check_added_monitors(&nodes[1], 1);
31633205

3164-
expect_payment_forwarded!(nodes[1], &nodes[0], &nodes[2], Some(1_000), false, false);
3206+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3207+
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3208+
}
31653209

31663210
// Finally, check that the payment was, ultimately, seen as sent by node A.
31673211
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
31683212
}
31693213

31703214
#[test]
31713215
fn test_inverted_mon_completion_order() {
3172-
do_test_inverted_mon_completion_order(true);
3173-
do_test_inverted_mon_completion_order(false);
3216+
do_test_inverted_mon_completion_order(true, true);
3217+
do_test_inverted_mon_completion_order(true, false);
3218+
do_test_inverted_mon_completion_order(false, true);
3219+
do_test_inverted_mon_completion_order(false, false);
3220+
}
3221+
3222+
fn do_test_durable_preimages_on_closed_channel(close_chans_before_reload: bool, close_only_a: bool) {
3223+
// Test that we can apply a `ChannelMonitorUpdate` with a payment preimage even if the channel
3224+
// is force-closed between when we generate the update on reload and when we go to handle the
3225+
// update or prior to generating the update at all.
3226+
3227+
if !close_chans_before_reload && close_only_a {
3228+
// If we're not closing, it makes no sense to "only close A"
3229+
panic!();
3230+
}
3231+
3232+
let chanmon_cfgs = create_chanmon_cfgs(3);
3233+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3234+
3235+
let persister;
3236+
let new_chain_monitor;
3237+
let nodes_1_deserialized;
3238+
3239+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3240+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3241+
3242+
let chan_id_ab = create_announced_chan_between_nodes(&nodes, 0, 1).2;
3243+
let chan_id_bc = create_announced_chan_between_nodes(&nodes, 1, 2).2;
3244+
3245+
// Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3246+
// `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3247+
// on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3248+
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
3249+
3250+
let mon_ab = get_monitor!(nodes[1], chan_id_ab).encode();
3251+
3252+
nodes[2].node.claim_funds(payment_preimage);
3253+
check_added_monitors(&nodes[2], 1);
3254+
expect_payment_claimed!(nodes[2], payment_hash, 1_000_000);
3255+
3256+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3257+
let cs_updates = get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
3258+
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
3259+
3260+
// B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3261+
// for it since the monitor update is marked in-progress.
3262+
check_added_monitors(&nodes[1], 1);
3263+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3264+
3265+
// Now step the Commitment Signed Dance between B and C forward a bit, ensuring we won't get
3266+
// the preimage when the nodes reconnect, at which point we have to ensure we get it from the
3267+
// ChannelMonitor.
3268+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &cs_updates.commitment_signed);
3269+
check_added_monitors(&nodes[1], 1);
3270+
let _ = get_revoke_commit_msgs!(nodes[1], nodes[2].node.get_our_node_id());
3271+
3272+
let mon_bc = get_monitor!(nodes[1], chan_id_bc).encode();
3273+
3274+
if close_chans_before_reload {
3275+
if !close_only_a {
3276+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3277+
nodes[1].node.force_close_broadcasting_latest_txn(&chan_id_bc, &nodes[2].node.get_our_node_id()).unwrap();
3278+
check_closed_broadcast(&nodes[1], 1, true);
3279+
check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false);
3280+
}
3281+
3282+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3283+
nodes[1].node.force_close_broadcasting_latest_txn(&chan_id_ab, &nodes[0].node.get_our_node_id()).unwrap();
3284+
check_closed_broadcast(&nodes[1], 1, true);
3285+
check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false);
3286+
}
3287+
3288+
// Now reload node B
3289+
let manager_b = nodes[1].node.encode();
3290+
reload_node!(nodes[1], &manager_b, &[&mon_ab, &mon_bc], persister, new_chain_monitor, nodes_1_deserialized);
3291+
3292+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3293+
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3294+
3295+
if close_chans_before_reload {
3296+
// If the channels were already closed, B will rebroadcast its closing transactions here.
3297+
let bs_close_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3298+
if close_only_a {
3299+
assert_eq!(bs_close_txn.len(), 2);
3300+
} else {
3301+
assert_eq!(bs_close_txn.len(), 3);
3302+
}
3303+
}
3304+
3305+
nodes[0].node.force_close_broadcasting_latest_txn(&chan_id_ab, &nodes[1].node.get_our_node_id()).unwrap();
3306+
check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false);
3307+
let as_closing_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3308+
assert_eq!(as_closing_tx.len(), 1);
3309+
3310+
// In order to give B A's closing transaction without processing background events first, use
3311+
// the _without_checks utility method. This is similar to connecting blocks during startup
3312+
// prior to the node being full initialized.
3313+
mine_transaction_without_checks(&nodes[1], &as_closing_tx[0]);
3314+
3315+
// After a timer tick a payment preimage ChannelMonitorUpdate is applied to the A<->B
3316+
// ChannelMonitor, even though the channel has since been closed.
3317+
check_added_monitors(&nodes[1], 0);
3318+
nodes[1].node.timer_tick_occurred();
3319+
check_added_monitors(&nodes[1], if close_chans_before_reload && !close_only_a { 3 } else { 2 });
3320+
3321+
// Finally, check that B created a payment preimage transaction and close out the payment.
3322+
let bs_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3323+
let bs_preimage_tx = if close_chans_before_reload && !close_only_a {
3324+
assert_eq!(bs_txn.len(), 2);
3325+
&bs_txn[1]
3326+
} else {
3327+
assert_eq!(bs_txn.len(), 1);
3328+
&bs_txn[0]
3329+
};
3330+
check_spends!(bs_preimage_tx, as_closing_tx[0]);
3331+
3332+
if !close_chans_before_reload {
3333+
check_closed_broadcast(&nodes[1], 1, true);
3334+
check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false);
3335+
}
3336+
3337+
mine_transactions(&nodes[0], &[&as_closing_tx[0], bs_preimage_tx]);
3338+
check_closed_broadcast(&nodes[0], 1, true);
3339+
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
3340+
}
3341+
3342+
#[test]
3343+
fn test_durable_preimages_on_closed_channel() {
3344+
do_test_durable_preimages_on_closed_channel(true, true);
3345+
do_test_durable_preimages_on_closed_channel(true, false);
3346+
do_test_durable_preimages_on_closed_channel(false, false);
31743347
}

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub(super) enum HTLCForwardInfo {
168168
}
169169

170170
/// Tracks the inbound corresponding to an outbound HTLC
171-
#[derive(Clone, Hash, PartialEq, Eq)]
171+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
172172
pub(crate) struct HTLCPreviousHopData {
173173
// Note that this may be an outbound SCID alias for the associated channel.
174174
short_channel_id: u64,
@@ -248,7 +248,7 @@ impl Readable for InterceptId {
248248
}
249249
}
250250

251-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
251+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
252252
/// Uniquely describes an HTLC by its source. Just the guaranteed-unique subset of [`HTLCSource`].
253253
pub(crate) enum SentHTLCId {
254254
PreviousHopData { short_channel_id: u64, htlc_id: u64 },
@@ -279,7 +279,7 @@ impl_writeable_tlv_based_enum!(SentHTLCId,
279279

280280
/// Tracks the inbound corresponding to an outbound HTLC
281281
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
282-
#[derive(Clone, PartialEq, Eq)]
282+
#[derive(Clone, Debug, PartialEq, Eq)]
283283
pub(crate) enum HTLCSource {
284284
PreviousHopData(HTLCPreviousHopData),
285285
OutboundRoute {

0 commit comments

Comments
 (0)