Merge pull request #417 from shesek/202003-block-size-weight

Add Block::get_size() and Block::get_weight()
This commit is contained in:
Andrew Poelstra 2020-04-07 14:12:15 +00:00 committed by GitHub
commit c3a7d1b27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 1 deletions

View File

@ -29,7 +29,8 @@ use util::uint::Uint256;
use consensus::encode::Encodable;
use network::constants::Network;
use blockdata::transaction::Transaction;
use blockdata::constants::max_target;
use blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
use VarInt;
/// A block header, which contains all the block's information except
/// the actual transactions
@ -124,6 +125,21 @@ impl Block {
);
bitcoin_merkle_root(hashes).into()
}
/// Get the size of the block
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();
base_size + txs_size
}
/// Get the weight of the block
pub fn get_weight(&self) -> usize {
let base_weight = WITNESS_SCALE_FACTOR * (80 + VarInt(self.txdata.len() as u64).len());
let txs_weight: usize = self.txdata.iter().map(Transaction::get_weight).sum();
base_weight + txs_weight
}
}
impl BlockHeader {
@ -240,6 +256,9 @@ mod tests {
assert_eq!(real_decode.header.nonce, 2067413810);
// [test] TODO: check the transaction data
assert_eq!(real_decode.get_size(), some_block.len());
assert_eq!(real_decode.get_weight(), some_block.len() * 4);
// should be also ok for a non-witness block as commitment is optional in that case
assert!(real_decode.check_witness_commitment());
@ -267,6 +286,9 @@ mod tests {
assert_eq!(real_decode.header.nonce, 1879759182);
// [test] TODO: check the transaction data
assert_eq!(real_decode.get_size(), segwit_block.len());
assert_eq!(real_decode.get_weight(), 17168);
assert!(real_decode.check_witness_commitment());
assert_eq!(serialize(&real_decode), segwit_block);