Skip to content

Commit

Permalink
Extract helper method in DualFundingChannelContext to extract prev outs
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Jan 9, 2025
1 parent 6c45e4f commit fbde31f
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,21 +1695,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
funding_inputs.push(prev_funding_input);
}

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

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

impl DualFundingChannelContext {
/// Obtain prev outputs for each supplied input and matching transaction.
/// Can error when there a prev tx does not have an output for the specified vout number.
/// Also checks for matching of transaction IDs.
fn txouts_from_input_prev_txs(inputs: &Vec<(TxIn, TransactionU16LenLimited)>) -> Result<Vec<&TxOut>, ChannelError> {
let mut prev_outputs: Vec<&TxOut> = Vec::with_capacity(inputs.len());
// Check that vouts exist for each TxIn in provided transactions.
for (idx, (txin, tx)) in inputs.iter().enumerate() {
let txid = tx.as_transaction().compute_txid();
if txin.previous_output.txid != txid {
return Err(ChannelError::Warn(
format!("Transaction input txid mismatch, {} vs. {}, at index {}", txin.previous_output.txid, txid, idx)
));
}
if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
prev_outputs.push(output);
} else {
return Err(ChannelError::Warn(
format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn, at index {}",
txid, txin.previous_output.vout, idx)
));
}
}
Ok(prev_outputs)
}
}

// Holder designates channel data owned for the benefit of the user client.
// Counterparty designates channel data owned by the another channel participant entity.
pub(super) struct Channel<SP: Deref> where SP::Target: SignerProvider {
Expand Down Expand Up @@ -8934,16 +8950,17 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
unfunded_channel_age_ticks: 0,
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
};
let dual_funding_context = DualFundingChannelContext {
our_funding_satoshis: funding_satoshis,
their_funding_satoshis: None,
funding_tx_locktime,
funding_feerate_sat_per_1000_weight,
our_funding_inputs: funding_inputs,
};
let chan = Self {
context,
unfunded_context,
dual_funding_context: DualFundingChannelContext {
our_funding_satoshis: funding_satoshis,
their_funding_satoshis: None,
funding_tx_locktime,
funding_feerate_sat_per_1000_weight,
our_funding_inputs: funding_inputs,
},
dual_funding_context,
interactive_tx_constructor: None,
};
Ok(chan)
Expand Down

0 comments on commit fbde31f

Please sign in to comment.