diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 0ed381ef4..137110f4f 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -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);