@@ -2239,26 +2239,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2239
2239
) -> Result<Option<InteractiveTxMessageSend>, APIError>
2240
2240
where ES::Target: EntropySource
2241
2241
{
2242
- let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
2242
+ let mut funding_inputs = Vec::new();
2243
+ mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
2243
2244
2244
2245
if let Some(prev_funding_input) = prev_funding_input {
2245
2246
funding_inputs.push(prev_funding_input);
2246
2247
}
2247
2248
2248
- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
2249
+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
2249
2250
// Check that vouts exist for each TxIn in provided transactions.
2250
- for (idx, input ) in funding_inputs.iter().enumerate() {
2251
- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
2252
- funding_inputs_prev_outputs.push(output.clone() );
2251
+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
2252
+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
2253
+ funding_inputs_prev_outputs.push(output);
2253
2254
} else {
2254
2255
return Err(APIError::APIMisuseError {
2255
2256
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2256
- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
2257
+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
2257
2258
}
2258
2259
}
2259
2260
2260
2261
let total_input_satoshis: u64 = funding_inputs.iter().map(
2261
- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2262
+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2262
2263
).sum();
2263
2264
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
2264
2265
return Err(APIError::APIMisuseError {
@@ -2300,8 +2301,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2300
2301
|err| APIError::APIMisuseError {
2301
2302
err: format!("Failed to get change script as new destination script, {:?}", err),
2302
2303
})?;
2303
- add_funding_change_output(change_value, change_script,
2304
- &mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
2304
+ let mut change_output = TxOut {
2305
+ value: Amount::from_sat(change_value),
2306
+ script_pubkey: change_script,
2307
+ };
2308
+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2309
+ let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2310
+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2311
+ funding_outputs.push(OutputOwned::Single(change_output));
2305
2312
}
2306
2313
2307
2314
let constructor_args = InteractiveTxConstructorArgs {
@@ -2320,7 +2327,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2320
2327
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
2321
2328
let msg = tx_constructor.take_initiator_first_message();
2322
2329
2323
- self.interactive_tx_constructor.replace (tx_constructor);
2330
+ self.interactive_tx_constructor = Some (tx_constructor);
2324
2331
2325
2332
Ok(msg)
2326
2333
}
@@ -5037,8 +5044,10 @@ pub(super) struct DualFundingChannelContext {
5037
5044
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
5038
5045
/// minus any fees paid for our contributed weight. This means that change will never be generated
5039
5046
/// and the maximum value possible will go towards funding the channel.
5047
+ ///
5048
+ /// Note that this field may be emptied once the interactive negotiation has been started.
5040
5049
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
5041
- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
5050
+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5042
5051
}
5043
5052
5044
5053
// Holder designates channel data owned for the benefit of the user client.
@@ -10029,7 +10038,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10029
10038
their_funding_satoshis: None,
10030
10039
funding_tx_locktime,
10031
10040
funding_feerate_sat_per_1000_weight,
10032
- our_funding_inputs: Some( funding_inputs) ,
10041
+ our_funding_inputs: funding_inputs,
10033
10042
},
10034
10043
interactive_tx_constructor: None,
10035
10044
interactive_tx_signing_session: None,
@@ -10175,7 +10184,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10175
10184
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
10176
10185
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
10177
10186
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
10178
- our_funding_inputs: Some( our_funding_inputs.clone() ),
10187
+ our_funding_inputs: our_funding_inputs.clone(),
10179
10188
};
10180
10189
10181
10190
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments