mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-06 13:10:09 +02:00
test: use MiniWallet for make_utxo helper in feature_rbf.py
This commit is contained in:
parent
0f27524602
commit
f680d27155
@ -7,7 +7,6 @@
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
BIP125_SEQUENCE_NUMBER,
|
BIP125_SEQUENCE_NUMBER,
|
||||||
COIN,
|
COIN,
|
||||||
@ -18,10 +17,18 @@ from test_framework.messages import (
|
|||||||
)
|
)
|
||||||
from test_framework.script import CScript, OP_DROP
|
from test_framework.script import CScript, OP_DROP
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
|
from test_framework.util import (
|
||||||
from test_framework.script_util import DUMMY_P2WPKH_SCRIPT, DUMMY_2_P2WPKH_SCRIPT
|
assert_equal,
|
||||||
|
assert_greater_than,
|
||||||
|
assert_raises_rpc_error,
|
||||||
|
)
|
||||||
|
from test_framework.script_util import (
|
||||||
|
DUMMY_P2WPKH_SCRIPT,
|
||||||
|
DUMMY_2_P2WPKH_SCRIPT,
|
||||||
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
|
|
||||||
|
|
||||||
MAX_REPLACEMENT_LIMIT = 100
|
MAX_REPLACEMENT_LIMIT = 100
|
||||||
class ReplaceByFeeTest(BitcoinTestFramework):
|
class ReplaceByFeeTest(BitcoinTestFramework):
|
||||||
def set_test_params(self):
|
def set_test_params(self):
|
||||||
@ -89,29 +96,23 @@ class ReplaceByFeeTest(BitcoinTestFramework):
|
|||||||
def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
|
def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
|
||||||
"""Create a txout with a given amount and scriptPubKey
|
"""Create a txout with a given amount and scriptPubKey
|
||||||
|
|
||||||
Mines coins as needed.
|
Assumes that MiniWallet has enough funds to cover the amount and the fixed fee
|
||||||
|
(from it's internal utxos, the one with the largest value is taken).
|
||||||
|
|
||||||
confirmed - txouts created will be confirmed in the blockchain;
|
confirmed - txouts created will be confirmed in the blockchain;
|
||||||
unconfirmed otherwise.
|
unconfirmed otherwise.
|
||||||
"""
|
"""
|
||||||
fee = 1 * COIN
|
# MiniWallet only supports sweeping utxos to its own internal scriptPubKey, so in
|
||||||
while node.getbalance() < satoshi_round((amount + fee) / COIN):
|
# order to create an output with arbitrary amount/scriptPubKey, we have to add it
|
||||||
self.generate(node, COINBASE_MATURITY)
|
# manually after calling the create_self_transfer method. The MiniWallet output's
|
||||||
|
# nValue has to be adapted accordingly (amount and fee deduction). To keep things
|
||||||
new_addr = node.getnewaddress()
|
# simple, we use a fixed fee of 1000 Satoshis here.
|
||||||
txid = node.sendtoaddress(new_addr, satoshi_round((amount + fee) / COIN))
|
fee = 1000
|
||||||
tx1 = node.getrawtransaction(txid, 1)
|
tx = self.wallet.create_self_transfer(from_node=node, fee_rate=0, mempool_valid=False)['tx']
|
||||||
txid = int(txid, 16)
|
assert_greater_than(tx.vout[0].nValue, amount + fee)
|
||||||
i, _ = next(filter(lambda vout: new_addr == vout[1]['scriptPubKey']['address'], enumerate(tx1['vout'])))
|
tx.vout[0].nValue -= (amount + fee) # change output -> MiniWallet
|
||||||
|
tx.vout.append(CTxOut(amount, scriptPubKey)) # desired output -> to be returned
|
||||||
tx2 = CTransaction()
|
txid = self.wallet.sendrawtransaction(from_node=node, tx_hex=tx.serialize().hex())
|
||||||
tx2.vin = [CTxIn(COutPoint(txid, i))]
|
|
||||||
tx2.vout = [CTxOut(amount, scriptPubKey)]
|
|
||||||
tx2.rehash()
|
|
||||||
|
|
||||||
signed_tx = node.signrawtransactionwithwallet(tx2.serialize().hex())
|
|
||||||
|
|
||||||
txid = node.sendrawtransaction(signed_tx['hex'], 0)
|
|
||||||
|
|
||||||
# If requested, ensure txouts are confirmed.
|
# If requested, ensure txouts are confirmed.
|
||||||
if confirmed:
|
if confirmed:
|
||||||
@ -124,7 +125,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
|
|||||||
assert new_size < mempool_size
|
assert new_size < mempool_size
|
||||||
mempool_size = new_size
|
mempool_size = new_size
|
||||||
|
|
||||||
return COutPoint(int(txid, 16), 0)
|
return COutPoint(int(txid, 16), 1)
|
||||||
|
|
||||||
def test_simple_doublespend(self):
|
def test_simple_doublespend(self):
|
||||||
"""Simple doublespend"""
|
"""Simple doublespend"""
|
||||||
|
@ -179,8 +179,10 @@ class MiniWallet:
|
|||||||
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex, 'tx': tx}
|
return {'txid': tx_info['txid'], 'wtxid': tx_info['wtxid'], 'hex': tx_hex, 'tx': tx}
|
||||||
|
|
||||||
def sendrawtransaction(self, *, from_node, tx_hex):
|
def sendrawtransaction(self, *, from_node, tx_hex):
|
||||||
from_node.sendrawtransaction(tx_hex)
|
txid = from_node.sendrawtransaction(tx_hex)
|
||||||
self.scan_tx(from_node.decoderawtransaction(tx_hex))
|
self.scan_tx(from_node.decoderawtransaction(tx_hex))
|
||||||
|
return txid
|
||||||
|
|
||||||
|
|
||||||
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):
|
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):
|
||||||
"""Build a transaction that spends parent_txid.vout[n] and produces one output with
|
"""Build a transaction that spends parent_txid.vout[n] and produces one output with
|
||||||
|
Loading…
x
Reference in New Issue
Block a user