Skip to content

Commit a6a2541

Browse files
committed
Allow holding ChannelMonitorUpdates until later, completing one
In the coming commits, we need to delay `ChannelMonitorUpdate`s until future actions (specifically `Event` handling). However, because we should only notify users once of a given `ChannelMonitorUpdate` and they must be provided in-order, we need to track which ones have or have not been given to users and, once updating resumes, fly the ones that haven't already made it to users. To do this we simply add a `bool` in the `ChannelMonitorUpdate` set stored in the `Channel` which indicates if an update flew and decline to provide new updates back to the `ChannelManager` if any updates have their flown bit unset. Further, because we'll now by releasing `ChannelMonitorUpdate`s which were already stored in the pending list, we now need to support getting a `Completed` result for a monitor which isn't the only pending monitor (or even out of order), thus we also rewrite the way monitor updates are marked completed.
1 parent 38143be commit a6a2541

File tree

4 files changed

+156
-62
lines changed

4 files changed

+156
-62
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn test_monitor_and_persister_update_fail() {
144144
let mut node_0_per_peer_lock;
145145
let mut node_0_peer_state_lock;
146146
let mut channel = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan.2);
147-
if let Ok(update) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
147+
if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
148148
// Check that even though the persister is returning a InProgress,
149149
// because the update is bogus, ultimately the error that's returned
150150
// should be a PermanentFailure.

lightning/src/ln/channel.rs

Lines changed: 137 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,17 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
479479
/// * `EXPIRE_PREV_CONFIG_TICKS` = convergence_delay / tick_interval
480480
pub(crate) const EXPIRE_PREV_CONFIG_TICKS: usize = 5;
481481

482+
struct PendingChannelMonitorUpdate {
483+
update: ChannelMonitorUpdate,
484+
/// In some cases we need to delay letting the [`ChannelMonitorUpdate`] fly until after an
485+
/// `Event` is processed by the user. This bool indicates the [`ChannelMonitorUpdate`] has
486+
/// flown and we're waiting to hear back, otherwise the update is waiting on some external
487+
/// event and the [`ChannelManager`] will update us when we're ready.
488+
///
489+
/// [`ChannelManager`]: super::channelmanager::ChannelManager
490+
flown: bool,
491+
}
492+
482493
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
483494
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
484495
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -744,7 +755,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
744755
/// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
745756
/// completes we still need to be able to complete the persistence. Thus, we have to keep a
746757
/// copy of the [`ChannelMonitorUpdate`] here until it is complete.
747-
pending_monitor_updates: Vec<ChannelMonitorUpdate>,
758+
pending_monitor_updates: Vec<PendingChannelMonitorUpdate>,
748759
}
749760

750761
#[cfg(any(test, fuzzing))]
@@ -1977,28 +1988,52 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
19771988
}
19781989

19791990
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 {
1991+
let fly_cs_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
19801992
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
1981-
UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, msg: Some(_) } => {
1982-
let mut additional_update = self.build_commitment_no_status_check(logger);
1983-
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
1984-
// strictly increasing by one, so decrement it here.
1985-
self.latest_monitor_update_id = monitor_update.update_id;
1986-
monitor_update.updates.append(&mut additional_update.updates);
1987-
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
1988-
self.pending_monitor_updates.push(monitor_update);
1993+
UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, msg } => {
1994+
// Even if we aren't supposed to let new monitor updates with commitment state
1995+
// updates fly, we still need to push the preimage ChannelMonitorUpdateStep no
1996+
// matter what. Sadly, to push a new monitor update which flies before others
1997+
// already queued, we have to insert it into the pending queue and update the
1998+
// update_ids of all the following monitors.
1999+
let flown_monitor_pos = if fly_cs_monitor && msg.is_some() {
2000+
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
2001+
// strictly increasing by one, so decrement it here.
2002+
let mut additional_update = self.build_commitment_no_status_check(logger);
2003+
self.latest_monitor_update_id = monitor_update.update_id;
2004+
monitor_update.updates.append(&mut additional_update.updates);
2005+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
2006+
update: monitor_update, flown: true,
2007+
});
2008+
self.pending_monitor_updates.len() - 1
2009+
} else {
2010+
let insert_pos = self.pending_monitor_updates.iter().position(|upd| !upd.flown)
2011+
.unwrap_or(self.pending_monitor_updates.len());
2012+
let new_mon_id = self.pending_monitor_updates.get(insert_pos)
2013+
.map(|upd| upd.update.update_id).unwrap_or(monitor_update.update_id);
2014+
monitor_update.update_id = new_mon_id;
2015+
self.pending_monitor_updates.insert(insert_pos, PendingChannelMonitorUpdate {
2016+
update: monitor_update, flown: true,
2017+
});
2018+
for held_update in self.pending_monitor_updates.iter_mut().skip(insert_pos + 1) {
2019+
held_update.update.update_id += 1;
2020+
}
2021+
if msg.is_some() {
2022+
debug_assert!(false, "If there is a pending unflown monitor we should have AwaitingMonitorUpdate set");
2023+
let update = self.build_commitment_no_status_check(logger);
2024+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
2025+
update, flown: false,
2026+
});
2027+
}
2028+
insert_pos
2029+
};
2030+
self.monitor_updating_paused(false, msg.is_some(), false, Vec::new(), Vec::new(), Vec::new());
19892031
UpdateFulfillCommitFetch::NewClaim {
1990-
monitor_update: self.pending_monitor_updates.last().unwrap(),
2032+
monitor_update: &self.pending_monitor_updates.get(flown_monitor_pos)
2033+
.expect("We just pushed the monitor update").update,
19912034
htlc_value_msat,
19922035
}
19932036
},
1994-
UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None } => {
1995-
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
1996-
self.pending_monitor_updates.push(monitor_update);
1997-
UpdateFulfillCommitFetch::NewClaim {
1998-
monitor_update: self.pending_monitor_updates.last().unwrap(),
1999-
htlc_value_msat,
2000-
}
2001-
}
20022037
UpdateFulfillFetch::DuplicateClaim {} => UpdateFulfillCommitFetch::DuplicateClaim {},
20032038
}
20042039
}
@@ -3066,7 +3101,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
30663101
Ok(())
30673102
}
30683103

