Skip to content

Commit 5f9e99b

Browse files
committed
Return owned ChannelMonitorUpdates from Channel
In the coming commits we'll move to storing in-flight `ChannelMonitorUpdate`s in the `ChannelManager` rather in the `Channel` (which will then only retain `ChannelMonitorUpdate`s which have not yet been released/are blocked. This will simplify handling of pending `ChannelMonitorUpdate` after a channel has closed by not having to move them into the `ChannelManager`.
1 parent c3c1050 commit 5f9e99b

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

lightning/src/ln/channel.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,13 @@ enum UpdateFulfillFetch {
485485
}
486486

487487
/// The return type of get_update_fulfill_htlc_and_commit.
488-
pub enum UpdateFulfillCommitFetch<'a> {
488+
pub enum UpdateFulfillCommitFetch {
489489
/// Indicates the HTLC fulfill is new, and either generated an update_fulfill message, placed
490490
/// it in the holding cell, or re-generated the update_fulfill message after the same claim was
491491
/// previously placed in the holding cell (and has since been removed).
492492
NewClaim {
493493
/// The ChannelMonitorUpdate which places the new payment preimage in the channel monitor
494-
monitor_update: &'a ChannelMonitorUpdate,
494+
monitor_update: ChannelMonitorUpdate,
495495
/// The value of the HTLC which was claimed, in msat.
496496
htlc_value_msat: u64,
497497
},
@@ -2302,8 +2302,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
23022302
};
23032303
self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
23042304
UpdateFulfillCommitFetch::NewClaim {
2305-
monitor_update: &self.context.pending_monitor_updates.get(unblocked_update_pos)
2306-
.expect("We just pushed the monitor update").update,
2305+
monitor_update: self.context.pending_monitor_updates.get(unblocked_update_pos)
2306+
.expect("We just pushed the monitor update").update.clone(),
23072307
htlc_value_msat,
23082308
}
23092309
},
@@ -2795,7 +2795,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
27952795
Ok(())
27962796
}
27972797

