Skip to content

Commit ae06c73

Browse files
committed
test: add unit tests for calculate_input_weight
1 parent b441cad commit ae06c73

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

test/functional/test_framework/wallet_util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Useful util functions for testing the wallet"""
66
from collections import namedtuple
7+
import unittest
78

89
from test_framework.address import (
910
byte_to_base58,
@@ -158,3 +159,34 @@ def __enter__(self):
158159
def __exit__(self, *args):
159160
_ = args
160161
self.wallet.walletlock()
162+
163+
164+
class TestFrameworkWalletUtil(unittest.TestCase):
165+
def test_calculate_input_weight(self):
166+
SKELETON_BYTES = 32 + 4 + 4 # prevout-txid, prevout-index, sequence
167+
SMALL_LEN_BYTES = 1 # bytes needed for encoding scriptSig / witness item lenghts < 253
168+
LARGE_LEN_BYTES = 3 # bytes needed for encoding scriptSig / witness item lengths >= 253
169+
170+
# empty scriptSig, empty witness stack
171+
self.assertEqual(calculate_input_weight("", None),
172+
(SKELETON_BYTES + SMALL_LEN_BYTES) * WITNESS_SCALE_FACTOR)
173+
# small scriptSig, empty witness stack
174+
scriptSig_small = "00"*252
175+
self.assertEqual(calculate_input_weight(scriptSig_small, None),
176+
(SKELETON_BYTES + SMALL_LEN_BYTES + 252) * WITNESS_SCALE_FACTOR)
177+
# large scriptSig, empty witness stack
178+
scriptSig_large = "00"*253
179+
self.assertEqual(calculate_input_weight(scriptSig_large, None),
180+
(SKELETON_BYTES + LARGE_LEN_BYTES + 253) * WITNESS_SCALE_FACTOR)
181+
# empty scriptSig, 5 small witness stack items
182+
self.assertEqual(calculate_input_weight("", ["00", "11", "22", "33", "44"]),
183+
((SKELETON_BYTES + SMALL_LEN_BYTES) * WITNESS_SCALE_FACTOR) + SMALL_LEN_BYTES + 5 * SMALL_LEN_BYTES + 5)
184+
# empty scriptSig, 253 small witness stack items
185+
self.assertEqual(calculate_input_weight("", ["00"]*253),
186+
((SKELETON_BYTES + SMALL_LEN_BYTES) * WITNESS_SCALE_FACTOR) + LARGE_LEN_BYTES + 253 * SMALL_LEN_BYTES + 253)
187+
# small scriptSig, 3 large witness stack items
188+
self.assertEqual(calculate_input_weight(scriptSig_small, ["00"*253]*3),
189+
((SKELETON_BYTES + SMALL_LEN_BYTES + 252) * WITNESS_SCALE_FACTOR) + SMALL_LEN_BYTES + 3 * LARGE_LEN_BYTES + 3*253)
190+
# large scriptSig, 3 large witness stack items
191+
self.assertEqual(calculate_input_weight(scriptSig_large, ["00"*253]*3),
192+
((SKELETON_BYTES + LARGE_LEN_BYTES + 253) * WITNESS_SCALE_FACTOR) + SMALL_LEN_BYTES + 3 * LARGE_LEN_BYTES + 3*253)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"crypto.ripemd160",
8686
"script",
8787
"segwit_addr",
88+
"wallet_util",
8889
]
8990

9091
EXTENDED_SCRIPTS = [

0 commit comments

Comments
 (0)