3069-
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<&ChannelMonitorUpdate, ChannelError>
3104+
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<&ChannelMonitorUpdate>, ChannelError>
30703105
where L::Target: Logger
30713106
{
30723107
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -3242,8 +3277,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
32423277
}
32433278
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
32443279
log_bytes!(self.channel_id));
3245-
self.pending_monitor_updates.push(monitor_update);
3246-
return Ok(self.pending_monitor_updates.last().unwrap());
3280+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3281+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3282+
update: monitor_update, flown: fly_monitor
3283+
});
3284+
return Ok(if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None });
32473285
}
32483286

32493287
let need_commitment_signed = if need_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
@@ -3260,9 +3298,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
32603298

32613299
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack.",
32623300
log_bytes!(self.channel_id()), if need_commitment_signed { " our own commitment_signed and" } else { "" });
3263-
self.pending_monitor_updates.push(monitor_update);
3301+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3302+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3303+
update: monitor_update, flown: fly_monitor,
3304+
});
32643305
self.monitor_updating_paused(true, need_commitment_signed, false, Vec::new(), Vec::new(), Vec::new());
3265-
return Ok(self.pending_monitor_updates.last().unwrap());
3306+
return Ok(if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None });
32663307
}
32673308

32683309
/// Public version of the below, checking relevant preconditions first.
@@ -3377,8 +3418,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33773418
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len());
33783419

33793420
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
3380-
self.pending_monitor_updates.push(monitor_update);
3381-
(Some(self.pending_monitor_updates.last().unwrap()), htlcs_to_fail)
3421+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3422+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3423+
update: monitor_update, flown: fly_monitor,
3424+
});
3425+
(if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None },
3426+
htlcs_to_fail)
33823427
} else {
33833428
(None, Vec::new())
33843429
}
@@ -3389,7 +3434,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33893434
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
33903435
/// generating an appropriate error *after* the channel state has been updated based on the
33913436
/// revoke_and_ack message.
3392-
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, &ChannelMonitorUpdate), ChannelError>
3437+
pub fn revoke_and_ack<L: Deref>(&mut self, msg: &msgs::RevokeAndACK, logger: &L) -> Result<(Vec<(HTLCSource, PaymentHash)>, Option<&ChannelMonitorUpdate>), ChannelError>
33933438
where L::Target: Logger,
33943439
{
33953440
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
@@ -3586,21 +3631,29 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
35863631
self.monitor_pending_failures.append(&mut revoked_htlcs);
35873632
self.monitor_pending_finalized_fulfills.append(&mut finalized_claimed_htlcs);
35883633
log_debug!(logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply.", log_bytes!(self.channel_id()));
3589-
self.pending_monitor_updates.push(monitor_update);
3590-
return Ok((Vec::new(), self.pending_monitor_updates.last().unwrap()));
3634+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3635+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3636+
update: monitor_update, flown: fly_monitor,
3637+
});
3638+
return Ok((Vec::new(),
3639+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }));
35913640
}
35923641

