Skip to content

Commit 9503f6f

Browse files
committed
f Adjust initial spend with actual satisfaction fee
1 parent 3c2e413 commit 9503f6f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

lightning/src/util/anchor_channel_reserves.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ pub struct AnchorChannelReserveContext {
145145
///
146146
/// [ChannelHandshakeConfig::our_max_accepted_htlcs]: crate::util::config::ChannelHandshakeConfig::our_max_accepted_htlcs
147147
pub expected_accepted_htlcs: u16,
148-
/// Whether the wallet providing the anchor channel reserve uses Taproot P2TR outputs for its
149-
/// funds, or Segwit P2WPKH outputs otherwise.
148+
/// Whether the wallet handling anchor channel reserves creates Taproot P2TR outputs for any new
149+
/// outputs, or Segwit P2WPKH outputs otherwise.
150150
pub taproot_wallet: bool,
151151
}
152152

@@ -194,21 +194,26 @@ pub fn get_reserve_per_channel(context: &AnchorChannelReserveContext) -> Amount
194194
pub fn get_supportable_anchor_channels(
195195
context: &AnchorChannelReserveContext, utxos: &[Utxo],
196196
) -> u64 {
197-
let reserve_per_channel = get_reserve_per_channel(context);
197+
// Get the reserve needed per channel, replacing the fee for an initial spend with the actual value
198+
// below.
199+
let default_satisfaction_fee = context.upper_bound_fee_rate.fee_wu(
200+
Weight::from_wu(if context.taproot_wallet { P2TR_INPUT_WEIGHT } else { P2WPKH_INPUT_WEIGHT })
201+
).unwrap_or(Amount::MAX);
202+
let reserve_per_channel = get_reserve_per_channel(context) - default_satisfaction_fee;
203+
198204
let mut total_fractional_amount = Amount::from_sat(0);
199205
let mut num_whole_utxos = 0;
200206
for utxo in utxos {
201-
if utxo.output.value >= reserve_per_channel {
207+
let satisfaction_fee = context
208+
.upper_bound_fee_rate
209+
.fee_wu(Weight::from_wu(utxo.satisfaction_weight))
210+
.unwrap_or(Amount::MAX);
211+
let amount = utxo.output.value.checked_sub(satisfaction_fee).unwrap_or(Amount::MIN);
212+
if amount >= reserve_per_channel {
202213
num_whole_utxos += 1;
203214
} else {
204215
total_fractional_amount =
205-
total_fractional_amount.checked_add(utxo.output.value).unwrap_or(Amount::MAX);
206-
let satisfaction_fee = context
207-
.upper_bound_fee_rate
208-
.fee_wu(Weight::from_wu(utxo.satisfaction_weight))
209-
.unwrap_or(Amount::MAX);
210-
total_fractional_amount =
211-
total_fractional_amount.checked_sub(satisfaction_fee).unwrap_or(Amount::MIN);
216+
total_fractional_amount.checked_add(amount).unwrap_or(Amount::MAX);
212217
}
213218
}
214219
// We require disjoint sets of UTXOs for the reserve of each channel,

0 commit comments

Comments
 (0)