Use Weight type for scaled_size

This commit is contained in:
yancy 2023-08-29 15:17:16 +02:00
parent e746341f33
commit f796d6fef9
1 changed files with 22 additions and 21 deletions

View File

@ -693,7 +693,7 @@ impl Transaction {
/// Returns the regular byte-wise consensus-serialized size of this transaction. /// Returns the regular byte-wise consensus-serialized size of this transaction.
#[inline] #[inline]
pub fn size(&self) -> usize { self.scaled_size(1) } pub fn size(&self) -> usize { self.scaled_size(1).to_wu() as usize }
/// Returns the "virtual size" (vsize) of this transaction. /// Returns the "virtual size" (vsize) of this transaction.
/// ///
@ -741,39 +741,40 @@ impl Transaction {
} }
/// Internal utility function for size/weight functions. /// Internal utility function for size/weight functions.
fn scaled_size(&self, scale_factor: usize) -> usize { fn scaled_size(&self, scale_factor: u64) -> Weight {
let mut input_weight = 0; let mut input_weight: Weight = Weight::ZERO;
let mut inputs_with_witnesses = 0; let mut inputs_with_witnesses = 0;
for input in &self.input { for input in &self.input {
input_weight += scale_factor let non_scaled_input_weight: Weight = TxIn::BASE_WEIGHT
* (TxIn::BASE_WEIGHT.to_wu() as usize + Weight::from_wu_usize(VarInt(input.script_sig.len() as u64).len())
+ VarInt(input.script_sig.len() as u64).len() + Weight::from_wu_usize(input.script_sig.len());
+ input.script_sig.len()); input_weight += non_scaled_input_weight * scale_factor;
if !input.witness.is_empty() { if !input.witness.is_empty() {
inputs_with_witnesses += 1; inputs_with_witnesses += 1;
input_weight += input.witness.serialized_len(); input_weight += Weight::from_wu_usize(input.witness.serialized_len());
} }
} }
let mut output_size = 0; let mut output_size = Weight::ZERO;
for output in &self.output { for output in &self.output {
output_size += 8 + // value output_size += Weight::from_wu(8)+ // value
VarInt(output.script_pubkey.len() as u64).len() + Weight::from_wu_usize(VarInt(output.script_pubkey.len() as u64).len()) +
output.script_pubkey.len(); Weight::from_wu_usize(output.script_pubkey.len());
} }
let non_input_size = let non_input_size =
// version: // version:
4 + Weight::from_wu(4) +
// count varints: // count varints:
VarInt(self.input.len() as u64).len() + Weight::from_wu_usize(VarInt(self.input.len() as u64).len()) +
VarInt(self.output.len() as u64).len() + Weight::from_wu_usize(VarInt(self.output.len() as u64).len()) +
output_size + output_size +
// lock_time // lock_time
4; Weight::from_wu(4);
if inputs_with_witnesses == 0 { if inputs_with_witnesses == 0 {
non_input_size * scale_factor + input_weight non_input_size.checked_mul(scale_factor).unwrap() + input_weight
} else { } else {
non_input_size * scale_factor + input_weight + self.input.len() - inputs_with_witnesses non_input_size.checked_mul(scale_factor).unwrap()
+ 2 + input_weight
+ Weight::from_wu_usize(self.input.len() - inputs_with_witnesses + 2)
} }
} }
@ -1397,7 +1398,7 @@ mod tests {
assert_eq!(realtx.size(), tx_bytes.len()); assert_eq!(realtx.size(), tx_bytes.len());
assert_eq!(realtx.vsize(), tx_bytes.len()); assert_eq!(realtx.vsize(), tx_bytes.len());
assert_eq!(realtx.stripped_size(), Weight::from_wu_usize(tx_bytes.len())); assert_eq!(realtx.stripped_size(), Weight::from_wu_usize(tx_bytes.len()));
assert_eq!(realtx.scaled_size(4), 772); assert_eq!(realtx.scaled_size(4), Weight::from_wu(772));
} }
#[test] #[test]
@ -1458,7 +1459,7 @@ mod tests {
assert_eq!(Weight::from_wu_usize(tx_without_witness.size()), expected_strippedsize); assert_eq!(Weight::from_wu_usize(tx_without_witness.size()), expected_strippedsize);
assert_eq!(Weight::from_wu_usize(tx_without_witness.vsize()), expected_strippedsize); assert_eq!(Weight::from_wu_usize(tx_without_witness.vsize()), expected_strippedsize);
assert_eq!(tx_without_witness.stripped_size(), expected_strippedsize); assert_eq!(tx_without_witness.stripped_size(), expected_strippedsize);
assert_eq!(tx_without_witness.scaled_size(1), 83); assert_eq!(tx_without_witness.scaled_size(1), Weight::from_wu(83));
} }
// We temporarily abuse `Transaction` for testing consensus serde adapter. // We temporarily abuse `Transaction` for testing consensus serde adapter.