35933642
match self.free_holding_cell_htlcs(logger) {
35943643
(Some(_), htlcs_to_fail) => {
3595-
let mut additional_update = self.pending_monitor_updates.pop().unwrap();
3644+
let mut additional_update = self.pending_monitor_updates.pop().unwrap().update;
35963645
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
35973646
// strictly increasing by one, so decrement it here.
35983647
self.latest_monitor_update_id = monitor_update.update_id;
35993648
monitor_update.updates.append(&mut additional_update.updates);
36003649

36013650
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3602-
self.pending_monitor_updates.push(monitor_update);
3603-
Ok((htlcs_to_fail, self.pending_monitor_updates.last().unwrap()))
3651+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3652+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3653+
update: monitor_update, flown: fly_monitor,
3654+
});
3655+
Ok((htlcs_to_fail,
3656+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
36043657
},
36053658
(None, htlcs_to_fail) => {
36063659
if require_commitment {
@@ -3614,13 +3667,21 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
36143667
log_debug!(logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed.",
36153668
log_bytes!(self.channel_id()), update_fail_htlcs.len() + update_fail_malformed_htlcs.len());
36163669
self.monitor_updating_paused(false, true, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3617-
self.pending_monitor_updates.push(monitor_update);
3618-
Ok((htlcs_to_fail, self.pending_monitor_updates.last().unwrap()))
3670+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3671+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3672+
update: monitor_update, flown: fly_monitor,
3673+
});
3674+
Ok((htlcs_to_fail,
3675+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
36193676
} else {
36203677
log_debug!(logger, "Received a valid revoke_and_ack for channel {} with no reply necessary.", log_bytes!(self.channel_id()));
36213678
self.monitor_updating_paused(false, false, false, to_forward_infos, revoked_htlcs, finalized_claimed_htlcs);
3622-
self.pending_monitor_updates.push(monitor_update);
3623-
Ok((htlcs_to_fail, self.pending_monitor_updates.last().unwrap()))
3679+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
3680+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
3681+
update: monitor_update, flown: fly_monitor,
3682+
});
3683+
Ok((htlcs_to_fail,
3684+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }))
36243685
}
36253686
}
36263687
}
@@ -3809,7 +3870,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
38093870
{
38103871
assert_eq!(self.channel_state & ChannelState::MonitorUpdateInProgress as u32, ChannelState::MonitorUpdateInProgress as u32);
38113872
self.channel_state &= !(ChannelState::MonitorUpdateInProgress as u32);
3812-
self.pending_monitor_updates.clear();
3873+
let mut found_unflown = false;
3874+
self.pending_monitor_updates.retain(|upd| {
3875+
if found_unflown { debug_assert!(!upd.flown, "No mons may fly after one is paused"); }
3876+
if !upd.flown { found_unflown = true; }
3877+
!upd.flown
3878+
});
38133879

38143880
// If we're past (or at) the FundingSent stage on an outbound channel, try to
38153881
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -4352,8 +4418,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
43524418
}],
43534419
};
43544420
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
4355-
self.pending_monitor_updates.push(monitor_update);
4356-
Some(self.pending_monitor_updates.last().unwrap())
4421+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
4422+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
4423+
update: monitor_update, flown: fly_monitor,
4424+
});
4425+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
43574426
} else { None };
43584427
let shutdown = if send_shutdown {
43594428
Some(msgs::Shutdown {
@@ -4925,8 +4994,25 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
49254994
(self.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0
49264995
}
49274996

4928-
pub fn get_next_monitor_update(&self) -> Option<&ChannelMonitorUpdate> {
4929-
self.pending_monitor_updates.first()
4997+
/// Returns the next unflown monitor update, if one exists, and a bool which indicates a
4998+
/// further unflown monitor update exists after the next.
4999+
pub fn fly_next_unflown_monitor_update(&mut self) -> Option<(&ChannelMonitorUpdate, bool)> {
5000+
for i in 0..self.pending_monitor_updates.len() {
5001+
if !self.pending_monitor_updates[i].flown {
5002+
self.pending_monitor_updates[i].flown = true;
5003+
return Some((&self.pending_monitor_updates[i].update,
5004+
self.pending_monitor_updates.len() > i + 1));
5005+
}
5006+
}
5007+
None
5008+
}
5009+
5010+
pub fn no_monitor_updates_pending(&self) -> bool {
5011+
self.pending_monitor_updates.is_empty()
5012+
}
5013+
5014+
pub fn complete_one_mon_update(&mut self, update_id: u64) {
5015+
self.pending_monitor_updates.retain(|upd| upd.update.update_id != update_id);
49305016
}
49315017

49325018
/// Returns true if funding_created was sent/received.
@@ -5974,8 +6060,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
59746060
Some(_) => {
59756061
let monitor_update = self.build_commitment_no_status_check(logger);
59766062
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
5977-
self.pending_monitor_updates.push(monitor_update);
5978-
Ok(Some(self.pending_monitor_updates.last().unwrap()))
6063+
6064+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
6065+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6066+
update: monitor_update, flown: fly_monitor,
6067+
});
6068+
Ok(if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None })
59796069
},
59806070
None => Ok(None)
59816071
}
@@ -6064,8 +6154,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
60646154
}],
60656155
};
60666156
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
6067-
self.pending_monitor_updates.push(monitor_update);
6068-
Some(self.pending_monitor_updates.last().unwrap())
6157+
let fly_monitor = self.pending_monitor_updates.iter().all(|upd| upd.flown);
6158+
self.pending_monitor_updates.push(PendingChannelMonitorUpdate {
6159+
update: monitor_update, flown: fly_monitor,
6160+
});
6161+
if fly_monitor { self.pending_monitor_updates.last().map(|upd| &upd.update) } else { None }
60696162
} else { None };
60706163
let shutdown = msgs::Shutdown {
60716164
channel_id: self.channel_id,

0 commit comments

Comments
 (0)