Skip to content

Commit c40504a

Browse files
committed
Drop completed blocked ChannelMonitorUpdates on startup
If a user receives a payment preimage for an outbound payment, the `PaymentSent` event will block any eventual RAA `ChannelMonitorUpdate` from the same channel, assuming it comes in before the event can be processed. If this blocking kicks in, but the flow eventually completes with the RAA `ChannelMonitorUpdate` being persisted, but the `ChannelManager` is only persisted prior to the event being handled, on startup we'll have a fully up-to-date `ChannelMonitor` but a pending, blocked `ChannelMonitorUpdate`. When the `PaymentSent` event is replayed we'll end up trying to apply a redundant `ChannelMonitorUpdate` which will panic. See the test added in this commit for an implementation of this situation. In this commit we fix this issue by simply dropping blocked `ChannelMonitorUpdate`s the same as we do pending ones.
1 parent 4bab9c8 commit c40504a

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5179,6 +5179,26 @@ impl<SP: Deref> Channel<SP> where
51795179
}
51805180
}
51815181

5182+
/// On startup, its possible we detect some monitor updates have actually completed (and the
5183+
/// ChannelManager was simply stale). In that case, we should simply drop them, which we do
5184+
/// here after logging them.
5185+
pub fn on_startup_drop_completed_blocked_mon_updates_through<L: Logger>(&mut self, logger: &L, loaded_mon_update_id: u64) {
5186+
let channel_id = self.context.channel_id();
5187+
self.context.blocked_monitor_updates.retain(|update| {
5188+
if update.update.update_id <= loaded_mon_update_id {
5189+
log_info!(
5190+
logger,
5191+
"Dropping completed ChannelMonitorUpdate id {} on channel {} due to a stale ChannelManager",
5192+
update.update.update_id,
5193+
channel_id,
5194+
);
5195+
false
5196+
} else {
5197+
true
5198+
}
5199+
});
5200+
}
5201+
51825202
pub fn blocked_monitor_updates_pending(&self) -> usize {
51835203
self.context.blocked_monitor_updates.len()
51845204
}

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10360,9 +10360,10 @@ where
1036010360
}
1036110361
}
1036210362
} else {
10363-
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {}",
10363+
channel.on_startup_drop_completed_blocked_mon_updates_through(&logger, monitor.get_latest_update_id());
10364+
log_info!(logger, "Successfully loaded channel {} at update_id {} against monitor at update id {} with {} blocked updates",
1036410365
&channel.context.channel_id(), channel.context.get_latest_monitor_update_id(),
10365-
monitor.get_latest_update_id());
10366+
monitor.get_latest_update_id(), channel.blocked_monitor_updates_pending());
1036610367
if let Some(short_channel_id) = channel.context.get_short_channel_id() {
1036710368
short_to_chan_info.insert(short_channel_id, (channel.context.get_counterparty_node_id(), channel.context.channel_id()));
1036810369
}

lightning/src/ln/monitor_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,3 +2821,40 @@ fn test_monitor_claims_with_random_signatures() {
28212821
do_test_monitor_claims_with_random_signatures(true, false);
28222822
do_test_monitor_claims_with_random_signatures(true, true);
28232823
}
2824+
2825+
#[test]
2826+
fn test_event_replay_causing_monitor_replay() {
2827+
// In LDK 0.0.121 there was a bug where if a `PaymentSent` event caused an RAA
2828+
// `ChannelMonitorUpdate` hold and then the node was restarted after the `PaymentSent` event
2829+
// and `ChannelMonitorUpdate` both completed but without persisting the `ChannelManager` we'd
2830+
// replay the `ChannelMonitorUpdate` on restart (which is fine, but triggered a safety panic).
2831+
let chanmon_cfgs = create_chanmon_cfgs(2);
2832+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2833+
let persister;
2834+
let new_chain_monitor;
2835+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2836+
let node_deserialized;
2837+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2838+
2839+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
2840+
2841+
let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1_000_000).0;
2842+
2843+
do_claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, payment_preimage);
2844+
2845+
// At this point the `PaymentSent` event has not been processed but the full commitment signed
2846+
// dance has completed.
2847+
let serialized_channel_manager = nodes[0].node.encode();
2848+
2849+
// Now process the `PaymentSent` to get the final RAA `ChannelMonitorUpdate`, checking that it
2850+
// resulted in a `ChannelManager` persistence request.
2851+
nodes[0].node.get_and_clear_needs_persistence();
2852+
expect_payment_sent(&nodes[0], payment_preimage, None, true, true /* expected post-event monitor update*/);
2853+
assert!(nodes[0].node.get_and_clear_needs_persistence());
2854+
2855+
let serialized_monitor = get_monitor!(nodes[0], chan.2).encode();
2856+
reload_node!(nodes[0], &serialized_channel_manager, &[&serialized_monitor], persister, new_chain_monitor, node_deserialized);
2857+
2858+
// Expect the `PaymentSent` to get replayed, this time without the duplicate monitor update
2859+
expect_payment_sent(&nodes[0], payment_preimage, None, false, false /* expected post-event monitor update*/);
2860+
}

0 commit comments

Comments
 (0)