Skip to content

Commit 365674e

Browse files
committed
Block the mon update removing a preimage until upstream mon writes
When we forward a payment and receive an `update_fulfill_htlc` message from the downstream channel, we immediately claim the HTLC on the upstream channel, before even doing a `commitment_signed` dance on the downstream channel. This implies that our `ChannelMonitorUpdate`s "go out" in the right order - first we ensure we'll get our money by writing the preimage down, then we write the update that resolves giving money on the downstream node. This is safe as long as `ChannelMonitorUpdate`s complete in the order in which they are generated, but of course looking forward we want to support asynchronous updates, which may complete in any order. Thus, here, we enforce the correct ordering by blocking the downstream `ChannelMonitorUpdate` until the upstream one completes. Like the `PaymentSent` event handling we do so only for the `revoke_and_ack` `ChannelMonitorUpdate`, ensuring the preimage-containing upstream update has a full RTT to complete before we actually manage to slow anything down.
1 parent 61d896d commit 365674e

File tree

3 files changed

+217
-35
lines changed

3 files changed

+217
-35
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,18 +3067,27 @@ fn test_blocked_chan_preimage_release() {
30673067
check_added_monitors(&nodes[1], 1); // We generate only a preimage monitor update
30683068
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
30693069

3070-
// Finish the CS dance between nodes[0] and nodes[1].
3071-
do_commitment_signed_dance(&nodes[1], &nodes[0], &as_htlc_fulfill_updates.commitment_signed, false, false);
3070+
// Finish the CS dance between nodes[0] and nodes[1]. Note that until the final RAA CS is held
3071+
// until the full set of `ChannelMonitorUpdate`s on the nodes[1] <-> nodes[2] channel are
3072+
// complete, while the preimage that we care about ensuring is on disk did make it there above,
3073+
// the holding logic doesn't care about the type of update, it just cares that there is one.
3074+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_htlc_fulfill_updates.commitment_signed);
3075+
check_added_monitors(&nodes[1], 1);
3076+
let (a, raa) = do_main_commitment_signed_dance(&nodes[1], &nodes[0], false);
3077+
assert!(a.is_none());
3078+
3079+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
30723080
check_added_monitors(&nodes[1], 0);
3081+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
30733082

30743083
let events = nodes[1].node.get_and_clear_pending_events();
30753084
assert_eq!(events.len(), 3);
30763085
if let Event::PaymentSent { .. } = events[0] {} else { panic!(); }
30773086
if let Event::PaymentPathSuccessful { .. } = events[2] {} else { panic!(); }
30783087
if let Event::PaymentForwarded { .. } = events[1] {} else { panic!(); }
30793088

3080-
// The event processing should release the last RAA update.
3081-
check_added_monitors(&nodes[1], 1);
3089+
// The event processing should release the last RAA updates on both channels.
3090+
check_added_monitors(&nodes[1], 2);
30823091

