Skip to content

Commit 9d2affd

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 f1177dc commit 9d2affd

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
@@ -3250,7 +3250,7 @@ impl<Signer: Sign> Channel<Signer> {
32503250
return Ok((None, htlcs_to_fail));
32513251
}
32523252
let update_fee = if let Some(feerate) = self.holding_cell_update_fee.take() {
3253-
self.send_update_fee(feerate, logger)
3253+
self.send_update_fee(feerate, false, logger)
32543254
} else {
32553255
None
32563256
};
@@ -3550,12 +3550,20 @@ impl<Signer: Sign> Channel<Signer> {
35503550
}
35513551
}
35523552

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

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

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

5683-
/// Creates a signed commitment transaction to send to the remote peer.
5684-
/// Always returns a ChannelError::Close if an immediately-preceding (read: the
5685-
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
5686-
/// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
5687-
pub fn send_commitment<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
5688-
if (self.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
5689-
panic!("Cannot create commitment tx until channel is fully established");
5690-
}
5691-
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
5692-
panic!("Cannot create commitment tx until remote revokes their previous commitment");
5693-
}
5694-
if (self.channel_state & (ChannelState::PeerDisconnected as u32)) == (ChannelState::PeerDisconnected as u32) {
5695-
panic!("Cannot create commitment tx while disconnected, as send_htlc will have returned an Err so a send_commitment precondition has been violated");
5696-
}
5697-
if (self.channel_state & (ChannelState::MonitorUpdateInProgress as u32)) == (ChannelState::MonitorUpdateInProgress as u32) {
5698-
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");
5699-
}
5700-
let mut have_updates = self.is_outbound() && self.pending_update_fee.is_some();
5701-
for htlc in self.pending_outbound_htlcs.iter() {
5702-
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
5703-
have_updates = true;
5704-
}
5705-
if have_updates { break; }
5706-
}
5707-
for htlc in self.pending_inbound_htlcs.iter() {
5708-
if let InboundHTLCState::LocalRemoved(_) = htlc.state {
5709-
have_updates = true;
5710-
}
5711-
if have_updates { break; }
5712-
}
5713-
if !have_updates {
5714-
panic!("Cannot create commitment tx until we have some updates to send");
5715-
}
5716-
self.send_commitment_no_status_check(logger)
5717-
}
57185685
/// Only fails in case of bad keys
57195686
fn send_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> Result<(msgs::CommitmentSigned, ChannelMonitorUpdate), ChannelError> where L::Target: Logger {
57205687
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
@@ -3550,59 +3550,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35503550
self.process_background_events();
35513551
}
35523552

3553-
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>) {
3554-
if !chan.is_outbound() { return (true, NotifyOption::SkipPersist, Ok(())); }
3553+
fn update_channel_fee(&self, chan_id: &[u8; 32], chan: &mut Channel<<K::Target as KeysInterface>::Signer>, new_feerate: u32) -> NotifyOption {
3554+
if !chan.is_outbound() { return NotifyOption::SkipPersist; }
35553555
// If the feerate has decreased by less than half, don't bother
35563556
if new_feerate <= chan.get_feerate() && new_feerate * 2 > chan.get_feerate() {
35573557
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
35583558
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3559-
return (true, NotifyOption::SkipPersist, Ok(()));
3559+
return NotifyOption::SkipPersist;
35603560
}
35613561
if !chan.is_live() {
35623562
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).",
35633563
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
3564-
return (true, NotifyOption::SkipPersist, Ok(()));
3564+
return NotifyOption::SkipPersist;
35653565
}
35663566
log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
35673567
log_bytes!(chan_id[..]), chan.get_feerate(), new_feerate);
35683568

3569-
let mut retain_channel = true;
3570-
let res = match chan.send_update_fee_and_commit(new_feerate, &self.logger) {
3571-
Ok(res) => Ok(res),
3572-
Err(e) => {
3573-
let (drop, res) = convert_chan_err!(self, e, chan, chan_id);
3574-
if drop { retain_channel = false; }
3575-
Err(res)
3576-
}
3577-
};
3578-
let ret_err = match res {
3579-
Ok(Some((update_fee, commitment_signed, monitor_update))) => {
3580-
match self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
3581-
ChannelMonitorUpdateStatus::Completed => {
3582-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3583-
node_id: chan.get_counterparty_node_id(),
3584-
updates: msgs::CommitmentUpdate {
3585-
update_add_htlcs: Vec::new(),
3586-
update_fulfill_htlcs: Vec::new(),
3587-
update_fail_htlcs: Vec::new(),
3588-
update_fail_malformed_htlcs: Vec::new(),
3589-
update_fee: Some(update_fee),
3590-
commitment_signed,
3591-
},
3592-
});
3593-
Ok(())
3594-
},
3595-
e => {
3596-
let (res, drop) = handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, chan_id, COMMITMENT_UPDATE_ONLY);
3597-
if drop { retain_channel = false; }
3598-
res
3599-
}
3600-
}
3601-
},
3602-
Ok(None) => Ok(()),
3603-
Err(e) => Err(e),
3604-
};
3605-
(retain_channel, NotifyOption::DoPersist, ret_err)
3569+
chan.queue_update_fee(new_feerate, &self.logger);
3570+
NotifyOption::DoPersist
36063571
}
36073572

36083573
#[cfg(fuzzing)]
@@ -3616,19 +3581,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
36163581

36173582
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
36183583

3619-
let mut handle_errors = Vec::new();
3620-
{
3621-
let mut channel_state_lock = self.channel_state.lock().unwrap();
3622-
let channel_state = &mut *channel_state_lock;
3623-
let pending_msg_events = &mut channel_state.pending_msg_events;
3624-
channel_state.by_id.retain(|chan_id, chan| {
3625-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3626-
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3627-
if err.is_err() {
3628-
handle_errors.push(err);
3629-
}
3630-
retain_channel
3631-
});
3584+
let mut channel_state = self.channel_state.lock().unwrap();
3585+
for (chan_id, chan) in channel_state.by_id.iter_mut() {
3586+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
3587+
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
36323588
}
36333589

36343590
should_persist
@@ -3693,20 +3649,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
36933649

36943650
let new_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
36953651

3696-
let mut handle_errors = Vec::new();
3652+
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
36973653
let mut timed_out_mpp_htlcs = Vec::new();
36983654
{
36993655
let mut channel_state_lock = self.channel_state.lock().unwrap();
37003656
let channel_state = &mut *channel_state_lock;
37013657
let pending_msg_events = &mut channel_state.pending_msg_events;
37023658
channel_state.by_id.retain(|chan_id, chan| {
3703-
let counterparty_node_id = chan.get_counterparty_node_id();
3704-
let (retain_channel, chan_needs_persist, err) = self.update_channel_fee(pending_msg_events, chan_id, chan, new_feerate);
3659+
let chan_needs_persist = self.update_channel_fee(chan_id, chan, new_feerate);
37053660
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
3706-
if err.is_err() {
3707-
handle_errors.push((err, counterparty_node_id));
3708-
}
3709-
if !retain_channel { return false; }
37103661

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

0 commit comments

Comments
 (0)