Skip to content

Commit b4803c7

Browse files
committed
Fix MAX_STANDARD_TX_WEIGHT check
The interactive-tx construction protocol needs to make sure the constructed transaction does not exceed MAX_STANDARD_TX_WEIGHT. A naive estimate of the transaction weight after signing was used, but was not accurate. Specifically, it double-counted EMPTY_SCRIPT_SIG_WEIGHT and didn't include SEGWIT_MARKER_FLAG_WEIGHT.
1 parent 3aa4574 commit b4803c7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lightning/src/ln/interactivetxs.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use bitcoin::{
2929
use crate::chain::chaininterface::fee_for_weight;
3030
use crate::ln::chan_utils::{
3131
BASE_INPUT_WEIGHT, EMPTY_SCRIPT_SIG_WEIGHT, FUNDING_TRANSACTION_WITNESS_WEIGHT,
32+
SEGWIT_MARKER_FLAG_WEIGHT,
3233
};
3334
use crate::ln::channel::{FundingNegotiationContext, TOTAL_BITCOIN_SUPPLY_SATOSHIS};
3435
use crate::ln::funding::FundingTxInput;
@@ -266,10 +267,11 @@ impl ConstructedTransaction {
266267
let remote_outputs_value = context.remote_outputs_value();
267268
let remote_weight_contributed = context.remote_weight_contributed();
268269

269-
let satisfaction_weight =
270-
Weight::from_wu(context.inputs.iter().fold(0u64, |value, (_, input)| {
271-
value.saturating_add(input.satisfaction_weight().to_wu())
272-
}));
270+
let expected_witness_weight = context.inputs.iter().fold(0u64, |value, (_, input)| {
271+
value
272+
.saturating_add(input.satisfaction_weight().to_wu())
273+
.saturating_sub(EMPTY_SCRIPT_SIG_WEIGHT)
274+
});
273275

274276
let lock_time = context.tx_locktime;
275277

@@ -342,8 +344,13 @@ impl ConstructedTransaction {
342344
return Err(AbortReason::MissingFundingOutput);
343345
}
344346

345-
let tx_weight = tx.tx.weight().checked_add(satisfaction_weight).unwrap_or(Weight::MAX);
346-
if tx_weight > Weight::from_wu(MAX_STANDARD_TX_WEIGHT as u64) {
347+
let tx_weight = tx
348+
.tx
349+
.weight()
350+
.to_wu()
351+
.saturating_add(SEGWIT_MARKER_FLAG_WEIGHT)
352+
.saturating_add(expected_witness_weight);
353+
if tx_weight > MAX_STANDARD_TX_WEIGHT as u64 {
347354
return Err(AbortReason::TransactionTooLarge);
348355
}
349356

0 commit comments

Comments
 (0)