Skip to content

Commit 1433e9e

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 15b1c9b commit 1433e9e

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

lightning/src/ln/channel.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,13 @@ enum UpdateFulfillFetch {
488488
}
489489

490490
/// The return type of get_update_fulfill_htlc_and_commit.
491-
pub enum UpdateFulfillCommitFetch<'a> {
491+
pub enum UpdateFulfillCommitFetch {
492492
/// Indicates the HTLC fulfill is new, and either generated an update_fulfill message, placed
493493
/// it in the holding cell, or re-generated the update_fulfill message after the same claim was
494494
/// previously placed in the holding cell (and has since been removed).
495495
NewClaim {
496496
/// The ChannelMonitorUpdate which places the new payment preimage in the channel monitor
497-
monitor_update: &'a ChannelMonitorUpdate,
497+
monitor_update: ChannelMonitorUpdate,
498498
/// The value of the HTLC which was claimed, in msat.
499499
htlc_value_msat: u64,
500500
},
@@ -2305,8 +2305,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
23052305
};
23062306
self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
23072307
UpdateFulfillCommitFetch::NewClaim {
2308-
monitor_update: &self.context.pending_monitor_updates.get(unblocked_update_pos)
2309-
.expect("We just pushed the monitor update").update,
2308+
monitor_update: self.context.pending_monitor_updates.get(unblocked_update_pos)
2309+
.expect("We just pushed the monitor update").update.clone(),
23102310
htlc_value_msat,
23112311
}
23122312
},
@@ -2798,7 +2798,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
27982798
Ok(())
27992799
}
28002800

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

