Skip to content

Commit f52ed23

Browse files
committed
Move in-flight ChannelMonitorUpdates to ChannelManager
Because `ChannelMonitorUpdate`s can be generated for a channel which is already closed, and must still be tracked through their completion, storing them in a `Channel` doesn't make sense - we'd have to have a redundant place to put them post-closure and handle both storage locations equivalently. Instead, here, we move to storing in-flight `ChannelMonitorUpdate`s to the `ChannelManager`, leaving blocked `ChannelMonitorUpdate`s in the `Channel` as they were.
1 parent 76cec92 commit f52ed23

File tree

3 files changed

+171
-111
lines changed

3 files changed

+171
-111
lines changed

lightning/src/ln/channel.rs

+27-60
Original file line numberDiff line numberDiff line change
@@ -2261,50 +2261,38 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
22612261
}
22622262

22632263
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> UpdateFulfillCommitFetch where L::Target: Logger {
2264-
let release_cs_monitor = self.context.pending_monitor_updates.iter().all(|upd| !upd.blocked);
2264+
let release_cs_monitor = self.context.pending_monitor_updates.is_empty();
22652265
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
22662266
UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, msg } => {
22672267
// Even if we aren't supposed to let new monitor updates with commitment state
22682268
// updates run, we still need to push the preimage ChannelMonitorUpdateStep no
22692269
// matter what. Sadly, to push a new monitor update which flies before others
22702270
// already queued, we have to insert it into the pending queue and update the
22712271
// update_ids of all the following monitors.
2272-
let unblocked_update_pos = if release_cs_monitor && msg.is_some() {
2272+
if release_cs_monitor && msg.is_some() {
22732273
let mut additional_update = self.build_commitment_no_status_check(logger);
22742274
// build_commitment_no_status_check may bump latest_monitor_id but we want them
22752275
// to be strictly increasing by one, so decrement it here.
22762276
self.context.latest_monitor_update_id = monitor_update.update_id;
22772277
monitor_update.updates.append(&mut additional_update.updates);
2278-
self.context.pending_monitor_updates.push(PendingChannelMonitorUpdate {
2279-
update: monitor_update, blocked: false,
2280-
});
2281-
self.context.pending_monitor_updates.len() - 1
22822278
} else {
2283-
let insert_pos = self.context.pending_monitor_updates.iter().position(|upd| upd.blocked)
2284-
.unwrap_or(self.context.pending_monitor_updates.len());
2285-
let new_mon_id = self.context.pending_monitor_updates.get(insert_pos)
2279+
let new_mon_id = self.context.pending_monitor_updates.get(0)
22862280
.map(|upd| upd.update.update_id).unwrap_or(monitor_update.update_id);
22872281
monitor_update.update_id = new_mon_id;
2288-
self.context.pending_monitor_updates.insert(insert_pos, PendingChannelMonitorUpdate {
2289-
update: monitor_update, blocked: false,
2290-
});
2291-
for held_update in self.context.pending_monitor_updates.iter_mut().skip(insert_pos + 1) {
2282+
for held_update in self.context.pending_monitor_updates.iter_mut() {
22922283
held_update.update.update_id += 1;
22932284
}
22942285
if msg.is_some() {
2295-
debug_assert!(false, "If there is a pending blocked monitor we should have MonitorUpdateInProgress set");
22962286
let update = self.build_commitment_no_status_check(logger);
22972287
self.context.pending_monitor_updates.push(PendingChannelMonitorUpdate {
22982288
update, blocked: true,
22992289
});
23002290
}
2301-
insert_pos
2302-
};
2291+
}
2292+
23032293
self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
23042294
UpdateFulfillCommitFetch::NewClaim {
2305-
monitor_update: self.context.pending_monitor_updates.get(unblocked_update_pos)
2306-
.expect("We just pushed the monitor update").update.clone(),
2307-
htlc_value_msat,
2295+
monitor_update, htlc_value_msat,
23082296
}
23092297
},
23102298
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
@@ -3341,8 +3329,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33413329
}
33423330

