Skip to content

Commit c28b4e5

Browse files
committed
Extract helper method in DualFundingChannelContext to extract prev outs
1 parent eae9641 commit c28b4e5

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,21 +2253,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22532253
funding_inputs.push(prev_funding_input);
22542254
}
22552255

2256-
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
2257-
// Check that vouts exist for each TxIn in provided transactions.
2258-
for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
2259-
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
2260-
funding_inputs_prev_outputs.push(output);
2261-
} else {
2262-
return Err(APIError::APIMisuseError {
2263-
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2264-
tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
2265-
}
2266-
}
2256+
let funding_inputs_prev_outputs = DualFundingChannelContext::txouts_from_input_prev_txs(&funding_inputs)
2257+
.map_err(|err| APIError::APIMisuseError { err: err.to_string() })?;
22672258

2268-
let total_input_satoshis: u64 = funding_inputs.iter().map(
2269-
|(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2270-
).sum();
2259+
let total_input_satoshis: u64 = funding_inputs_prev_outputs.iter().map(|txout| txout.value.to_sat()).sum();
22712260
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
22722261
return Err(APIError::APIMisuseError {
22732262
err: format!("Total value of funding inputs must be at least funding amount. It was {} sats",
@@ -5055,6 +5044,33 @@ pub(super) struct DualFundingChannelContext {
50555044
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
50565045
}
50575046

5047+
impl DualFundingChannelContext {
5048+
/// Obtain prev outputs for each supplied input and matching transaction.
5049+
/// Can error when there a prev tx does not have an output for the specified vout number.
5050+
/// Also checks for matching of transaction IDs.
5051+
fn txouts_from_input_prev_txs(inputs: &Vec<(TxIn, TransactionU16LenLimited)>) -> Result<Vec<&TxOut>, ChannelError> {
5052+
let mut prev_outputs: Vec<&TxOut> = Vec::with_capacity(inputs.len());
5053+
// Check that vouts exist for each TxIn in provided transactions.
5054+
for (idx, (txin, tx)) in inputs.iter().enumerate() {
5055+
let txid = tx.as_transaction().compute_txid();
5056+
if txin.previous_output.txid != txid {
5057+
return Err(ChannelError::Warn(
5058+
format!("Transaction input txid mismatch, {} vs. {}, at index {}", txin.previous_output.txid, txid, idx)
5059+
));
5060+
}
5061+
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
5062+
prev_outputs.push(output);
5063+
} else {
5064+
return Err(ChannelError::Warn(
5065+
format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn, at index {}",
5066+
txid, txin.previous_output.vout, idx)
5067+
));
5068+
}
5069+
}
5070+
Ok(prev_outputs)
5071+
}
5072+
}
5073+
50585074
// Holder designates channel data owned for the benefit of the user client.
50595075
// Counterparty designates channel data owned by the another channel participant entity.
50605076
pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
@@ -10035,17 +10051,18 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1003510051
unfunded_channel_age_ticks: 0,
1003610052
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
1003710053
};
10054+
let dual_funding_context = DualFundingChannelContext {
10055+
our_funding_satoshis: funding_satoshis,
10056+
their_funding_satoshis: None,
10057+
funding_tx_locktime,
10058+
funding_feerate_sat_per_1000_weight,
10059+
our_funding_inputs: funding_inputs,
10060+
};
1003810061
let chan = Self {
1003910062
funding,
1004010063
context,
1004110064
unfunded_context,
10042-
dual_funding_context: DualFundingChannelContext {
10043-
our_funding_satoshis: funding_satoshis,
10044-
their_funding_satoshis: None,
10045-
funding_tx_locktime,
10046-
funding_feerate_sat_per_1000_weight,
10047-
our_funding_inputs: funding_inputs,
10048-
},
10065+
dual_funding_context,
1004910066
interactive_tx_constructor: None,
1005010067
interactive_tx_signing_session: None,
1005110068
};

0 commit comments

Comments
 (0)