@@ -4471,48 +4471,33 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
4471
4471
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
4472
4472
}
4473
4473
4474
- #[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
4475
- pub(super) fn calculate_our_funding_satoshis(
4476
- is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
4477
- total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
4478
- holder_dust_limit_satoshis: u64,
4479
- ) -> Result<u64, APIError> {
4480
- let mut total_input_satoshis = 0u64;
4481
-
4482
- for (idx, input) in funding_inputs.iter().enumerate() {
4483
- if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
4484
- total_input_satoshis = total_input_satoshis.saturating_add(output.value.to_sat());
4485
- } else {
4486
- return Err(APIError::APIMisuseError {
4487
- err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
4488
- input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
4489
- }
4490
- }
4491
- // inputs:
4492
- let mut our_contributed_weight = (funding_inputs.len() as u64) * BASE_INPUT_WEIGHT;
4493
- // witnesses:
4494
- our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
4474
+ /// Estimate our part of the fee of the new funding transaction.
4475
+ /// input_count: Number of contributed inputs.
4476
+ /// witness_weight: The witness weight for contributed inputs.
4477
+ #[allow(dead_code)] // TODO(dual_funding): TODO(splicing): Remove allow once used.
4478
+ fn estimate_v2_funding_transaction_fee(
4479
+ is_initiator: bool, input_count: usize, witness_weight: Weight,
4480
+ funding_feerate_sat_per_1000_weight: u32,
4481
+ ) -> u64 {
4482
+ // Inputs
4483
+ let mut weight = (input_count as u64) * BASE_INPUT_WEIGHT;
4484
+
4485
+ // Witnesses
4486
+ weight = weight.saturating_add(witness_weight.to_wu());
4495
4487
4496
4488
// If we are the initiator, we must pay for weight of all common fields in the funding transaction.
4497
4489
if is_initiator {
4498
- our_contributed_weight = our_contributed_weight
4490
+ weight = weight
4499
4491
.saturating_add(TX_COMMON_FIELDS_WEIGHT)
4500
- // The weight of a P2WSH output to be added later.
4501
- //
4492
+ // The weight of the funding output, a P2WSH output
4502
4493
// NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
4503
4494
// to calculate the contributed weight, so we use an all-zero hash.
4504
4495
.saturating_add(get_output_weight(&ScriptBuf::new_p2wsh(
4505
4496
&WScriptHash::from_raw_hash(Hash::all_zeros())
4506
4497
)).to_wu())
4507
4498
}
4508
4499
4509
- let funding_satoshis = total_input_satoshis
4510
- .saturating_sub(fee_for_weight(funding_feerate_sat_per_1000_weight, our_contributed_weight));
4511
- if funding_satoshis < holder_dust_limit_satoshis {
4512
- Ok(0)
4513
- } else {
4514
- Ok(funding_satoshis)
4515
- }
4500
+ fee_for_weight(funding_feerate_sat_per_1000_weight, weight)
4516
4501
}
4517
4502
4518
4503
/// Context for dual-funded channels.
@@ -9252,27 +9237,23 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9252
9237
9253
9238
/// Creates a new dual-funded channel from a remote side's request for one.
9254
9239
/// Assumes chain_hash has already been checked and corresponds with what we expect!
9240
+ /// TODO(dual_funding): Allow contributions, pass intended amount and inputs
9255
9241
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
9256
9242
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
9257
9243
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
9258
9244
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
9259
9245
their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
9260
- funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, total_witness_weight: Weight,
9261
9246
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
9262
9247
) -> Result<Self, ChannelError>
9263
9248
where ES::Target: EntropySource,
9264
9249
F::Target: FeeEstimator,
9265
9250
L::Target: Logger,
9266
9251
{
9267
- let funding_satoshis = calculate_our_funding_satoshis(
9268
- false, &funding_inputs, total_witness_weight, msg.funding_feerate_sat_per_1000_weight,
9269
- msg.common_fields.dust_limit_satoshis
9270
- ).map_err(|_| ChannelError::Close(
9271
- (
9272
- "Failed to accept channel".to_string(),
9273
- ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
9274
- )))?;
9275
- let channel_value_satoshis = funding_satoshis.saturating_add(msg.common_fields.funding_satoshis);
9252
+ // TODO(dual_funding): Take these as input once supported
9253
+ let our_funding_satoshis = 0u64;
9254
+ let our_funding_inputs = Vec::new();
9255
+
9256
+ let channel_value_satoshis = our_funding_satoshis.saturating_add(msg.common_fields.funding_satoshis);
9276
9257
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
9277
9258
channel_value_satoshis, msg.common_fields.dust_limit_satoshis);
9278
9259
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
@@ -9306,7 +9287,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9306
9287
logger,
9307
9288
false,
9308
9289
9309
- funding_satoshis ,
9290
+ our_funding_satoshis ,
9310
9291
9311
9292
counterparty_pubkeys,
9312
9293
channel_type,
@@ -9321,10 +9302,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9321
9302
context.channel_id = channel_id;
9322
9303
9323
9304
let dual_funding_context = DualFundingChannelContext {
9324
- our_funding_satoshis: funding_satoshis ,
9305
+ our_funding_satoshis: our_funding_satoshis ,
9325
9306
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9326
9307
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9327
- our_funding_inputs: funding_inputs .clone(),
9308
+ our_funding_inputs: our_funding_inputs .clone(),
9328
9309
};
9329
9310
9330
9311
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
@@ -9336,7 +9317,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9336
9317
feerate_sat_per_kw: dual_funding_context.funding_feerate_sat_per_1000_weight,
9337
9318
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
9338
9319
is_initiator: false,
9339
- inputs_to_contribute: funding_inputs ,
9320
+ inputs_to_contribute: our_funding_inputs ,
9340
9321
outputs_to_contribute: Vec::new(),
9341
9322
expected_remote_shared_funding_output: Some((context.get_funding_redeemscript().to_p2wsh(), context.channel_value_satoshis)),
9342
9323
}
@@ -10469,7 +10450,7 @@ mod tests {
10469
10450
use bitcoin::amount::Amount;
10470
10451
use bitcoin::constants::ChainHash;
10471
10452
use bitcoin::script::{ScriptBuf, Builder};
10472
- use bitcoin::transaction::{Transaction, TxIn, TxOut, Version};
10453
+ use bitcoin::transaction::{Transaction, TxOut, Version};
10473
10454
use bitcoin::opcodes;
10474
10455
use bitcoin::network::Network;
10475
10456
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
@@ -10491,7 +10472,7 @@ mod tests {
10491
10472
use crate::routing::router::{Path, RouteHop};
10492
10473
use crate::util::config::UserConfig;
10493
10474
use crate::util::errors::APIError;
10494
- use crate::util::ser::{ReadableArgs, TransactionU16LenLimited, Writeable};
10475
+ use crate::util::ser::{ReadableArgs, Writeable};
10495
10476
use crate::util::test_utils;
10496
10477
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
10497
10478
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -12242,82 +12223,39 @@ mod tests {
12242
12223
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
12243
12224
}
12244
12225
12245
- fn funding_input_sats(input_value_sats: u64) -> (TxIn, TransactionU16LenLimited) {
12246
- let input_1_prev_out = TxOut { value: Amount::from_sat(input_value_sats), script_pubkey: ScriptBuf::default() };
12247
- let input_1_prev_tx = Transaction {
12248
- input: vec![], output: vec![input_1_prev_out],
12249
- version: Version::TWO, lock_time: bitcoin::absolute::LockTime::ZERO,
12250
- };
12251
- let input_1_txin = TxIn {
12252
- previous_output: bitcoin::OutPoint { txid: input_1_prev_tx.compute_txid(), vout: 0 },
12253
- ..Default::default()
12254
- };
12255
- (input_1_txin, TransactionU16LenLimited::new(input_1_prev_tx).unwrap())
12256
- }
12257
-
12258
12226
#[test]
12259
- fn test_calculate_our_funding_satoshis () {
12260
- use crate::ln::channel::calculate_our_funding_satoshis ;
12227
+ fn test_estimate_v2_funding_transaction_fee () {
12228
+ use crate::ln::channel::estimate_v2_funding_transaction_fee ;
12261
12229
use bitcoin::Weight;
12262
12230
12263
- // normal use case, output is less than the available inputs
12231
+ // 2 inputs with weight 300, initiator, 2000 sat/kw feerate
12264
12232
assert_eq!(
12265
- calculate_our_funding_satoshis(
12266
- true,
12267
- &[
12268
- funding_input_sats(200_000),
12269
- funding_input_sats(100_000),
12270
- ],
12271
- Weight::from_wu(300),
12272
- 2000,
12273
- 1000,
12274
- ).unwrap(),
12275
- 298332
12233
+ estimate_v2_funding_transaction_fee(true, 2, Weight::from_wu(300), 2000),
12234
+ 1668
12276
12235
);
12277
12236
12237
+ // higher feerate
12278
12238
assert_eq!(
12279
- calculate_our_funding_satoshis(
12280
- true,
12281
- &[funding_input_sats(20_000)],
12282
- Weight::from_wu(300),
12283
- 2000,
12284
- 1000,
12285
- ).unwrap(),
12286
- 18652
12239
+ estimate_v2_funding_transaction_fee(true, 2, Weight::from_wu(300), 3000),
12240
+ 2502
12287
12241
);
12288
12242
12243
+ // only 1 input
12289
12244
assert_eq!(
12290
- calculate_our_funding_satoshis(
12291
- true,
12292
- &[funding_input_sats(20_000)],
12293
- Weight::from_wu(0),
12294
- 2000,
12295
- 1000,
12296
- ).unwrap(),
12297
- 19252
12245
+ estimate_v2_funding_transaction_fee(true, 1, Weight::from_wu(300), 2000),
12246
+ 1348
12298
12247
);
12299
12248
12249
+ // 0 input weight
12300
12250
assert_eq!(
12301
- calculate_our_funding_satoshis(
12302
- false,
12303
- &[funding_input_sats(20_000)],
12304
- Weight::from_wu(0),
12305
- 2000,
12306
- 1000,
12307
- ).unwrap(),
12308
- 19680
12251
+ estimate_v2_funding_transaction_fee(true, 1, Weight::from_wu(0), 2000),
12252
+ 748
12309
12253
);
12310
12254
12311
- // below dust limit
12255
+ // not initiator
12312
12256
assert_eq!(
12313
- calculate_our_funding_satoshis(
12314
- true,
12315
- &[funding_input_sats(20_000)],
12316
- Weight::from_wu(300),
12317
- 2000,
12318
- 20_000,
12319
- ).unwrap(),
12320
- 0
12257
+ estimate_v2_funding_transaction_fee(false, 1, Weight::from_wu(0), 2000),
12258
+ 320
12321
12259
);
12322
12260
}
12323
12261
}
0 commit comments