Skip to content

Commit 9dfe42c

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 756b5aa commit 9dfe42c

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
@@ -489,6 +489,11 @@ struct PendingChannelMonitorUpdate {
489489
blocked: bool,
490490
}
491491

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

4992+
pub fn get_latest_complete_monitor_update_id(&self) -> u64 {
4993+
if self.pending_monitor_updates.is_empty() { return self.get_latest_monitor_update_id(); }
4994+
self.pending_monitor_updates[0].update.update_id - 1
4995+
}
4996+
49874997
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a
49884998
/// further blocked monitor update exists after the next.
49894999
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
@@ -6597,6 +6607,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65976607
(28, holder_max_accepted_htlcs, option),
65986608
(29, self.temporary_channel_id, option),
65996609
(31, channel_pending_event_emitted, option),
6610+
(33, self.pending_monitor_updates, vec_type),
66006611
});
66016612

66026613
Ok(())
@@ -6873,6 +6884,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68736884
let mut temporary_channel_id: Option<[u8; 32]> = None;
68746885
let mut holder_max_accepted_htlcs: Option<u16> = None;
68756886

6887+
let mut pending_monitor_updates = Some(Vec::new());
6888+
68766889
read_tlv_fields!(reader, {
68776890
(0, announcement_sigs, option),
68786891
(1, minimum_depth, option),
@@ -6895,6 +6908,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68956908
(28, holder_max_accepted_htlcs, option),
68966909
(29, temporary_channel_id, option),
68976910
(31, channel_pending_event_emitted, option),
6911+
(33, pending_monitor_updates, vec_type),
68986912
});
68996913

69006914
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -7064,7 +7078,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
70647078
channel_type: channel_type.unwrap(),
70657079
channel_keys_id,
70667080

7067-
pending_monitor_updates: Vec::new(),
7081+
pending_monitor_updates: pending_monitor_updates.unwrap(),
70687082
})
70697083
}
70707084
}

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7646,14 +7646,11 @@ where
76467646
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
76477647
funding_txo_set.insert(funding_txo.clone());
76487648
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
7649-
if channel.get_cur_holder_commitment_transaction_number() < monitor.get_cur_holder_commitment_number() ||
7650-
channel.get_revoked_counterparty_commitment_transaction_number() < monitor.get_min_seen_secret() ||
7651-
channel.get_cur_counterparty_commitment_transaction_number() < monitor.get_cur_counterparty_commitment_number() ||
7652-
channel.get_latest_monitor_update_id() > monitor.get_latest_update_id() {
7649+
if channel.get_latest_complete_monitor_update_id() > monitor.get_latest_update_id() {
76537650
// If the channel is ahead of the monitor, return InvalidValue:
76547651
log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
76557652
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
7656-
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_monitor_update_id());
7653+
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_complete_monitor_update_id());
76577654
log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
76587655
log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
76597656
log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");

0 commit comments

Comments
 (0)