30833092
// When we fetch the next update the message getter will generate the next update for nodes[2],
30843093
// generating a further monitor update.
@@ -3089,3 +3098,126 @@ fn test_blocked_chan_preimage_release() {
30893098
do_commitment_signed_dance(&nodes[2], &nodes[1], &bs_htlc_fulfill_updates.commitment_signed, false, false);
30903099
expect_payment_sent(&nodes[2], payment_preimage_2, None, true, true);
30913100
}
3101+
3102+
fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
3103+
// When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
3104+
// channel, we immediately claim the HTLC on the upstream channel, before even doing a
3105+
// `commitment_signed` dance on the downstream channel. This implies that our
3106+
// `ChannelMonitorUpdate`s "go out" in the right order - first we ensure we'll get our money,
3107+
// then we write the update that resolves giving money on the downstream node. This is safe as
3108+
// long as `ChannelMonitorUpdate`s complete in the order in which they are generated, but of
3109+
// course this may not be the case. For asynchronous update writes, we have to ensure monitor
3110+
// updates can block each other, preventing the inversion all together.
3111+
let chanmon_cfgs = create_chanmon_cfgs(3);
3112+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3113+
3114+
let persister;
3115+
let new_chain_monitor;
3116+
let nodes_1_deserialized;
3117+
3118+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3119+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3120+
3121+
let chan_id_ab = create_announced_chan_between_nodes(&nodes, 0, 1).2;
3122+
let chan_id_bc = create_announced_chan_between_nodes(&nodes, 1, 2).2;
3123+
3124+
// Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3125+
// `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3126+
// on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3127+
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100_000);
3128+
3129+
let mon_ab = get_monitor!(nodes[1], chan_id_ab).encode();
3130+
3131+
nodes[2].node.claim_funds(payment_preimage);
3132+
check_added_monitors(&nodes[2], 1);
3133+
expect_payment_claimed!(nodes[2], payment_hash, 100_000);
3134+
3135+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3136+
let cs_updates = get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
3137+
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
3138+
3139+
// B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3140+
// for it since the monitor update is marked in-progress.
3141+
check_added_monitors(&nodes[1], 1);
3142+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3143+
3144+
// Now step the Commitment Signed Dance between B and C forward a bit (or fully), ensuring we
3145+
// won't get the preimage when the nodes reconnect, at which point we have to ensure we get it
3146+
// from the ChannelMonitor.
3147+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &cs_updates.commitment_signed);
3148+
check_added_monitors(&nodes[1], 1);
3149+
if complete_bc_commitment_dance {
3150+
let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[2].node.get_our_node_id());
3151+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3152+
check_added_monitors(&nodes[2], 1);
3153+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
3154+
check_added_monitors(&nodes[2], 1);
3155+
let cs_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3156+
3157+
// At this point node B still hasn't persisted the `ChannelMonitorUpdate` with the
3158+
// preimage in the A <-> B channel, which will prevent it from persisting the
3159+
// `ChannelMonitorUpdate` here to avoid "losing" the preimage.
3160+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &cs_raa);
3161+
check_added_monitors(&nodes[1], 0);
3162+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3163+
}
3164+
3165+
// Now reload node B
3166+
let manager_b = nodes[1].node.encode();
3167+
3168+
let mon_bc = get_monitor!(nodes[1], chan_id_bc).encode();
3169+
reload_node!(nodes[1], &manager_b, &[&mon_ab, &mon_bc], persister, new_chain_monitor, nodes_1_deserialized);
3170+
3171+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3172+
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3173+
3174+
// If we used the latest ChannelManager to reload from, we should have both channels still
3175+
// live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3176+
// before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3177+
// When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3178+
// complete after reconnecting to our peers.
3179+
persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3180+
nodes[1].node.timer_tick_occurred();
3181+
check_added_monitors(&nodes[1], 1);
3182+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3183+
3184+
// Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3185+
// the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3186+
// expect to *not* receive the final RAA ChannelMonitorUpdate.
3187+
if complete_bc_commitment_dance {
3188+
reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[2]));
3189+
} else {
3190+
let mut reconnect_args = ReconnectArgs::new(&nodes[1], &nodes[2]);
3191+
reconnect_args.pending_htlc_adds = (0, -2);
3192+
reconnect_args.pending_raa = (false, true);
3193+
reconnect_nodes(reconnect_args);
3194+
}
3195+
3196+
reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3197+
3198+
// (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3199+
// disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3200+
// process.
3201+
let (outpoint, _, ab_update_id) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_ab).unwrap().clone();
3202+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, ab_update_id).unwrap();
3203+
3204+
// When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3205+
// completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3206+
// channel.
3207+
let bs_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id());
3208+
check_added_monitors(&nodes[1], 1);
3209+
3210+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
3211+
do_commitment_signed_dance(&nodes[0], &nodes[1], &bs_updates.commitment_signed, false, false);
3212+
3213+
expect_payment_forwarded!(nodes[1], &nodes[0], &nodes[2], Some(1_000), false, false);
3214+
3215+
// Finally, check that the payment was, ultimately, seen as sent by node A.
3216+
expect_payment_sent(&nodes[0], payment_preimage, None, true, true);
3217+
}
3218+
3219+
#[test]
3220+
fn test_inverted_mon_completion_order() {
3221+
do_test_inverted_mon_completion_order(true);
3222+
do_test_inverted_mon_completion_order(false);
3223+
}

