@@ -2136,26 +2136,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2136
2136
) -> Result<Option<InteractiveTxMessageSend>, APIError>
2137
2137
where ES::Target: EntropySource
2138
2138
{
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);
2140
2141
2141
2142
if let Some(prev_funding_input) = prev_funding_input {
2142
2143
funding_inputs.push(prev_funding_input);
2143
2144
}
2144
2145
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());
2146
2147
// 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);
2150
2151
} else {
2151
2152
return Err(APIError::APIMisuseError {
2152
2153
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) });
2154
2155
}
2155
2156
}
2156
2157
2157
2158
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)
2159
2160
).sum();
2160
2161
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
2161
2162
return Err(APIError::APIMisuseError {
@@ -2197,8 +2198,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2197
2198
|err| APIError::APIMisuseError {
2198
2199
err: format!("Failed to get change script as new destination script, {:?}", err),
2199
2200
})?;
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));
2202
2209
}
2203
2210
2204
2211
let constructor_args = InteractiveTxConstructorArgs {
@@ -2217,7 +2224,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2217
2224
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
2218
2225
let msg = tx_constructor.take_initiator_first_message();
2219
2226
2220
- self.interactive_tx_constructor.replace (tx_constructor);
2227
+ self.interactive_tx_constructor = Some (tx_constructor);
2221
2228
2222
2229
Ok(msg)
2223
2230
}
@@ -4757,8 +4764,10 @@ pub(super) struct DualFundingChannelContext {
4757
4764
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
4758
4765
/// minus any fees paid for our contributed weight. This means that change will never be generated
4759
4766
/// 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.
4760
4769
#[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)>,
4762
4771
}
4763
4772
4764
4773
// 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 {
9687
9696
their_funding_satoshis: None,
9688
9697
funding_tx_locktime,
9689
9698
funding_feerate_sat_per_1000_weight,
9690
- our_funding_inputs: Some( funding_inputs) ,
9699
+ our_funding_inputs: funding_inputs,
9691
9700
},
9692
9701
interactive_tx_constructor: None,
9693
9702
};
@@ -9834,7 +9843,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9834
9843
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
9835
9844
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9836
9845
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(),
9838
9847
};
9839
9848
9840
9849
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments