Skip to content

Commit 3a5c8b3

Browse files
committed
Minor changes post review
1 parent 09a0b98 commit 3a5c8b3

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,26 +2136,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21362136
) -> Result<Option<InteractiveTxMessageSend>, APIError>
21372137
where ES::Target: EntropySource
21382138
{
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);
21402141

21412142
if let Some(prev_funding_input) = prev_funding_input {
21422143
funding_inputs.push(prev_funding_input);
21432144
}
21442145

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());
21462147
// 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);
21502151
} else {
21512152
return Err(APIError::APIMisuseError {
21522153
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) });
21542155
}
21552156
}
21562157

21572158
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)
21592160
).sum();
21602161
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
21612162
return Err(APIError::APIMisuseError {
@@ -2197,8 +2198,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21972198
|err| APIError::APIMisuseError {
21982199
err: format!("Failed to get change script as new destination script, {:?}", err),
21992200
})?;
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));
22022209
}
22032210

22042211
let constructor_args = InteractiveTxConstructorArgs {
@@ -2217,7 +2224,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22172224
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
22182225
let msg = tx_constructor.take_initiator_first_message();
22192226

2220-
self.interactive_tx_constructor.replace(tx_constructor);
2227+
self.interactive_tx_constructor = Some(tx_constructor);
22212228

22222229
Ok(msg)
22232230
}
@@ -4757,8 +4764,10 @@ pub(super) struct DualFundingChannelContext {
47574764
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
47584765
/// minus any fees paid for our contributed weight. This means that change will never be generated
47594766
/// 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.
47604769
#[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)>,
47624771
}
47634772

47644773
// 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 {
96879696
their_funding_satoshis: None,
96889697
funding_tx_locktime,
96899698
funding_feerate_sat_per_1000_weight,
9690-
our_funding_inputs: Some(funding_inputs),
9699+
our_funding_inputs: funding_inputs,
96919700
},
96929701
interactive_tx_constructor: None,
96939702
};
@@ -9834,7 +9843,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
98349843
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
98359844
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
98369845
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(),
98389847
};
98399848

98409849
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(

lightning/src/ln/interactivetxs.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ impl InteractiveTxConstructor {
16691669
/// or None if a change is not needed/possible.
16701670
#[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
16711671
pub(super) fn calculate_change_output_value(
1672-
is_initiator: bool, our_contribution: u64, funding_inputs_prev_outputs: &Vec<TxOut>,
1672+
is_initiator: bool, our_contribution: u64, funding_inputs_prev_outputs: &Vec<&TxOut>,
16731673
funding_outputs: &Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
16741674
holder_dust_limit_satoshis: u64,
16751675
) -> Option<u64> {
@@ -2640,10 +2640,11 @@ mod tests {
26402640

26412641
#[test]
26422642
fn test_calculate_change_output_value_open() {
2643-
let input_prevouts = vec![
2643+
let input_prevouts_owned = vec![
26442644
TxOut { value: Amount::from_sat(70_000), script_pubkey: ScriptBuf::new() },
26452645
TxOut { value: Amount::from_sat(60_000), script_pubkey: ScriptBuf::new() },
26462646
];
2647+
let input_prevouts: Vec<&TxOut> = input_prevouts_owned.iter().collect();
26472648
let our_contributed = 110_000;
26482649
let txout = TxOut { value: Amount::from_sat(128_000), script_pubkey: ScriptBuf::new() };
26492650
let outputs = vec![OutputOwned::SharedControlFullyOwned(txout)];
@@ -2729,10 +2730,11 @@ mod tests {
27292730

27302731
#[test]
27312732
fn test_calculate_change_output_value_splice() {
2732-
let input_prevouts = vec![
2733+
let input_prevouts_owned = vec![
27332734
TxOut { value: Amount::from_sat(70_000), script_pubkey: ScriptBuf::new() },
27342735
TxOut { value: Amount::from_sat(60_000), script_pubkey: ScriptBuf::new() },
27352736
];
2737+
let input_prevouts: Vec<&TxOut> = input_prevouts_owned.iter().collect();
27362738
let our_contributed = 110_000;
27372739
let txout = TxOut { value: Amount::from_sat(148_000), script_pubkey: ScriptBuf::new() };
27382740
let outputs = vec![OutputOwned::Shared(SharedOwnedOutput::new(txout, our_contributed))];

0 commit comments

Comments
 (0)