Skip to content

Commit 5db254a

Browse files
committed
fixup: Never expect get_next_commitment_stats calls
1 parent ff95850 commit 5db254a

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

lightning/src/ln/channel.rs

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,9 +4453,11 @@ where
44534453
self.feerate_per_kw,
44544454
dust_exposure_limiting_feerate,
44554455
)
4456-
.expect(
4457-
"Adding an inbound HTLC should never exhaust the holder's balance before fees",
4458-
);
4456+
.map_err(|()| {
4457+
ChannelError::close(String::from(
4458+
"Balance after HTLCs and anchors exhausted on local commitment",
4459+
))
4460+
})?;
44594461
// Check that they won't violate our local required channel reserve by adding this HTLC.
44604462
if next_local_commitment_stats.holder_balance_before_fee_msat
44614463
< funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000
@@ -4492,7 +4494,11 @@ where
44924494
msg.feerate_per_kw,
44934495
dust_exposure_limiting_feerate,
44944496
)
4495-
.expect("Updating the fee should never exhaust the balance after HTLCs and anchors");
4497+
.map_err(|()| {
4498+
ChannelError::close(String::from(
4499+
"Balance after HTLCs and anchors exhausted on local commitment",
4500+
))
4501+
})?;
44964502
let next_remote_commitment_stats = self
44974503
.get_next_remote_commitment_stats(
44984504
funding,
@@ -4502,7 +4508,11 @@ where
45024508
msg.feerate_per_kw,
45034509
dust_exposure_limiting_feerate,
45044510
)
4505-
.expect("Updating the fee should never exhaust the balance after HTLCs and anchors");
4511+
.map_err(|()| {
4512+
ChannelError::close(String::from(
4513+
"Balance after HTLCs and anchors exhausted on remote commitment",
4514+
))
4515+
})?;
45064516

45074517
let max_dust_htlc_exposure_msat =
45084518
self.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
@@ -4626,16 +4636,22 @@ where
46264636
// Include outbound update_add_htlc's in the holding cell, and those which haven't yet been ACK'ed by
46274637
// the counterparty (ie. LocalAnnounced HTLCs)
46284638
let include_counterparty_unknown_htlcs = true;
4629-
let next_remote_commitment_stats = self
4630-
.get_next_remote_commitment_stats(
4631-
funding,
4632-
None,
4633-
include_counterparty_unknown_htlcs,
4634-
CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize,
4635-
feerate_per_kw,
4636-
dust_exposure_limiting_feerate,
4637-
)
4638-
.expect("Updating the fee should never exhaust the balance after HTLCs and anchors");
4639+
let next_remote_commitment_stats = if let Ok(stats) = self.get_next_remote_commitment_stats(
4640+
funding,
4641+
None,
4642+
include_counterparty_unknown_htlcs,
4643+
CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize,
4644+
feerate_per_kw,
4645+
dust_exposure_limiting_feerate,
4646+
) {
4647+
stats
4648+
} else {
4649+
log_debug!(
4650+
logger,
4651+
"Cannot afford to send new feerate due to balance after HTLCs and anchors exhausted on remote commitment",
4652+
);
4653+
return false;
4654+
};
46394655
// Note that `stats.commit_tx_fee_sat` accounts for any HTLCs that transition from non-dust to dust
46404656
// under a higher feerate (in the case where HTLC-transactions pay endogenous fees).
46414657
if next_remote_commitment_stats.holder_balance_before_fee_msat
@@ -4660,16 +4676,22 @@ where
46604676
return false;
46614677
}
46624678

4663-
let next_local_commitment_stats = self
4664-
.get_next_local_commitment_stats(
4665-
funding,
4666-
None,
4667-
include_counterparty_unknown_htlcs,
4668-
CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize,
4669-
feerate_per_kw,
4670-
dust_exposure_limiting_feerate,
4671-
)
4672-
.expect("Updating the fee should never exhaust the balance after HTLCs and anchors");
4679+
let next_local_commitment_stats = if let Ok(stats) = self.get_next_local_commitment_stats(
4680+
funding,
4681+
None,
4682+
include_counterparty_unknown_htlcs,
4683+
CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize,
4684+
feerate_per_kw,
4685+
dust_exposure_limiting_feerate,
4686+
) {
4687+
stats
4688+
} else {
4689+
log_debug!(
4690+
logger,
4691+
"Cannot afford to send new feerate due to balance after HTLCs and anchors exhausted on local commitment",
4692+
);
4693+
return false;
4694+
};
46734695
if next_local_commitment_stats.dust_exposure_msat > max_dust_htlc_exposure_msat {
46744696
log_debug!(
46754697
logger,
@@ -4716,7 +4738,10 @@ where
47164738
feerate,
47174739
dust_exposure_limiting_feerate,
47184740
)
4719-
.expect("Balances after HTLCs and anchors should never be exhausted at this point");
4741+
.map_err(|()| {
4742+
log_info!(logger, "Attempting to fail HTLC due to balance after HTLCs and anchors exhausted on local commitment");
4743+
LocalHTLCFailureReason::TemporaryChannelFailure
4744+
})?;
47204745
let next_remote_commitment_stats = self
47214746
.get_next_remote_commitment_stats(
47224747
funding,
@@ -4726,7 +4751,10 @@ where
47264751
feerate,
47274752
dust_exposure_limiting_feerate,
47284753
)
4729-
.expect("Balances after HTLCs and anchors should never be exhausted at this point");
4754+
.map_err(|()| {
4755+
log_info!(logger, "Attempting to fail HTLC due to balance after HTLCs and anchors exhausted on remote commitment");
4756+
LocalHTLCFailureReason::TemporaryChannelFailure
4757+
})?;
47304758

47314759
let max_dust_htlc_exposure_msat =
47324760
self.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);

0 commit comments

Comments
 (0)