test: introduce get_weight() helper for CBlock

This commit is contained in:
Sebastian Falbesoner 2021-07-01 00:10:43 +02:00
parent a084ebe133
commit 4af97c74ed
2 changed files with 8 additions and 2 deletions

View File

@ -450,8 +450,7 @@ class SegWitTest(BitcoinTestFramework):
rpc_details = self.nodes[0].getblock(block.hash, True)
assert_equal(rpc_details["size"], len(block.serialize()))
assert_equal(rpc_details["strippedsize"], len(block.serialize(False)))
weight = 3 * len(block.serialize(False)) + len(block.serialize())
assert_equal(rpc_details["weight"], weight)
assert_equal(rpc_details["weight"], block.get_weight())
# Upgraded node should not ask for blocks from unupgraded
block4 = self.build_next_block(version=4)

View File

@ -746,6 +746,13 @@ class CBlock(CBlockHeader):
self.nNonce += 1
self.rehash()
# Calculate the block weight using witness and non-witness
# serialization size (does NOT use sigops).
def get_weight(self):
with_witness_size = len(self.serialize(with_witness=True))
without_witness_size = len(self.serialize(with_witness=False))
return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
def __repr__(self):
return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" \
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot,