Skip to content

Commit 1c7ba90

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

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/ln/channelmonitor.rs

Lines changed: 18 additions & 8 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(&[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(&[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(&[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 }))
@@ -2831,7 +2841,7 @@ mod tests {
28312841
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
28322842
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
28332843
}
2834-
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
2844+
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
28352845

28362846
// Claim tx with 1 offered HTLCs, 3 received HTLCs
28372847
claim_tx.input.clear();
@@ -2853,7 +2863,7 @@ mod tests {
28532863
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
28542864
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
28552865
}
2856-
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
2866+
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
28572867

28582868
// Justice tx with 1 revoked HTLC-Success tx output
28592869
claim_tx.input.clear();
@@ -2873,7 +2883,7 @@ mod tests {
28732883
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
28742884
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
28752885
}
2876-
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
2886+
assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() as u64 - sum_actual_sigs));
28772887
}
28782888

28792889
// Further testing is done in the ChannelManager integration tests.

0 commit comments

Comments
 (0)