Skip to content

Commit 232f793

Browse files
committed
Minor changes post review
1 parent 710f381 commit 232f793

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
@@ -2247,26 +2247,27 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22472247
) -> Result<Option<InteractiveTxMessageSend>, APIError>
22482248
where ES::Target: EntropySource
22492249
{
2250-
let mut funding_inputs = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
2250+
let mut funding_inputs = Vec::new();
2251+
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
22512252

22522253
if let Some(prev_funding_input) = prev_funding_input {
22532254
funding_inputs.push(prev_funding_input);
22542255
}
22552256

2256-
let mut funding_inputs_prev_outputs: Vec<TxOut> = Vec::with_capacity(funding_inputs.len());
2257+
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
22572258
// Check that vouts exist for each TxIn in provided transactions.
2258-
for (idx, input) in funding_inputs.iter().enumerate() {
2259-
if let Some(output) = input.1.as_transaction().output.get(input.0.previous_output.vout as usize) {
2260-
funding_inputs_prev_outputs.push(output.clone());
2259+
for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
2260+
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
2261+
funding_inputs_prev_outputs.push(output);
22612262
} else {
22622263
return Err(APIError::APIMisuseError {
22632264
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2264-
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
2265+
tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
22652266
}
22662267
}
22672268

22682269
let total_input_satoshis: u64 = funding_inputs.iter().map(
2269-
|input| input.1.as_transaction().output.get(input.0.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2270+
|(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
22702271
).sum();
22712272
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
22722273
return Err(APIError::APIMisuseError {
@@ -2308,8 +2309,14 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23082309
|err| APIError::APIMisuseError {
23092310
err: format!("Failed to get change script as new destination script, {:?}", err),
23102311
})?;
2311-
add_funding_change_output(change_value, change_script,
2312-
&mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
2312+
let mut change_output = TxOut {
2313+
value: Amount::from_sat(change_value),
2314+
script_pubkey: change_script,
2315+
};
2316+
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2317+
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2318+
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2319+
funding_outputs.push(OutputOwned::Single(change_output));
23132320
}
23142321

23152322
let constructor_args = InteractiveTxConstructorArgs {
@@ -2328,7 +2335,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23282335
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
23292336
let msg = tx_constructor.take_initiator_first_message();
23302337

2331-
self.interactive_tx_constructor.replace(tx_constructor);
2338+
self.interactive_tx_constructor = Some(tx_constructor);
23322339

23332340
Ok(msg)
23342341
}
@@ -4900,8 +4907,10 @@ pub(super) struct DualFundingChannelContext {
49004907
/// Note that the `our_funding_satoshis` field is equal to the total value of `our_funding_inputs`
49014908
/// minus any fees paid for our contributed weight. This means that change will never be generated
49024909
/// and the maximum value possible will go towards funding the channel.
4910+
///
4911+
/// Note that this field may be emptied once the interactive negotiation has been started.
49034912
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4904-
pub our_funding_inputs: Option<Vec<(TxIn, TransactionU16LenLimited)>>,
4913+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
49054914
}
49064915

49074916
// Holder designates channel data owned for the benefit of the user client.
@@ -9990,7 +9999,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
99909999
their_funding_satoshis: None,
999110000
funding_tx_locktime,
999210001
funding_feerate_sat_per_1000_weight,
9993-
our_funding_inputs: Some(funding_inputs),
10002+
our_funding_inputs: funding_inputs,
999410003
},
999510004
interactive_tx_constructor: None,
999610005
interactive_tx_signing_session: None,
@@ -10136,7 +10145,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1013610145
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
1013710146
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
1013810147
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
10139-
our_funding_inputs: Some(our_funding_inputs.clone()),
10148+
our_funding_inputs: our_funding_inputs.clone(),
1014010149
};
1014110150

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