Skip to content

Commit 5f5e3b7

Browse files
committed
fix Use SharedOwnedInput for shared_funding_input
1 parent 7e2b4ba commit 5f5e3b7

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ struct NegotiationContext {
496496
/// - For the acceptor:
497497
/// The expected previous funding input. It should be added by the initiator node.
498498
/// The values are the output value and the holder's part of the shared input.
499-
shared_funding_input: Option<(OutPoint, u64, u64)>,
499+
shared_funding_input: Option<SharedOwnedInput>,
500500
/// The intended/extended funding output, potentially co-owned by both peers (shared).
501501
/// - For the initiator:
502502
/// The output intended to be the new funding output. This will be added alonside to the
@@ -540,9 +540,8 @@ fn is_serial_id_valid_for_counterparty(holder_is_initiator: bool, serial_id: Ser
540540
impl NegotiationContext {
541541
fn new(
542542
holder_node_id: PublicKey, counterparty_node_id: PublicKey, holder_is_initiator: bool,
543-
shared_funding_input: Option<(OutPoint, u64, u64)>,
544-
shared_funding_output: SharedOwnedOutput, tx_locktime: AbsoluteLockTime,
545-
feerate_sat_per_kw: u32,
543+
shared_funding_input: Option<SharedOwnedInput>, shared_funding_output: SharedOwnedOutput,
544+
tx_locktime: AbsoluteLockTime, feerate_sat_per_kw: u32,
546545
) -> Self {
547546
NegotiationContext {
548547
holder_node_id,
@@ -636,21 +635,13 @@ impl NegotiationContext {
636635
return Err(AbortReason::DuplicateFundingInput);
637636
}
638637
// Check if received shared input matches the expected
639-
if !(shared_funding_input.0.txid == *shared_txid
640-
&& shared_funding_input.0.vout == msg.prevtx_out)
638+
if !(shared_funding_input.input.previous_output.txid == *shared_txid
639+
&& shared_funding_input.input.previous_output.vout == msg.prevtx_out)
641640
{
642641
return Err(AbortReason::UnexpectedFundingInput);
643642
} else {
644643
let previous_output = OutPoint { txid: *shared_txid, vout: msg.prevtx_out };
645-
let txin = TxIn {
646-
previous_output,
647-
sequence: Sequence(msg.sequence),
648-
..Default::default()
649-
};
650-
let local_owned_sats = shared_funding_input.2;
651-
let shared_input =
652-
SharedOwnedInput::new(txin, shared_funding_input.1, local_owned_sats);
653-
(InputOwned::Shared(shared_input), previous_output)
644+
(InputOwned::Shared(shared_funding_input.clone()), previous_output)
654645
}
655646
} else {
656647
return Err(AbortReason::UnexpectedFundingInput);
@@ -837,15 +828,8 @@ impl NegotiationContext {
837828
let vout = msg.prevtx_out as usize;
838829
let (prev_outpoint, input) = if let Some(shared_input_txid) = msg.shared_input_txid {
839830
let prev_outpoint = OutPoint { txid: shared_input_txid, vout: msg.prevtx_out };
840-
let txin = TxIn {
841-
previous_output: prev_outpoint,
842-
sequence: Sequence(msg.sequence),
843-
..Default::default()
844-
};
845831
if let Some(shared_funding_input) = &self.shared_funding_input {
846-
let value = shared_funding_input.1;
847-
let local_owned = shared_funding_input.2;
848-
(prev_outpoint, InputOwned::Shared(SharedOwnedInput::new(txin, value, local_owned)))
832+
(prev_outpoint, InputOwned::Shared(shared_funding_input.clone()))
849833
} else {
850834
return Err(AbortReason::UnexpectedFundingInput);
851835
}
@@ -952,11 +936,9 @@ impl NegotiationContext {
952936
let opt_shared_funding_input = self.shared_funding_input.clone();
953937
let constructed_tx = ConstructedTransaction::new(self);
954938
if let Some(shared_funding_input) = &opt_shared_funding_input {
955-
if !constructed_tx
956-
.inputs
957-
.iter()
958-
.any(|input| input.txin().previous_output == shared_funding_input.0)
959-
{
939+
if !constructed_tx.inputs.iter().any(|input| {
940+
input.txin().previous_output == shared_funding_input.input.previous_output
941+
}) {
960942
return Err(AbortReason::MissingFundingInput);
961943
}
962944
}
@@ -1179,8 +1161,7 @@ impl StateMachine {
11791161
fn new(
11801162
holder_node_id: PublicKey, counterparty_node_id: PublicKey, feerate_sat_per_kw: u32,
11811163
is_initiator: bool, tx_locktime: AbsoluteLockTime,
1182-
shared_funding_input: Option<(OutPoint, u64, u64)>,
1183-
shared_funding_output: SharedOwnedOutput,
1164+
shared_funding_input: Option<SharedOwnedInput>, shared_funding_output: SharedOwnedOutput,
11841165
) -> Self {
11851166
let context = NegotiationContext::new(
11861167
holder_node_id,
@@ -1275,7 +1256,7 @@ impl_writeable_tlv_based!(SingleOwnedInput, {
12751256
});
12761257

12771258
#[derive(Clone, Debug, Eq, PartialEq)]
1278-
struct SharedOwnedInput {
1259+
pub(super) struct SharedOwnedInput {
12791260
input: TxIn,
12801261
value: u64,
12811262
local_owned: u64,
@@ -1691,7 +1672,7 @@ where
16911672
pub is_initiator: bool,
16921673
pub funding_tx_locktime: AbsoluteLockTime,
16931674
pub inputs_to_contribute: Vec<(TxIn, TransactionU16LenLimited)>,
1694-
pub shared_funding_input: Option<(OutPoint, u64, u64)>,
1675+
pub shared_funding_input: Option<SharedOwnedInput>,
16951676
pub shared_funding_output: SharedOwnedOutput,
16961677
pub outputs_to_contribute: Vec<TxOut>,
16971678
}
@@ -1754,19 +1735,12 @@ impl InteractiveTxConstructor {
17541735
if is_initiator {
17551736
// Add shared funding input
17561737
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
1757-
let value = shared_funding_input.1;
1758-
let local_owned = shared_funding_input.2;
17591738
// Sanity check
1760-
if local_owned > value {
1739+
if shared_funding_input.local_owned > shared_funding_input.value {
17611740
return Err(AbortReason::InvalidLowFundingInputValue);
17621741
}
1763-
let txin = TxIn {
1764-
previous_output: shared_funding_input.0,
1765-
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
1766-
..Default::default()
1767-
};
1768-
let input = SharedOwnedInput::new(txin, value, local_owned);
1769-
inputs_to_contribute.push((serial_id, InputOwned::Shared(input)));
1742+
inputs_to_contribute
1743+
.push((serial_id, InputOwned::Shared(shared_funding_input.clone())));
17701744
}
17711745
}
17721746
// We'll sort by the randomly generated serial IDs, effectively shuffling the order of the inputs
@@ -1991,7 +1965,7 @@ mod tests {
19911965
use crate::ln::interactivetxs::{
19921966
calculate_change_output_value, generate_holder_serial_id, AbortReason,
19931967
HandleTxCompleteValue, InteractiveTxConstructor, InteractiveTxConstructorArgs,
1994-
InteractiveTxMessageSend, SharedOwnedOutput, MAX_INPUTS_OUTPUTS_COUNT,
1968+
InteractiveTxMessageSend, SharedOwnedInput, SharedOwnedOutput, MAX_INPUTS_OUTPUTS_COUNT,
19951969
MAX_RECEIVED_TX_ADD_INPUT_COUNT, MAX_RECEIVED_TX_ADD_OUTPUT_COUNT,
19961970
};
19971971
use crate::ln::types::ChannelId;
@@ -2111,7 +2085,17 @@ mod tests {
21112085
is_initiator: true,
21122086
funding_tx_locktime,
21132087
inputs_to_contribute: session.inputs_a,
2114-
shared_funding_input: session.a_shared_input,
2088+
shared_funding_input: session.a_shared_input.map(|(op, val, lo)| {
2089+
SharedOwnedInput::new(
2090+
TxIn {
2091+
previous_output: op,
2092+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
2093+
..Default::default()
2094+
},
2095+
val,
2096+
lo,
2097+
)
2098+
}),
21152099
shared_funding_output: SharedOwnedOutput::new(
21162100
session.shared_output_a.0,
21172101
session.shared_output_a.1,
@@ -2138,7 +2122,17 @@ mod tests {
21382122
is_initiator: false,
21392123
funding_tx_locktime,
21402124
inputs_to_contribute: session.inputs_b,
2141-
shared_funding_input: session.b_shared_input,
2125+
shared_funding_input: session.b_shared_input.map(|(op, val, lo)| {
2126+
SharedOwnedInput::new(
2127+
TxIn {
2128+
previous_output: op,
2129+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
2130+
..Default::default()
2131+
},
2132+
val,
2133+
lo,
2134+
)
2135+
}),
21422136
shared_funding_output: SharedOwnedOutput::new(
21432137
session.shared_output_b.0,
21442138
session.shared_output_b.1,

0 commit comments

Comments
 (0)