@@ -2136,26 +2136,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21362136 ) -> Result<Option<InteractiveTxMessageSend>, APIError>
21372137 where ES::Target: EntropySource
21382138 {
2139- let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
2139+ let mut funding_inputs = Vec::new();
2140+ mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
21402141
21412142 if let Some(prev_funding_input) = prev_funding_input {
21422143 funding_inputs.push(prev_funding_input);
21432144 }
21442145
2145- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
2146+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
21462147 // Check that vouts exist for each TxIn in provided transactions.
2147- for (idx, input ) in funding_inputs.iter().enumerate() {
2148- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
2149- funding_inputs_prev_outputs.push(output.clone() );
2148+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
2149+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
2150+ funding_inputs_prev_outputs.push(output);
21502151 } else {
21512152 return Err(APIError::APIMisuseError {
21522153 err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2153- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
2154+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
21542155 }
21552156 }
21562157
21572158 let total_input_satoshis: u64 = funding_inputs.iter().map(
2158- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2159+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
21592160 ).sum();
21602161 if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
21612162 return Err(APIError::APIMisuseError {
@@ -2197,8 +2198,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21972198 |err| APIError::APIMisuseError {
21982199 err: format!("Failed to get change script as new destination script, {:?}", err),
21992200 })?;
2200- add_funding_change_output(change_value, change_script,
2201- &mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
2201+ let mut change_output = TxOut {
2202+ value: Amount::from_sat(change_value),
2203+ script_pubkey: change_script,
2204+ };
2205+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2206+ let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2207+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2208+ funding_outputs.push(OutputOwned::Single(change_output));
22022209 }
22032210
22042211 let constructor_args = InteractiveTxConstructorArgs {
@@ -2217,7 +2224,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22172224 .map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
22182225 let msg = tx_constructor.take_initiator_first_message();
22192226
2220- self.interactive_tx_constructor.replace (tx_constructor);
2227+ self.interactive_tx_constructor = Some (tx_constructor);
22212228
22222229 Ok(msg)
22232230 }
@@ -4757,8 +4764,10 @@ pub(super) struct DualFundingChannelContext {
47574764 /// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
47584765 /// minus any fees paid for our contributed weight. This means that change will never be generated
47594766 /// and the maximum value possible will go towards funding the channel.
4767+ ///
4768+ /// Note that this field may be emptied once the interactive negotiation has been started.
47604769 #[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4761- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
4770+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
47624771}
47634772
47644773// Holder designates channel data owned for the benefit of the user client.
@@ -9687,7 +9696,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
96879696 their_funding_satoshis: None,
96889697 funding_tx_locktime,
96899698 funding_feerate_sat_per_1000_weight,
9690- our_funding_inputs: Some( funding_inputs) ,
9699+ our_funding_inputs: funding_inputs,
96919700 },
96929701 interactive_tx_constructor: None,
96939702 };
@@ -9834,7 +9843,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
98349843 their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
98359844 funding_tx_locktime: LockTime::from_consensus(msg.locktime),
98369845 funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9837- our_funding_inputs: Some( our_funding_inputs.clone() ),
9846+ our_funding_inputs: our_funding_inputs.clone(),
98389847 };
98399848
98409849 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments