Skip to content

Commit 94a4b8e

Browse files
committed
Lean on the holding cell for commitments when updating fees
Like the previous commit, here we update the update_fee+commit logic to simply push the fee update into the holding cell and then use the standard holding-cell-freeing codepaths to actually send the commitment update. This removes a substantial amount of code, reducing redundant codepaths and keeping channel state machine logic in channel.rs.
1 parent b872684 commit 94a4b8e

File tree

2 files changed

+26
-108
lines changed

2 files changed

+26
-108
lines changed

lightning/src/ln/channel.rs

+14-47
Original file line numberDiff line numberDiff line change
@@ -3249,7 +3249,7 @@ impl<Signer: Sign> Channel<Signer> {
32493249
return Ok((None, htlcs_to_fail));
32503250
}
32513251
let update_fee = if let Some(feerate) = self.holding_cell_update_fee.take() {
3252-
self.send_update_fee(feerate, logger)
3252+
self.send_update_fee(feerate, false, logger)
32533253
} else {
32543254
None
32553255
};
@@ -3549,12 +3549,20 @@ impl<Signer: Sign> Channel<Signer> {
35493549
}
35503550
}
35513551

3552+
/// Queues up an outbound update fee by placing it in the holding cell. You should call
3553+
/// `maybe_free_holding_cell_htlcs` in order to actually generate and send the commitment
3554+
/// update.
3555+
pub fn queue_update_fee<L: Deref>(&mut self, feerate_per_kw: u32, logger: &L) where L::Target: Logger {
3556+
let msg_opt = self.send_update_fee(feerate_per_kw, true, logger);
3557+
assert!(msg_opt.is_none(), "We forced holding cell?");
3558+
}
3559+
35523560
/// Adds a pending update to this channel. See the doc for send_htlc for
35533561
/// further details on the optionness of the return value.
35543562
/// If our balance is too low to cover the cost of the next commitment transaction at the
35553563
/// new feerate, the update is cancelled.
35563564
/// You MUST call send_commitment prior to any other calls on this Channel
3557-
fn send_update_fee<L: Deref>(&mut self, feerate_per_kw: u32, logger: &L) -> Option<msgs::UpdateFee> where L::Target: Logger {
3565+
fn send_update_fee<L: Deref>(&mut self, feerate_per_kw: u32, mut force_holding_cell: bool, logger: &L) -> Option<msgs::UpdateFee> where L::Target: Logger {
35583566
if !self.is_outbound() {
35593567
panic!("Cannot send fee from inbound channel");
35603568
}
@@ -3591,6 +3599,10 @@ impl<Signer: Sign> Channel<Signer> {
35913599
}
35923600

35933601
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
3602+
force_holding_cell = true;
3603+
}
3604+
3605+
if force_holding_cell {
35943606
self.holding_cell_update_fee = Some(feerate_per_kw);
35953607
return None;
35963608
}
@@ -3604,16 +3616,6 @@ impl<Signer: Sign> Channel<Signer> {
36043616
})
36053617
}
36063618

3607-
pub fn send_update_fee_and_commit<L: Deref>(&mut self, feerate_per_kw: u32, logger: &L) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitorUpdate)>, ChannelError> where L::Target: Logger {
3608-
match self.send_update_fee(feerate_per_kw, logger) {
3609-
Some(update_fee) => {
3610-
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check(logger)?;
3611-
Ok(Some((update_fee, commitment_signed, monitor_update)))
3612-
},
3613-
None => Ok(None)
3614-
}
3615-
}
3616-
36173619
/// Removes any uncommitted inbound HTLCs and resets the state of uncommitted outbound HTLC
36183620
/// updates, to be used on peer disconnection. After this, update_*_htlc messages need to be
36193621
/// resent.
@@ -5641,41 +5643,6 @@ impl<Signer: Sign> Channel<Signer> {
56415643
Ok(Some(res))
56425644
}
56435645

5644-
/// Creates a signed commitment transaction to send to the remote peer.
5645-
/// Always returns a ChannelError::Close if an immediately-preceding (read: the
5646-
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
5647-
/// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
5648-
pub fn send_commitment<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
5649-
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
5650-
panic!("Cannot create commitment tx until channel is fully established");
5651-
}
5652-
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
5653-
panic!("Cannot create commitment tx until remote revokes their previous commitment");
5654-
}
5655-
if (self.channel_state & (ChannelState::PeerDisconnected as u32)) == (ChannelState::PeerDisconnected as u32) {
5656-
panic!("Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
5657-
}
5658-
if (self.channel_state & (ChannelState::MonitorUpdateInProgress as u32)) == (ChannelState::MonitorUpdateInProgress as u32) {
5659-
panic!("Cannot create commitment tx while awaiting monitor update unfreeze, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
5660-
}
5661-
let mut have_updates = self.is_outbound() && self.pending_update_fee.is_some();
5662-
for htlc in self.pending_outbound_htlcs.iter() {
5663-
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
5664-
have_updates = true;
5665-
}
5666-
if have_updates { break; }
5667-
}
5668-
for htlc in self.pending_inbound_htlcs.iter() {
5669-
if let InboundHTLCState::LocalRemoved(_) = htlc.state {
5670-
have_updates = true;
5671-
}
5672-
if have_updates { break; }
5673-
}
5674-
if !have_updates {
5675-
panic!("Cannot create commitment tx until we have some updates to send");
5676-
}
5677-
self.send_commitment_no_status_check(logger)
5678-
}
56795646
/// Only fails in case of bad keys
56805647
fn send_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
56815648
log_trace!(logger, "Updating HTLC state for a newly-sent commitment_signed...");

