Skip to content

Commit 6b951d6

Browse files
authored
Merge pull request #3946 from joostjager/simplify-send-htlc
Simplify send htlc
2 parents 20e51fe + 5e3af3b commit 6b951d6

File tree

1 file changed

+96
-66
lines changed

1 file changed

+96
-66
lines changed

lightning/src/ln/channel.rs

Lines changed: 96 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7245,12 +7245,12 @@ where
72457245
fee_estimator,
72467246
logger,
72477247
) {
7248-
Ok(update_add_msg_opt) => {
7249-
// `send_htlc` only returns `Ok(None)`, when an update goes into
7248+
Ok(can_add_htlc) => {
7249+
// `send_htlc` only returns `Ok(false)`, when an update goes into
72507250
// the holding cell, but since we're currently freeing it, we should
7251-
// always expect to see the `update_add` go out.
7251+
// always expect to see the htlc added.
72527252
debug_assert!(
7253-
update_add_msg_opt.is_some(),
7253+
can_add_htlc,
72547254
"Must generate new update if we're freeing the holding cell"
72557255
);
72567256
update_add_count += 1;
@@ -10581,34 +10581,43 @@ where
1058110581
))
1058210582
}
1058310583

10584-
// Send stuff to our remote peers:
10585-
1058610584
/// Queues up an outbound HTLC to send by placing it in the holding cell. You should call
1058710585
/// [`Self::maybe_free_holding_cell_htlcs`] in order to actually generate and send the
1058810586
/// commitment update.
10589-
#[rustfmt::skip]
1059010587
pub fn queue_add_htlc<F: Deref, L: Deref>(
10591-
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
10592-
onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
10593-
blinding_point: Option<PublicKey>, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
10588+
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32,
10589+
source: HTLCSource, onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
10590+
blinding_point: Option<PublicKey>, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1059410591
) -> Result<(), (LocalHTLCFailureReason, String)>
10595-
where F::Target: FeeEstimator, L::Target: Logger
10592+
where
10593+
F::Target: FeeEstimator,
10594+
L::Target: Logger,
1059610595
{
10597-
self
10598-
.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true,
10599-
skimmed_fee_msat, blinding_point, fee_estimator, logger)
10600-
.map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
10601-
.map_err(|err| {
10602-
debug_assert!(err.0.is_temporary(), "Queuing HTLC should return temporary error");
10603-
err
10604-
})
10596+
self.send_htlc(
10597+
amount_msat,
10598+
payment_hash,
10599+
cltv_expiry,
10600+
source,
10601+
onion_routing_packet,
10602+
true,
10603+
skimmed_fee_msat,
10604+
blinding_point,
10605+
fee_estimator,
10606+
logger,
10607+
)
10608+
.map(|can_add_htlc| assert!(!can_add_htlc, "We forced holding cell?"))
10609+
.map_err(|err| {
10610+
debug_assert!(err.0.is_temporary(), "Queuing HTLC should return temporary error");
10611+
err
10612+
})
1060510613
}
1060610614