30323032
/// Frees any pending commitment updates in the holding cell, generating the relevant messages
30333033
/// for our counterparty.
3034-
fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
3034+
fn free_holding_cell_htlcs<L: Deref>(&mut self, logger: &L) -> (Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>) where L::Target: Logger {
30353035
assert_eq!(self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32, 0);
30363036
if self.context.holding_cell_htlc_updates.len() != 0 || self.context.holding_cell_update_fee.is_some() {
30373037
log_trace!(logger, "Freeing holding cell with {} HTLC updates{} in channel {}", self.context.holding_cell_htlc_updates.len(),
@@ -3147,7 +3147,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31473147
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
31483148
/// generating an appropriate error *after* the channel state has been updated based on the
31493149
/// revoke_and_ack message.
3150-
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
3150+
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<ChannelMonitorUpdate>), ChannelError>
31513151
where L::Target: Logger,
31523152
{
31533153
if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -4075,7 +4075,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
40754075

40764076
pub fn shutdown<SP: Deref>(
40774077
&mut self, signer_provider: &SP, their_features: &InitFeatures, msg: &msgs::Shutdown
4078-
) -> Result<(Option<msgs::Shutdown>, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
4078+
) -> Result<(Option<msgs::Shutdown>, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), ChannelError>
40794079
where SP::Target: SignerProvider
40804080
{
40814081
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
@@ -4141,9 +4141,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
41414141
}],
41424142
};
41434143
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
4144-
if self.push_blockable_mon_update(monitor_update) {
4145-
self.context.pending_monitor_updates.last().map(|upd| &upd.update)
4146-
} else { None }
4144+
self.push_ret_blockable_mon_update(monitor_update)
41474145
} else { None };
41484146
let shutdown = if send_shutdown {
41494147
Some(msgs::Shutdown {
@@ -4440,11 +4438,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44404438

44414439
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a
44424440
/// further blocked monitor update exists after the next.
4443-
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
4441+
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(ChannelMonitorUpdate, bool)> {
44444442
for i in 0..self.context.pending_monitor_updates.len() {
44454443
if self.context.pending_monitor_updates[i].blocked {
44464444
self.context.pending_monitor_updates[i].blocked = false;
4447-
return Some((&self.context.pending_monitor_updates[i].update,
4445+
return Some((self.context.pending_monitor_updates[i].update.clone(),
44484446
self.context.pending_monitor_updates.len() > i + 1));
44494447
}
44504448
}
@@ -4465,9 +4463,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44654463
/// it should be immediately given to the user for persisting or `None` if it should be held as
44664464
/// blocked.
44674465
fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
4468-
-> Option<&ChannelMonitorUpdate> {
4466+
-> Option<ChannelMonitorUpdate> {
44694467
let release_monitor = self.push_blockable_mon_update(update);
4470-
if release_monitor { self.context.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
4468+
if release_monitor { self.context.pending_monitor_updates.last().map(|upd| upd.update.clone()) } else { None }
44714469
}
44724470

44734471
pub fn no_monitor_updates_pending(&self) -> bool {
@@ -5302,7 +5300,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53025300
pub fn send_htlc_and_commit<L: Deref>(
53035301
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
53045302
onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>, logger: &L
5305-
) -> Result<Option<&ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
5303+
) -> Result<Option<ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
53065304
let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source,
53075305
onion_routing_packet, false, skimmed_fee_msat, logger);
53085306
if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
@@ -5336,7 +5334,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53365334
/// [`ChannelMonitorUpdate`] will be returned).
53375335
pub fn get_shutdown<SP: Deref>(&mut self, signer_provider: &SP, their_features: &InitFeatures,
53385336
target_feerate_sats_per_kw: Option<u32>, override_shutdown_script: Option<ShutdownScript>)
5339-
-> Result<(msgs::Shutdown, Option<&ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
5337+
-> Result<(msgs::Shutdown, Option<ChannelMonitorUpdate>, Vec<(HTLCSource, PaymentHash)>), APIError>
53405338
where SP::Target: SignerProvider {
53415339
for htlc in self.context.pending_outbound_htlcs.iter() {
53425340
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
@@ -5407,9 +5405,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
54075405
}],
54085406
};
54095407
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
5410-
if self.push_blockable_mon_update(monitor_update) {
5411-
self.context.pending_monitor_updates.last().map(|upd| &upd.update)
5412-
} else { None }
5408+
self.push_ret_blockable_mon_update(monitor_update)
54135409
} else { None };
54145410
let shutdown = msgs::Shutdown {
54155411
channel_id: self.context.channel_id,

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ where
23102310
// Update the monitor with the shutdown script if necessary.
23112311
if let Some(monitor_update) = monitor_update_opt.take() {
23122312
let update_id = monitor_update.update_id;
2313-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
2313+
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
23142314
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
23152315
}
23162316

@@ -3036,7 +3036,7 @@ where
30363036
match break_chan_entry!(self, send_res, chan) {
30373037
Some(monitor_update) => {
30383038
let update_id = monitor_update.update_id;
3039-
let update_res = self.chain_monitor.update_channel(funding_txo, monitor_update);
3039+
let update_res = self.chain_monitor.update_channel(funding_txo, &monitor_update);
30403040
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan) {
30413041
break Err(e);
30423042
}
@@ -4678,7 +4678,7 @@ where
46784678
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
46794679
}
46804680
let update_id = monitor_update.update_id;
4681-
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, monitor_update);
4681+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &monitor_update);
46824682
let res = handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
46834683
peer_state, per_peer_state, chan);
46844684
if let Err(e) = res {
@@ -5347,7 +5347,7 @@ where
53475347
// Update the monitor with the shutdown script if necessary.
53485348
if let Some(monitor_update) = monitor_update_opt {
53495349
let update_id = monitor_update.update_id;
5350-
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), monitor_update);
5350+
let update_res = self.chain_monitor.update_channel(funding_txo_opt.unwrap(), &monitor_update);
53515351
break handle_new_monitor_update!(self, update_res, update_id, peer_state_lock, peer_state, per_peer_state, chan_entry);
53525352
}
53535353
break Ok(());
@@ -5544,7 +5544,7 @@ where
55445544
let funding_txo = chan.get().context.get_funding_txo();
55455545
let monitor_update_opt = try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &self.logger), chan);
55465546
if let Some(monitor_update) = monitor_update_opt {
5547-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
5547+
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
55485548
let update_id = monitor_update.update_id;
55495549
handle_new_monitor_update!(self, update_res, update_id, peer_state_lock,
55505550
peer_state, per_peer_state, chan)
@@ -5683,7 +5683,7 @@ where
56835683
let funding_txo = chan.get().context.get_funding_txo();
56845684
let (htlcs_to_fail, monitor_update_opt) = try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.logger), chan);
56855685
let res = if let Some(monitor_update) = monitor_update_opt {
5686-
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), monitor_update);
5686+
let update_res = self.chain_monitor.update_channel(funding_txo.unwrap(), &monitor_update);
56875687
let update_id = monitor_update.update_id;
56885688
handle_new_monitor_update!(self, update_res, update_id,
56895689
peer_state_lock, peer_state, per_peer_state, chan)
@@ -5962,7 +5962,7 @@ where
59625962
has_monitor_update = true;
59635963

59645964
let update_res = self.chain_monitor.update_channel(
5965-
funding_txo.expect("channel is live"), monitor_update);
5965+
funding_txo.expect("channel is live"), &monitor_update);
59665966
let update_id = monitor_update.update_id;
59675967
let channel_id: [u8; 32] = *channel_id;
59685968
let res = handle_new_monitor_update!(self, update_res, update_id,
@@ -6307,7 +6307,7 @@ where
63076307
if let Some((monitor_update, further_update_exists)) = chan.get_mut().unblock_next_blocked_monitor_update() {
63086308
log_debug!(self.logger, "Unlocking monitor updating for channel {} and updating monitor",
63096309
log_bytes!(&channel_funding_outpoint.to_channel_id()[..]));
6310-
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, monitor_update);
6310+
let update_res = self.chain_monitor.update_channel(channel_funding_outpoint, &monitor_update);
63116311
let update_id = monitor_update.update_id;
63126312
if let Err(e) = handle_new_monitor_update!(self, update_res, update_id,
63136313
peer_state_lck, peer_state, per_peer_state, chan)

0 commit comments

Comments
 (0)