Skip to content

Commit 455d34a

Browse files
committed
f - use prev_output in SharedOwnedInput
1 parent cb245ac commit 455d34a

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ pub(crate) enum AbortReason {
121121
DuplicateFundingOutput,
122122
/// More than one funding (shared) input found.
123123
DuplicateFundingInput,
124-
/// The intended local part of the funding output is higher than the actual shared funding output,
125-
/// if funding output is provided by the peer this is an interop error,
126-
/// if provided by the same node than internal input consistency error.
127-
InvalidLowFundingOutputValue,
128-
/// The intended local part of the funding input is higher than the actual shared funding input.
129-
InvalidLowFundingInputValue,
130124
/// Internal error
131125
InternalError(&'static str),
132126
}
@@ -186,12 +180,6 @@ impl Display for AbortReason {
186180
f.write_str("More than one funding output found")
187181
},
188182
AbortReason::DuplicateFundingInput => f.write_str("More than one funding input found"),
189-
AbortReason::InvalidLowFundingOutputValue => f.write_str(
190-
"Local part of funding output value is greater than the funding output value",
191-
),
192-
AbortReason::InvalidLowFundingInputValue => f.write_str(
193-
"Local part of shared input value is greater than the shared input value",
194-
),
195183
AbortReason::InternalError(text) => {
196184
f.write_fmt(format_args!("Internal error: {}", text))
197185
},
@@ -1268,23 +1256,24 @@ struct SingleOwnedInput {
12681256
#[derive(Clone, Debug, Eq, PartialEq)]
12691257
pub(super) struct SharedOwnedInput {
12701258
input: TxIn,
1271-
value: u64,
1259+
prev_output: TxOut,
12721260
local_owned: u64,
12731261
}
12741262

12751263
impl SharedOwnedInput {
1276-
pub fn new(input: TxIn, value: u64, local_owned: u64) -> Self {
1264+
pub fn new(input: TxIn, prev_output: TxOut, local_owned: u64) -> Self {
1265+
let value = prev_output.value.to_sat();
12771266
debug_assert!(
12781267
local_owned <= value,
12791268
"SharedOwnedInput: Inconsistent local_owned value {}, larger than prev out value {}",
12801269
local_owned,
12811270
value,
12821271
);
1283-
Self { input, value, local_owned }
1272+
Self { input, prev_output, local_owned }
12841273
}
12851274

12861275
fn remote_owned(&self) -> u64 {
1287-
self.value.saturating_sub(self.local_owned)
1276+
self.prev_output.value.to_sat().saturating_sub(self.local_owned)
12881277
}
12891278
}
12901279

@@ -1296,7 +1285,7 @@ enum InputOwned {
12961285
/// Belongs to a single party -- controlled exclusively and fully belonging to a single party
12971286
/// Includes the input and the previous output
12981287
Single(SingleOwnedInput),
1299-
// Input with shared control and value split between the two ends (or fully at one side)
1288+
/// Input with shared control and value split between the counterparties (or fully by one).
13001289
Shared(SharedOwnedInput),
13011290
}
13021291

@@ -1325,7 +1314,7 @@ impl InputOwned {
13251314
pub fn value(&self) -> u64 {
13261315
match self {
13271316
InputOwned::Single(single) => single.prev_output.value.to_sat(),
1328-
InputOwned::Shared(shared) => shared.value,
1317+
InputOwned::Shared(shared) => shared.prev_output.value.to_sat(),
13291318
}
13301319
}
13311320

@@ -1359,7 +1348,7 @@ impl InputOwned {
13591348
fn estimate_input_weight(&self) -> Weight {
13601349
match self {
13611350
InputOwned::Single(single) => estimate_input_weight(&single.prev_output),
1362-
InputOwned::Shared(_shared) => Weight::from_wu(P2WSH_INPUT_WEIGHT_LOWER_BOUND),
1351+
InputOwned::Shared(shared) => estimate_input_weight(&shared.prev_output),
13631352
}
13641353
}
13651354
}
@@ -1728,10 +1717,6 @@ impl InteractiveTxConstructor {
17281717
if is_initiator {
17291718
// Add shared funding input
17301719
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
1731-
// Sanity check
1732-
if shared_funding_input.local_owned > shared_funding_input.value {
1733-
return Err(AbortReason::InvalidLowFundingInputValue);
1734-
}
17351720
inputs_to_contribute
17361721
.push((serial_id, InputOwned::Shared(shared_funding_input.clone())));
17371722
}
@@ -2041,12 +2026,12 @@ mod tests {
20412026
struct TestSession {
20422027
description: &'static str,
20432028
inputs_a: Vec<(TxIn, TransactionU16LenLimited)>,
2044-
a_shared_input: Option<(OutPoint, u64, u64)>,
2029+
a_shared_input: Option<(OutPoint, TxOut, u64)>,
20452030
/// The funding output, with the value contributed
20462031
shared_output_a: (TxOut, u64),
20472032
outputs_a: Vec<TxOut>,
20482033
inputs_b: Vec<(TxIn, TransactionU16LenLimited)>,
2049-
b_shared_input: Option<(OutPoint, u64, u64)>,
2034+
b_shared_input: Option<(OutPoint, TxOut, u64)>,
20502035
/// The funding output, with the value contributed
20512036
shared_output_b: (TxOut, u64),
20522037
outputs_b: Vec<TxOut>,
@@ -2091,14 +2076,14 @@ mod tests {
20912076
is_initiator: true,
20922077
funding_tx_locktime,
20932078
inputs_to_contribute: session.inputs_a,
2094-
shared_funding_input: session.a_shared_input.map(|(op, val, lo)| {
2079+
shared_funding_input: session.a_shared_input.map(|(op, prev_output, lo)| {
20952080
SharedOwnedInput::new(
20962081
TxIn {
20972082
previous_output: op,
20982083
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
20992084
..Default::default()
21002085
},
2101-
val,
2086+
prev_output,
21022087
lo,
21032088
)
21042089
}),
@@ -2128,14 +2113,14 @@ mod tests {
21282113
is_initiator: false,
21292114
funding_tx_locktime,
21302115
inputs_to_contribute: session.inputs_b,
2131-
shared_funding_input: session.b_shared_input.map(|(op, val, lo)| {
2116+
shared_funding_input: session.b_shared_input.map(|(op, prev_output, lo)| {
21322117
SharedOwnedInput::new(
21332118
TxIn {
21342119
previous_output: op,
21352120
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
21362121
..Default::default()
21372122
},
2138-
val,
2123+
prev_output,
21392124
lo,
21402125
)
21412126
}),
@@ -2314,16 +2299,17 @@ mod tests {
23142299

23152300
fn generate_shared_input(
23162301
prev_funding_tx: &Transaction, vout: u32, local_owned: u64,
2317-
) -> (OutPoint, u64, u64) {
2302+
) -> (OutPoint, TxOut, u64) {
23182303
let txid = prev_funding_tx.compute_txid();
2319-
let value = prev_funding_tx.output.get(vout as usize).unwrap().value.to_sat();
2304+
let prev_output = prev_funding_tx.output.get(vout as usize).unwrap();
2305+
let value = prev_output.value.to_sat();
23202306
assert!(
23212307
local_owned <= value,
23222308
"local owned > value for shared input, {} {}",
23232309
local_owned,
23242310
value,
23252311
);
2326-
(OutPoint { txid, vout }, value, local_owned)
2312+
(OutPoint { txid, vout }, prev_output.clone(), local_owned)
23272313
}
23282314

23292315
fn generate_p2wsh_script_pubkey() -> ScriptBuf {

0 commit comments

Comments
 (0)