Skip to content

Commit 92fd015

Browse files
committed
Store + process pending ChannelMonitorUpdates in Channel
The previous commits set up the ability for us to hold `ChannelMonitorUpdate`s which are pending until we're ready to pass them to users and have them be applied. However, if the `ChannelManager` is persisted while we're waiting to give the user a `ChannelMonitorUpdate` we'll be confused on restart - seeing our latest `ChannelMonitor` state as stale compared to our `ChannelManager` - a critical error. Luckily the solution is trivial, we simply need to store the pending `ChannelMonitorUpdate` state and load it with the `ChannelManager` data, allowing stale monitors on load as long as we have the missing pending updates between where we are and the latest `ChannelMonitor` state.
1 parent ab21fbe commit 92fd015

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lightning/src/ln/channel.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,11 @@ struct PendingChannelMonitorUpdate {
491491
flown: bool,
492492
}
493493

494+
impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
495+
(0, update, required),
496+
(2, flown, required),
497+
});
498+
494499
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
495500
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
496501
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -4940,6 +4945,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
49404945
(self.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0
49414946
}
49424947

4948+
pub fn get_latest_complete_monitor_update_id(&self) -> u64 {
4949+
if self.pending_monitor_updates.is_empty() { return self.get_latest_monitor_update_id(); }
4950+
self.pending_monitor_updates[0].update.update_id - 1
4951+
}
4952+
49434953
/// Returns the next unflown monitor update, if one exists, and a bool which indicates a
49444954
/// further unflown monitor update exists after the next.
49454955
pub fn fly_next_unflown_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
@@ -6527,6 +6537,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65276537
(23, channel_ready_event_emitted, option),
65286538
(25, user_id_high_opt, option),
65296539
(27, self.channel_keys_id, required),
6540+
(29, self.pending_monitor_updates, vec_type),
65306541
});
65316542

65326543
Ok(())
@@ -6799,6 +6810,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67996810
let mut user_id_high_opt: Option<u64> = None;
68006811
let mut channel_keys_id: Option<[u8; 32]> = None;
68016812

6813+
let mut pending_monitor_updates = Some(Vec::new());
6814+
68026815
read_tlv_fields!(reader, {
68036816
(0, announcement_sigs, option),
68046817
(1, minimum_depth, option),
@@ -6818,6 +6831,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68186831
(23, channel_ready_event_emitted, option),
68196832
(25, user_id_high_opt, option),
68206833
(27, channel_keys_id, option),
6834+
(29, pending_monitor_updates, vec_type),
68216835
});
68226836

68236837
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -6982,7 +6996,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
69826996
channel_type: channel_type.unwrap(),
69836997
channel_keys_id,
69846998

6985-
pending_monitor_updates: Vec::new(),
6999+
pending_monitor_updates: pending_monitor_updates.unwrap(),
69867000
})
69877001
}
69887002
}

lightning/src/ln/channelmanager.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -7442,14 +7442,11 @@ where
74427442
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
74437443
funding_txo_set.insert(funding_txo.clone());
74447444
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
7445-
if channel.get_cur_holder_commitment_transaction_number() < monitor.get_cur_holder_commitment_number() ||
7446-
channel.get_revoked_counterparty_commitment_transaction_number() < monitor.get_min_seen_secret() ||
7447-
channel.get_cur_counterparty_commitment_transaction_number() < monitor.get_cur_counterparty_commitment_number() ||
7448-
channel.get_latest_monitor_update_id() > monitor.get_latest_update_id() {
7445+
if channel.get_latest_complete_monitor_update_id() > monitor.get_latest_update_id() {
74497446
// If the channel is ahead of the monitor, return InvalidValue:
74507447
log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
74517448
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
7452-
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_monitor_update_id());
7449+
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_complete_monitor_update_id());
74537450
log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
74547451
log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
74557452
log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");

0 commit comments

Comments
 (0)