Use Weight type for block base_size

This commit is contained in:
yancy 2023-08-29 15:11:47 +02:00
parent 9536a9947c
commit 97b7a2dee9
1 changed files with 7 additions and 5 deletions

View File

@ -276,14 +276,16 @@ impl Block {
} }
/// base_size == size of header + size of encoded transaction count. /// base_size == size of header + size of encoded transaction count.
fn base_size(&self) -> usize { 80 + VarInt::from(self.txdata.len()).len() } fn base_size(&self) -> Weight {
Weight::from_wu_usize(80 + VarInt::from(self.txdata.len()).len())
}
/// Returns the size of the block. /// Returns the size of the block.
/// ///
/// size == size of header + size of encoded transaction count + total size of transactions. /// size == size of header + size of encoded transaction count + total size of transactions.
pub fn size(&self) -> usize { pub fn size(&self) -> usize {
let txs_size: usize = self.txdata.iter().map(Transaction::size).sum(); let txs_size: usize = self.txdata.iter().map(Transaction::size).sum();
self.base_size() + txs_size self.base_size().to_wu() as usize + txs_size
} }
/// Returns the stripped size of the block. /// Returns the stripped size of the block.
@ -296,12 +298,12 @@ impl Block {
/// Returns the stripped size of the block. /// Returns the stripped size of the block.
pub fn stripped_size(&self) -> Weight { pub fn stripped_size(&self) -> Weight {
let txs_size: Weight = self.txdata.iter().map(Transaction::stripped_size).sum(); let txs_size: Weight = self.txdata.iter().map(Transaction::stripped_size).sum();
Weight::from_wu_usize(self.base_size()) + txs_size self.base_size() + txs_size
} }
/// Returns the weight of the block. /// Returns the weight of the block.
pub fn weight(&self) -> Weight { pub fn weight(&self) -> Weight {
let base_weight = Weight::from_non_witness_data_size(self.base_size() as u64); let base_weight = self.base_size() * Weight::WITNESS_SCALE_FACTOR;
let txs_weight: Weight = self.txdata.iter().map(Transaction::weight).sum(); let txs_weight: Weight = self.txdata.iter().map(Transaction::weight).sum();
base_weight + txs_weight base_weight + txs_weight
} }
@ -487,7 +489,7 @@ mod tests {
assert_eq!(real_decode.header.difficulty_float(), 1.0); assert_eq!(real_decode.header.difficulty_float(), 1.0);
// [test] TODO: check the transaction data // [test] TODO: check the transaction data
assert_eq!(real_decode.base_size(), 81); assert_eq!(real_decode.base_size(), Weight::from_wu(81));
assert_eq!(real_decode.size(), some_block.len()); assert_eq!(real_decode.size(), some_block.len());
assert_eq!(real_decode.stripped_size(), Weight::from_wu_usize(some_block.len())); assert_eq!(real_decode.stripped_size(), Weight::from_wu_usize(some_block.len()));
assert_eq!( assert_eq!(