@@ -1932,26 +1932,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
19321932 ) -> Result<Option<InteractiveTxMessageSend>, APIError>
19331933 where ES::Target: EntropySource
19341934 {
1935- let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
1935+ let mut funding_inputs = Vec::new();
1936+ mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
19361937
19371938 if let Some(prev_funding_input) = prev_funding_input {
19381939 funding_inputs.push(prev_funding_input);
19391940 }
19401941
1941- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
1942+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
19421943 // Check that vouts exist for each TxIn in provided transactions.
1943- for (idx, input ) in funding_inputs.iter().enumerate() {
1944- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
1945- funding_inputs_prev_outputs.push(output.clone() );
1944+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
1945+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
1946+ funding_inputs_prev_outputs.push(output);
19461947 } else {
19471948 return Err(APIError::APIMisuseError {
19481949 err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
1949- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
1950+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
19501951 }
19511952 }
19521953
19531954 let total_input_satoshis: u64 = funding_inputs.iter().map(
1954- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1955+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
19551956 ).sum();
19561957 if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
19571958 return Err(APIError::APIMisuseError {
@@ -1993,8 +1994,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
19931994 |err| APIError::APIMisuseError {
19941995 err: format!("Failed to get change script as new destination script, {:?}", err),
19951996 })?;
1996- add_funding_change_output(change_value, change_script,
1997- &mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
1997+ let mut change_output = TxOut {
1998+ value: Amount::from_sat(change_value),
1999+ script_pubkey: change_script,
2000+ };
2001+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2002+ let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2003+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2004+ funding_outputs.push(OutputOwned::Single(change_output));
19982005 }
19992006
20002007 let constructor_args = InteractiveTxConstructorArgs {
@@ -2013,7 +2020,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20132020 .map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
20142021 let msg = tx_constructor.take_initiator_first_message();
20152022
2016- self.interactive_tx_constructor.replace (tx_constructor);
2023+ self.interactive_tx_constructor = Some (tx_constructor);
20172024
20182025 Ok(msg)
20192026 }
@@ -4440,21 +4447,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
44404447 cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
44414448}
44424449
4443- #[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4444- fn add_funding_change_output(
4445- change_value: u64, change_script: ScriptBuf,
4446- funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4447- ) {
4448- let mut change_output = TxOut {
4449- value: Amount::from_sat(change_value),
4450- script_pubkey: change_script,
4451- };
4452- let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4453- let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4454- change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4455- funding_outputs.push(OutputOwned::Single(change_output.clone()));
4456- }
4457-
44584450pub(super) fn calculate_our_funding_satoshis(
44594451 is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
44604452 total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4514,8 +4506,10 @@ pub(super) struct DualFundingChannelContext {
45144506 /// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
45154507 /// minus any fees paid for our contributed weight. This means that change will never be generated
45164508 /// and the maximum value possible will go towards funding the channel.
4509+ ///
4510+ /// Note that this field may be emptied once the interactive negotiation has been started.
45174511 #[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4518- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
4512+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
45194513}
45204514
45214515// Holder designates channel data owned for the benefit of the user client.
@@ -9194,7 +9188,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
91949188 their_funding_satoshis: None,
91959189 funding_tx_locktime,
91969190 funding_feerate_sat_per_1000_weight,
9197- our_funding_inputs: Some( funding_inputs) ,
9191+ our_funding_inputs: funding_inputs,
91989192 },
91999193 interactive_tx_constructor: None,
92009194 };
@@ -9345,7 +9339,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
93459339 their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
93469340 funding_tx_locktime: LockTime::from_consensus(msg.locktime),
93479341 funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9348- our_funding_inputs: Some( funding_inputs.clone() ),
9342+ our_funding_inputs: funding_inputs.clone(),
93499343 };
93509344
93519345 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments