@@ -1696,26 +1696,27 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
16961696 ) -> Result<Option<InteractiveTxMessageSend>, APIError>
16971697 where ES::Target: EntropySource
16981698 {
1699- let mut funding_inputs = self.dual_funding_context_mut().our_funding_inputs.take().unwrap_or_else(|| vec![]);
1699+ let mut funding_inputs = Vec::new();
1700+ mem::swap(&mut self.dual_funding_context_mut().our_funding_inputs, &mut funding_inputs);
17001701
17011702 if let Some(prev_funding_input) = prev_funding_input {
17021703 funding_inputs.push(prev_funding_input);
17031704 }
17041705
1705- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
1706+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
17061707 // Check that vouts exist for each TxIn in provided transactions.
1707- for (idx, input ) in funding_inputs.iter().enumerate() {
1708- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
1709- funding_inputs_prev_outputs.push(output.clone() );
1708+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
1709+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
1710+ funding_inputs_prev_outputs.push(output);
17101711 } else {
17111712 return Err(APIError::APIMisuseError {
17121713 err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
1713- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
1714+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
17141715 }
17151716 }
17161717
17171718 let total_input_satoshis: u64 = funding_inputs.iter().map(
1718- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1719+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
17191720 ).sum();
17201721 if total_input_satoshis < self.dual_funding_context().our_funding_satoshis {
17211722 return Err(APIError::APIMisuseError {
@@ -1757,18 +1758,24 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17571758 |err| APIError::APIMisuseError {
17581759 err: format!("Failed to get change script as new destination script, {:?}", err),
17591760 })?;
1760- add_funding_change_output(change_value, change_script,
1761- &mut funding_outputs, self.dual_funding_context().funding_feerate_sat_per_1000_weight);
1761+ let mut change_output = TxOut {
1762+ value: Amount::from_sat(change_value),
1763+ script_pubkey: change_script,
1764+ };
1765+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
1766+ let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
1767+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
1768+ funding_outputs.push(OutputOwned::Single(change_output));
17621769 }
17631770
17641771 let constructor_args = InteractiveTxConstructorArgs {
17651772 entropy_source,
17661773 holder_node_id,
17671774 counterparty_node_id: self.context().counterparty_node_id,
17681775 channel_id: self.context().channel_id(),
1769- feerate_sat_per_kw: self.dual_funding_context_mut ().funding_feerate_sat_per_1000_weight,
1776+ feerate_sat_per_kw: self.dual_funding_context ().funding_feerate_sat_per_1000_weight,
17701777 is_initiator: self.is_initiator(),
1771- funding_tx_locktime: self.dual_funding_context_mut ().funding_tx_locktime,
1778+ funding_tx_locktime: self.dual_funding_context ().funding_tx_locktime,
17721779 inputs_to_contribute: funding_inputs,
17731780 outputs_to_contribute: funding_outputs,
17741781 expected_remote_shared_funding_output,
@@ -1777,7 +1784,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17771784 .map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
17781785 let msg = tx_constructor.take_initiator_first_message();
17791786
1780- self.interactive_tx_constructor_mut().replace (tx_constructor);
1787+ * self.interactive_tx_constructor_mut() = Some (tx_constructor);
17811788
17821789 Ok(msg)
17831790 }
@@ -4264,21 +4271,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
42644271 cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
42654272}
42664273
4267- #[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4268- fn add_funding_change_output(
4269- change_value: u64, change_script: ScriptBuf,
4270- funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4271- ) {
4272- let mut change_output = TxOut {
4273- value: Amount::from_sat(change_value),
4274- script_pubkey: change_script,
4275- };
4276- let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4277- let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4278- change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4279- funding_outputs.push(OutputOwned::Single(change_output.clone()));
4280- }
4281-
42824274pub(super) fn calculate_our_funding_satoshis(
42834275 is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
42844276 total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4337,8 +4329,10 @@ pub(super) struct DualFundingChannelContext {
43374329 /// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
43384330 /// minus any fees paid for our contributed weight. This means that change will never be generated
43394331 /// and the maximum value possible will go towards funding the channel.
4332+ ///
4333+ /// Note that this field may be emptied once the interactive negotiation has been started.
43404334 #[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4341- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
4335+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
43424336}
43434337
43444338// Holder designates channel data owned for the benefit of the user client.
@@ -8920,7 +8914,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
89208914 their_funding_satoshis: None,
89218915 funding_tx_locktime,
89228916 funding_feerate_sat_per_1000_weight,
8923- our_funding_inputs: Some( funding_inputs) ,
8917+ our_funding_inputs: funding_inputs,
89248918 },
89258919 interactive_tx_constructor: None,
89268920 };
@@ -9084,7 +9078,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
90849078 their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
90859079 funding_tx_locktime: LockTime::from_consensus(msg.locktime),
90869080 funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9087- our_funding_inputs: Some( funding_inputs.clone() ),
9081+ our_funding_inputs: funding_inputs.clone(),
90889082 };
90899083
90909084 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments