@@ -4469,14 +4469,44 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
4469
4469
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
4470
4470
}
4471
4471
4472
+ /// Estimate our part of the fee of the new funding transaction.
4473
+ /// input_count: Number of contributed inputs.
4474
+ /// witness_weight: The witness weight for contributed inputs.
4475
+ #[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
4476
+ fn estimate_funding_transaction_fee(
4477
+ is_initiator: bool, input_count: usize, witness_weight: Weight,
4478
+ funding_feerate_sat_per_1000_weight: u32,
4479
+ ) -> u64 {
4480
+ // Inputs
4481
+ let mut weight = (input_count as u64) * BASE_INPUT_WEIGHT;
4482
+
4483
+ // Witnesses
4484
+ weight = weight.saturating_add(witness_weight.to_wu());
4485
+
4486
+ // If we are the initiator, we must pay for weight of all common fields in the funding transaction.
4487
+ if is_initiator {
4488
+ weight = weight
4489
+ .saturating_add(TX_COMMON_FIELDS_WEIGHT)
4490
+ // The weight of the funding output, a P2WSH output
4491
+ // NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
4492
+ // to calculate the contributed weight, so we use an all-zero hash.
4493
+ .saturating_add(get_output_weight(&ScriptBuf::new_p2wsh(
4494
+ &WScriptHash::from_raw_hash(Hash::all_zeros())
4495
+ )).to_wu())
4496
+ }
4497
+
4498
+ fee_for_weight(funding_feerate_sat_per_1000_weight, weight)
4499
+ }
4500
+
4472
4501
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
4473
4502
pub(super) fn calculate_our_funding_satoshis(
4474
4503
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
4475
4504
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
4476
4505
holder_dust_limit_satoshis: u64,
4477
4506
) -> Result<u64, APIError> {
4478
- let mut total_input_satoshis = 0u64 ;
4507
+ let estimated_fee = estimate_funding_transaction_fee(is_initiator, funding_inputs.len(), total_witness_weight, funding_feerate_sat_per_1000_weight) ;
4479
4508
4509
+ let mut total_input_satoshis = 0u64;
4480
4510
for (idx, input) in funding_inputs.iter().enumerate() {
4481
4511
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
4482
4512
total_input_satoshis = total_input_satoshis.saturating_add(output.value.to_sat());
@@ -4486,26 +4516,8 @@ pub(super) fn calculate_our_funding_satoshis(
4486
4516
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
4487
4517
}
4488
4518
}
4489
- // inputs:
4490
- let mut our_contributed_weight = (funding_inputs.len() as u64) * BASE_INPUT_WEIGHT;
4491
- // witnesses:
4492
- our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
4493
4519
4494
- // If we are the initiator, we must pay for weight of all common fields in the funding transaction.
4495
- if is_initiator {
4496
- our_contributed_weight = our_contributed_weight
4497
- .saturating_add(TX_COMMON_FIELDS_WEIGHT)
4498
- // The weight of a P2WSH output to be added later.
4499
- //
4500
- // NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
4501
- // to calculate the contributed weight, so we use an all-zero hash.
4502
- .saturating_add(get_output_weight(&ScriptBuf::new_p2wsh(
4503
- &WScriptHash::from_raw_hash(Hash::all_zeros())
4504
- )).to_wu())
4505
- }
4506
-
4507
- let funding_satoshis = total_input_satoshis
4508
- .saturating_sub(fee_for_weight(funding_feerate_sat_per_1000_weight, our_contributed_weight));
4520
+ let funding_satoshis = total_input_satoshis.saturating_sub(estimated_fee);
4509
4521
if funding_satoshis < holder_dust_limit_satoshis {
4510
4522
Ok(0)
4511
4523
} else {
@@ -12240,6 +12252,39 @@ mod tests {
12240
12252
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
12241
12253
}
12242
12254
12255
+ #[test]
12256
+ fn test_estimate_funding_transaction_fee() {
12257
+ use crate::ln::channel::estimate_funding_transaction_fee;
12258
+ use bitcoin::Weight;
12259
+
12260
+ let witness_weight = Weight::from_wu(300);
12261
+
12262
+ assert_eq!(
12263
+ estimate_funding_transaction_fee(true, 2, witness_weight, 2000),
12264
+ 1668
12265
+ );
12266
+
12267
+ assert_eq!(
12268
+ estimate_funding_transaction_fee(true, 2, witness_weight, 3000),
12269
+ 2502
12270
+ );
12271
+
12272
+ assert_eq!(
12273
+ estimate_funding_transaction_fee(true, 1, witness_weight, 2000),
12274
+ 1348
12275
+ );
12276
+
12277
+ assert_eq!(
12278
+ estimate_funding_transaction_fee(true, 1, Weight::from_wu(0), 2000),
12279
+ 748
12280
+ );
12281
+
12282
+ assert_eq!(
12283
+ estimate_funding_transaction_fee(false, 1, Weight::from_wu(0), 2000),
12284
+ 320
12285
+ );
12286
+ }
12287
+
12243
12288
fn prepare_input(value: u64) -> (TxIn, TransactionU16LenLimited) {
12244
12289
let input_1_prev_out = TxOut { value: Amount::from_sat(value), script_pubkey: ScriptBuf::default() };
12245
12290
let input_1_prev_tx = Transaction {
@@ -12274,24 +12319,6 @@ mod tests {
12274
12319
298332
12275
12320
);
12276
12321
12277
- assert_eq!(
12278
- calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 1000)
12279
- .unwrap(),
12280
- 18652
12281
- );
12282
-
12283
- assert_eq!(
12284
- calculate_our_funding_satoshis(true, &inputs_1, Weight::from_wu(0), 2000, 1000)
12285
- .unwrap(),
12286
- 19252
12287
- );
12288
-
12289
- assert_eq!(
12290
- calculate_our_funding_satoshis(false, &inputs_1, Weight::from_wu(0), 2000, 1000)
12291
- .unwrap(),
12292
- 19680
12293
- );
12294
-
12295
12322
// below dust limit
12296
12323
assert_eq!(
12297
12324
calculate_our_funding_satoshis(true, &inputs_1, witness_weight, 2000, 20_000)
0 commit comments