Skip to content

Commit

Permalink
Update test_framework/wallet.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
janus committed Aug 27, 2024
1 parent d48b032 commit a650fec
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions test/functional/test_framework/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from copy import deepcopy
from decimal import Decimal
from enum import Enum
import math
from typing import (
Any,
Optional,
Expand All @@ -33,10 +34,13 @@
CTxInWitness,
CTxOut,
hash256,
ser_compact_size,
WITNESS_SCALE_FACTOR,
)
from test_framework.script import (
CScript,
LEAF_VERSION_TAPSCRIPT,
OP_1,
OP_NOP,
OP_RETURN,
OP_TRUE,
Expand All @@ -52,9 +56,8 @@
from test_framework.util import (
assert_equal,
assert_greater_than_or_equal,
get_fee,
)
from enum import Enum
from test_framework.blocktools import COINBASE_MATURITY
from test_framework.wallet_util import generate_keypair

DEFAULT_FEE = Decimal("0.0001")
Expand Down Expand Up @@ -109,7 +112,7 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE, tag_name=N

# When the pre-mined test framework chain is used, it contains coinbase
# outputs to the MiniWallet's default address in blocks 76-100
# (see method BGLTestFramework._initialize_chain())
# (see method BitcoinTestFramework._initialize_chain())
# The MiniWallet needs to rescan_utxos() in order to account
# for those mature UTXOs, so that all txs spend confirmed coins
self.rescan_utxos()
Expand All @@ -121,13 +124,16 @@ def _bulk_tx(self, tx, target_weight):
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
returns the tx
"""
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN, b'a'])))
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
tx.vout[-1].scriptPubKey = CScript([OP_RETURN, b'a' * dummy_vbytes])
# Lower bound should always be off by at most 3
# 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)
# Higher bound should always be off by at most 3 + 12 weight (for encoding the length)
assert_greater_than_or_equal(target_weight + 15, tx.get_weight())
assert_greater_than_or_equal(target_weight + 3, tx.get_weight())

def get_balance(self):
return sum(u['value'] for u in self._utxos)
Expand Down Expand Up @@ -369,6 +375,10 @@ def create_self_transfer(
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)
send_value = utxo_to_spend["value"] - (fee or (fee_rate * vsize / 1000))

# create tx
Expand Down

0 comments on commit a650fec

Please sign in to comment.