Skip to content

Commit 4248cbb

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 b22b936 commit 4248cbb

File tree

2 files changed

+26
-108
lines changed

2 files changed

+26
-108
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,7 +3251,7 @@ impl<Signer: Sign> Channel<Signer> {
32513251
return Ok((None, htlcs_to_fail));
32523252
}
32533253
let update_fee = if let Some(feerate) = self.holding_cell_update_fee.take() {
3254-
self.send_update_fee(feerate, logger)
3254+
self.send_update_fee(feerate, false, logger)
32553255
} else {
32563256
None
32573257
};
@@ -3551,12 +3551,20 @@ impl<Signer: Sign> Channel<Signer> {
35513551
}
35523552
}
35533553

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

35953603
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32 | ChannelState::MonitorUpdateInProgress as u32)) != 0 {
3604+
force_holding_cell = true;
3605+
}
3606+
3607+
if force_holding_cell {
35963608
self.holding_cell_update_fee = Some(feerate_per_kw);
35973609
return None;
35983610
}
@@ -3606,16 +3618,6 @@ impl<Signer: Sign> Channel<Signer> {
36063618
})
36073619
}
36083620

3609-
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 {
3610-
match self.send_update_fee(feerate_per_kw, logger) {
3611-
Some(update_fee) => {
3612-
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check(logger)?;
3613-
Ok(Some((update_fee, commitment_signed, monitor_update)))
3614-
},
3615-
None => Ok(None)
3616-
}
3617-
}
3618-
36193621
/// Removes any uncommitted inbound HTLCs and resets the state of uncommitted outbound HTLC
36203622
/// updates, to be used on peer disconnection. After this, update_*_htlc messages need to be
36213623
/// resent.
@@ -5653,41 +5655,6 @@ impl<Signer: Sign> Channel<Signer> {
56535655
Ok(Some(res))
56545656
}
56555657

5656-
/// Creates a signed commitment transaction to send to the remote peer.
5657-
/// Always returns a ChannelError::Close if an immediately-preceding (read: the
5658-
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
5659-
/// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
5660-
pub fn send_commitment<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
5661-
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
5662-
panic!("Cannot create commitment tx until channel is fully established");
5663-
}
5664-
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
5665-
panic!("Cannot create commitment tx until remote revokes their previous commitment");
5666-
}
5667-
if (self.channel_state & (ChannelState::PeerDisconnected as u32)) == (ChannelState::PeerDisconnected as u32) {
5668-
panic!("Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
5669-
}
5670-
if (self.channel_state & (ChannelState::MonitorUpdateInProgress as u32)) == (ChannelState::MonitorUpdateInProgress as u32) {
5671-
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");
5672-
}
5673-
let mut have_updates = self.is_outbound() && self.pending_update_fee.is_some();
5674-
for htlc in self.pending_outbound_htlcs.iter() {
5675-
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
5676-
have_updates = true;
5677-
}
5678-
if have_updates { break; }
5679-
}
5680-
for htlc in self.pending_inbound_htlcs.iter() {
5681-
if let InboundHTLCState::LocalRemoved(_) = htlc.state {
5682-
have_updates = true;
5683-
}
5684-
if have_updates { break; }
5685-
}
5686-
if !have_updates {
5687-
panic!("Cannot create commitment tx until we have some updates to send");
5688-
}
5689-
self.send_commitment_no_status_check(logger)
5690-
}
56915658
/// Only fails in case of bad keys
56925659
fn send_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
56935660
log_trace!(logger, "Updating HTLC state for a newly-sent commitment_signed...");

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3449,59 +3449,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34493449
self.process_background_events();
34503450
}
34513451

