Skip to content

Commit 1d507f2

Browse files
authored
Merge pull request #3842 from optout21/shared-input
Add Shared Input support in interactive TX construction
2 parents ee4211e + cd2ffaf commit 1d507f2

File tree

5 files changed

+889
-619
lines changed

5 files changed

+889
-619
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::ln::channelmanager::{
5858
use crate::ln::interactivetxs::{
5959
calculate_change_output_value, get_output_weight, AbortReason, HandleTxCompleteResult,
6060
InteractiveTxConstructor, InteractiveTxConstructorArgs, InteractiveTxMessageSend,
61-
InteractiveTxMessageSendResult, InteractiveTxSigningSession, OutputOwned, SharedOwnedOutput,
61+
InteractiveTxMessageSendResult, InteractiveTxSigningSession, SharedOwnedOutput,
6262
TX_COMMON_FIELDS_WEIGHT,
6363
};
6464
use crate::ln::msgs;
@@ -2785,24 +2785,12 @@ where
27852785
// Note: For the error case when the inputs are insufficient, it will be handled after
27862786
// the `calculate_change_output_value` call below
27872787
let mut funding_outputs = Vec::new();
2788-
let mut expected_remote_shared_funding_output = None;
27892788

27902789
let shared_funding_output = TxOut {
27912790
value: Amount::from_sat(self.funding.get_value_satoshis()),
27922791
script_pubkey: self.funding.get_funding_redeemscript().to_p2wsh(),
27932792
};
27942793

2795-
if self.funding.is_outbound() {
2796-
funding_outputs.push(
2797-
OutputOwned::Shared(SharedOwnedOutput::new(
2798-
shared_funding_output, self.dual_funding_context.our_funding_satoshis,
2799-
))
2800-
);
2801-
} else {
2802-
let TxOut { value, script_pubkey } = shared_funding_output;
2803-
expected_remote_shared_funding_output = Some((script_pubkey, value.to_sat()));
2804-
}
2805-
28062794
// Optionally add change output
28072795
let change_script = if let Some(script) = change_destination_opt {
28082796
script
@@ -2812,7 +2800,8 @@ where
28122800
};
28132801
let change_value_opt = calculate_change_output_value(
28142802
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
2815-
&funding_inputs, &funding_outputs,
2803+
&funding_inputs, None,
2804+
&shared_funding_output.script_pubkey, &funding_outputs,
28162805
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
28172806
change_script.minimal_non_dust().to_sat(),
28182807
)?;
@@ -2827,7 +2816,7 @@ where
28272816
// Check dust limit again
28282817
if change_value_decreased_with_fee > self.context.holder_dust_limit_satoshis {
28292818
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
2830-
funding_outputs.push(OutputOwned::Single(change_output));
2819+
funding_outputs.push(change_output);
28312820
}
28322821
}
28332822

@@ -2840,8 +2829,9 @@ where
28402829
is_initiator: self.funding.is_outbound(),
28412830
funding_tx_locktime: self.dual_funding_context.funding_tx_locktime,
28422831
inputs_to_contribute: funding_inputs,
2832+
shared_funding_input: None,
2833+
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, self.dual_funding_context.our_funding_satoshis),
28432834
outputs_to_contribute: funding_outputs,
2844-
expected_remote_shared_funding_output,
28452835
};
28462836
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
28472837
let msg = tx_constructor.take_initiator_first_message();
@@ -12036,6 +12026,10 @@ where
1203612026
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
1203712027
our_funding_inputs: our_funding_inputs.clone(),
1203812028
};
12029+
let shared_funding_output = TxOut {
12030+
value: Amount::from_sat(funding.get_value_satoshis()),
12031+
script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
12032+
};
1203912033

1204012034
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1204112035
InteractiveTxConstructorArgs {
@@ -12047,8 +12041,9 @@ where
1204712041
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
1204812042
is_initiator: false,
1204912043
inputs_to_contribute: our_funding_inputs,
12044+
shared_funding_input: None,
12045+
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, our_funding_satoshis),
1205012046
outputs_to_contribute: Vec::new(),
12051-
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
1205212047
}
1205312048
).map_err(|err| {
1205412049
let reason = ClosureReason::ProcessingError { err: err.to_string() };

lightning/src/ln/dual_funding_tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
8989
let tx_add_input_msg = TxAddInput {
9090
channel_id,
9191
serial_id: 2, // Even serial_id from initiator.
92-
prevtx: initiator_funding_inputs[0].1.clone(),
92+
prevtx: Some(initiator_funding_inputs[0].1.clone()),
9393
prevtx_out: 0,
9494
sequence: initiator_funding_inputs[0].0.sequence.0,
9595
shared_input_txid: None,
9696
};
97-
let input_value =
98-
tx_add_input_msg.prevtx.as_transaction().output[tx_add_input_msg.prevtx_out as usize].value;
97+
let input_value = tx_add_input_msg.prevtx.as_ref().unwrap().as_transaction().output
98+
[tx_add_input_msg.prevtx_out as usize]
99+
.value;
99100
assert_eq!(input_value.to_sat(), session.initiator_input_value_satoshis);
100101

101102
nodes[1].node.handle_tx_add_input(nodes[0].node.get_our_node_id(), &tx_add_input_msg);

0 commit comments

Comments
 (0)