Skip to content

Add Shared Input support in interactive TX construction #3842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use crate::ln::channelmanager::{
use crate::ln::interactivetxs::{
calculate_change_output_value, get_output_weight, AbortReason, HandleTxCompleteResult,
InteractiveTxConstructor, InteractiveTxConstructorArgs, InteractiveTxMessageSend,
InteractiveTxMessageSendResult, InteractiveTxSigningSession, OutputOwned, SharedOwnedOutput,
InteractiveTxMessageSendResult, InteractiveTxSigningSession, SharedOwnedOutput,
TX_COMMON_FIELDS_WEIGHT,
};
use crate::ln::msgs;
Expand Down Expand Up @@ -2756,24 +2756,12 @@ where
// Note: For the error case when the inputs are insufficient, it will be handled after
// the `calculate_change_output_value` call below
let mut funding_outputs = Vec::new();
let mut expected_remote_shared_funding_output = None;

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

if self.funding.is_outbound() {
funding_outputs.push(
OutputOwned::Shared(SharedOwnedOutput::new(
shared_funding_output, self.dual_funding_context.our_funding_satoshis,
))
);
} else {
let TxOut { value, script_pubkey } = shared_funding_output;
expected_remote_shared_funding_output = Some((script_pubkey, value.to_sat()));
}

// Optionally add change output
let change_script = if let Some(script) = change_destination_opt {
script
Expand All @@ -2783,7 +2771,8 @@ where
};
let change_value_opt = calculate_change_output_value(
self.funding.is_outbound(), self.dual_funding_context.our_funding_satoshis,
&funding_inputs, &funding_outputs,
&funding_inputs, None,
&shared_funding_output.script_pubkey, &funding_outputs,
self.dual_funding_context.funding_feerate_sat_per_1000_weight,
change_script.minimal_non_dust().to_sat(),
)?;
Expand All @@ -2798,7 +2787,7 @@ where
// Check dust limit again
if change_value_decreased_with_fee > self.context.holder_dust_limit_satoshis {
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
funding_outputs.push(OutputOwned::Single(change_output));
funding_outputs.push(change_output);
}
}

Expand All @@ -2811,8 +2800,9 @@ where
is_initiator: self.funding.is_outbound(),
funding_tx_locktime: self.dual_funding_context.funding_tx_locktime,
inputs_to_contribute: funding_inputs,
shared_funding_input: None,
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, self.dual_funding_context.our_funding_satoshis),
outputs_to_contribute: funding_outputs,
expected_remote_shared_funding_output,
};
let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)?;
let msg = tx_constructor.take_initiator_first_message();
Expand Down Expand Up @@ -11809,6 +11799,10 @@ where
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
our_funding_inputs: our_funding_inputs.clone(),
};
let shared_funding_output = TxOut {
value: Amount::from_sat(funding.get_value_satoshis()),
script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
};

let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
InteractiveTxConstructorArgs {
Expand All @@ -11820,8 +11814,9 @@ where
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
is_initiator: false,
inputs_to_contribute: our_funding_inputs,
shared_funding_input: None,
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, our_funding_satoshis),
outputs_to_contribute: Vec::new(),
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
}
).map_err(|_| ChannelError::Close((
"V2 channel rejected due to sender error".into(),
Expand Down
7 changes: 4 additions & 3 deletions lightning/src/ln/dual_funding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
let tx_add_input_msg = TxAddInput {
channel_id,
serial_id: 2, // Even serial_id from initiator.
prevtx: initiator_funding_inputs[0].1.clone(),
prevtx: Some(initiator_funding_inputs[0].1.clone()),
prevtx_out: 0,
sequence: initiator_funding_inputs[0].0.sequence.0,
shared_input_txid: None,
};
let input_value =
tx_add_input_msg.prevtx.as_transaction().output[tx_add_input_msg.prevtx_out as usize].value;
let input_value = tx_add_input_msg.prevtx.as_ref().unwrap().as_transaction().output
[tx_add_input_msg.prevtx_out as usize]
.value;
assert_eq!(input_value.to_sat(), session.initiator_input_value_satoshis);

nodes[1].node.handle_tx_add_input(nodes[0].node.get_our_node_id(), &tx_add_input_msg);
Expand Down
Loading
Loading