lightning/src/ln/channelmanager.rs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,6 @@ pub(crate) enum RAAMonitorUpdateBlockingAction {
650650
}
651651

652652
impl RAAMonitorUpdateBlockingAction {
653-
#[allow(unused)]
654653
fn from_prev_hop_data(prev_hop: &HTLCPreviousHopData) -> Self {
655654
Self::ForwardedPaymentInboundClaim {
656655
channel_id: prev_hop.outpoint.to_channel_id(),
@@ -5120,11 +5119,14 @@ where
51205119
self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
51215120
}
51225121

5123-
fn claim_funds_internal(&self, source: HTLCSource, payment_preimage: PaymentPreimage, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool, next_channel_outpoint: OutPoint) {
5122+
fn claim_funds_internal(&self, source: HTLCSource, payment_preimage: PaymentPreimage, forwarded_htlc_value_msat: Option<u64>, from_onchain: bool, next_channel_counterparty_node_id: Option<PublicKey>, next_channel_outpoint: OutPoint) {
51245123
match source {
51255124
HTLCSource::OutboundRoute { session_priv, payment_id, path, .. } => {
51265125
debug_assert!(self.background_events_processed_since_startup.load(Ordering::Acquire),
51275126
"We don't support claim_htlc claims during startup - monitors may not be available yet");
5127+
if let Some(pubkey) = next_channel_counterparty_node_id {
5128+
debug_assert_eq!(pubkey, path.hops[0].pubkey);
5129+
}
51285130
let ev_completion_action = EventCompletionAction::ReleaseRAAChannelMonitorUpdate {
51295131
channel_funding_outpoint: next_channel_outpoint,
51305132
counterparty_node_id: path.hops[0].pubkey,
@@ -5135,6 +5137,7 @@ where
51355137
},
51365138
HTLCSource::PreviousHopData(hop_data) => {
51375139
let prev_outpoint = hop_data.outpoint;
5140+
let completed_blocker = RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
51385141
let res = self.claim_funds_from_hop(hop_data, payment_preimage,
51395142
|htlc_claim_value_msat| {
51405143
if let Some(forwarded_htlc_value) = forwarded_htlc_value_msat {
@@ -5150,7 +5153,17 @@ where
51505153
next_channel_id: Some(next_channel_outpoint.to_channel_id()),
51515154
outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
51525155
},
5153-
downstream_counterparty_and_funding_outpoint: None,
5156+
downstream_counterparty_and_funding_outpoint:
5157+
if let Some(node_id) = next_channel_counterparty_node_id {
5158+
Some((node_id, next_channel_outpoint, completed_blocker))
5159+
} else {
5160+
// We can only get `None` here if we are processing a
5161+
// `ChannelMonitor`-originated event, in which case we
5162+
// don't care about ensuring we wake the downstream
5163+
// channel's monitor updating - the channel is already
5164+
// closed.
5165+
None
5166+
},
51545167
})
51555168
} else { None }
51565169
});
@@ -5946,13 +5959,27 @@ where
59465959
match peer_state.channel_by_id.entry(msg.channel_id) {
59475960
hash_map::Entry::Occupied(mut chan) => {
59485961
let res = try_chan_entry!(self, chan.get_mut().update_fulfill_htlc(&msg), chan);
5962+
if let HTLCSource::PreviousHopData(prev_hop) = &res.0 {
5963+
peer_state.actions_blocking_raa_monitor_updates.entry(msg.channel_id)
5964+
.or_insert_with(Vec::new)
5965+
.push(RAAMonitorUpdateBlockingAction::from_prev_hop_data(&prev_hop));
5966+
}
5967+
// Note that we do not need to push an `actions_blocking_raa_monitor_updates`
5968+
// entry here, even though we *do* need to block the next RAA coming in from
5969+
// generating a monitor update which we let fly. We do this instead in the
5970+
// `claim_funds_internal` by attaching a `ReleaseRAAChannelMonitorUpdate`
5971+
// action to the event generated when we "claim" the sent payment. This is
5972+
// guaranteed to all complete before we process the RAA even though there is no
5973+
// lock held through that point as we aren't allowed to see another P2P message
5974+
// from the counterparty until we return, but `claim_funds_internal` runs
5975+
// first.
59495976
funding_txo = chan.get().context.get_funding_txo().expect("We won't accept a fulfill until funded");
59505977
res
59515978
},
59525979
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
59535980
}
59545981
};
5955-
self.claim_funds_internal(htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), false, funding_txo);
5982+
self.claim_funds_internal(htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), false, Some(*counterparty_node_id), funding_txo);
59565983
Ok(())
59575984
}
59585985

