@@ -1688,26 +1688,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1688
1688
) -> Result<Option<InteractiveTxMessageSend>, APIError>
1689
1689
where ES::Target: EntropySource
1690
1690
{
1691
- let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
1691
+ let mut funding_inputs = Vec::new();
1692
+ mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
1692
1693
1693
1694
if let Some(prev_funding_input) = prev_funding_input {
1694
1695
funding_inputs.push(prev_funding_input);
1695
1696
}
1696
1697
1697
- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
1698
+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
1698
1699
// Check that vouts exist for each TxIn in provided transactions.
1699
- for (idx, input ) in funding_inputs.iter().enumerate() {
1700
- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
1701
- funding_inputs_prev_outputs.push(output.clone() );
1700
+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
1701
+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
1702
+ funding_inputs_prev_outputs.push(output);
1702
1703
} else {
1703
1704
return Err(APIError::APIMisuseError {
1704
1705
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
1705
- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
1706
+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
1706
1707
}
1707
1708
}
1708
1709
1709
1710
let total_input_satoshis: u64 = funding_inputs.iter().map(
1710
- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1711
+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1711
1712
).sum();
1712
1713
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
1713
1714
return Err(APIError::APIMisuseError {
@@ -1749,8 +1750,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1749
1750
|err| APIError::APIMisuseError {
1750
1751
err: format!("Failed to get change script as new destination script, {:?}", err),
1751
1752
})?;
1752
- add_funding_change_output(change_value, change_script,
1753
- &mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
1753
+ let mut change_output = TxOut {
1754
+ value: Amount::from_sat(change_value),
1755
+ script_pubkey: change_script,
1756
+ };
1757
+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
1758
+ let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
1759
+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
1760
+ funding_outputs.push(OutputOwned::Single(change_output));
1754
1761
}
1755
1762
1756
1763
let constructor_args = InteractiveTxConstructorArgs {
@@ -1769,7 +1776,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1769
1776
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
1770
1777
let msg = tx_constructor.take_initiator_first_message();
1771
1778
1772
- self.interactive_tx_constructor.replace (tx_constructor);
1779
+ self.interactive_tx_constructor = Some (tx_constructor);
1773
1780
1774
1781
Ok(msg)
1775
1782
}
@@ -4196,21 +4203,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
4196
4203
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
4197
4204
}
4198
4205
4199
- #[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4200
- fn add_funding_change_output(
4201
- change_value: u64, change_script: ScriptBuf,
4202
- funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4203
- ) {
4204
- let mut change_output = TxOut {
4205
- value: Amount::from_sat(change_value),
4206
- script_pubkey: change_script,
4207
- };
4208
- let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4209
- let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4210
- change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4211
- funding_outputs.push(OutputOwned::Single(change_output.clone()));
4212
- }
4213
-
4214
4206
pub(super) fn calculate_our_funding_satoshis(
4215
4207
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
4216
4208
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4270,8 +4262,10 @@ pub(super) struct DualFundingChannelContext {
4270
4262
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
4271
4263
/// minus any fees paid for our contributed weight. This means that change will never be generated
4272
4264
/// and the maximum value possible will go towards funding the channel.
4265
+ ///
4266
+ /// Note that this field may be emptied once the interactive negotiation has been started.
4273
4267
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4274
- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
4268
+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
4275
4269
}
4276
4270
4277
4271
// Holder designates channel data owned for the benefit of the user client.
@@ -8947,7 +8941,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
8947
8941
their_funding_satoshis: None,
8948
8942
funding_tx_locktime,
8949
8943
funding_feerate_sat_per_1000_weight,
8950
- our_funding_inputs: Some( funding_inputs) ,
8944
+ our_funding_inputs: funding_inputs,
8951
8945
},
8952
8946
interactive_tx_constructor: None,
8953
8947
};
@@ -9096,7 +9090,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9096
9090
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
9097
9091
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9098
9092
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9099
- our_funding_inputs: Some( funding_inputs.clone() ),
9093
+ our_funding_inputs: funding_inputs.clone(),
9100
9094
};
9101
9095
9102
9096
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments