Skip to content

Commit 7e2b4ba

Browse files
committed
fix SharedOwnedInput has no prev_output
1 parent eeaa9b7 commit 7e2b4ba

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl ConstructedTransaction {
272272

273273
pub fn weight(&self) -> Weight {
274274
let inputs_weight = self.inputs.iter().fold(Weight::from_wu(0), |weight, input| {
275-
weight.checked_add(estimate_input_weight(input.prev_output())).unwrap_or(Weight::MAX)
275+
weight.checked_add(input.estimate_input_weight()).unwrap_or(Weight::MAX)
276276
});
277277
let outputs_weight = self.outputs.iter().fold(Weight::from_wu(0), |weight, output| {
278278
weight.checked_add(get_output_weight(output.script_pubkey())).unwrap_or(Weight::MAX)
@@ -578,7 +578,7 @@ impl NegotiationContext {
578578
.iter()
579579
.filter(|(serial_id, _)| self.is_serial_id_valid_for_counterparty(serial_id))
580580
.fold(0u64, |weight, (_, input)| {
581-
weight.saturating_add(estimate_input_weight(input.prev_output()).to_wu())
581+
weight.saturating_add(input.estimate_input_weight().to_wu())
582582
}),
583583
)
584584
}
@@ -595,9 +595,7 @@ impl NegotiationContext {
595595
}
596596

597597
fn local_inputs_value(&self) -> u64 {
598-
self.inputs
599-
.iter()
600-
.fold(0u64, |acc, (_, input)| acc.saturating_add(input.prev_output().value.to_sat()))
598+
self.inputs.iter().fold(0u64, |acc, (_, input)| acc.saturating_add(input.value()))
601599
}
602600

603601
fn received_tx_add_input(&mut self, msg: &msgs::TxAddInput) -> Result<(), AbortReason> {
@@ -649,12 +647,9 @@ impl NegotiationContext {
649647
sequence: Sequence(msg.sequence),
650648
..Default::default()
651649
};
652-
let prev_output = TxOut {
653-
value: Amount::from_sat(shared_funding_input.1),
654-
script_pubkey: txin.script_sig.to_p2wsh(),
655-
};
656650
let local_owned_sats = shared_funding_input.2;
657-
let shared_input = SharedOwnedInput::new(txin, prev_output, local_owned_sats);
651+
let shared_input =
652+
SharedOwnedInput::new(txin, shared_funding_input.1, local_owned_sats);
658653
(InputOwned::Shared(shared_input), previous_output)
659654
}
660655
} else {
@@ -850,14 +845,7 @@ impl NegotiationContext {
850845
if let Some(shared_funding_input) = &self.shared_funding_input {
851846
let value = shared_funding_input.1;
852847
let local_owned = shared_funding_input.2;
853-
let prev_output = TxOut {
854-
value: Amount::from_sat(value),
855-
script_pubkey: txin.script_sig.to_p2wsh(),
856-
};
857-
(
858-
prev_outpoint,
859-
InputOwned::Shared(SharedOwnedInput::new(txin, prev_output, local_owned)),
860-
)
848+
(prev_outpoint, InputOwned::Shared(SharedOwnedInput::new(txin, value, local_owned)))
861849
} else {
862850
return Err(AbortReason::UnexpectedFundingInput);
863851
}
@@ -1289,29 +1277,29 @@ impl_writeable_tlv_based!(SingleOwnedInput, {
12891277
#[derive(Clone, Debug, Eq, PartialEq)]
12901278
struct SharedOwnedInput {
12911279
input: TxIn,
1292-
prev_output: TxOut,
1280+
value: u64,
12931281
local_owned: u64,
12941282
}
12951283

12961284
impl_writeable_tlv_based!(SharedOwnedInput, {
12971285
(1, input, required),
1298-
(3, prev_output, required),
1286+
(3, value, required),
12991287
(5, local_owned, required),
13001288
});
13011289

13021290
impl SharedOwnedInput {
1303-
pub fn new(input: TxIn, prev_output: TxOut, local_owned: u64) -> Self {
1291+
pub fn new(input: TxIn, value: u64, local_owned: u64) -> Self {
13041292
debug_assert!(
1305-
local_owned <= prev_output.value.to_sat(),
1293+
local_owned <= value,
13061294
"SharedOwnedInput: Inconsistent local_owned value {}, larger than prev out value {}",
13071295
local_owned,
1308-
prev_output.value.to_sat(),
1296+
value,
13091297
);
1310-
Self { input, prev_output, local_owned }
1298+
Self { input, value, local_owned }
13111299
}
13121300

13131301
fn remote_owned(&self) -> u64 {
1314-
self.prev_output.value.to_sat().saturating_sub(self.local_owned)
1302+
self.value.saturating_sub(self.local_owned)
13151303
}
13161304
}
13171305

@@ -1354,10 +1342,10 @@ impl InputOwned {
13541342
}
13551343
}
13561344

1357-
pub fn prev_output(&self) -> &TxOut {
1345+
pub fn value(&self) -> u64 {
13581346
match self {
1359-
InputOwned::Single(single) => &single.prev_output,
1360-
InputOwned::Shared(shared) => &shared.prev_output,
1347+
InputOwned::Single(single) => single.prev_output.value.to_sat(),
1348+
InputOwned::Shared(shared) => shared.value,
13611349
}
13621350
}
13631351

@@ -1387,6 +1375,13 @@ impl InputOwned {
13871375
InputOwned::Shared(shared) => shared.remote_owned(),
13881376
}
13891377
}
1378+
1379+
fn estimate_input_weight(&self) -> Weight {
1380+
match self {
1381+
InputOwned::Single(single) => estimate_input_weight(&single.prev_output),
1382+
InputOwned::Shared(_shared) => Weight::from_wu(P2WSH_INPUT_WEIGHT_LOWER_BOUND),
1383+
}
1384+
}
13901385
}
13911386

13921387
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -1548,12 +1543,8 @@ impl InteractiveTxInput {
15481543
self.input.into_tx_in()
15491544
}
15501545

1551-
pub fn prev_output(&self) -> &TxOut {
1552-
self.input.prev_output()
1553-
}
1554-
15551546
pub fn value(&self) -> u64 {
1556-
self.prev_output().value.to_sat()
1547+
self.input.value()
15571548
}
15581549

15591550
pub fn local_value(&self) -> u64 {
@@ -1563,6 +1554,10 @@ impl InteractiveTxInput {
15631554
pub fn remote_value(&self) -> u64 {
15641555
self.input.remote_value(self.added_by)
15651556
}
1557+
1558+
pub fn estimate_input_weight(&self) -> Weight {
1559+
self.input.estimate_input_weight()
1560+
}
15661561
}
15671562

15681563
pub(super) struct InteractiveTxConstructor {
@@ -1770,11 +1765,7 @@ impl InteractiveTxConstructor {
17701765
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
17711766
..Default::default()
17721767
};
1773-
let prev_out = TxOut {
1774-
value: Amount::from_sat(value),
1775-
script_pubkey: txin.script_sig.to_p2wsh(),
1776-
};
1777-
let input = SharedOwnedInput::new(txin, prev_out, local_owned);
1768+
let input = SharedOwnedInput::new(txin, value, local_owned);
17781769
inputs_to_contribute.push((serial_id, InputOwned::Shared(input)));
17791770
}
17801771
}

0 commit comments

Comments
 (0)