Skip to content

Commit c17550b

Browse files
committed
test: MiniWallet: fix tx padding (target_weight) for large sizes, improve accuracy
1 parent ef44726 commit c17550b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

test/functional/test_framework/wallet.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
CTxInWitness,
3434
CTxOut,
3535
hash256,
36+
ser_compact_size,
3637
)
3738
from test_framework.script import (
3839
CScript,
3940
LEAF_VERSION_TAPSCRIPT,
41+
OP_1,
4042
OP_NOP,
4143
OP_RETURN,
4244
OP_TRUE,
@@ -119,13 +121,16 @@ def _bulk_tx(self, tx, target_weight):
119121
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
120122
returns the tx
121123
"""
122-
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN, b'a'])))
124+
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN])))
125+
# determine number of needed padding bytes by converting weight difference to vbytes
123126
dummy_vbytes = (target_weight - tx.get_weight() + 3) // 4
124-
tx.vout[-1].scriptPubKey = CScript([OP_RETURN, b'a' * dummy_vbytes])
125-
# Lower bound should always be off by at most 3
127+
# compensate for the increase of the compact-size encoded script length
128+
# (note that the length encoding of the unpadded output script needs one byte)
129+
dummy_vbytes -= len(ser_compact_size(dummy_vbytes)) - 1
130+
tx.vout[-1].scriptPubKey = CScript([OP_RETURN] + [OP_1] * dummy_vbytes)
131+
# Actual weight should be at most 3 higher than target weight
126132
assert_greater_than_or_equal(tx.get_weight(), target_weight)
127-
# Higher bound should always be off by at most 3 + 12 weight (for encoding the length)
128-
assert_greater_than_or_equal(target_weight + 15, tx.get_weight())
133+
assert_greater_than_or_equal(target_weight + 3, tx.get_weight())
129134

130135
def get_balance(self):
131136
return sum(u['value'] for u in self._utxos)

0 commit comments

Comments
 (0)