@@ -1706,26 +1706,27 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
1706
1706
) -> Result<Option<InteractiveTxMessageSend>, APIError>
1707
1707
where ES::Target: EntropySource
1708
1708
{
1709
- let mut funding_inputs = self.dual_funding_context_mut().our_funding_inputs.take().unwrap_or_else(|| vec![]);
1709
+ let mut funding_inputs = Vec::new();
1710
+ mem::swap(&mut self.dual_funding_context_mut().our_funding_inputs, &mut funding_inputs);
1710
1711
1711
1712
if let Some(prev_funding_input) = prev_funding_input {
1712
1713
funding_inputs.push(prev_funding_input);
1713
1714
}
1714
1715
1715
- let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
1716
+ let mut funding_inputs_prev_outputs: Vec<& TxOut> = Vec::with_capacity(funding_inputs.len());
1716
1717
// Check that vouts exist for each TxIn in provided transactions.
1717
- for (idx, input ) in funding_inputs.iter().enumerate() {
1718
- if let Some(output) = input.1. as_transaction().output.get(input.0 .previous_output.vout as usize) {
1719
- funding_inputs_prev_outputs.push(output.clone() );
1718
+ for (idx, (txin, tx) ) in funding_inputs.iter().enumerate() {
1719
+ if let Some(output) = tx. as_transaction().output.get(txin .previous_output.vout as usize) {
1720
+ funding_inputs_prev_outputs.push(output);
1720
1721
} else {
1721
1722
return Err(APIError::APIMisuseError {
1722
1723
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
1723
- input.1. as_transaction().compute_txid(), input.0 .previous_output.vout, idx) });
1724
+ tx. as_transaction().compute_txid(), txin .previous_output.vout, idx) });
1724
1725
}
1725
1726
}
1726
1727
1727
1728
let total_input_satoshis: u64 = funding_inputs.iter().map(
1728
- |input| input.1. as_transaction().output.get(input.0 .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1729
+ |(txin, tx)| tx. as_transaction().output.get(txin .previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1729
1730
).sum();
1730
1731
if total_input_satoshis < self.dual_funding_context().our_funding_satoshis {
1731
1732
return Err(APIError::APIMisuseError {
@@ -1767,18 +1768,24 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
1767
1768
|err| APIError::APIMisuseError {
1768
1769
err: format!("Failed to get change script as new destination script, {:?}", err),
1769
1770
})?;
1770
- add_funding_change_output(change_value, change_script,
1771
- &mut funding_outputs, self.dual_funding_context().funding_feerate_sat_per_1000_weight);
1771
+ let mut change_output = TxOut {
1772
+ value: Amount::from_sat(change_value),
1773
+ script_pubkey: change_script,
1774
+ };
1775
+ let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
1776
+ let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
1777
+ change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
1778
+ funding_outputs.push(OutputOwned::Single(change_output));
1772
1779
}
1773
1780
1774
1781
let constructor_args = InteractiveTxConstructorArgs {
1775
1782
entropy_source,
1776
1783
holder_node_id,
1777
1784
counterparty_node_id: self.context().counterparty_node_id,
1778
1785
channel_id: self.context().channel_id(),
1779
- feerate_sat_per_kw: self.dual_funding_context_mut ().funding_feerate_sat_per_1000_weight,
1786
+ feerate_sat_per_kw: self.dual_funding_context ().funding_feerate_sat_per_1000_weight,
1780
1787
is_initiator: self.is_initiator(),
1781
- funding_tx_locktime: self.dual_funding_context_mut ().funding_tx_locktime,
1788
+ funding_tx_locktime: self.dual_funding_context ().funding_tx_locktime,
1782
1789
inputs_to_contribute: funding_inputs,
1783
1790
outputs_to_contribute: funding_outputs,
1784
1791
expected_remote_shared_funding_output,
@@ -1787,7 +1794,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
1787
1794
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
1788
1795
let msg = tx_constructor.take_initiator_first_message();
1789
1796
1790
- self.interactive_tx_constructor_mut().replace (tx_constructor);
1797
+ * self.interactive_tx_constructor_mut() = Some (tx_constructor);
1791
1798
1792
1799
Ok(msg)
1793
1800
}
@@ -4267,21 +4274,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
4267
4274
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
4268
4275
}
4269
4276
4270
- #[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4271
- fn add_funding_change_output(
4272
- change_value: u64, change_script: ScriptBuf,
4273
- funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4274
- ) {
4275
- let mut change_output = TxOut {
4276
- value: Amount::from_sat(change_value),
4277
- script_pubkey: change_script,
4278
- };
4279
- let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4280
- let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4281
- change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4282
- funding_outputs.push(OutputOwned::Single(change_output.clone()));
4283
- }
4284
-
4285
4277
pub(super) fn calculate_our_funding_satoshis(
4286
4278
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
4287
4279
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4341,8 +4333,10 @@ pub(super) struct DualFundingChannelContext {
4341
4333
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
4342
4334
/// minus any fees paid for our contributed weight. This means that change will never be generated
4343
4335
/// and the maximum value possible will go towards funding the channel.
4336
+ ///
4337
+ /// Note that this field may be emptied once the interactive negotiation has been started.
4344
4338
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4345
- pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
4339
+ pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
4346
4340
}
4347
4341
4348
4342
// Holder designates channel data owned for the benefit of the user client.
@@ -9018,7 +9012,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
9018
9012
their_funding_satoshis: None,
9019
9013
funding_tx_locktime,
9020
9014
funding_feerate_sat_per_1000_weight,
9021
- our_funding_inputs: Some( funding_inputs) ,
9015
+ our_funding_inputs: funding_inputs,
9022
9016
},
9023
9017
interactive_tx_constructor: None,
9024
9018
};
@@ -9187,7 +9181,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
9187
9181
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
9188
9182
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9189
9183
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9190
- our_funding_inputs: Some( funding_inputs.clone() ),
9184
+ our_funding_inputs: funding_inputs.clone(),
9191
9185
};
9192
9186
9193
9187
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments