Merge rust-bitcoin/rust-bitcoin#3663: Fix internal weight calculation

f961f3c0ec Add test (yancy)
3352032892 Fix internal weight calculation (yancy)

Pull request description:

  A fix for 261c8d8ae6.  Maybe it's better to roll back that commit instead.  Clearly there needs to be a test or two added here as well since this change causes not test failures.

ACKs for top commit:
  apoelstra:
    ACK f961f3c0eca4e9b175761756f48699012b63d70a; successfully ran local tests
  sanket1729:
    utACK f961f3c0ec
  tcharding:
    ACK f961f3c0ec

Tree-SHA512: e6bb6d492b6a85f328bdfae6d0e5df9ea48c9c06bd0c310816826c893163bd0a6071af893fa7012fbd4eea0394bc8997277151486807fb8bd06ac78ce8cfc5fd
This commit is contained in:
merge-script 2024-11-28 12:52:53 +00:00
commit 4123f2c00f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 18 additions and 3 deletions

View File

@ -920,14 +920,14 @@ pub const fn predict_weight_from_slices(
inputs: &[InputWeightPrediction],
output_script_lens: &[usize],
) -> Weight {
let mut partial_input_weight = 0;
let mut input_weight = 0;
let mut inputs_with_witnesses = 0;
// for loops not supported in const fn
let mut i = 0;
while i < inputs.len() {
let prediction = inputs[i];
partial_input_weight += prediction.witness_weight().to_wu() as usize;
input_weight += prediction.total_weight().to_wu() as usize;
inputs_with_witnesses += (prediction.witness_size > 0) as usize;
i += 1;
}
@ -943,7 +943,7 @@ pub const fn predict_weight_from_slices(
predict_weight_internal(
inputs.len(),
partial_input_weight,
input_weight,
inputs_with_witnesses,
output_script_lens.len(),
output_scripts_size,
@ -1964,6 +1964,21 @@ mod tests {
);
}
#[test]
fn weight_prediction_const_from_slices() {
let predict = [
InputWeightPrediction::P2WPKH_MAX,
InputWeightPrediction::NESTED_P2WPKH_MAX,
InputWeightPrediction::P2PKH_COMPRESSED_MAX,
InputWeightPrediction::P2PKH_UNCOMPRESSED_MAX,
InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH,
InputWeightPrediction::P2TR_KEY_NON_DEFAULT_SIGHASH
];
let weight = predict_weight_from_slices(&predict, &[1]);
assert_eq!(weight, Weight::from_wu(2493));
}
#[test]
fn sequence_debug_output() {
let seq = Sequence::from_seconds_floor(1000);