Skip to content

Commit 45aa90e

Browse files
committed
Consider anchor outputs value on channel open
We should make sure the funding amount of a channel can cover all its associated costs, including the value of anchor outputs, to make sure that it is actually usable once "opened".
1 parent 3fda47b commit 45aa90e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5745,16 +5745,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
57455745
let channel_type = Self::get_initial_channel_type(&config, their_features);
57465746
debug_assert!(channel_type.is_subset(&channelmanager::provided_channel_type_features(&config)));
57475747

5748-
let commitment_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5749-
ConfirmationTarget::MempoolMinimum
5748+
let (commitment_conf_target, anchor_outputs_value_msat) = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5749+
(ConfirmationTarget::MempoolMinimum, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
57505750
} else {
5751-
ConfirmationTarget::Normal
5751+
(ConfirmationTarget::Normal, 0)
57525752
};
57535753
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
57545754

57555755
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
57565756
let commitment_tx_fee = commit_tx_fee_msat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
5757-
if value_to_self_msat < commitment_tx_fee {
5757+
if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commitment_tx_fee {
57585758
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) });
57595759
}
57605760

@@ -6371,13 +6371,18 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
63716371

63726372
// check if the funder's amount for the initial commitment tx is sufficient
63736373
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
6374+
let anchor_outputs_value = if channel_type.supports_anchors_zero_fee_htlc_tx() {
6375+
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
6376+
} else {
6377+
0
6378+
};
63746379
let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
63756380
let commitment_tx_fee = commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) / 1000;
6376-
if funders_amount_msat / 1000 < commitment_tx_fee {
6377-
return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
6381+
if (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commitment_tx_fee {
6382+
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)));
63786383
}
63796384

6380-
let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee;
6385+
let to_remote_satoshis = funders_amount_msat / 1000 - commitment_tx_fee - anchor_outputs_value;
63816386
// While it's reasonable for us to not meet the channel reserve initially (if they don't
63826387
// want to push much to us), our counterparty should always have more than our reserve.
63836388
if to_remote_satoshis < holder_selected_channel_reserve_satoshis {

0 commit comments

Comments
 (0)