@@ -54,8 +54,9 @@ use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBounde
5454use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
5555use crate::chain::transaction::{OutPoint, TransactionData};
5656use crate::sign::ecdsa::EcdsaChannelSigner;
57- use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
57+ use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient, P2WPKH_WITNESS_WEIGHT };
5858use crate::events::{ClosureReason, Event};
59+ use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
5960use crate::routing::gossip::NodeId;
6061use crate::util::ser::{Readable, ReadableArgs, TransactionU16LenLimited, Writeable, Writer};
6162use crate::util::logger::{Logger, Record, WithContext};
@@ -4552,15 +4553,44 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
45524553 cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
45534554}
45544555
4556+ /// Estimate our part of the fee of the new funding transaction.
4557+ /// input_count: Number of contributed inputs.
4558+ /// witness_weight: The witness weight for contributed inputs.
4559+ #[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
4560+ fn estimate_funding_transaction_fee(
4561+ is_initiator: bool, input_count: usize, witness_weight: Weight,
4562+ funding_feerate_sat_per_1000_weight: u32,
4563+ ) -> u64 {
4564+ // Inputs
4565+ let mut weight = (input_count as u64) * BASE_INPUT_WEIGHT;
4566+
4567+ // Witnesses
4568+ weight = weight.saturating_add(witness_weight.to_wu());
4569+
4570+ // If we are the initiator, we must pay for weight of all common fields in the funding transaction.
4571+ if is_initiator {
4572+ weight = weight
4573+ .saturating_add(TX_COMMON_FIELDS_WEIGHT)
4574+ // The weight of the funding output, a P2WSH output
4575+ // NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
4576+ // to calculate the contributed weight, so we use an all-zero hash.
4577+ .saturating_add(get_output_weight(&ScriptBuf::new_p2wsh(
4578+ &WScriptHash::from_raw_hash(Hash::all_zeros())
4579+ )).to_wu())
4580+ }
4581+
4582+ fee_for_weight(funding_feerate_sat_per_1000_weight, weight)
4583+ }
4584+
45554585#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
45564586pub(super) fn calculate_our_funding_satoshis(
45574587 is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
45584588 total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
45594589 holder_dust_limit_satoshis: u64,
45604590) -> Result<u64, APIError> {
4561- let mut total_input_satoshis = 0u64;
4562- let mut our_contributed_weight = 0u64;
4591+ let estimated_fee = estimate_funding_transaction_fee(is_initiator, funding_inputs.len(), total_witness_weight, funding_feerate_sat_per_1000_weight);
45634592
4593+ let mut total_input_satoshis = 0u64;
45644594 for (idx, input) in funding_inputs.iter().enumerate() {
45654595 if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
45664596 total_input_satoshis = total_input_satoshis.saturating_add(output.value.to_sat());
@@ -4570,23 +4600,8 @@ pub(super) fn calculate_our_funding_satoshis(
45704600 input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
45714601 }
45724602 }
4573- our_contributed_weight = our_contributed_weight.saturating_add(total_witness_weight.to_wu());
45744603
4575- // If we are the initiator, we must pay for weight of all common fields in the funding transaction.
4576- if is_initiator {
4577- our_contributed_weight = our_contributed_weight
4578- .saturating_add(TX_COMMON_FIELDS_WEIGHT)
4579- // The weight of a P2WSH output to be added later.
4580- //
4581- // NOTE: The witness script hash given here is irrelevant as it's a fixed size and we just want
4582- // to calculate the contributed weight, so we use an all-zero hash.
4583- .saturating_add(get_output_weight(&ScriptBuf::new_p2wsh(
4584- &WScriptHash::from_raw_hash(Hash::all_zeros())
4585- )).to_wu())
4586- }
4587-
4588- let funding_satoshis = total_input_satoshis
4589- .saturating_sub(fee_for_weight(funding_feerate_sat_per_1000_weight, our_contributed_weight));
4604+ let funding_satoshis = total_input_satoshis.saturating_sub(estimated_fee);
45904605 if funding_satoshis < holder_dust_limit_satoshis {
45914606 Ok(0)
45924607 } else {
@@ -8241,10 +8256,21 @@ impl<SP: Deref> FundedChannel<SP> where
82418256
82428257 // Pre-check that inputs are sufficient to cover our contribution.
82438258 // Note: fees are not taken into account here.
8244- let sum_input: i64 = our_funding_inputs.into_iter ().map(
8245- |(txin, tx)| tx.output.get(txin.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64 ).unwrap_or(0)
8259+ let sum_input: u64 = our_funding_inputs.iter ().map(
8260+ |(txin, tx)| tx.output.get(txin.previous_output.vout as usize).map(|tx| tx.value.to_sat()).unwrap_or(0)
82468261 ).sum();
8247- if sum_input < our_funding_contribution_satoshis {
8262+
8263+ // The +1 is to include the input of the old funding
8264+ let funding_input_count = our_funding_inputs.len() + 1;
8265+ // Add weight for inputs (estimated as P2WPKH) *and* spending old funding
8266+ let total_witness_weight = Weight::from_wu(
8267+ our_funding_inputs.len() as u64 * P2WPKH_WITNESS_WEIGHT +
8268+ 2 * P2WPKH_WITNESS_WEIGHT
8269+ );
8270+ let estimated_fee = estimate_funding_transaction_fee(true, funding_input_count, total_witness_weight, funding_feerate_per_kw);
8271+ let available_input = sum_input.saturating_sub(estimated_fee);
8272+
8273+ if (available_input as i64) < our_funding_contribution_satoshis {
82488274 return Err(ChannelError::Warn(format!(
82498275 "Provided inputs are insufficient for our contribution, {} {}",
82508276 sum_input, our_funding_contribution_satoshis,
0 commit comments