Skip to content

Commit df765a4

Browse files
committed
tests: rpc_fundrawtx lock to UTXO types
For some of the tests within rpc_fundrawtx, there is the expectation that two independent calls to coin selection RPCs will use the same type of UTXO. This is not necessarily guaranteed, so to make sure it is, use lockunspent prior to those tests.
1 parent d2dd169 commit df765a4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/functional/rpc_fundrawtransaction.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,40 @@ def setup_network(self):
4747
self.connect_nodes(0, 2)
4848
self.connect_nodes(0, 3)
4949

50+
def lock_outputs_type(self, wallet, outputtype):
51+
"""
52+
Only allow UTXOs of the given type
53+
"""
54+
if outputtype in ["legacy", "p2pkh", "pkh"]:
55+
prefixes = ["pkh(", "sh(multi("]
56+
elif outputtype in ["p2sh-segwit", "sh_wpkh"]:
57+
prefixes = ["sh(wpkh(", "sh(wsh("]
58+
elif outputtype in ["bech32", "wpkh"]:
59+
prefixes = ["wpkh(", "wsh("]
60+
else:
61+
assert False, f"Unknown output type {outputtype}"
62+
63+
to_lock = []
64+
for utxo in wallet.listunspent():
65+
if "desc" in utxo:
66+
for prefix in prefixes:
67+
if utxo["desc"].startswith(prefix):
68+
to_lock.append({"txid": utxo["txid"], "vout": utxo["vout"]})
69+
wallet.lockunspent(False, to_lock)
70+
71+
def unlock_utxos(self, wallet):
72+
"""
73+
Unlock all UTXOs except the watchonly one
74+
"""
75+
to_keep = []
76+
if self.watchonly_txid is not None and self.watchonly_vout is not None:
77+
to_keep.append({"txid": self.watchonly_txid, "vout": self.watchonly_vout})
78+
wallet.lockunspent(True)
79+
wallet.lockunspent(False, to_keep)
80+
5081
def run_test(self):
82+
self.watchonly_txid = None
83+
self.watchonly_vout = None
5184
self.log.info("Connect nodes, set fees, generate blocks, and sync")
5285
self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
5386
# This test is not meant to test fee estimation and we'd like
@@ -373,6 +406,7 @@ def test_invalid_input(self):
373406
def test_fee_p2pkh(self):
374407
"""Compare fee of a standard pubkeyhash transaction."""
375408
self.log.info("Test fundrawtxn p2pkh fee")
409+
self.lock_outputs_type(self.nodes[0], "p2pkh")
376410
inputs = []
377411
outputs = {self.nodes[1].getnewaddress():1.1}
378412
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
@@ -386,9 +420,12 @@ def test_fee_p2pkh(self):
386420
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
387421
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
388422

423+
self.unlock_utxos(self.nodes[0])
424+
389425
def test_fee_p2pkh_multi_out(self):
390426
"""Compare fee of a standard pubkeyhash transaction with multiple outputs."""
391427
self.log.info("Test fundrawtxn p2pkh fee with multiple outputs")
428+
self.lock_outputs_type(self.nodes[0], "p2pkh")
392429
inputs = []
393430
outputs = {
394431
self.nodes[1].getnewaddress():1.1,
@@ -409,8 +446,11 @@ def test_fee_p2pkh_multi_out(self):
409446
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
410447
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
411448

449+
self.unlock_utxos(self.nodes[0])
450+
412451
def test_fee_p2sh(self):
413452
"""Compare fee of a 2-of-2 multisig p2sh transaction."""
453+
self.lock_outputs_type(self.nodes[0], "p2pkh")
414454
# Create 2-of-2 addr.
415455
addr1 = self.nodes[1].getnewaddress()
416456
addr2 = self.nodes[1].getnewaddress()
@@ -433,9 +473,12 @@ def test_fee_p2sh(self):
433473
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
434474
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
435475

476+
self.unlock_utxos(self.nodes[0])
477+
436478
def test_fee_4of5(self):
437479
"""Compare fee of a standard pubkeyhash transaction."""
438480
self.log.info("Test fundrawtxn fee with 4-of-5 addresses")
481+
self.lock_outputs_type(self.nodes[0], "p2pkh")
439482

440483
# Create 4-of-5 addr.
441484
addr1 = self.nodes[1].getnewaddress()
@@ -474,6 +517,8 @@ def test_fee_4of5(self):
474517
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
475518
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
476519

520+
self.unlock_utxos(self.nodes[0])
521+
477522
def test_spend_2of2(self):
478523
"""Spend a 2-of-2 multisig transaction over fundraw."""
479524
self.log.info("Test fundpsbt spending 2-of-2 multisig")

0 commit comments

Comments
 (0)