3452-
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>) {
3453-
if !chan.is_outbound() { return (true, NotifyOption::SkipPersist, Ok(())); }
3452+
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<K::Target as KeysInterface>::Signer>, new_feerate: u32) -> NotifyOption {
3453+
if !chan.is_outbound() { return NotifyOption::SkipPersist; }
34543454
// If the feerate has decreased by less than half, don't bother
34553455
if new_feerate <= chan.get_feerate() && new_feerate * 2 > chan.get_feerate() {
34563456
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
34573457
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3458-
return (true, NotifyOption::SkipPersist, Ok(()));
3458+
return NotifyOption::SkipPersist;
34593459
}
34603460
if !chan.is_live() {
34613461
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).",
34623462
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3463-
return (true, NotifyOption::SkipPersist, Ok(()));
3463+
return NotifyOption::SkipPersist;
34643464
}
34653465
log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
34663466
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
34673467

3468-
let mut retain_channel = true;
3469-
let res = match chan.send_update_fee_and_commit(new_feerate, &self.logger) {
3470-
Ok(res) => Ok(res),
3471-
Err(e) => {
3472-
let (drop, res) = convert_chan_err!(self, e, chan, chan_id);
3473-
if drop { retain_channel = false; }
3474-
Err(res)
3475-
}
3476-
};
3477-
let ret_err = match res {
3478-
Ok(Some((update_fee, commitment_signed, monitor_update))) => {
3479-
match self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
3480-
ChannelMonitorUpdateStatus::Completed => {
3481-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3482-
node_id: chan.get_counterparty_node_id(),
3483-
updates: msgs::CommitmentUpdate {
3484-
update_add_htlcs: Vec::new(),
3485-
update_fulfill_htlcs: Vec::new(),
3486-
update_fail_htlcs: Vec::new(),
3487-
update_fail_malformed_htlcs: Vec::new(),
3488-
update_fee: Some(update_fee),
3489-
commitment_signed,
3490-
},
3491-
});
3492-
Ok(())
3493-
},
3494-
e => {
3495-
let (res, drop) = handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, chan_id, COMMITMENT_UPDATE_ONLY);
3496-
if drop { retain_channel = false; }
3497-
res
3498-
}
3499-
}
3500-
},
3501-
Ok(None) => Ok(()),
3502-
Err(e) => Err(e),
3503-
};
3504-
(retain_channel, NotifyOption::DoPersist, ret_err)
3468+
chan.queue_update_fee(new_feerate, &self.logger);
3469+
NotifyOption::DoPersist
35053470
}
35063471

35073472
#[cfg(fuzzing)]
@@ -3515,19 +3480,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35153480

35163481
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
35173482

3518-
let mut handle_errors = Vec::new();
3519-
{
3520-
let mut channel_state_lock = self.channel_state.lock().unwrap();
3521-
let channel_state = &mut *channel_state_lock;
3522-
let pending_msg_events = &mut channel_state.pending_msg_events;
3523-
channel_state.by_id.retain(|chan_id, chan| {
3524-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3525-
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3526-
if err.is_err() {
3527-
handle_errors.push(err);
3528-
}
3529-
retain_channel
3530-
});
3483+
let mut channel_state = self.channel_state.lock().unwrap();
3484+
for (chan_id, chan) in channel_state.by_id.iter_mut() {
3485+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
3486+
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
35313487
}
35323488

35333489
should_persist
@@ -3592,20 +3548,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35923548

35933549
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
35943550

3595-
let mut handle_errors = Vec::new();
3551+
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
35963552
let mut timed_out_mpp_htlcs = Vec::new();
35973553
{
35983554
let mut channel_state_lock = self.channel_state.lock().unwrap();
35993555
let channel_state = &mut *channel_state_lock;
36003556
let pending_msg_events = &mut channel_state.pending_msg_events;
36013557
channel_state.by_id.retain(|chan_id, chan| {
3602-
let counterparty_node_id = chan.get_counterparty_node_id();
3603-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3558+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
36043559
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3605-
if err.is_err() {
3606-
handle_errors.push((err, counterparty_node_id));
3607-
}
3608-
if !retain_channel { return false; }
36093560

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

0 commit comments

Comments
 (0)