Skip to content

Commit 70fde13

Browse files
committed
Minor changes post review
1 parent b7e7a34 commit 70fde13

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

lightning/src/ln/channel.rs

+17-26
Original file line numberDiff line numberDiff line change
@@ -1702,20 +1702,20 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17021702
funding_inputs.push(prev_funding_input);
17031703
}
17041704

1705-
let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
1705+
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
17061706
// Check that vouts exist for each TxIn in provided transactions.
1707-
for (idx, input) in funding_inputs.iter().enumerate() {
1708-
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
1709-
funding_inputs_prev_outputs.push(output.clone());
1707+
for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
1708+
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
1709+
funding_inputs_prev_outputs.push(output);
17101710
} else {
17111711
return Err(APIError::APIMisuseError {
17121712
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
1713-
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
1713+
tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
17141714
}
17151715
}
17161716

17171717
let total_input_satoshis: u64 = funding_inputs.iter().map(
1718-
|input| input.1.as_transaction().output.get(input.0.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
1718+
|(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
17191719
).sum();
17201720
if total_input_satoshis < self.dual_funding_context().our_funding_satoshis {
17211721
return Err(APIError::APIMisuseError {
@@ -1757,18 +1757,24 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17571757
|err| APIError::APIMisuseError {
17581758
err: format!("Failed to get change script as new destination script, {:?}", err),
17591759
})?;
1760-
add_funding_change_output(change_value, change_script,
1761-
&mut funding_outputs, self.dual_funding_context().funding_feerate_sat_per_1000_weight);
1760+
let mut change_output = TxOut {
1761+
value: Amount::from_sat(change_value),
1762+
script_pubkey: change_script,
1763+
};
1764+
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
1765+
let change_output_fee = fee_for_weight(self.dual_funding_context().funding_feerate_sat_per_1000_weight, change_output_weight);
1766+
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
1767+
funding_outputs.push(OutputOwned::Single(change_output));
17621768
}
17631769

17641770
let constructor_args = InteractiveTxConstructorArgs {
17651771
entropy_source,
17661772
holder_node_id,
17671773
counterparty_node_id: self.context().counterparty_node_id,
17681774
channel_id: self.context().channel_id(),
1769-
feerate_sat_per_kw: self.dual_funding_context_mut().funding_feerate_sat_per_1000_weight,
1775+
feerate_sat_per_kw: self.dual_funding_context().funding_feerate_sat_per_1000_weight,
17701776
is_initiator: self.is_initiator(),
1771-
funding_tx_locktime: self.dual_funding_context_mut().funding_tx_locktime,
1777+
funding_tx_locktime: self.dual_funding_context().funding_tx_locktime,
17721778
inputs_to_contribute: funding_inputs,
17731779
outputs_to_contribute: funding_outputs,
17741780
expected_remote_shared_funding_output,
@@ -1777,7 +1783,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17771783
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
17781784
let msg = tx_constructor.take_initiator_first_message();
17791785

1780-
self.interactive_tx_constructor_mut().replace(tx_constructor);
1786+
*self.interactive_tx_constructor_mut() = Some(tx_constructor);
17811787

17821788
Ok(msg)
17831789
}
@@ -4264,21 +4270,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
42644270
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
42654271
}
42664272

4267-
#[allow(dead_code)] // TODO(dual_funding): Remove once begin_interactive_funding_tx_construction() is used
4268-
fn add_funding_change_output(
4269-
change_value: u64, change_script: ScriptBuf,
4270-
funding_outputs: &mut Vec<OutputOwned>, funding_feerate_sat_per_1000_weight: u32,
4271-
) {
4272-
let mut change_output = TxOut {
4273-
value: Amount::from_sat(change_value),
4274-
script_pubkey: change_script,
4275-
};
4276-
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
4277-
let change_output_fee = fee_for_weight(funding_feerate_sat_per_1000_weight, change_output_weight);
4278-
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
4279-
funding_outputs.push(OutputOwned::Single(change_output.clone()));
4280-
}
4281-
42824273
pub(super) fn calculate_our_funding_satoshis(
42834274
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
42844275
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,

lightning/src/ln/interactivetxs.rs

+5-3
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)