@@ -6139,6 +6166,23 @@ where
61396166
})
61406167
}
61416168

6169+
#[cfg(any(test, feature = "_test_utils"))]
6170+
pub(crate) fn test_raa_monitor_updates_held(&self, counterparty_node_id: PublicKey,
6171+
channel_id: ChannelId)
6172+
-> bool {
6173+
let per_peer_state = self.per_peer_state.read().unwrap();
6174+
if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
6175+
let mut peer_state_lck = peer_state_mtx.lock().unwrap();
6176+
let peer_state = &mut *peer_state_lck;
6177+
6178+
if let Some(chan) = peer_state.channel_by_id.get(&channel_id) {
6179+
return self.raa_monitor_updates_held(&peer_state.actions_blocking_raa_monitor_updates,
6180+
chan.context.get_funding_txo().unwrap(), counterparty_node_id);
6181+
}
6182+
}
6183+
false
6184+
}
6185+
61426186
fn internal_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
61436187
let (htlcs_to_fail, res) = {
61446188
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -6335,8 +6379,8 @@ where
63356379
match monitor_event {
63366380
MonitorEvent::HTLCEvent(htlc_update) => {
63376381
if let Some(preimage) = htlc_update.payment_preimage {
6338-
log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", &preimage);
6339-
self.claim_funds_internal(htlc_update.source, preimage, htlc_update.htlc_value_satoshis.map(|v| v * 1000), true, funding_outpoint);
6382+
log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", preimage);
6383+
self.claim_funds_internal(htlc_update.source, preimage, htlc_update.htlc_value_satoshis.map(|v| v * 1000), true, counterparty_node_id, funding_outpoint);
63406384
} else {
63416385
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", &htlc_update.payment_hash);
63426386
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
@@ -9114,6 +9158,7 @@ where
91149158
// downstream chan is closed (because we don't have a
91159159
// channel_id -> peer map entry).
91169160
counterparty_opt.is_none(),
9161+
counterparty_opt.cloned().or(monitor.get_counterparty_node_id()),
91179162
monitor.get_funding_txo().0))
91189163
} else { None }
91199164
} else {
@@ -9385,12 +9430,12 @@ where
93859430
channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
93869431
}
93879432

9388-
for (source, preimage, downstream_value, downstream_closed, downstream_funding) in pending_claims_to_replay {
9433+
for (source, preimage, downstream_value, downstream_closed, downstream_node_id, downstream_funding) in pending_claims_to_replay {
93899434
// We use `downstream_closed` in place of `from_onchain` here just as a guess - we
93909435
// don't remember in the `ChannelMonitor` where we got a preimage from, but if the
93919436
// channel is closed we just assume that it probably came from an on-chain claim.
93929437
channel_manager.claim_funds_internal(source, preimage, Some(downstream_value),
9393-
downstream_closed, downstream_funding);
9438+
downstream_closed, downstream_node_id, downstream_funding);
93949439
}
93959440

93969441
//TODO: Broadcast channel update for closed channels, but only after we've made a

0 commit comments

Comments
 (0)