lightning/src/ln/channelmanager.rs

+12-61
Original file line numberDiff line numberDiff line change
@@ -3428,59 +3428,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34283428
self.process_background_events();
34293429
}
34303430

3431-
fn update_channel_fee(&self, pending_msg_events: &mut Vec<events::MessageSendEvent>, chan_id: &[u8; 32], chan: &mut Channel<<K::Target as KeysInterface>::Signer>, new_feerate: u32) -> (bool, NotifyOption, Result<(), MsgHandleErrInternal>) {
3432-
if !chan.is_outbound() { return (true, NotifyOption::SkipPersist, Ok(())); }
3431+
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<K::Target as KeysInterface>::Signer>, new_feerate: u32) -> NotifyOption {
3432+
if !chan.is_outbound() { return NotifyOption::SkipPersist; }
34333433
// If the feerate has decreased by less than half, don't bother
34343434
if new_feerate <= chan.get_feerate() && new_feerate * 2 > chan.get_feerate() {
34353435
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
34363436
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3437-
return (true, NotifyOption::SkipPersist, Ok(()));
3437+
return NotifyOption::SkipPersist;
34383438
}
34393439
if !chan.is_live() {
34403440
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {} as it cannot currently be updated (probably the peer is disconnected).",
34413441
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3442-
return (true, NotifyOption::SkipPersist, Ok(()));
3442+
return NotifyOption::SkipPersist;
34433443
}
34443444
log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
34453445
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
34463446

3447-
let mut retain_channel = true;
3448-
let res = match chan.send_update_fee_and_commit(new_feerate, &self.logger) {
3449-
Ok(res) => Ok(res),
3450-
Err(e) => {
3451-
let (drop, res) = convert_chan_err!(self, e, chan, chan_id);
3452-
if drop { retain_channel = false; }
3453-
Err(res)
3454-
}
3455-
};
3456-
let ret_err = match res {
3457-
Ok(Some((update_fee, commitment_signed, monitor_update))) => {
3458-
match self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
3459-
ChannelMonitorUpdateStatus::Completed => {
3460-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3461-
node_id: chan.get_counterparty_node_id(),
3462-
updates: msgs::CommitmentUpdate {
3463-
update_add_htlcs: Vec::new(),
3464-
update_fulfill_htlcs: Vec::new(),
3465-
update_fail_htlcs: Vec::new(),
3466-
update_fail_malformed_htlcs: Vec::new(),
3467-
update_fee: Some(update_fee),
3468-
commitment_signed,
3469-
},
3470-
});
3471-
Ok(())
3472-
},
3473-
e => {
3474-
let (res, drop) = handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, chan_id, COMMITMENT_UPDATE_ONLY);
3475-
if drop { retain_channel = false; }
3476-
res
3477-
}
3478-
}
3479-
},
3480-
Ok(None) => Ok(()),
3481-
Err(e) => Err(e),
3482-
};
3483-
(retain_channel, NotifyOption::DoPersist, ret_err)
3447+
chan.queue_update_fee(new_feerate, &self.logger);
3448+
NotifyOption::DoPersist
34843449
}
34853450

34863451
#[cfg(fuzzing)]
@@ -3494,19 +3459,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34943459

34953460
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
34963461

3497-
let mut handle_errors = Vec::new();
3498-
{
3499-
let mut channel_state_lock = self.channel_state.lock().unwrap();
3500-
let channel_state = &mut *channel_state_lock;
3501-
let pending_msg_events = &mut channel_state.pending_msg_events;
3502-
channel_state.by_id.retain(|chan_id, chan| {
3503-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3504-
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3505-
if err.is_err() {
3506-
handle_errors.push(err);
3507-
}
3508-
retain_channel
3509-
});
3462+
let mut channel_state = self.channel_state.lock().unwrap();
3463+
for (chan_id, chan) in channel_state.by_id.iter_mut() {
3464+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
3465+
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
35103466
}
35113467

35123468
should_persist
@@ -3571,20 +3527,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35713527

35723528
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
35733529

3574-
let mut handle_errors = Vec::new();
3530+
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
35753531
let mut timed_out_mpp_htlcs = Vec::new();
35763532
{
35773533
let mut channel_state_lock = self.channel_state.lock().unwrap();
35783534
let channel_state = &mut *channel_state_lock;
35793535
let pending_msg_events = &mut channel_state.pending_msg_events;
35803536
channel_state.by_id.retain(|chan_id, chan| {
3581-
let counterparty_node_id = chan.get_counterparty_node_id();
3582-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3537+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
35833538
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3584-
if err.is_err() {
3585-
handle_errors.push((err, counterparty_node_id));
3586-
}
3587-
if !retain_channel { return false; }
35883539

35893540
if let Err(e) = chan.timer_check_closing_negotiation_progress() {
35903541
let (needs_close, err) = convert_chan_err!(self, e, chan, chan_id);

0 commit comments

Comments
 (0)