Skip to content

Commit 227d383

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

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 15 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,13 @@ 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 = Weight::from_wu(context
280+
.inputs
281+
.iter()
282+
.fold(0u64, |value, (_, input)| value.saturating_add(input.satisfaction_weight().to_wu()))
283+
);
284+
281285
let mut inputs: Vec<NegotiatedTxInput> =
282286
context.inputs.into_values().map(|tx_input| tx_input.into_negotiated_input()).collect();
283287
let mut outputs: Vec<InteractiveTxOutput> = context.outputs.into_values().collect();
@@ -310,17 +314,18 @@ impl ConstructedTransaction {
310314
shared_input_index,
311315
};
312316

313-
if constructed_tx.weight().to_wu() > MAX_STANDARD_TX_WEIGHT as u64 {
317+
let tx_weight = constructed_tx.weight(satisfaction_weight);
318+
if tx_weight > Weight::from_wu(MAX_STANDARD_TX_WEIGHT as u64) {
314319
return Err(AbortReason::TransactionTooLarge);
315320
}
316321

317322
Ok(constructed_tx)
318323
}
319324

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-
});
325+
fn weight(&self, satisfaction_weight: Weight) -> Weight {
326+
let inputs_weight = Weight::from_wu(self.inputs.len() as u64 * BASE_INPUT_WEIGHT)
327+
.checked_add(satisfaction_weight)
328+
.unwrap_or(Weight::MAX);
324329
let outputs_weight = self.outputs.iter().fold(Weight::from_wu(0), |weight, output| {
325330
weight.checked_add(get_output_weight(output.script_pubkey())).unwrap_or(Weight::MAX)
326331
});
@@ -1844,9 +1849,8 @@ impl InteractiveTxInput {
18441849
}
18451850

18461851
fn into_negotiated_input(self) -> NegotiatedTxInput {
1847-
let weight = Weight::from_wu(BASE_INPUT_WEIGHT) + self.input.satisfaction_weight();
18481852
let (txin, prev_output) = self.input.into_tx_in_with_prev_output();
1849-
NegotiatedTxInput { serial_id: self.serial_id, txin, weight, prev_output }
1853+
NegotiatedTxInput { serial_id: self.serial_id, txin, prev_output }
18501854
}
18511855
}
18521856

@@ -3316,7 +3320,6 @@ mod tests {
33163320
NegotiatedTxInput {
33173321
serial_id: idx as u64, // even values will be holder (initiator in this test)
33183322
txin,
3319-
weight: Weight::from_wu(0), // N/A for test
33203323
prev_output,
33213324
}
33223325
})

0 commit comments

Comments
 (0)