add weight method to InputWeightPrediction

This method computes the weight an InputWeightPrediction
would to a transaction, not including witness flag bytes.
This commit is contained in:
conduition 2023-11-21 05:57:46 +00:00
parent 8aa5501827
commit b5ce219c62
1 changed files with 8 additions and 2 deletions

View File

@ -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)]