Skip to content

Commit 304d811

Browse files
committed
Re-claim forwarded HTLCs on startup
Now that we let `commitment_signed` `ChannelMonitorUpdate`s from a downstream channel complete prior to the preimage `ChannelMonitorUpdate` on the upstream channel, we may not get a `update_fulfill_htlc` replay on startup. Thus, we have to ensure any payment preimages contained in that downstream update are re-claimed on startup. Here we do this during the existing walk of the `ChannelMonitor` preimages for closed channels.
1 parent 0d002f7 commit 304d811

File tree

4 files changed

+261
-43
lines changed

4 files changed

+261
-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
@@ -437,7 +437,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
437437
/// channel basepoints via the new function, or they were obtained via
438438
/// CommitmentTransaction.trust().keys() because we trusted the source of the
439439
/// pre-calculated keys.
440-
#[derive(PartialEq, Eq, Clone)]
440+
#[derive(PartialEq, Eq, Clone, Debug)]
441441
pub struct TxCreationKeys {
442442
/// The broadcaster's per-commitment public key which was used to derive the other keys.
443443
pub per_commitment_point: PublicKey,
@@ -949,7 +949,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
949949
/// Information needed to build and sign a holder's commitment transaction.
950950
///
951951
/// The transaction is only signed once we are ready to broadcast.
952-
#[derive(Clone)]
952+
#[derive(Clone, Debug)]
953953
pub struct HolderCommitmentTransaction {
954954
inner: CommitmentTransaction,
955955
/// Our counterparty's signature for the transaction
@@ -1052,7 +1052,7 @@ impl HolderCommitmentTransaction {
10521052
}
10531053

10541054
/// A pre-built Bitcoin commitment transaction and its txid.
1055-
#[derive(Clone)]
1055+
#[derive(Clone, Debug)]
10561056
pub struct BuiltCommitmentTransaction {
10571057
/// The commitment transaction
10581058
pub transaction: Transaction,
@@ -1215,7 +1215,7 @@ impl<'a> TrustedClosingTransaction<'a> {
12151215
///
12161216
/// This class can be used inside a signer implementation to generate a signature given the relevant
12171217
/// secret key.
1218-
#[derive(Clone)]
1218+
#[derive(Clone, Debug)]
12191219
pub struct CommitmentTransaction {
12201220
commitment_number: u64,
12211221
to_broadcaster_value_sat: u64,

lightning/src/ln/chanmon_update_fail_tests.rs

+207-34
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,7 @@ fn test_blocked_chan_preimage_release() {
30213021
expect_payment_sent(&nodes[2], payment_preimage_2, None, true, true);
30223022
}
30233023

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

30513051
let mon_ab = get_monitor!(nodes[1], chan_id_ab).encode();
3052+
let mut manager_b = Vec::new();
3053+
if !with_latest_manager {
3054+
manager_b = nodes[1].node.encode();
3055+
}
30523056

30533057
nodes[2].node.claim_funds(payment_preimage);
30543058
check_added_monitors(&nodes[2], 1);
@@ -3085,58 +3089,227 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
30853089
}
30863090

30873091
// Now reload node B
3088-
let manager_b = nodes[1].node.encode();
3092+
if with_latest_manager {
3093+
manager_b = nodes[1].node.encode();
3094+
}
30893095

30903096
let mon_bc = get_monitor!(nodes[1], chan_id_bc).encode();
30913097
reload_node!(nodes[1], &manager_b, &[&mon_ab, &mon_bc], persister, new_chain_monitor, nodes_1_deserialized);
30923098

30933099
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
30943100
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
30953101

3096-
// If we used the latest ChannelManager to reload from, we should have both channels still
3097-
// live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3098-
// before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3099-
// When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3100-
// complete after reconnecting to our peers.
3101-
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3102-
nodes[1].node.timer_tick_occurred();
3103-
check_added_monitors(&nodes[1], 1);
3104-
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3102+
if with_latest_manager {
3103+
// If we used the latest ChannelManager to reload from, we should have both channels still
3104+
// live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3105+
// before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3106+
// When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3107+
// complete after reconnecting to our peers.
3108+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3109+
nodes[1].node.timer_tick_occurred();
3110+
check_added_monitors(&nodes[1], 1);
3111+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
31053112

3106-
// Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3107-
// the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3108-
// expect to *not* receive the final RAA ChannelMonitorUpdate.
3109-
if complete_bc_commitment_dance {
3110-
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3113+
// Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3114+
// the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3115+
// expect to *not* receive the final RAA ChannelMonitorUpdate.
3116+
if complete_bc_commitment_dance {
3117+
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3118+
} else {
3119+
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, -2), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3120+
}
3121+
3122+
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3123+
3124+
// (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3125+
// disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3126+
// process.
3127+
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3128+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
3129+
3130+
// When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3131+
// completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3132+
// channel.
3133+
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3134+
check_added_monitors(&nodes[1], 1);
3135+
3136+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3137+
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3138+
3139+
expect_payment_forwarded!(nodes[1], &nodes[0], &nodes[2], Some(1_000), false, false);
31113140
} else {
3112-
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, -2), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3113-
}
3141+
// If the ChannelManager used in the reload was stale, check that the B <-> C channel was
3142+
// closed.
3143+
//
3144+
// Note that this will also process the ChannelMonitorUpdates which were queued up when we
3145+
// reloaded the ChannelManager. This will re-emit the A<->B preimage as well as the B<->C
3146+
// force-closure ChannelMonitorUpdate. Once the A<->B preimage update completes, the claim
3147+
// commitment update will be allowed to go out.
3148+
check_added_monitors(&nodes[1], 0);
3149+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3150+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3151+
check_closed_event(&nodes[1], 1, ClosureReason::OutdatedChannelManager, false);
3152+
check_added_monitors(&nodes[1], 2);
31143153

3115-
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3154+
nodes[1].node.timer_tick_occurred();
3155+
check_added_monitors(&nodes[1], 0);
31163156

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

3123-
// When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3124-
// completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3125-
// channel.
3126-
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3127-
check_added_monitors(&nodes[1], 1);
3162+
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3163+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
31283164

3129-
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3130-
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3165+
// The ChannelMonitorUpdate which was completed prior to the reconnect only contained the
3166+
// preimage (as it was a replay of the original ChannelMonitorUpdate from before we
3167+
// restarted). When we go to fetch the commitment transaction updates we'll poll the
3168+
// ChannelMonitorUpdate completion, then generate (and complete) a new ChannelMonitorUpdate
3169+
// with the actual commitment transaction, which will allow us to fulfill the HTLC with
3170+
// node A.
3171+
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3172+
check_added_monitors(&nodes[1], 1);
31313173

3132-
expect_payment_forwarded!(nodes[1], &nodes[0], &nodes[2], Some(1_000), false, false);
3174+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3175+
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3176+
}
31333177

31343178
// Finally, check that the payment was, ultimately, seen as sent by node A.
31353179
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
31363180
}
31373181

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

0 commit comments

Comments
 (0)