Skip to content

Commit 23c35bf

Browse files
committed
[test] add get_vsize util for more programmatic testing
1 parent 2233a93 commit 23c35bf

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

test/functional/mempool_accept.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def run_test(self):
9292
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
9393
txid_0 = tx.rehash()
9494
self.check_mempool_result(
95-
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': 110, 'fees': {'base': Decimal(str(fee))}}],
95+
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee))}}],
9696
rawtxs=[raw_tx_0],
9797
)
9898

@@ -107,7 +107,7 @@ def run_test(self):
107107
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_final)))
108108
fee_expected = int(coin['amount']) - output_amount
109109
self.check_mempool_result(
110-
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': 188, 'fees': {'base': Decimal(str(fee_expected))}}],
110+
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee_expected))}}],
111111
rawtxs=[tx.serialize().hex()],
112112
maxfeerate=0,
113113
)
@@ -130,7 +130,7 @@ def run_test(self):
130130
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
131131
txid_0 = tx.rehash()
132132
self.check_mempool_result(
133-
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': 110, 'fees': {'base': Decimal(str(2 * fee))}}],
133+
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(2 * fee))}}],
134134
rawtxs=[raw_tx_0],
135135
)
136136

@@ -190,7 +190,7 @@ def run_test(self):
190190
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference)))
191191
# Reference tx should be valid on itself
192192
self.check_mempool_result(
193-
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': 110, 'fees': { 'base': Decimal(str(0.1 - 0.05))}}],
193+
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal(str(0.1 - 0.05))}}],
194194
rawtxs=[tx.serialize().hex()],
195195
maxfeerate=0,
196196
)

test/functional/p2p_segwit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,13 @@ def test_standardness_v0(self):
696696
if not self.segwit_active:
697697
# Just check mempool acceptance, but don't add the transaction to the mempool, since witness is disallowed
698698
# in blocks and the tx is impossible to mine right now.
699-
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': 93, 'fees': { 'base': Decimal('0.00001000')}}])
699+
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00001000')}}])
700700
# Create the same output as tx3, but by replacing tx
701701
tx3_out = tx3.vout[0]
702702
tx3 = tx
703703
tx3.vout = [tx3_out]
704704
tx3.rehash()
705-
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': 93, 'fees': { 'base': Decimal('0.00011000')}}])
705+
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00011000')}}])
706706
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
707707

708708
self.nodes[0].generate(1)

test/functional/test_framework/messages.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import copy
2323
import hashlib
2424
from io import BytesIO
25+
import math
2526
import random
2627
import socket
2728
import struct
@@ -67,6 +68,8 @@
6768

6869
FILTER_TYPE_BASIC = 0
6970

71+
WITNESS_SCALE_FACTOR = 4
72+
7073
# Serialization/deserialization tools
7174
def sha256(s):
7275
return hashlib.new('sha256', s).digest()
@@ -537,6 +540,13 @@ def is_valid(self):
537540
return False
538541
return True
539542

543+
# Calculate the virtual transaction size using witness and non-witness
544+
# serialization size (does NOT use sigops).
545+
def get_vsize(self):
546+
with_witness_size = len(self.serialize_with_witness())
547+
without_witness_size = len(self.serialize_without_witness())
548+
return math.ceil(((WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size) / WITNESS_SCALE_FACTOR)
549+
540550
def __repr__(self):
541551
return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
542552
% (self.nVersion, repr(self.vin), repr(self.vout), repr(self.wit), self.nLockTime)

0 commit comments

Comments
 (0)