@@ -4559,7 +4559,7 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
45594559/// input_count: Number of contributed inputs.
45604560/// witness_weight: The witness weight for contributed inputs.
45614561#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
4562- fn estimate_funding_transaction_fee (
4562+ fn estimate_v2_funding_transaction_fee (
45634563 is_initiator: bool, input_count: usize, witness_weight: Weight,
45644564 funding_feerate_sat_per_1000_weight: u32,
45654565) -> u64 {
@@ -4590,7 +4590,7 @@ pub(super) fn calculate_our_funding_satoshis(
45904590 total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
45914591 holder_dust_limit_satoshis: u64,
45924592) -> Result<u64, APIError> {
4593- let estimated_fee = estimate_funding_transaction_fee (is_initiator, funding_inputs.len(), total_witness_weight, funding_feerate_sat_per_1000_weight);
4593+ let estimated_fee = estimate_v2_funding_transaction_fee (is_initiator, funding_inputs.len(), total_witness_weight, funding_feerate_sat_per_1000_weight);
45944594
45954595 let mut total_input_satoshis = 0u64;
45964596 for (idx, input) in funding_inputs.iter().enumerate() {
@@ -8219,10 +8219,11 @@ impl<SP: Deref> FundedChannel<SP> where
82198219 }
82208220
82218221 /// Initiate splicing.
8222- /// - witness_weight: The witness weight for contributed inputs.
8222+ /// - our_funding_inputs: the inputs we contribute to the new funding transaction.
8223+ /// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
82238224 #[cfg(splicing)]
82248225 pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8225- our_funding_inputs: Vec<(TxIn, Transaction)>, witness_weight: Weight,
8226+ our_funding_inputs: Vec<(TxIn, Transaction, Weight)> ,
82268227 funding_feerate_per_kw: u32, locktime: u32,
82278228 ) -> Result<msgs::SpliceInit, ChannelError> {
82288229 // Check if a splice has been initiated already.
@@ -8261,16 +8262,17 @@ impl<SP: Deref> FundedChannel<SP> where
82618262 // Pre-check that inputs are sufficient to cover our contribution.
82628263 // Note: fees are not taken into account here.
82638264 let sum_input: u64 = our_funding_inputs.iter().map(
8264- |(txin, tx)| tx.output.get(txin.previous_output.vout as usize).map(|tx| tx.value.to_sat()).unwrap_or(0)
8265+ |(txin, tx, _ )| tx.output.get(txin.previous_output.vout as usize).map(|tx| tx.value.to_sat()).unwrap_or(0)
82658266 ).sum();
82668267
82678268 // The +1 is to include the input of the old funding
82688269 let funding_input_count = our_funding_inputs.len() + 1;
82698270 // Input witness weight, extended with weight for spending old funding
82708271 let total_witness_weight = Weight::from_wu(
8271- witness_weight.to_wu().saturating_add(FUNDING_TRANSACTION_WITNESS_WEIGHT)
8272+ our_funding_inputs.iter().map(|(_, _, w)| w.to_wu()).sum::<u64>()
8273+ .saturating_add(FUNDING_TRANSACTION_WITNESS_WEIGHT)
82728274 );
8273- let estimated_fee = estimate_funding_transaction_fee (true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
8275+ let estimated_fee = estimate_v2_funding_transaction_fee (true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
82748276 let available_input = sum_input.saturating_sub(estimated_fee);
82758277
82768278 if (available_input as i64) < our_funding_contribution_satoshis {
@@ -9504,6 +9506,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
95049506
95059507 /// Creates a new dual-funded channel from a remote side's request for one.
95069508 /// Assumes chain_hash has already been checked and corresponds with what we expect!
9509+ /// TODO(dual_funding): Include witness weight info with the inputs.
95079510 #[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
95089511 pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
95099512 fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
@@ -12496,6 +12499,43 @@ mod tests {
1249612499 assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1249712500 }
1249812501
12502+
12503+ #[test]
12504+ fn test_estimate_v2_funding_transaction_fee() {
12505+ use crate::ln::channel::estimate_v2_funding_transaction_fee;
12506+ use bitcoin::Weight;
12507+
12508+ // 2 inputs with weight 300, initiator, 2000 sat/kw feerate
12509+ assert_eq!(
12510+ estimate_v2_funding_transaction_fee(true, 2, Weight::from_wu(300), 2000),
12511+ 1668
12512+ );
12513+
12514+ // higher feerate
12515+ assert_eq!(
12516+ estimate_v2_funding_transaction_fee(true, 2, Weight::from_wu(300), 3000),
12517+ 2502
12518+ );
12519+
12520+ // only 1 input
12521+ assert_eq!(
12522+ estimate_v2_funding_transaction_fee(true, 1, Weight::from_wu(300), 2000),
12523+ 1348
12524+ );
12525+
12526+ // 0 input weight
12527+ assert_eq!(
12528+ estimate_v2_funding_transaction_fee(true, 1, Weight::from_wu(0), 2000),
12529+ 748
12530+ );
12531+
12532+ // not initiator
12533+ assert_eq!(
12534+ estimate_v2_funding_transaction_fee(false, 1, Weight::from_wu(0), 2000),
12535+ 320
12536+ );
12537+ }
12538+
1249912539 #[cfg(all(test, splicing))]
1250012540 fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
1250112541 use crate::ln::channel::PendingSplice;
0 commit comments