@@ -1932,26 +1932,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1932
1932
) -> Result<Option<InteractiveTxMessageSend>, APIError>
1933
1933
where ES::Target: EntropySource
1934
1934
{
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);
1936
1937
1937
1938
if let Some(prev_funding_input) = prev_funding_input {
1938
1939
funding_inputs.push(prev_funding_input);
1939
1940
}
1940
1941
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());
1942
1943
// 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);
1946
1947
} else {
1947
1948
return Err(APIError::APIMisuseError {
1948
1949
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) });
1950
1951
}
1951
1952
}
1952
1953
1953
1954
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)
1955
1956
).sum();
1956
1957
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
1957
1958
return Err(APIError::APIMisuseError {
@@ -1993,8 +1994,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1993
1994
|err| APIError::APIMisuseError {
1994
1995
err: format!("Failed to get change script as new destination script, {:?}", err),
1995
1996
})?;
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));
1998
2005
}
1999
2006
2000
2007
let constructor_args = InteractiveTxConstructorArgs {
@@ -2013,7 +2020,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2013
2020
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
2014
2021
let msg = tx_constructor.take_initiator_first_message();
2015
2022
2016
- self.interactive_tx_constructor.replace (tx_constructor);
2023
+ self.interactive_tx_constructor = Some (tx_constructor);
2017
2024
2018
2025
Ok(msg)
2019
2026
}
@@ -4440,21 +4447,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
4440
4447
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
4441
4448
}
4442
4449
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
-
4458
4450
pub(super) fn calculate_our_funding_satoshis(
4459
4451
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
4460
4452
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4514,8 +4506,10 @@ pub(super) struct DualFundingChannelContext {
4514
4506
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
4515
4507
/// minus any fees paid for our contributed weight. This means that change will never be generated
4516
4508
/// 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.
4517
4511
#[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)>,
4519
4513
}
4520
4514
4521
4515
// 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 {
9194
9188
their_funding_satoshis: None,
9195
9189
funding_tx_locktime,
9196
9190
funding_feerate_sat_per_1000_weight,
9197
- our_funding_inputs: Some( funding_inputs) ,
9191
+ our_funding_inputs: funding_inputs,
9198
9192
},
9199
9193
interactive_tx_constructor: None,
9200
9194
};
@@ -9345,7 +9339,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9345
9339
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
9346
9340
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9347
9341
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(),
9349
9343
};
9350
9344
9351
9345
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments