Skip to content

Commit 03587f6

Browse files
committed
Minor changes post review
1 parent a5c46a0 commit 03587f6

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
@@ -2039,26 +2039,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20392039
) -> Result<Option<InteractiveTxMessageSend>, APIError>
20402040
where ES::Target: EntropySource
20412041
{
2042-
let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
2042+
let mut funding_inputs = Vec::new();
2043+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
20432044

20442045
if let Some(prev_funding_input) = prev_funding_input {
20452046
funding_inputs.push(prev_funding_input);
20462047
}
20472048

2048-
let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
2049+
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
20492050
// Check that vouts exist for each TxIn in provided transactions.
2050-
for (idx, input) in funding_inputs.iter().enumerate() {
2051-
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
2052-
funding_inputs_prev_outputs.push(output.clone());
2051+
for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
2052+
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
2053+
funding_inputs_prev_outputs.push(output);
20532054
} else {
20542055
return Err(APIError::APIMisuseError {
20552056
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2056-
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
2057+
tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
20572058
}
20582059
}
20592060

20602061
let total_input_satoshis: u64 = funding_inputs.iter().map(
2061-
|input| input.1.as_transaction().output.get(input.0.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2062+
|(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
20622063
).sum();
20632064
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
20642065
return Err(APIError::APIMisuseError {
@@ -2100,8 +2101,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21002101
|err| APIError::APIMisuseError {
21012102
err: format!("Failed to get change script as new destination script, {:?}", err),
21022103
})?;
2103-
add_funding_change_output(change_value, change_script,
2104-
&mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
2104+
let mut change_output = TxOut {
2105+
value: Amount::from_sat(change_value),
2106+
script_pubkey: change_script,
2107+
};
2108+
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2109+
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2110+
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2111+
funding_outputs.push(OutputOwned::Single(change_output));
21052112
}
21062113

21072114
let constructor_args = InteractiveTxConstructorArgs {
@@ -2120,7 +2127,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21202127
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
21212128
let msg = tx_constructor.take_initiator_first_message();
21222129

2123-
self.interactive_tx_constructor.replace(tx_constructor);
2130+
self.interactive_tx_constructor = Some(tx_constructor);
21242131

21252132
Ok(msg)
21262133
}
@@ -4563,21 +4570,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
45634570
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
45644571
}
45654572

4566-
#[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4567-
fn add_funding_change_output(
4568-
change_value: u64, change_script: ScriptBuf,
4569-
funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4570-
) {
4571-
let mut change_output = TxOut {
4572-
value: Amount::from_sat(change_value),
4573-
script_pubkey: change_script,
4574-
};
4575-
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4576-
let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4577-
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4578-
funding_outputs.push(OutputOwned::Single(change_output.clone()));
4579-
}
4580-
45814573
pub(super) fn calculate_our_funding_satoshis(
45824574
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
45834575
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4637,8 +4629,10 @@ pub(super) struct DualFundingChannelContext {
46374629
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
46384630
/// minus any fees paid for our contributed weight. This means that change will never be generated
46394631
/// and the maximum value possible will go towards funding the channel.
4632+
///
4633+
/// Note that this field may be emptied once the interactive negotiation has been started.
46404634
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4641-
pub our_funding_inputs: Option<Vec<(TxIn, TransactionU16LenLimited)>>,
4635+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
46424636
}
46434637

46444638
// Holder designates channel data owned for the benefit of the user client.
@@ -9282,7 +9276,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
92829276
their_funding_satoshis: None,
92839277
funding_tx_locktime,
92849278
funding_feerate_sat_per_1000_weight,
9285-
our_funding_inputs: Some(funding_inputs),
9279+
our_funding_inputs: funding_inputs,
92869280
},
92879281
interactive_tx_constructor: None,
92889282
};
@@ -9433,7 +9427,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
94339427
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
94349428
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
94359429
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9436-
our_funding_inputs: Some(funding_inputs.clone()),
9430+
our_funding_inputs: funding_inputs.clone(),
94379431
};
94389432

94399433
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)