Skip to content

Commit d5646d6

Browse files
committed
Minor changes post review
1 parent c0236df commit d5646d6

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,26 +2219,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22192219
) -> Result<Option<InteractiveTxMessageSend>, APIError>
22202220
where ES::Target: EntropySource
22212221
{
2222-
let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
2222+
let mut funding_inputs = Vec::new();
2223+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
22232224

22242225
if let Some(prev_funding_input) = prev_funding_input {
22252226
funding_inputs.push(prev_funding_input);
22262227
}
22272228

2228-
let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
2229+
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
22292230
// Check that vouts exist for each TxIn in provided transactions.
2230-
for (idx, input) in funding_inputs.iter().enumerate() {
2231-
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
2232-
funding_inputs_prev_outputs.push(output.clone());
2231+
for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
2232+
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
2233+
funding_inputs_prev_outputs.push(output);
22332234
} else {
22342235
return Err(APIError::APIMisuseError {
22352236
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2236-
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
2237+
tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
22372238
}
22382239
}
22392240

22402241
let total_input_satoshis: u64 = funding_inputs.iter().map(
2241-
|input| input.1.as_transaction().output.get(input.0.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2242+
|(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
22422243
).sum();
22432244
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
22442245
return Err(APIError::APIMisuseError {
@@ -2280,8 +2281,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22802281
|err| APIError::APIMisuseError {
22812282
err: format!("Failed to get change script as new destination script, {:?}", err),
22822283
})?;
2283-
add_funding_change_output(change_value, change_script,
2284-
&mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
2284+
let mut change_output = TxOut {
2285+
value: Amount::from_sat(change_value),
2286+
script_pubkey: change_script,
2287+
};
2288+
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2289+
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2290+
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2291+
funding_outputs.push(OutputOwned::Single(change_output));
22852292
}
22862293

22872294
let constructor_args = InteractiveTxConstructorArgs {
@@ -2300,7 +2307,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23002307
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
23012308
let msg = tx_constructor.take_initiator_first_message();
23022309

2303-
self.interactive_tx_constructor.replace(tx_constructor);
2310+
self.interactive_tx_constructor = Some(tx_constructor);
23042311

23052312
Ok(msg)
23062313
}
@@ -4730,21 +4737,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
47304737
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
47314738
}
47324739

4733-
#[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4734-
fn add_funding_change_output(
4735-
change_value: u64, change_script: ScriptBuf,
4736-
funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4737-
) {
4738-
let mut change_output = TxOut {
4739-
value: Amount::from_sat(change_value),
4740-
script_pubkey: change_script,
4741-
};
4742-
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4743-
let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4744-
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4745-
funding_outputs.push(OutputOwned::Single(change_output.clone()));
4746-
}
4747-
47484740
/// Estimate our part of the fee of the new funding transaction.
47494741
/// input_count: Number of contributed inputs.
47504742
/// witness_weight: The witness weight for contributed inputs.
@@ -4792,8 +4784,10 @@ pub(super) struct DualFundingChannelContext {
47924784
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
47934785
/// minus any fees paid for our contributed weight. This means that change will never be generated
47944786
/// and the maximum value possible will go towards funding the channel.
4787+
///
4788+
/// Note that this field may be emptied once the interactive negotiation has been started.
47954789
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4796-
pub our_funding_inputs: Option<Vec<(TxIn, TransactionU16LenLimited)>>,
4790+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
47974791
}
47984792

47994793
// Holder designates channel data owned for the benefit of the user client.
@@ -9721,7 +9715,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
97219715
their_funding_satoshis: None,
97229716
funding_tx_locktime,
97239717
funding_feerate_sat_per_1000_weight,
9724-
our_funding_inputs: Some(funding_inputs),
9718+
our_funding_inputs: funding_inputs,
97259719
},
97269720
interactive_tx_constructor: None,
97279721
interactive_tx_signing_session: None,
@@ -9867,7 +9861,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
98679861
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
98689862
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
98699863
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9870-
our_funding_inputs: Some(our_funding_inputs.clone()),
9864+
our_funding_inputs: our_funding_inputs.clone(),
98719865
};
98729866

98739867
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
@@ -1671,7 +1671,7 @@ impl InteractiveTxConstructor {
16711671
/// or None if a change is not needed/possible.
16721672
#[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
16731673
pub(super) fn calculate_change_output_value(
1674-
is_initiator: bool, our_contribution: u64, funding_inputs_prev_outputs: &Vec<TxOut>,
1674+
is_initiator: bool, our_contribution: u64, funding_inputs_prev_outputs: &Vec<&TxOut>,
16751675
funding_outputs: &Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
16761676
holder_dust_limit_satoshis: u64,
16771677
) -> Option<u64> {
@@ -2642,10 +2642,11 @@ mod tests {
26422642

26432643
#[test]
26442644
fn test_calculate_change_output_value_open() {
2645-
let input_prevouts = vec![
2645+
let input_prevouts_owned = vec![
26462646
TxOut { value: Amount::from_sat(70_000), script_pubkey: ScriptBuf::new() },
26472647
TxOut { value: Amount::from_sat(60_000), script_pubkey: ScriptBuf::new() },
26482648
];
2649+
let input_prevouts: Vec<&TxOut> = input_prevouts_owned.iter().collect();
26492650
let our_contributed = 110_000;
26502651
let txout = TxOut { value: Amount::from_sat(128_000), script_pubkey: ScriptBuf::new() };
26512652
let outputs = vec![OutputOwned::SharedControlFullyOwned(txout)];
@@ -2731,10 +2732,11 @@ mod tests {
27312732

27322733
#[test]
27332734
fn test_calculate_change_output_value_splice() {
2734-
let input_prevouts = vec![
2735+
let input_prevouts_owned = vec![
27352736
TxOut { value: Amount::from_sat(70_000), script_pubkey: ScriptBuf::new() },
27362737
TxOut { value: Amount::from_sat(60_000), script_pubkey: ScriptBuf::new() },
27372738
];
2739+
let input_prevouts: Vec<&TxOut> = input_prevouts_owned.iter().collect();
27382740
let our_contributed = 110_000;
27392741
let txout = TxOut { value: Amount::from_sat(148_000), script_pubkey: ScriptBuf::new() };
27402742
let outputs = vec![OutputOwned::Shared(SharedOwnedOutput::new(txout, our_contributed))];

0 commit comments

Comments
 (0)