Skip to content

Commit 91707fb

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 e7868d2 commit 91707fb

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ struct PendingChannelMonitorUpdate {
490490
flown: bool,
491491
}
492492

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

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

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

6812+
let mut pending_monitor_updates = Some(Vec::new());
6813+
68016814
read_tlv_fields!(reader, {
68026815
(0, announcement_sigs, option),
68036816
(1, minimum_depth, option),
@@ -6817,6 +6830,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68176830
(23, channel_ready_event_emitted, option),
68186831
(25, user_id_high_opt, option),
68196832
(27, channel_keys_id, option),
6833+
(29, pending_monitor_updates, vec_type),
68206834
});
68216835

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

6984-
pending_monitor_updates: Vec::new(),
6998+
pending_monitor_updates: pending_monitor_updates.unwrap(),
69856999
})
69867000
}
69877001
}

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 5 deletions
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)