Skip to content

Commit 926af38

Browse files
committed
Add a BackgroundEvent to replay non-closing monitor updates
`BackgroundEvent` is used to store `ChannelMonitorUpdate`s which result in a channel force-close, avoiding relying on `ChannelMonitor`s having been loaded while `ChannelManager` block-connection methods are called during startup. In the coming commit(s) we'll also generate non-channel-closing `ChannelMonitorUpdate`s during startup, which will need to be replayed prior to any other `ChannelMonitorUpdate`s generated from normal operation. In the next commit we'll handle that by handling `BackgroundEvent`s immediately after locking the `total_consistency_lock`. The new `BackgroundEvent` variant is restricted to only `ChannelMonitorUpdate`s which will be regenerated on the next restart, as they're not persisted to disk.
1 parent 049eba4 commit 926af38

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ enum BackgroundEvent {
486486
/// Handle a ChannelMonitorUpdate that closes a channel, broadcasting its current latest holder
487487
/// commitment transaction.
488488
ClosingMonitorUpdate((OutPoint, ChannelMonitorUpdate)),
489+
/// Handle a ChannelMonitorUpdate that does not close a channel, potentially unblocking the
490+
/// channel to continue normal operation.
491+
///
492+
/// Note that any such events are lost on shutdown, so in general they must be updates which
493+
/// are regenerated on startup.
494+
MonitorUpdateRegeneratedOnStartup((PublicKey, OutPoint, ChannelMonitorUpdate)),
489495
}
490496

491497
#[derive(Debug)]
@@ -3685,6 +3691,36 @@ where
36853691
// monitor updating completing.
36863692
let _ = self.chain_monitor.update_channel(funding_txo, &update);
36873693
},
3694+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup((counterparty_id, funding_txo, update)) => {
3695+
// The channel has already been closed, so no use bothering to care about the
3696+
// monitor updating completing.
3697+
let update_res = self.chain_monitor.update_channel(funding_txo, &update);
3698+
3699+
let mut found_chan = false;
3700+
{
3701+
let per_peer_state = self.per_peer_state.read().unwrap();
3702+
let res = if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_id) {
3703+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
3704+
let peer_state = &mut *peer_state_lock;
3705+
match peer_state.channel_by_id.entry(funding_txo.to_channel_id()) {
3706+
hash_map::Entry::Occupied(mut chan) => {
3707+
found_chan = true;
3708+
handle_new_monitor_update!(self, update_res, update.update_id, peer_state_lock, peer_state, per_peer_state, chan)
3709+
},
3710+
hash_map::Entry::Vacant(_) => {
3711+
Ok(())
3712+
},
3713+
}
3714+
} else { Ok(()) };
3715+
let _ = handle_error!(self, res, counterparty_id);
3716+
}
3717+
if !found_chan {
3718+
// TODO: If this channel has since closed, we're likely providing a payment
3719+
// preimage update, which we must ensure is durable! We currently don't,
3720+
// however, ensure that, and when we have a strategy therefor we should
3721+
// apply it here.
3722+
}
3723+
},
36883724
}
36893725
}
36903726
true
@@ -7290,14 +7326,18 @@ where
72907326
}
72917327

72927328
let background_events = self.pending_background_events.lock().unwrap();
7293-
(background_events.len() as u64).write(writer)?;
7329+
(background_events.iter()
7330+
.filter(|ev| if let BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) = ev { false } else { true })
7331+
.count() as u64)
7332+
.write(writer)?;
72947333
for event in background_events.iter() {
72957334
match event {
72967335
BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
72977336
0u8.write(writer)?;
72987337
funding_txo.write(writer)?;
72997338
monitor_update.write(writer)?;
73007339
},
7340+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup(_) => { },
73017341
}
73027342
}
73037343

@@ -7731,8 +7771,9 @@ where
77317771
0 => {
77327772
let (funding_txo, monitor_update): (OutPoint, ChannelMonitorUpdate) = (Readable::read(reader)?, Readable::read(reader)?);
77337773
if pending_background_events.iter().find(|e| {
7734-
let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e;
7735-
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7774+
if let BackgroundEvent::ClosingMonitorUpdate((pending_funding_txo, pending_monitor_update)) = e {
7775+
*pending_funding_txo == funding_txo && *pending_monitor_update == monitor_update
7776+
} else { false }
77367777
}).is_none() {
77377778
pending_background_events.push(BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)));
77387779
}

0 commit comments

Comments
 (0)