Skip to content

Commit 2ee043c

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 dbda41e commit 2ee043c

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
@@ -3248,7 +3248,7 @@ impl<Signer: Sign> Channel<Signer> {
32483248
return Ok((None, htlcs_to_fail));
32493249
}
32503250
let update_fee = if let Some(feerate) = self.holding_cell_update_fee.take() {
3251-
self.send_update_fee(feerate, logger)
3251+
self.send_update_fee(feerate, false, logger)
32523252
} else {
32533253
None
32543254
};
@@ -3548,12 +3548,20 @@ impl<Signer: Sign> Channel<Signer> {
35483548
}
35493549
}
35503550

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

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

3606-
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 {
3607-
match self.send_update_fee(feerate_per_kw, logger) {
3608-
Some(update_fee) => {
3609-
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check(logger)?;
3610-
Ok(Some((update_fee, commitment_signed, monitor_update)))
3611-
},
3612-
None => Ok(None)
3613-
}
3614-
}
3615-
36163618
/// Removes any uncommitted inbound HTLCs and resets the state of uncommitted outbound HTLC
36173619
/// updates, to be used on peer disconnection. After this, update_*_htlc messages need to be
36183620
/// resent.
@@ -5639,41 +5641,6 @@ impl<Signer: Sign> Channel<Signer> {
56395641
Ok(Some(res))
56405642
}
56415643

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

3432-
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>) {
3433-
if !chan.is_outbound() { return (true, NotifyOption::SkipPersist, Ok(())); }
3432+
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<K::Target as KeysInterface>::Signer>, new_feerate: u32) -> NotifyOption {
3433+
if !chan.is_outbound() { return NotifyOption::SkipPersist; }
34343434
// If the feerate has decreased by less than half, don't bother
34353435
if new_feerate <= chan.get_feerate() && new_feerate * 2 > chan.get_feerate() {
34363436
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
34373437
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3438-
return (true, NotifyOption::SkipPersist, Ok(()));
3438+
return NotifyOption::SkipPersist;
34393439
}
34403440
if !chan.is_live() {
34413441
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).",
34423442
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3443-
return (true, NotifyOption::SkipPersist, Ok(()));
3443+
return NotifyOption::SkipPersist;
34443444
}
34453445
log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
34463446
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
34473447

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

34873452
#[cfg(fuzzing)]
@@ -3495,19 +3460,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34953460

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

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

35133469
should_persist
@@ -3572,20 +3528,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35723528

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

3575-
let mut handle_errors = Vec::new();
3531+
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
35763532
let mut timed_out_mpp_htlcs = Vec::new();
35773533
{
35783534
let mut channel_state_lock = self.channel_state.lock().unwrap();
35793535
let channel_state = &mut *channel_state_lock;
35803536
let pending_msg_events = &mut channel_state.pending_msg_events;
35813537
channel_state.by_id.retain(|chan_id, chan| {
3582-
let counterparty_node_id = chan.get_counterparty_node_id();
3583-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3538+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
35843539
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3585-
if err.is_err() {
3586-
handle_errors.push((err, counterparty_node_id));
3587-
}
3588-
if !retain_channel { return false; }
35893540

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

0 commit comments

Comments
 (0)