2798-
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<&ChannelMonitorUpdate>, ChannelError>
2798+
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
27992799
where L::Target: Logger
28002800
{
28012801
if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -3019,7 +3019,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
30193019
/// Public version of the below, checking relevant preconditions first.
30203020
/// If we're not in a state where freeing the holding cell makes sense, this is a no-op and
30213021
/// returns `(None, Vec::new())`.
3022-
pub fn maybe_free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
3022+
pub fn maybe_free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
30233023
if self.context.channel_state >= ChannelState::ChannelReady as u32 &&
30243024
(self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::PeerDisconnected as u32 | ChannelState::MonitorUpdateInProgress as u32)) == 0 {
30253025
self.free_holding_cell_htlcs(logger)
@@ -3028,7 +3028,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
30283028

30293029
/// Frees any pending commitment updates in the holding cell, generating the relevant messages
30303030
/// for our counterparty.
3031-
fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
3031+
fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
30323032
assert_eq!(self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32, 0);
30333033
if self.context.holding_cell_htlc_updates.len() != 0 || self.context.holding_cell_update_fee.is_some() {
30343034
log_trace!(logger, "Freeing holding cell with {} HTLC updates{} in channel {}", self.context.holding_cell_htlc_updates.len(),
@@ -3139,7 +3139,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31393139
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
31403140
/// generating an appropriate error *after* the channel state has been updated based on the
31413141
/// revoke_and_ack message.
3142-
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
3142+
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<ChannelMonitorUpdate>), ChannelError>
31433143
where L::Target: Logger,
31443144
{
31453145
if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -4066,7 +4066,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
40664066

40674067
pub fn shutdown<SP: Deref>(
40684068
&mut self, signer_provider: &SP, their_features: &InitFeatures, msg: &msgs::Shutdown
4069-
) -> Result<(Option<msgs::Shutdown>, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
4069+
) -> Result<(Option<msgs::Shutdown>, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
40704070
where SP::Target: SignerProvider
40714071
{
40724072
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
@@ -4132,9 +4132,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
41324132
}],
41334133
};
41344134
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
4135-
if self.push_blockable_mon_update(monitor_update) {
4136-
self.context.pending_monitor_updates.last().map(|upd| &upd.update)
4137-
} else { None }
4135+
self.push_ret_blockable_mon_update(monitor_update)
41384136
} else { None };
41394137
let shutdown = if send_shutdown {
41404138
Some(msgs::Shutdown {
@@ -4431,11 +4429,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44314429

44324430
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a
44334431
/// further blocked monitor update exists after the next.
4434-
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
4432+
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(ChannelMonitorUpdate, bool)> {
44354433
for i in 0..self.context.pending_monitor_updates.len() {
44364434
if self.context.pending_monitor_updates[i].blocked {
44374435
self.context.pending_monitor_updates[i].blocked = false;
4438-
return Some((&self.context.pending_monitor_updates[i].update,
4436+
return Some((self.context.pending_monitor_updates[i].update.clone(),
44394437
self.context.pending_monitor_updates.len() > i + 1));
44404438
}
44414439
}
@@ -4456,9 +4454,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44564454
/// it should be immediately given to the user for persisting or `None` if it should be held as
44574455
/// blocked.
44584456
fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
4459-
-> Option<&ChannelMonitorUpdate> {
4457+
-> Option<ChannelMonitorUpdate> {
44604458
let release_monitor = self.push_blockable_mon_update(update);
4461-
if release_monitor { self.context.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
4459+
if release_monitor { self.context.pending_monitor_updates.last().map(|upd| upd.update.clone()) } else { None }
44624460
}
44634461

44644462
pub fn no_monitor_updates_pending(&self) -> bool {
@@ -5283,7 +5281,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
52835281
///
52845282
/// Shorthand for calling [`Self::send_htlc`] followed by a commitment update, see docs on
52855283
/// [`Self::send_htlc`] and [`Self::build_commitment_no_state_update`] for more info.
5286-
pub fn send_htlc_and_commit<L: Deref>(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket, logger: &L) -> Result<Option<&ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
5284+
pub fn send_htlc_and_commit<L: Deref>(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
52875285
let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, false, logger);
52885286
if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
52895287
match send_res? {
@@ -5316,7 +5314,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53165314
/// [`ChannelMonitorUpdate`] will be returned).
53175315
pub fn get_shutdown<SP: Deref>(&mut self, signer_provider: &SP, their_features: &InitFeatures,
53185316
target_feerate_sats_per_kw: Option<u32>, override_shutdown_script: Option<ShutdownScript>)
5319-
-> Result<(msgs::Shutdown, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
5317+
-> Result<(msgs::Shutdown, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
53205318
where SP::Target: SignerProvider {
53215319
for htlc in self.context.pending_outbound_htlcs.iter() {
53225320
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
@@ -5387,9 +5385,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53875385
}],
53885386
};
53895387
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
5390-
if self.push_blockable_mon_update(monitor_update) {
5391-
self.context.pending_monitor_updates.last().map(|upd| &upd.update)
5392-
} else { None }
5388+
self.push_ret_blockable_mon_update(monitor_update)
53935389
} else { None };
53945390
let shutdown = msgs::Shutdown {
53955391
channel_id: self.context.channel_id,

lightning/src/ln/channelmanager.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ where
23052305
// Update the monitor with the shutdown script if necessary.
23062306
if let Some(monitor_update) = monitor_update_opt.take() {
23072307
let update_id = monitor_update.update_id;
2308-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
2308+
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
23092309
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
23102310
}
23112311

@@ -2988,7 +2988,7 @@ where
29882988
match break_chan_entry!(self, send_res, chan) {
29892989
Some(monitor_update) => {
29902990
let update_id = monitor_update.update_id;
2991-
let update_res = self.chain_monitor.update_channel(funding_txo, monitor_update);
2991+
let update_res = self.chain_monitor.update_channel(funding_txo, &monitor_update);
29922992
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan) {
29932993
break Err(e);
29942994
}
@@ -4614,7 +4614,7 @@ where
46144614
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
46154615
}
46164616
let update_id = monitor_update.update_id;
4617-
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, monitor_update);
4617+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &monitor_update);
46184618
let res = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
46194619
peer_state, per_peer_state, chan);
46204620
if let Err(e) = res {
@@ -5283,7 +5283,7 @@ where
52835283
// Update the monitor with the shutdown script if necessary.
52845284
if let Some(monitor_update) = monitor_update_opt {
52855285
let update_id = monitor_update.update_id;
5286-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
5286+
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
52875287
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
52885288
}
52895289
break Ok(());
@@ -5474,7 +5474,7 @@ where
54745474
let funding_txo = chan.get().context.get_funding_txo();
54755475
let monitor_update_opt = try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &self.logger), chan);
54765476
if let Some(monitor_update) = monitor_update_opt {
5477-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
5477+
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
54785478
let update_id = monitor_update.update_id;
54795479
handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
54805480
peer_state, per_peer_state, chan)
@@ -5613,7 +5613,7 @@ where
56135613
let funding_txo = chan.get().context.get_funding_txo();
56145614
let (htlcs_to_fail, monitor_update_opt) = try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), chan);
56155615
let res = if let Some(monitor_update) = monitor_update_opt {
5616-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
5616+
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
56175617
let update_id = monitor_update.update_id;
56185618
handle_new_monitor_update!(self, update_res, update_id,
56195619
peer_state_lock, peer_state, per_peer_state, chan)
@@ -5892,7 +5892,7 @@ where
58925892
has_monitor_update = true;
58935893

58945894
let update_res = self.chain_monitor.update_channel(
5895-
funding_txo.expect("channel is live"), monitor_update);
5895+
funding_txo.expect("channel is live"), &monitor_update);
58965896
let update_id = monitor_update.update_id;
58975897
let channel_id: [u8; 32] = *channel_id;
58985898
let res = handle_new_monitor_update!(self, update_res, update_id,
@@ -6237,7 +6237,7 @@ where
62376237
if let Some((monitor_update, further_update_exists)) = chan.get_mut().unblock_next_blocked_monitor_update() {
62386238
log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
62396239
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
6240-
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, monitor_update);
6240+
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, &monitor_update);
62416241
let update_id = monitor_update.update_id;
62426242
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id,
62436243
peer_state_lck, peer_state, per_peer_state, chan)

0 commit comments

Comments
 (0)