From b5ce219c621378f7f6313aefb9a46011e4305e88 Mon Sep 17 00:00:00 2001 From: conduition Date: Tue, 21 Nov 2023 05:57:46 +0000 Subject: [PATCH] add weight method to InputWeightPrediction This method computes the weight an InputWeightPrediction would to a transaction, not including witness flag bytes. --- bitcoin/src/blockdata/transaction.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 68622ef1..f151c0bb 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -1202,7 +1202,7 @@ where |(count, partial_input_weight, inputs_with_witnesses), prediction| { ( count + 1, - partial_input_weight + prediction.script_size * 4 + prediction.witness_size, + partial_input_weight + prediction.weight().to_wu() as usize, inputs_with_witnesses + (prediction.witness_size > 0) as usize, ) }, @@ -1276,7 +1276,7 @@ pub const fn predict_weight_from_slices( let mut i = 0; while i < inputs.len() { let prediction = inputs[i]; - partial_input_weight += prediction.script_size * 4 + prediction.witness_size; + partial_input_weight += prediction.weight().to_wu() as usize; inputs_with_witnesses += (prediction.witness_size > 0) as usize; i += 1; } @@ -1440,6 +1440,12 @@ impl InputWeightPrediction { InputWeightPrediction { script_size, witness_size } } + + /// Tallies the total weight added to a transaction by an input with this weight prediction, + /// not counting potential witness flag bytes or the witness count varint. + pub const fn weight(&self) -> Weight { + Weight::from_wu_usize(self.script_size * 4 + self.witness_size) + } } #[cfg(test)]