Skip to content

Commit 8dff91c

Browse files
committed
Fold anchor amounts in new for inbound and outbound
1 parent 66866ba commit 8dff91c

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,18 +2765,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27652765

27662766
// check if the funder's amount for the initial commitment tx is sufficient
27672767
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
2768-
let anchor_outputs_value = if channel_type.supports_anchors_zero_fee_htlc_tx() {
2769-
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
2770-
} else {
2771-
0
2772-
};
27732768
let funders_amount_msat = open_channel_fields.funding_satoshis * 1000 - msg_push_msat;
2774-
let commitment_tx_fee = commit_tx_fee_sat(open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
2775-
if (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commitment_tx_fee {
2776-
return Err(ChannelError::close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value), commitment_tx_fee)));
2769+
let (_, remote_balance_before_fee_msat) = SpecTxBuilder {}.balances_excluding_tx_fee(false, &channel_type, msg_push_msat, funders_amount_msat);
2770+
let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
2771+
if remote_balance_before_fee_msat / 1000 < total_fee_sat {
2772+
return Err(ChannelError::close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", remote_balance_before_fee_msat / 1000, total_fee_sat)));
27772773
}
27782774

2779-
let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee - anchor_outputs_value;
2775+
let to_remote_satoshis = remote_balance_before_fee_msat / 1000 - total_fee_sat;
27802776
// While it's reasonable for us to not meet the channel reserve initially (if they don't
27812777
// want to push much to us), our counterparty should always have more than our reserve.
27822778
if to_remote_satoshis < holder_selected_channel_reserve_satoshis {
@@ -3034,17 +3030,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30343030
debug_assert!(!channel_type.supports_any_optional_bits());
30353031
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
30363032

3037-
let (commitment_conf_target, anchor_outputs_value_msat) = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3038-
(ConfirmationTarget::AnchorChannelFee, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
3033+
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3034+
ConfirmationTarget::AnchorChannelFee
30393035
} else {
3040-
(ConfirmationTarget::NonAnchorChannelFee, 0)
3036+
ConfirmationTarget::NonAnchorChannelFee
30413037
};
30423038
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
30433039

30443040
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
3045-
let commitment_tx_fee = commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) * 1000;
3046-
if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commitment_tx_fee {
3047-
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
3041+
let (local_balance_before_fee_msat, _) = SpecTxBuilder {}.balances_excluding_tx_fee(true, &channel_type, value_to_self_msat, push_msat);
3042+
let total_fee_sat = SpecTxBuilder {}.commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
3043+
if local_balance_before_fee_msat / 1000 < total_fee_sat {
3044+
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", local_balance_before_fee_msat / 1000, total_fee_sat) });
30483045
}
30493046

30503047
let mut secp_ctx = Secp256k1::new();

0 commit comments

Comments
 (0)