Skip to content

Commit a084ebe

Browse files
committed
test: introduce get_weight() helper for CTransaction
1 parent 3fc20ab commit a084ebe

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

test/functional/feature_segwit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ def run_test(self):
260260
assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True))
261261

262262
# Check that weight and vsize are properly reported in mempool entry (txid1)
263-
assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], (self.nodes[0].getmempoolentry(txid1)["weight"] + 3) // 4)
264-
assert_equal(self.nodes[0].getmempoolentry(txid1)["weight"], len(tx1.serialize_without_witness())*3 + len(tx1.serialize_with_witness()))
263+
assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize())
264+
assert_equal(self.nodes[0].getmempoolentry(txid1)["weight"], tx1.get_weight())
265265

266266
# Now create tx2, which will spend from txid1.
267267
tx = CTransaction()
@@ -276,8 +276,8 @@ def run_test(self):
276276
assert_equal(int(self.nodes[0].getmempoolentry(txid2)["wtxid"], 16), tx.calc_sha256(True))
277277

278278
# Check that weight and vsize are properly reported in mempool entry (txid2)
279-
assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], (self.nodes[0].getmempoolentry(txid2)["weight"] + 3) // 4)
280-
assert_equal(self.nodes[0].getmempoolentry(txid2)["weight"], len(tx.serialize_without_witness())*3 + len(tx.serialize_with_witness()))
279+
assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize())
280+
assert_equal(self.nodes[0].getmempoolentry(txid2)["weight"], tx.get_weight())
281281

282282
# Now create tx3, which will spend from txid2
283283
tx = CTransaction()
@@ -299,8 +299,8 @@ def run_test(self):
299299
assert_equal(int(self.nodes[0].getmempoolentry(txid3)["wtxid"], 16), tx.calc_sha256(True))
300300

301301
# Check that weight and vsize are properly reported in mempool entry (txid3)
302-
assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], (self.nodes[0].getmempoolentry(txid3)["weight"] + 3) // 4)
303-
assert_equal(self.nodes[0].getmempoolentry(txid3)["weight"], len(tx.serialize_without_witness())*3 + len(tx.serialize_with_witness()))
302+
assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize())
303+
assert_equal(self.nodes[0].getmempoolentry(txid3)["weight"], tx.get_weight())
304304

305305
# Mine a block to clear the gbt cache again.
306306
self.nodes[0].generate(1)

test/functional/p2p_segwit.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test segwit transactions and blocks on P2P network."""
66
from decimal import Decimal
7-
import math
87
import random
98
import struct
109
import time
@@ -1367,10 +1366,9 @@ def test_tx_relay_after_segwit_activation(self):
13671366
raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1)
13681367
assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True))
13691368
assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
1370-
weight = len(tx3.serialize_with_witness()) + 3 * len(tx3.serialize_without_witness())
1371-
vsize = math.ceil(weight / 4)
1369+
vsize = tx3.get_vsize()
13721370
assert_equal(raw_tx["vsize"], vsize)
1373-
assert_equal(raw_tx["weight"], weight)
1371+
assert_equal(raw_tx["weight"], tx3.get_weight())
13741372
assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
13751373
assert_equal(raw_tx["vin"][0]["txinwitness"][0], witness_program.hex())
13761374
assert vsize != raw_tx["size"]

test/functional/test_framework/messages.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,15 @@ def is_valid(self):
590590
return False
591591
return True
592592

593-
# Calculate the virtual transaction size using witness and non-witness
593+
# Calculate the transaction weight using witness and non-witness
594594
# serialization size (does NOT use sigops).
595-
def get_vsize(self):
595+
def get_weight(self):
596596
with_witness_size = len(self.serialize_with_witness())
597597
without_witness_size = len(self.serialize_without_witness())
598-
return math.ceil(((WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size) / WITNESS_SCALE_FACTOR)
598+
return (WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size
599+
600+
def get_vsize(self):
601+
return math.ceil(self.get_weight() / WITNESS_SCALE_FACTOR)
599602

600603
def __repr__(self):
601604
return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \

0 commit comments

Comments
 (0)