Refactoring: define `Block.get_base_size()`.

This commit is contained in:
Vis Virial 2021-06-28 20:07:07 +09:00
parent 4ac9cef9e9
commit 2085dc32a7
No known key found for this signature in database
GPG Key ID: 7CAA10504DE829B9
1 changed files with 9 additions and 7 deletions

View File

@ -231,18 +231,20 @@ impl Block {
bitcoin_merkle_root(hashes).into() bitcoin_merkle_root(hashes).into()
} }
/// The size of the header + the size of the varint with the tx count + the txs themselves
#[inline]
fn get_base_size(&self) -> usize {
80 + VarInt(self.txdata.len() as u64).len()
}
/// Get the size of the block /// Get the size of the block
pub fn get_size(&self) -> usize { pub fn get_size(&self) -> usize {
// The size of the header + the size of the varint with the tx count + the txs themselves
let base_size = 80 + VarInt(self.txdata.len() as u64).len();
let txs_size: usize = self.txdata.iter().map(Transaction::get_size).sum(); let txs_size: usize = self.txdata.iter().map(Transaction::get_size).sum();
base_size + txs_size self.get_base_size() + txs_size
} }
/// Get the strippedsize of the block /// Get the strippedsize of the block
pub fn get_strippedsize(&self) -> usize { pub fn get_strippedsize(&self) -> usize {
// The size of the header + the size of the varint with the tx count + the txs themselves
let base_size = 80 + VarInt(self.txdata.len() as u64).len();
let txs_size: usize = self.txdata.iter().map(|tx| { let txs_size: usize = self.txdata.iter().map(|tx| {
// size = non_witness_size + witness_size. // size = non_witness_size + witness_size.
let size = tx.get_size(); let size = tx.get_size();
@ -251,12 +253,12 @@ impl Block {
// weight - size = (WITNESS_SCALE_FACTOR - 1) * non_witness_size. // weight - size = (WITNESS_SCALE_FACTOR - 1) * non_witness_size.
(weight - size) / (WITNESS_SCALE_FACTOR - 1) (weight - size) / (WITNESS_SCALE_FACTOR - 1)
}).sum(); }).sum();
base_size + txs_size self.get_base_size() + txs_size
} }
/// Get the weight of the block /// Get the weight of the block
pub fn get_weight(&self) -> usize { pub fn get_weight(&self) -> usize {
let base_weight = WITNESS_SCALE_FACTOR * (80 + VarInt(self.txdata.len() as u64).len()); let base_weight = WITNESS_SCALE_FACTOR * self.get_base_size();
let txs_weight: usize = self.txdata.iter().map(Transaction::get_weight).sum(); let txs_weight: usize = self.txdata.iter().map(Transaction::get_weight).sum();
base_weight + txs_weight base_weight + txs_weight
} }