Skip to content

Commit 72fd3f5

Browse files
committed
can_send_update_fee
1 parent 697b41b commit 72fd3f5

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

lightning/src/ln/channel.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,6 @@ struct HTLCStats {
11121112
// htlc on the counterparty's commitment transaction.
11131113
extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat: Option<u64>,
11141114
on_holder_tx_dust_exposure_msat: u64,
1115-
outbound_holding_cell_msat: u64,
1116-
on_holder_tx_outbound_holding_cell_htlcs_count: u32, // dust HTLCs *non*-included
11171115
}
11181116

11191117
/// A struct gathering data on a commitment, either local or remote.
@@ -4375,27 +4373,39 @@ where
43754373
let dust_exposure_limiting_feerate = self.get_dust_exposure_limiting_feerate(
43764374
&fee_estimator, funding.get_channel_type(),
43774375
);
4378-
let htlc_stats = self.get_pending_htlc_stats(funding, Some(feerate_per_kw), dust_exposure_limiting_feerate);
4379-
let stats = self.build_commitment_stats(funding, true, true, Some(feerate_per_kw), Some(htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize));
4380-
let holder_balance_msat = stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
4376+
let next_commitment_htlcs = self.next_commitment_htlcs(None);
4377+
let value_to_self_msat = self.get_next_commitment_value_to_self_msat(funding);
4378+
let next_commitment_stats = SpecTxBuilder {}.get_builder_stats(funding.is_outbound(), funding.get_value_satoshis(), value_to_self_msat, &next_commitment_htlcs, CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, feerate_per_kw, dust_exposure_limiting_feerate, funding.get_channel_type(), self.holder_dust_limit_satoshis, self.counterparty_dust_limit_satoshis);
43814379
// Note that `stats.commit_tx_fee_sat` accounts for any HTLCs that transition from non-dust to dust under a higher feerate (in the case where HTLC-transactions pay endogenous fees).
4382-
if holder_balance_msat < stats.commit_tx_fee_sat * 1000 + funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
4380+
if next_commitment_stats.holder_balance_msat.unwrap() < next_commitment_stats.holder_commit_tx_fee_sat * 1000 + funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
43834381
//TODO: auto-close after a number of failures?
43844382
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
43854383
return false;
43864384
}
43874385

43884386
// Note, we evaluate pending htlc "preemptive" trimmed-to-dust threshold at the proposed `feerate_per_kw`.
43894387
let max_dust_htlc_exposure_msat = self.get_max_dust_htlc_exposure_msat(dust_exposure_limiting_feerate);
4390-
if htlc_stats.on_holder_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
4388+
if next_commitment_stats.on_holder_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
43914389
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
43924390
return false;
43934391
}
4394-
if htlc_stats.on_counterparty_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
4392+
if next_commitment_stats.on_counterparty_tx_dust_exposure_msat > max_dust_htlc_exposure_msat {
43954393
log_debug!(logger, "Cannot afford to send new feerate at {} without infringing max dust htlc exposure", feerate_per_kw);
43964394
return false;
43974395
}
43984396

4397+
#[cfg(any(test, fuzzing))]
4398+
{
4399+
let next_commitment_stats = SpecTxBuilder {}.get_builder_stats(funding.is_outbound(), funding.get_value_satoshis(), value_to_self_msat, &next_commitment_htlcs, 0, feerate_per_kw, dust_exposure_limiting_feerate, funding.get_channel_type(), self.holder_dust_limit_satoshis, self.counterparty_dust_limit_satoshis);
4400+
let mut predicted_htlcs = next_commitment_htlcs;
4401+
predicted_htlcs.sort_unstable();
4402+
*funding.next_local_fee.lock().unwrap() = PredictedNextFee {
4403+
predicted_feerate: feerate_per_kw,
4404+
predicted_htlcs,
4405+
predicted_fee_sat: next_commitment_stats.holder_commit_tx_fee_sat,
4406+
};
4407+
}
4408+
43994409
return true;
44004410
}
44014411

@@ -4786,8 +4796,6 @@ where
47864796
}
47874797

47884798
let mut pending_outbound_htlcs_value_msat = 0;
4789-
let mut outbound_holding_cell_msat = 0;
4790-
let mut on_holder_tx_outbound_holding_cell_htlcs_count = 0;
47914799
let mut pending_outbound_htlcs = self.pending_outbound_htlcs.len();
47924800
{
47934801
let counterparty_dust_limit_success_sat = htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
@@ -4808,16 +4816,13 @@ where
48084816
if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
48094817
pending_outbound_htlcs += 1;
48104818
pending_outbound_htlcs_value_msat += amount_msat;
4811-
outbound_holding_cell_msat += amount_msat;
48124819
if *amount_msat / 1000 < counterparty_dust_limit_success_sat {
48134820
on_counterparty_tx_dust_exposure_msat += amount_msat;
48144821
} else {
48154822
on_counterparty_tx_accepted_nondust_htlcs += 1;
48164823
}
48174824
if *amount_msat / 1000 < holder_dust_limit_timeout_sat {
48184825
on_holder_tx_dust_exposure_msat += amount_msat;
4819-
} else {
4820-
on_holder_tx_outbound_holding_cell_htlcs_count += 1;
48214826
}
48224827
}
48234828
}
@@ -4855,8 +4860,6 @@ where
48554860
on_counterparty_tx_dust_exposure_msat,
48564861
extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat,
48574862
on_holder_tx_dust_exposure_msat,
4858-
outbound_holding_cell_msat,
4859-
on_holder_tx_outbound_holding_cell_htlcs_count,
48604863
}
48614864
}
48624865

0 commit comments

Comments
 (0)