Use Weight type for stripped_size

This commit is contained in:
yancy 2023-08-25 09:19:05 +02:00
parent cb76f3ec43
commit 142dde64c3
2 changed files with 30 additions and 28 deletions

View File

@ -291,12 +291,12 @@ impl Block {
since = "0.31.0",
note = "Truncates on 32-bit machines, Use Block::stripped_size() instead"
)]
pub fn strippedsize(&self) -> usize { Self::stripped_size(self) }
pub fn strippedsize(&self) -> usize { Self::stripped_size(self).to_wu() as usize }
/// Returns the stripped size of the block.
pub fn stripped_size(&self) -> usize {
let txs_size: usize = self.txdata.iter().map(Transaction::stripped_size).sum();
self.base_size() + txs_size
pub fn stripped_size(&self) -> Weight {
let txs_size: Weight = self.txdata.iter().map(Transaction::stripped_size).sum();
Weight::from_wu_usize(self.base_size()) + txs_size
}
/// Returns the weight of the block.
@ -488,7 +488,7 @@ mod tests {
// [test] TODO: check the transaction data
assert_eq!(real_decode.size(), some_block.len());
assert_eq!(real_decode.stripped_size(), some_block.len());
assert_eq!(real_decode.stripped_size(), Weight::from_wu_usize(some_block.len()));
assert_eq!(
real_decode.weight(),
Weight::from_non_witness_data_size(some_block.len() as u64)
@ -530,7 +530,7 @@ mod tests {
// [test] TODO: check the transaction data
assert_eq!(real_decode.size(), segwit_block.len());
assert_eq!(real_decode.stripped_size(), 4283);
assert_eq!(real_decode.stripped_size(), Weight::from_wu(4283));
assert_eq!(real_decode.weight(), Weight::from_wu(17168));
assert!(real_decode.check_witness_commitment());

View File

@ -714,31 +714,31 @@ impl Transaction {
/// Returns the size of this transaction excluding the witness data.
#[deprecated(since = "0.31.0", note = "Use Transaction::stripped_size() instead")]
pub fn strippedsize(&self) -> usize { Self::stripped_size(self) }
pub fn strippedsize(&self) -> usize { Self::stripped_size(self).to_wu() as usize }
/// Returns the size of this transaction excluding the witness data.
pub fn stripped_size(&self) -> usize {
let mut input_size = 0;
pub fn stripped_size(&self) -> Weight {
let mut input_size: Weight = Weight::ZERO;
for input in &self.input {
input_size += TxIn::BASE_WEIGHT.to_wu() as usize
+ VarInt(input.script_sig.len() as u64).len()
+ input.script_sig.len();
input_size += TxIn::BASE_WEIGHT
+ Weight::from_wu_usize(VarInt(input.script_sig.len() as u64).len())
+ Weight::from_wu_usize(input.script_sig.len());
}
let mut output_size = 0;
let mut output_size = Weight::ZERO;
for output in &self.output {
output_size += 8 + // value
VarInt(output.script_pubkey.len() as u64).len() +
output.script_pubkey.len();
output_size += Weight::from_wu(8)+ // value
Weight::from_wu_usize(VarInt(output.script_pubkey.len() as u64).len()) +
Weight::from_wu_usize(output.script_pubkey.len());
}
let non_input_size =
let non_input_size: Weight =
// version:
4 +
Weight::from_wu(4)+
// count varints:
VarInt(self.input.len() as u64).len() +
VarInt(self.output.len() as u64).len() +
Weight::from_wu_usize(VarInt(self.input.len() as u64).len()) +
Weight::from_wu_usize(VarInt(self.output.len() as u64).len()) +
output_size +
// lock_time
4;
Weight::from_wu(4);
non_input_size + input_size
}
@ -1398,7 +1398,7 @@ mod tests {
assert_eq!(realtx.weight().to_wu() as usize, tx_bytes.len() * WITNESS_SCALE_FACTOR);
assert_eq!(realtx.size(), tx_bytes.len());
assert_eq!(realtx.vsize(), tx_bytes.len());
assert_eq!(realtx.stripped_size(), tx_bytes.len());
assert_eq!(realtx.stripped_size(), Weight::from_wu_usize(tx_bytes.len()));
}
#[test]
@ -1444,18 +1444,20 @@ mod tests {
// weight = WITNESS_SCALE_FACTOR * stripped_size + witness_size
// then,
// stripped_size = (weight - size) / (WITNESS_SCALE_FACTOR - 1)
let expected_strippedsize =
(EXPECTED_WEIGHT.to_wu() as usize - tx_bytes.len()) / (WITNESS_SCALE_FACTOR - 1);
let expected_strippedsize: Weight = Weight::from_wu(
(EXPECTED_WEIGHT - Weight::from_wu_usize(tx_bytes.len()))
/ (Weight::from_wu_usize(WITNESS_SCALE_FACTOR - 1)),
);
assert_eq!(realtx.stripped_size(), expected_strippedsize);
// Construct a transaction without the witness data.
let mut tx_without_witness = realtx;
tx_without_witness.input.iter_mut().for_each(|input| input.witness.clear());
assert_eq!(
tx_without_witness.weight().to_wu() as usize,
expected_strippedsize * WITNESS_SCALE_FACTOR
tx_without_witness.weight(),
expected_strippedsize.scale_by_witness_factor().unwrap()
);
assert_eq!(tx_without_witness.size(), expected_strippedsize);
assert_eq!(tx_without_witness.vsize(), 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!(tx_without_witness.stripped_size(), expected_strippedsize);
}