Make block_size free standing and rename

In preparation for adding a block extension trait move the only private
method off the `Block` type and make it free standing.

While we are at it just take a slice of transactions since we don't need
the block itself - rename appropriately.

Internal change only.
This commit is contained in:
Tobin C. Harding 2024-10-31 16:08:04 +11:00
parent 5016a73207
commit d5b148d400
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 16 additions and 16 deletions

View File

@ -197,23 +197,10 @@ impl Block {
/// > Block weight is defined as Base size * 3 + Total size.
pub fn weight(&self) -> Weight {
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
let wu = self.base_size() * 3 + self.total_size();
let wu = block_base_size(&self.txdata) * 3 + self.total_size();
Weight::from_wu_usize(wu)
}
/// Returns the base block size.
///
/// > Base size is the block size in bytes with the original transaction serialization without
/// > any witness-related data, as seen by a non-upgraded node.
fn base_size(&self) -> usize {
let mut size = Header::SIZE;
size += compact_size::encoded_size(self.txdata.len());
size += self.txdata.iter().map(|tx| tx.base_size()).sum::<usize>();
size
}
/// Returns the total block size.
///
/// > Total size is the block size in bytes with transactions serialized as described in BIP144,
@ -262,6 +249,19 @@ impl Block {
}
}
/// Returns the base block size.
///
/// > Base size is the block size in bytes with the original transaction serialization without
/// > any witness-related data, as seen by a non-upgraded node.
fn block_base_size(transactions: &[Transaction]) -> usize {
let mut size = Header::SIZE;
size += compact_size::encoded_size(transactions.len());
size += transactions.iter().map(|tx| tx.base_size()).sum::<usize>();
size
}
impl From<Block> for BlockHash {
fn from(block: Block) -> BlockHash { block.block_hash() }
}
@ -476,7 +476,7 @@ mod tests {
assert_eq!(real_decode.header.difficulty_float(&params), 1.0);
assert_eq!(real_decode.total_size(), some_block.len());
assert_eq!(real_decode.base_size(), some_block.len());
assert_eq!(block_base_size(&real_decode.txdata), some_block.len());
assert_eq!(
real_decode.weight(),
Weight::from_non_witness_data_size(some_block.len().to_u64())
@ -518,7 +518,7 @@ mod tests {
assert_eq!(real_decode.header.difficulty_float(&params), 2456598.4399242126);
assert_eq!(real_decode.total_size(), segwit_block.len());
assert_eq!(real_decode.base_size(), 4283);
assert_eq!(block_base_size(&real_decode.txdata), 4283);
assert_eq!(real_decode.weight(), Weight::from_wu(17168));
assert!(real_decode.check_witness_commitment());