test: switch MiniWallet padding unit from weight to vsize

The weight unit is merely a consensus rule detail and is largely
irrelevant for fee-rate calculations and mempool policy rules (e.g. for
package relay and TRUC limits), so there doesn't seem to be any value of
using a granularity that we can't even guarantee to reach exactly
anyway.

Switch to the more natural unit of vsize instead, which simplifies both
the padding implementation and the current tests that take use of this
padding. The rather annoying multiplications by `WITNESS_SCALE_FACTOR`
can then be removed and weird-looking magic numbers like `4004` can be
replaced by numbers that are more connected to actual policy limit
constants from the codebase, e.g. `1001` for exceeding
`TRUC_CHILD_MAX_VSIZE` by one.
This commit is contained in:
Sebastian Falbesoner
2024-08-26 19:35:00 +02:00
parent d812cf1189
commit c16ae71768
6 changed files with 61 additions and 72 deletions

View File

@@ -7,7 +7,6 @@
from copy import deepcopy
from decimal import Decimal
from enum import Enum
import math
from typing import (
Any,
Optional,
@@ -35,7 +34,6 @@ from test_framework.messages import (
CTxOut,
hash256,
ser_compact_size,
WITNESS_SCALE_FACTOR,
)
from test_framework.script import (
CScript,
@@ -119,20 +117,18 @@ class MiniWallet:
def _create_utxo(self, *, txid, vout, value, height, coinbase, confirmations):
return {"txid": txid, "vout": vout, "value": value, "height": height, "coinbase": coinbase, "confirmations": confirmations}
def _bulk_tx(self, tx, target_weight):
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
def _bulk_tx(self, tx, target_vsize):
"""Pad a transaction with extra outputs until it reaches a target vsize.
returns the tx
"""
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN])))
# determine number of needed padding bytes by converting weight difference to vbytes
dummy_vbytes = (target_weight - tx.get_weight() + 3) // 4
# determine number of needed padding bytes
dummy_vbytes = target_vsize - tx.get_vsize()
# compensate for the increase of the compact-size encoded script length
# (note that the length encoding of the unpadded output script needs one byte)
dummy_vbytes -= len(ser_compact_size(dummy_vbytes)) - 1
tx.vout[-1].scriptPubKey = CScript([OP_RETURN] + [OP_1] * dummy_vbytes)
# Actual weight should be at most 3 higher than target weight
assert_greater_than_or_equal(tx.get_weight(), target_weight)
assert_greater_than_or_equal(target_weight + 3, tx.get_weight())
assert_equal(tx.get_vsize(), target_vsize)
def get_balance(self):
return sum(u['value'] for u in self._utxos)
@@ -309,7 +305,7 @@ class MiniWallet:
locktime=0,
sequence=0,
fee_per_output=1000,
target_weight=0,
target_vsize=0,
confirmed_only=False,
):
"""
@@ -338,8 +334,8 @@ class MiniWallet:
self.sign_tx(tx)
if target_weight:
self._bulk_tx(tx, target_weight)
if target_vsize:
self._bulk_tx(tx, target_vsize)
txid = tx.rehash()
return {
@@ -364,7 +360,7 @@ class MiniWallet:
fee_rate=Decimal("0.003"),
fee=Decimal("0"),
utxo_to_spend=None,
target_weight=0,
target_vsize=0,
confirmed_only=False,
**kwargs,
):
@@ -379,20 +375,18 @@ class MiniWallet:
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
else:
assert False
if target_weight and not fee: # respect fee_rate if target weight is passed
# the actual weight might be off by 3 WUs, so calculate based on that (see self._bulk_tx)
max_actual_weight = target_weight + 3
fee = get_fee(math.ceil(max_actual_weight / WITNESS_SCALE_FACTOR), fee_rate)
if target_vsize and not fee: # respect fee_rate if target vsize is passed
fee = get_fee(target_vsize, fee_rate)
send_value = utxo_to_spend["value"] - (fee or (fee_rate * vsize / 1000))
# create tx
tx = self.create_self_transfer_multi(
utxos_to_spend=[utxo_to_spend],
amount_per_output=int(COIN * send_value),
target_weight=target_weight,
target_vsize=target_vsize,
**kwargs,
)
if not target_weight:
if not target_vsize:
assert_equal(tx["tx"].get_vsize(), vsize)
tx["new_utxo"] = tx.pop("new_utxos")[0]