Skip to content

Commit 704f3db

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 81c12e4 commit 704f3db

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

492+
impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
493+
(0, update, required),
494+
(2, flown, 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
@@ -4993,6 +4998,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
49934998
(self.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0
49944999
}
49955000

5001+
pub fn get_latest_complete_monitor_update_id(&self) -> u64 {
5002+
if self.pending_monitor_updates.is_empty() { return self.get_latest_monitor_update_id(); }
5003+
self.pending_monitor_updates[0].update.update_id - 1
5004+
}
5005+
49965006
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a
49975007
/// further blocked monitor update exists after the next.
49985008
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
@@ -6591,6 +6601,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65916601
(27, self.channel_keys_id, required),
65926602
(29, self.temporary_channel_id, option),
65936603
(31, channel_pending_event_emitted, option),
6604+
(33, self.pending_monitor_updates, vec_type),
65946605
});
65956606

65966607
Ok(())
@@ -6865,6 +6876,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68656876
let mut channel_keys_id: Option<[u8; 32]> = None;
68666877
let mut temporary_channel_id: Option<[u8; 32]> = None;
68676878

6879+
let mut pending_monitor_updates = Some(Vec::new());
6880+
68686881
read_tlv_fields!(reader, {
68696882
(0, announcement_sigs, option),
68706883
(1, minimum_depth, option),
@@ -6886,6 +6899,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68866899
(27, channel_keys_id, option),
68876900
(29, temporary_channel_id, option),
68886901
(31, channel_pending_event_emitted, option),
6902+
(33, pending_monitor_updates, vec_type),
68896903
});
68906904

68916905
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -7052,7 +7066,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
70527066
channel_type: channel_type.unwrap(),
70537067
channel_keys_id,
70547068

7055-
pending_monitor_updates: Vec::new(),
7069+
pending_monitor_updates: pending_monitor_updates.unwrap(),
70567070
})
70577071
}
70587072
}

lightning/src/ln/channelmanager.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -7560,14 +7560,11 @@ where
75607560
let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
75617561
funding_txo_set.insert(funding_txo.clone());
75627562
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
7563-
if channel.get_cur_holder_commitment_transaction_number() < monitor.get_cur_holder_commitment_number() ||
7564-
channel.get_revoked_counterparty_commitment_transaction_number() < monitor.get_min_seen_secret() ||
7565-
channel.get_cur_counterparty_commitment_transaction_number() < monitor.get_cur_counterparty_commitment_number() ||
7566-
channel.get_latest_monitor_update_id() > monitor.get_latest_update_id() {
7563+
if channel.get_latest_complete_monitor_update_id() > monitor.get_latest_update_id() {
75677564
// If the channel is ahead of the monitor, return InvalidValue:
75687565
log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
75697566
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} but the ChannelManager is at update_id {}.",
7570-
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_monitor_update_id());
7567+
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_complete_monitor_update_id());
75717568
log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
75727569
log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
75737570
log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");

0 commit comments

Comments
 (0)