Skip to content

Commit 9b7e431

Browse files
committed
Merge #44: Add Block::get_size() and Block::get_weight()
c8cc244 Add Block::get_size() and Block::get_weight() (Nadav Ivgi) Pull request description: ACKs for top commit: jonasnick: utACK c8cc244 Tree-SHA512: dec77d355b1309ca0d2b241eb117fb2b004800784f76b1c1ee21869f5b719b118308607b448dc0cfc3c069e12d73ee028708ed159f9f6efd222df6a9842812b0
2 parents 08dc933 + c8cc244 commit 9b7e431

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/block.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ use std::io;
1919

2020
use bitcoin;
2121
use bitcoin::blockdata::script::Script;
22-
use bitcoin::{BitcoinHash, BlockHash};
22+
use bitcoin::{BitcoinHash, BlockHash, VarInt};
2323
use bitcoin::hashes::{Hash, sha256};
2424
#[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer};
2525
#[cfg(feature = "serde")] use std::fmt;
2626

2727
use dynafed;
2828
use Transaction;
29-
use encode::{self, Encodable, Decodable};
29+
use encode::{self, Encodable, Decodable, serialize};
3030

3131
/// Data related to block signatures
3232
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
@@ -354,6 +354,23 @@ pub struct Block {
354354
serde_struct_impl!(Block, header, txdata);
355355
impl_consensus_encoding!(Block, header, txdata);
356356

357+
impl Block {
358+
/// Get the size of the block
359+
pub fn get_size(&self) -> usize {
360+
// The size of the header + the size of the varint with the tx count + the txs themselves
361+
let base_size = serialize(&self.header).len() + VarInt(self.txdata.len() as u64).len();
362+
let txs_size: usize = self.txdata.iter().map(Transaction::get_size).sum();
363+
base_size + txs_size
364+
}
365+
366+
/// Get the weight of the block
367+
pub fn get_weight(&self) -> usize {
368+
let base_weight = 4 * (serialize(&self.header).len() + VarInt(self.txdata.len() as u64).len());
369+
let txs_weight: usize = self.txdata.iter().map(Transaction::get_weight).sum();
370+
base_weight + txs_weight
371+
}
372+
}
373+
357374
impl BitcoinHash<BlockHash> for Block {
358375
fn bitcoin_hash(&self) -> BlockHash {
359376
self.header.bitcoin_hash()
@@ -391,6 +408,8 @@ mod tests {
391408
assert_eq!(block.header.version, 0x20000000);
392409
assert_eq!(block.header.height, 2);
393410
assert_eq!(block.txdata.len(), 1);
411+
assert_eq!(block.get_size(), serialize(&block).len());
412+
assert_eq!(block.get_weight(), 1089);
394413

395414
// Block with 3 transactions ... the rangeproofs are very large :)
396415
let block: Block = hex_deserialize!(
@@ -601,6 +620,7 @@ mod tests {
601620
assert_eq!(block.header.version, 0x20000000);
602621
assert_eq!(block.header.height, 1);
603622
assert_eq!(block.txdata.len(), 3);
623+
assert_eq!(block.get_size(), serialize(&block).len());
604624

605625
// 2-of-3 signed block from Liquid integration tests
606626
let block: Block = hex_deserialize!(

0 commit comments

Comments
 (0)