Skip to content

Commit 0a1d445

Browse files
committed
Remove NegotiatedTxInput::weight
The weight is no longer needed once a ConstructedTransaction is created, so it doesn't need to be persisted.
1 parent 1e510cc commit 0a1d445

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ pub(crate) struct ConstructedTransaction {
215215
pub(crate) struct NegotiatedTxInput {
216216
serial_id: SerialId,
217217
txin: TxIn,
218-
// The weight of the input including an estimate of its witness weight.
219-
weight: Weight,
220218
prev_output: TxOut,
221219
}
222220

@@ -233,8 +231,7 @@ impl NegotiatedTxInput {
233231
impl_writeable_tlv_based!(NegotiatedTxInput, {
234232
(1, serial_id, required),
235233
(3, txin, required),
236-
(5, weight, required),
237-
(7, prev_output, required),
234+
(5, prev_output, required),
238235
});
239236

240237
impl_writeable_tlv_based!(ConstructedTransaction, {
@@ -278,6 +275,12 @@ impl ConstructedTransaction {
278275

279276
let remote_inputs_value_satoshis = context.remote_inputs_value();
280277
let remote_outputs_value_satoshis = context.remote_outputs_value();
278+
279+
let satisfaction_weight =
280+
Weight::from_wu(context.inputs.iter().fold(0u64, |value, (_, input)| {
281+
value.saturating_add(input.satisfaction_weight().to_wu())
282+
}));
283+
281284
let mut inputs: Vec<NegotiatedTxInput> =
282285
context.inputs.into_values().map(|tx_input| tx_input.into_negotiated_input()).collect();
283286
let mut outputs: Vec<InteractiveTxOutput> = context.outputs.into_values().collect();
@@ -310,17 +313,18 @@ impl ConstructedTransaction {
310313
shared_input_index,
311314
};
312315

313-
if constructed_tx.weight().to_wu() > MAX_STANDARD_TX_WEIGHT as u64 {
316+
let tx_weight = constructed_tx.weight(satisfaction_weight);
317+
if tx_weight > Weight::from_wu(MAX_STANDARD_TX_WEIGHT as u64) {
314318
return Err(AbortReason::TransactionTooLarge);
315319
}
316320

317321
Ok(constructed_tx)
318322
}
319323

320-
pub fn weight(&self) -> Weight {
321-
let inputs_weight = self.inputs.iter().fold(Weight::from_wu(0), |weight, input| {
322-
weight.checked_add(input.weight).unwrap_or(Weight::MAX)
323-
});
324+
fn weight(&self, satisfaction_weight: Weight) -> Weight {
325+
let inputs_weight = Weight::from_wu(self.inputs.len() as u64 * BASE_INPUT_WEIGHT)
326+
.checked_add(satisfaction_weight)
327+
.unwrap_or(Weight::MAX);
324328
let outputs_weight = self.outputs.iter().fold(Weight::from_wu(0), |weight, output| {
325329
weight.checked_add(get_output_weight(output.script_pubkey())).unwrap_or(Weight::MAX)
326330
});
@@ -1852,9 +1856,8 @@ impl InteractiveTxInput {
18521856
}
18531857

18541858
fn into_negotiated_input(self) -> NegotiatedTxInput {
1855-
let weight = Weight::from_wu(BASE_INPUT_WEIGHT) + self.input.satisfaction_weight();
18561859
let (txin, prev_output) = self.input.into_tx_in_with_prev_output();
1857-
NegotiatedTxInput { serial_id: self.serial_id, txin, weight, prev_output }
1860+
NegotiatedTxInput { serial_id: self.serial_id, txin, prev_output }
18581861
}
18591862
}
18601863

@@ -3328,7 +3331,6 @@ mod tests {
33283331
NegotiatedTxInput {
33293332
serial_id: idx as u64, // even values will be holder (initiator in this test)
33303333
txin,
3331-
weight: Weight::from_wu(0), // N/A for test
33323334
prev_output,
33333335
}
33343336
})

0 commit comments

Comments
 (0)