1060710615
/// Adds a pending outbound HTLC to this channel, note that you probably want
1060810616
/// [`Self::send_htlc_and_commit`] instead cause you'll want both messages at once.
1060910617
///
10610-
/// This returns an optional UpdateAddHTLC as we may be in a state where we cannot add HTLCs on
10611-
/// the wire:
10618+
/// This returns a boolean indicating whether we are in a state where we can add HTLCs on the wire.
10619+
/// Reasons we may not be able to add HTLCs on the wire include:
10620+
///
1061210621
/// * In cases where we're waiting on the remote peer to send us a revoke_and_ack, we
1061310622
/// wouldn't be able to determine what they actually ACK'ed if we have two sets of updates
1061410623
/// awaiting ACK.
@@ -10620,18 +10629,19 @@ where
1062010629
/// on this [`FundedChannel`] if `force_holding_cell` is false.
1062110630
///
1062210631
/// `Err`'s will always be temporary channel failures.
10623-
#[rustfmt::skip]
1062410632
fn send_htlc<F: Deref, L: Deref>(
10625-
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
10626-
onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool,
10633+
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32,
10634+
source: HTLCSource, onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool,
1062710635
skimmed_fee_msat: Option<u64>, blinding_point: Option<PublicKey>,
10628-
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
10629-
) -> Result<Option<msgs::UpdateAddHTLC>, (LocalHTLCFailureReason, String)>
10630-
where F::Target: FeeEstimator, L::Target: Logger
10636+
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
10637+
) -> Result<bool, (LocalHTLCFailureReason, String)>
10638+
where
10639+
F::Target: FeeEstimator,
10640+
L::Target: Logger,
1063110641
{
10632-
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
10633-
self.context.channel_state.is_local_shutdown_sent() ||
10634-
self.context.channel_state.is_remote_shutdown_sent()
10642+
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_))
10643+
|| self.context.channel_state.is_local_shutdown_sent()
10644+
|| self.context.channel_state.is_remote_shutdown_sent()
1063510645
{
1063610646
return Err((LocalHTLCFailureReason::ChannelNotReady,
1063710647
"Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned()));
@@ -10643,13 +10653,23 @@ where
1064310653

1064410654
let available_balances = self.get_available_balances(fee_estimator);
1064510655
if amount_msat < available_balances.next_outbound_htlc_minimum_msat {
10646-
return Err((LocalHTLCFailureReason::HTLCMinimum, format!("Cannot send less than our next-HTLC minimum - {} msat",
10647-
available_balances.next_outbound_htlc_minimum_msat)));
10656+
return Err((
10657+
LocalHTLCFailureReason::HTLCMinimum,
10658+
format!(
10659+
"Cannot send less than our next-HTLC minimum - {} msat",
10660+
available_balances.next_outbound_htlc_minimum_msat
10661+
),
10662+
));
1064810663
}
1064910664

1065010665
if amount_msat > available_balances.next_outbound_htlc_limit_msat {
10651-
return Err((LocalHTLCFailureReason::HTLCMaximum, format!("Cannot send more than our next-HTLC maximum - {} msat",
10652-
available_balances.next_outbound_htlc_limit_msat)));
10666+
return Err((
10667+
LocalHTLCFailureReason::HTLCMaximum,
10668+
format!(
10669+
"Cannot send more than our next-HTLC maximum - {} msat",
10670+
available_balances.next_outbound_htlc_limit_msat
10671+
),
10672+
));
1065310673
}
1065410674

1065510675
if self.context.channel_state.is_peer_disconnected() {
@@ -10659,16 +10679,26 @@ where
1065910679
// disconnected during the time the previous hop was doing the commitment dance we may
1066010680
// end up getting here after the forwarding delay. In any case, returning an
1066110681
// IgnoreError will get ChannelManager to do the right thing and fail backwards now.
10662-
return Err((LocalHTLCFailureReason::PeerOffline,
10663-
"Cannot send an HTLC while disconnected from channel counterparty".to_owned()));
10682+
return Err((
10683+
LocalHTLCFailureReason::PeerOffline,
10684+
"Cannot send an HTLC while disconnected from channel counterparty".to_owned(),
10685+
));
1066410686
}
1066510687

1066610688
let need_holding_cell = !self.context.channel_state.can_generate_new_commitment();
10667-
log_debug!(logger, "Pushing new outbound HTLC with hash {} for {} msat {}",
10668-
payment_hash, amount_msat,
10669-
if force_holding_cell { "into holding cell" }
10670-
else if need_holding_cell { "into holding cell as we're awaiting an RAA or monitor" }
10671-
else { "to peer" });
10689+
log_debug!(
10690+
logger,
10691+
"Pushing new outbound HTLC with hash {} for {} msat {}",
10692+
payment_hash,
10693+
amount_msat,
10694+
if force_holding_cell {
10695+
"into holding cell"
10696+
} else if need_holding_cell {
10697+
"into holding cell as we're awaiting an RAA or monitor"
10698+
} else {
10699+
"to peer"
10700+
}
10701+
);
1067210702

1067310703
if need_holding_cell {
1067410704
force_holding_cell = true;
@@ -10685,7 +10715,7 @@ where
1068510715
skimmed_fee_msat,
1068610716
blinding_point,
1068710717
});
10688-
return Ok(None);
10718+
return Ok(false);
1068910719
}
1069010720

1069110721
// Record the approximate time when the HTLC is sent to the peer. This timestamp is later used to calculate the
@@ -10706,20 +10736,9 @@ where
1070610736
skimmed_fee_msat,
1070710737
send_timestamp,
1070810738
});
10709-
10710-
let res = msgs::UpdateAddHTLC {
10711-
channel_id: self.context.channel_id,
10712-
htlc_id: self.context.next_holder_htlc_id,
10713-
amount_msat,
10714-
payment_hash,
10715-
cltv_expiry,
10716-
onion_routing_packet,
10717-
skimmed_fee_msat,
10718-
blinding_point,
10719-
};
1072010739
self.context.next_holder_htlc_id += 1;
1072110740

10722-
Ok(Some(res))
10741+
Ok(true)
1072310742
}
1072410743

1072510744
#[rustfmt::skip]
@@ -10956,24 +10975,35 @@ where
1095610975
///
1095710976
/// Shorthand for calling [`Self::send_htlc`] followed by a commitment update, see docs on
1095810977
/// [`Self::send_htlc`] and [`Self::build_commitment_no_state_update`] for more info.
10959-
#[rustfmt::skip]
1096010978
pub fn send_htlc_and_commit<F: Deref, L: Deref>(
1096110979
&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32,
1096210980
source: HTLCSource, onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>,
10963-
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
10981+
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1096410982
) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
10965-
where F::Target: FeeEstimator, L::Target: Logger
10983+
where
10984+
F::Target: FeeEstimator,
10985+
L::Target: Logger,
1096610986
{
10967-
let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source,
10968-
onion_routing_packet, false, skimmed_fee_msat, None, fee_estimator, logger);
10987+
let send_res = self.send_htlc(
10988+
amount_msat,
10989+
payment_hash,
10990+
cltv_expiry,
10991+
source,
10992+
onion_routing_packet,
10993+
false,
10994+
skimmed_fee_msat,
10995+
None,
10996+
fee_estimator,
10997+
logger,
10998+
);
1096910999
// All [`LocalHTLCFailureReason`] errors are temporary, so they are [`ChannelError::Ignore`].
10970-
match send_res.map_err(|(_, msg)| ChannelError::Ignore(msg))? {
10971-
Some(_) => {
10972-
let monitor_update = self.build_commitment_no_status_check(logger);
10973-
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
10974-
Ok(self.push_ret_blockable_mon_update(monitor_update))
10975-
},
10976-
None => Ok(None)
11000+
let can_add_htlc = send_res.map_err(|(_, msg)| ChannelError::Ignore(msg))?;
11001+
if can_add_htlc {
11002+
let monitor_update = self.build_commitment_no_status_check(logger);
11003+
self.monitor_updating_paused(false, true, false, Vec::new(), Vec::new(), Vec::new());
11004+
Ok(self.push_ret_blockable_mon_update(monitor_update))
11005+
} else {
11006+
Ok(None)
1097711007
}
1097811008
}
1097911009

0 commit comments

Comments
 (0)