33433331
match self.free_holding_cell_htlcs(logger) {
3344-
(Some(_), htlcs_to_fail) => {
3345-
let mut additional_update = self.context.pending_monitor_updates.pop().unwrap().update;
3332+
(Some(mut additional_update), htlcs_to_fail) => {
33463333
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
33473334
// strictly increasing by one, so decrement it here.
33483335
self.context.latest_monitor_update_id = monitor_update.update_id;
@@ -3558,12 +3545,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
35583545
{
35593546
assert_eq!(self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32, ChannelState::MonitorUpdateInProgress as u32);
35603547
self.context.channel_state &= !(ChannelState::MonitorUpdateInProgress as u32);
3561-
let mut found_blocked = false;
3562-
self.context.pending_monitor_updates.retain(|upd| {
3563-
if found_blocked { debug_assert!(upd.blocked, "No mons may be unblocked after a blocked one"); }
3564-
if upd.blocked { found_blocked = true; }
3565-
upd.blocked
3566-
});
3548+
for upd in self.context.pending_monitor_updates.iter() {
3549+
debug_assert!(upd.blocked);
3550+
}
35673551

35683552
// If we're past (or at) the FundingSent stage on an outbound channel, try to
35693553
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4430,48 +4414,31 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
44304414
/// Returns the next blocked monitor update, if one exists, and a bool which indicates a
44314415
/// further blocked monitor update exists after the next.
44324416
pub fn unblock_next_blocked_monitor_update(&mut self) -> Option<(ChannelMonitorUpdate, bool)> {
4433-
for i in 0..self.context.pending_monitor_updates.len() {
4434-
if self.context.pending_monitor_updates[i].blocked {
4435-
self.context.pending_monitor_updates[i].blocked = false;
4436-
return Some((self.context.pending_monitor_updates[i].update.clone(),
4437-
self.context.pending_monitor_updates.len() > i + 1));
4438-
}
4417+
for upd in self.context.pending_monitor_updates.iter() {
4418+
debug_assert!(upd.blocked);
44394419
}
4440-
None
4420+
if self.context.pending_monitor_updates.is_empty() { return None; }
4421+
Some((self.context.pending_monitor_updates.remove(0).update,
4422+
!self.context.pending_monitor_updates.is_empty()))
44414423
}
44424424

44434425
/// Pushes a new monitor update into our monitor update queue, returning it if it should be
44444426
/// immediately given to the user for persisting or `None` if it should be held as blocked.
44454427
fn push_ret_blockable_mon_update(&mut self, update: ChannelMonitorUpdate)
44464428
-> Option<ChannelMonitorUpdate> {
4447-
let release_monitor = self.context.pending_monitor_updates.iter().all(|upd| !upd.blocked);
4448-
self.context.pending_monitor_updates.push(PendingChannelMonitorUpdate {
4449-
update, blocked: !release_monitor,
4450-
});
4451-
if release_monitor { self.context.pending_monitor_updates.last().map(|upd| upd.update.clone()) } else { None }
4452-
}
4453-
4454-
pub fn no_monitor_updates_pending(&self) -> bool {
4455-
self.context.pending_monitor_updates.is_empty()
4456-
}
4457-
4458-
pub fn complete_all_mon_updates_through(&mut self, update_id: u64) {
4459-
self.context.pending_monitor_updates.retain(|upd| {
4460-
if upd.update.update_id <= update_id {
4461-
assert!(!upd.blocked, "Completed update must have flown");
4462-
false
4463-
} else { true }
4464-
});
4465-
}
4466-
4467-
pub fn complete_one_mon_update(&mut self, update_id: u64) {
4468-
self.context.pending_monitor_updates.retain(|upd| upd.update.update_id != update_id);
4429+
let release_monitor = self.context.pending_monitor_updates.is_empty();
4430+
if !release_monitor {
4431+
self.context.pending_monitor_updates.push(PendingChannelMonitorUpdate {
4432+
update, blocked: true,
4433+
});
4434+
None
4435+
} else {
4436+
Some(update)
4437+
}
44694438
}
44704439

4471-
/// Returns an iterator over all unblocked monitor updates which have not yet completed.
4472-
pub fn uncompleted_unblocked_mon_updates(&self) -> impl Iterator<Item=&ChannelMonitorUpdate> {
4473-
self.context.pending_monitor_updates.iter()
4474-
.filter_map(|upd| if upd.blocked { None } else { Some(&upd.update) })
4440+
pub fn blocked_monitor_updates_pending(&self) -> usize {
4441+
self.context.pending_monitor_updates.len()
44754442
}
44764443

44774444
/// Returns true if the channel is awaiting the persistence of the initial ChannelMonitor.

0 commit comments

Comments
 (0)