Skip to content

Commit d0b3aad

Browse files
TheBlueMattAntoine Riard
authored and
Antoine Riard
committed
Assert that our weight prediction can't result in unspendable txn
1 parent bca12ea commit d0b3aad

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/ln/channelmonitor.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,11 @@ impl ChannelMonitor {
12091209
value: htlc.amount_msat / 1000,
12101210
}),
12111211
};
1212-
single_htlc_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * (single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }])) / 1000;
1212+
let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&vec![if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC }]);
1213+
single_htlc_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * predicted_weight / 1000;
12131214
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
12141215
sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
1216+
assert!(predicted_weight >= single_htlc_tx.get_weight());
12151217
txn_to_broadcast.push(single_htlc_tx);
12161218
}
12171219
}
@@ -1261,7 +1263,8 @@ impl ChannelMonitor {
12611263
input: inputs,
12621264
output: outputs,
12631265
};
1264-
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * (spend_tx.get_weight() + Self::get_witnesses_weight(&input_descriptors[..])) / 1000;
1266+
let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&input_descriptors);
1267+
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * predicted_weight / 1000;
12651268

12661269
let mut values_drain = values.drain(..);
12671270
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
@@ -1270,6 +1273,7 @@ impl ChannelMonitor {
12701273
let value = values_drain.next().unwrap();
12711274
sign_input!(sighash_parts, input, htlc_idx, value);
12721275
}
1276+
assert!(predicted_weight >= spend_tx.get_weight());
12731277

12741278
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
12751279
outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
@@ -1428,9 +1432,11 @@ impl ChannelMonitor {
14281432
value: htlc.amount_msat / 1000,
14291433
}),
14301434
};
1431-
single_htlc_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * (single_htlc_tx.get_weight() + Self::get_witnesses_weight(&[if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC }])) / 1000;
1435+
let predicted_weight = single_htlc_tx.get_weight() + Self::get_witnesses_weight(&vec![if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC }]);
1436+
single_htlc_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * predicted_weight / 1000;
14321437
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
14331438
sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
1439+
assert!(predicted_weight >= single_htlc_tx.get_weight());
14341440
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
14351441
outpoint: BitcoinOutPoint { txid: single_htlc_tx.txid(), vout: 0 },
14361442
output: single_htlc_tx.output[0].clone(),
@@ -1478,7 +1484,8 @@ impl ChannelMonitor {
14781484
input: inputs,
14791485
output: outputs,
14801486
};
1481-
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * (spend_tx.get_weight() + Self::get_witnesses_weight(&input_descriptors[..])) / 1000;
1487+
let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&input_descriptors);
1488+
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * predicted_weight / 1000;
14821489

14831490
let mut values_drain = values.drain(..);
14841491
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
@@ -1488,6 +1495,7 @@ impl ChannelMonitor {
14881495
sign_input!(sighash_parts, input, value.0, (value.1).0.to_vec());
14891496
}
14901497

1498+
assert!(predicted_weight >= spend_tx.get_weight());
14911499
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
14921500
outpoint: BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 },
14931501
output: spend_tx.output[0].clone(),
@@ -1562,7 +1570,8 @@ impl ChannelMonitor {
15621570
input: inputs,
15631571
output: outputs,
15641572
};
1565-
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * (spend_tx.get_weight() + Self::get_witnesses_weight(&vec![InputDescriptors::RevokedOutput])) / 1000;
1573+
let predicted_weight = spend_tx.get_weight() + Self::get_witnesses_weight(&vec![InputDescriptors::RevokedOutput]);
1574+
spend_tx.output[0].value -= fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * predicted_weight / 1000;
15661575

15671576
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
15681577

@@ -1581,6 +1590,7 @@ impl ChannelMonitor {
15811590
spend_tx.input[0].witness.push(vec!(1));
15821591
spend_tx.input[0].witness.push(redeemscript.into_bytes());
15831592

1593+
assert!(predicted_weight >= spend_tx.get_weight());
15841594
let outpoint = BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 };
15851595
let output = spend_tx.output[0].clone();
15861596
(Some(spend_tx), Some(SpendableOutputDescriptor::StaticOutput { outpoint, output }))

0 commit comments

Comments
 (0)