Skip to content

Commit 9cd7e5e

Browse files
committed
Add begin_interactive_funding_tx_construction()
1 parent d37f966 commit 9cd7e5e

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,16 +2035,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20352035
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled
20362036
fn begin_interactive_funding_tx_construction<ES: Deref>(
20372037
&mut self, signer_provider: &SP, entropy_source: &ES, holder_node_id: PublicKey,
2038+
extra_input: Option<(TxIn, TransactionU16LenLimited)>,
20382039
) -> Result<Option<InteractiveTxMessageSend>, APIError>
20392040
where ES::Target: EntropySource
20402041
{
2041-
let mut funding_inputs = Vec::new();
2042-
mem::swap(&mut self.dual_funding_context.our_funding_inputs, &mut funding_inputs);
2042+
let mut funding_inputs_with_extra = self.dual_funding_context.our_funding_inputs.take().unwrap_or_else(|| vec![]);
20432043

2044-
let funding_inputs_prev_outputs = DualFundingChannelContext::txouts_from_input_prev_txs(&funding_inputs)
2045-
.map_err(|err| APIError::APIMisuseError { err: err.to_string() })?;
2044+
if let Some(extra_input) = extra_input {
2045+
funding_inputs_with_extra.push(extra_input);
2046+
}
2047+
2048+
let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs_with_extra.len());
2049+
// Check that vouts exist for each TxIn in provided transactions.
2050+
for (idx, input) in funding_inputs_with_extra.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);
2053+
} else {
2054+
return Err(APIError::APIMisuseError {
2055+
err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs_with_extra[{}]",
2056+
input.1.as_transaction().compute_txid(), input.0.previous_output.vout, idx) });
2057+
}
2058+
}
20462059

2047-
let total_input_satoshis: u64 = funding_inputs_prev_outputs.iter().map(|txout| txout.value.to_sat()).sum();
2060+
let total_input_satoshis: u64 = funding_inputs_with_extra.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+
).sum();
20482063
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
20492064
return Err(APIError::APIMisuseError {
20502065
err: format!("Total value of funding inputs must be at least funding amount. It was {} sats",
@@ -2085,15 +2100,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20852100
|err| APIError::APIMisuseError {
20862101
err: format!("Failed to get change script as new destination script, {:?}", err),
20872102
})?;
2088-
let mut change_output = TxOut {
2089-
value: Amount::from_sat(change_value),
2090-
script_pubkey: change_script,
2091-
};
2092-
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
2093-
let change_output_fee = fee_for_weight(self.dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
2094-
change_output.value = Amount::from_sat(change_value.saturating_sub(change_output_fee));
2095-
// Note: dust check not done here, should be handled before
2096-
funding_outputs.push(OutputOwned::Single(change_output));
2103+
let _res = add_funding_change_output(
2104+
change_value, change_script, &mut funding_outputs, self.dual_funding_context.funding_feerate_sat_per_1000_weight);
20972105
}
20982106

20992107
let constructor_args = InteractiveTxConstructorArgs {
@@ -2104,15 +2112,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21042112
feerate_sat_per_kw: self.dual_funding_context.funding_feerate_sat_per_1000_weight,
21052113
is_initiator: self.context.is_outbound(),
21062114
funding_tx_locktime: self.dual_funding_context.funding_tx_locktime,
2107-
inputs_to_contribute: funding_inputs,
2115+
inputs_to_contribute: funding_inputs_with_extra,
21082116
outputs_to_contribute: funding_outputs,
21092117
expected_remote_shared_funding_output,
21102118
};
21112119
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)
21122120
.map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
21132121
let msg = tx_constructor.take_initiator_first_message();
21142122

2115-
self.interactive_tx_constructor = Some(tx_constructor);
2123+
self.interactive_tx_constructor.replace(tx_constructor);
21162124

21172125
Ok(msg)
21182126
}
@@ -4555,6 +4563,22 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
45554563
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
45564564
}
45574565

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+
) -> TxOut {
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+
change_output
4580+
}
4581+
45584582
pub(super) fn calculate_our_funding_satoshis(
45594583
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
45604584
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
@@ -4617,7 +4641,7 @@ pub(super) struct DualFundingChannelContext {
46174641
///
46184642
/// Note that this field may be emptied once the interactive negotiation has been started.
46194643
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4620-
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
4644+
pub our_funding_inputs: Option<Vec<(TxIn, TransactionU16LenLimited)>>,
46214645
}
46224646

46234647
impl DualFundingChannelContext {
@@ -9285,7 +9309,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
92859309
their_funding_satoshis: None,
92869310
funding_tx_locktime,
92879311
funding_feerate_sat_per_1000_weight,
9288-
our_funding_inputs: funding_inputs,
9312+
our_funding_inputs: Some(funding_inputs),
92899313
};
92909314
let chan = Self {
92919315
context,
@@ -9440,7 +9464,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
94409464
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
94419465
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
94429466
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9443-
our_funding_inputs: funding_inputs.clone(),
9467+
our_funding_inputs: Some(funding_inputs.clone()),
94449468
};
94459469

94469470
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(

0 commit comments

Comments
 (0)