Skip to content

Commit fab2e23

Browse files
author
MarcoFalke
committed
Use generate* from TestFramework
The changes in feature_rbf can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent faf7e92 commit fab2e23

14 files changed

+83
-84
lines changed

test/functional/feature_dbcrash.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def run_test(self):
217217

218218
# Start by creating a lot of utxos on node3
219219
initial_height = self.nodes[3].getblockcount()
220-
utxo_list = create_confirmed_utxos(self.nodes[3].getnetworkinfo()['relayfee'], self.nodes[3], 5000)
220+
utxo_list = create_confirmed_utxos(self, self.nodes[3].getnetworkinfo()['relayfee'], self.nodes[3], 5000)
221221
self.log.info(f"Prepped {len(utxo_list)} utxo entries")
222222

223223
# Sync these blocks with the other nodes
@@ -253,7 +253,8 @@ def run_test(self):
253253
self.log.debug("Mining longer tip")
254254
block_hashes = []
255255
while current_height + 1 > self.nodes[3].getblockcount():
256-
block_hashes.extend(self.nodes[3].generatetoaddress(
256+
block_hashes.extend(self.generatetoaddress(
257+
self.nodes[3],
257258
nblocks=min(10, current_height + 1 - self.nodes[3].getblockcount()),
258259
# new address to avoid mining a block that has just been invalidated
259260
address=self.nodes[3].getnewaddress(),

test/functional/feature_maxuploadtarget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run_test(self):
6767
p2p_conns.append(self.nodes[0].add_p2p_connection(TestP2PConn()))
6868

6969
# Now mine a big block
70-
mine_large_block(self.nodes[0], self.utxo_cache)
70+
mine_large_block(self, self.nodes[0], self.utxo_cache)
7171

7272
# Store the hash; we'll request this later
7373
big_old_block = self.nodes[0].getbestblockhash()
@@ -78,7 +78,7 @@ def run_test(self):
7878
self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
7979

8080
# Mine one more block, so that the prior block looks old
81-
mine_large_block(self.nodes[0], self.utxo_cache)
81+
mine_large_block(self, self.nodes[0], self.utxo_cache)
8282

8383
# We'll be requesting this new block too
8484
big_new_block = self.nodes[0].getbestblockhash()

test/functional/feature_rbf.py

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,6 @@
2323
from test_framework.wallet import MiniWallet
2424

2525
MAX_REPLACEMENT_LIMIT = 100
26-
27-
28-
def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
29-
"""Create a txout with a given amount and scriptPubKey
30-
31-
Mines coins as needed.
32-
33-
confirmed - txouts created will be confirmed in the blockchain;
34-
unconfirmed otherwise.
35-
"""
36-
fee = 1 * COIN
37-
while node.getbalance() < satoshi_round((amount + fee) / COIN):
38-
node.generate(COINBASE_MATURITY)
39-
40-
new_addr = node.getnewaddress()
41-
txid = node.sendtoaddress(new_addr, satoshi_round((amount + fee) / COIN))
42-
tx1 = node.getrawtransaction(txid, 1)
43-
txid = int(txid, 16)
44-
i, _ = next(filter(lambda vout: new_addr == vout[1]['scriptPubKey']['address'], enumerate(tx1['vout'])))
45-
46-
tx2 = CTransaction()
47-
tx2.vin = [CTxIn(COutPoint(txid, i))]
48-
tx2.vout = [CTxOut(amount, scriptPubKey)]
49-
tx2.rehash()
50-
51-
signed_tx = node.signrawtransactionwithwallet(tx2.serialize().hex())
52-
53-
txid = node.sendrawtransaction(signed_tx['hex'], 0)
54-
55-
# If requested, ensure txouts are confirmed.
56-
if confirmed:
57-
mempool_size = len(node.getrawmempool())
58-
while mempool_size > 0:
59-
node.generate(1)
60-
new_size = len(node.getrawmempool())
61-
# Error out if we have something stuck in the mempool, as this
62-
# would likely be a bug.
63-
assert new_size < mempool_size
64-
mempool_size = new_size
65-
66-
return COutPoint(int(txid, 16), 0)
67-
68-
6926
class ReplaceByFeeTest(BitcoinTestFramework):
7027
def set_test_params(self):
7128
self.num_nodes = 1
@@ -129,6 +86,46 @@ def run_test(self):
12986

13087
self.log.info("Passed")
13188

89+
def make_utxo(self, node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
90+
"""Create a txout with a given amount and scriptPubKey
91+
92+
Mines coins as needed.
93+
94+
confirmed - txouts created will be confirmed in the blockchain;
95+
unconfirmed otherwise.
96+
"""
97+
fee = 1 * COIN
98+
while node.getbalance() < satoshi_round((amount + fee) / COIN):
99+
self.generate(node, COINBASE_MATURITY)
100+
101+
new_addr = node.getnewaddress()
102+
txid = node.sendtoaddress(new_addr, satoshi_round((amount + fee) / COIN))
103+
tx1 = node.getrawtransaction(txid, 1)
104+
txid = int(txid, 16)
105+
i, _ = next(filter(lambda vout: new_addr == vout[1]['scriptPubKey']['address'], enumerate(tx1['vout'])))
106+
107+
tx2 = CTransaction()
108+
tx2.vin = [CTxIn(COutPoint(txid, i))]
109+
tx2.vout = [CTxOut(amount, scriptPubKey)]
110+
tx2.rehash()
111+
112+
signed_tx = node.signrawtransactionwithwallet(tx2.serialize().hex())
113+
114+
txid = node.sendrawtransaction(signed_tx['hex'], 0)
115+
116+
# If requested, ensure txouts are confirmed.
117+
if confirmed:
118+
mempool_size = len(node.getrawmempool())
119+
while mempool_size > 0:
120+
self.generate(node, 1)
121+
new_size = len(node.getrawmempool())
122+
# Error out if we have something stuck in the mempool, as this
123+
# would likely be a bug.
124+
assert new_size < mempool_size
125+
mempool_size = new_size
126+
127+
return COutPoint(int(txid, 16), 0)
128+
132129
def test_simple_doublespend(self):
133130
"""Simple doublespend"""
134131
# we use MiniWallet to create a transaction template with inputs correctly set,
@@ -165,7 +162,7 @@ def test_doublespend_chain(self):
165162
"""Doublespend of a long chain"""
166163

167164
initial_nValue = 50 * COIN
168-
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
165+
tx0_outpoint = self.make_utxo(self.nodes[0], initial_nValue)
169166

170167
prevout = tx0_outpoint
171168
remaining_value = initial_nValue
@@ -205,7 +202,7 @@ def test_doublespend_tree(self):
205202
"""Doublespend of a big tree of transactions"""
206203

207204
initial_nValue = 50 * COIN
208-
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
205+
tx0_outpoint = self.make_utxo(self.nodes[0], initial_nValue)
209206

210207
def branch(prevout, initial_value, max_txs, tree_width=5, fee=0.0001 * COIN, _total_txs=None):
211208
if _total_txs is None:
@@ -268,7 +265,7 @@ def branch(prevout, initial_value, max_txs, tree_width=5, fee=0.0001 * COIN, _to
268265
# double-spent at once" anti-DoS limit.
269266
for n in (MAX_REPLACEMENT_LIMIT + 1, MAX_REPLACEMENT_LIMIT * 2):
270267
fee = int(0.0001 * COIN)
271-
tx0_outpoint = make_utxo(self.nodes[0], initial_nValue)
268+
tx0_outpoint = self.make_utxo(self.nodes[0], initial_nValue)
272269
tree_txs = list(branch(tx0_outpoint, initial_nValue, n, fee=fee))
273270
assert_equal(len(tree_txs), n)
274271

@@ -285,7 +282,7 @@ def branch(prevout, initial_value, max_txs, tree_width=5, fee=0.0001 * COIN, _to
285282

286283
def test_replacement_feeperkb(self):
287284
"""Replacement requires fee-per-KB to be higher"""
288-
tx0_outpoint = make_utxo(self.nodes[0], int(1.1 * COIN))
285+
tx0_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))
289286

290287
tx1a = CTransaction()
291288
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
@@ -305,8 +302,8 @@ def test_replacement_feeperkb(self):
305302

306303
def test_spends_of_conflicting_outputs(self):
307304
"""Replacements that spend conflicting tx outputs are rejected"""
308-
utxo1 = make_utxo(self.nodes[0], int(1.2 * COIN))
309-
utxo2 = make_utxo(self.nodes[0], 3 * COIN)
305+
utxo1 = self.make_utxo(self.nodes[0], int(1.2 * COIN))
306+
utxo2 = self.make_utxo(self.nodes[0], 3 * COIN)
310307

311308
tx1a = CTransaction()
312309
tx1a.vin = [CTxIn(utxo1, nSequence=0)]
@@ -345,8 +342,8 @@ def test_spends_of_conflicting_outputs(self):
345342

346343
def test_new_unconfirmed_inputs(self):
347344
"""Replacements that add new unconfirmed inputs are rejected"""
348-
confirmed_utxo = make_utxo(self.nodes[0], int(1.1 * COIN))
349-
unconfirmed_utxo = make_utxo(self.nodes[0], int(0.1 * COIN), False)
345+
confirmed_utxo = self.make_utxo(self.nodes[0], int(1.1 * COIN))
346+
unconfirmed_utxo = self.make_utxo(self.nodes[0], int(0.1 * COIN), False)
350347

351348
tx1 = CTransaction()
352349
tx1.vin = [CTxIn(confirmed_utxo)]
@@ -369,7 +366,7 @@ def test_too_many_replacements(self):
369366

370367
# Start by creating a single transaction with many outputs
371368
initial_nValue = 10 * COIN
372-
utxo = make_utxo(self.nodes[0], initial_nValue)
369+
utxo = self.make_utxo(self.nodes[0], initial_nValue)
373370
fee = int(0.0001 * COIN)
374371
split_value = int((initial_nValue - fee) / (MAX_REPLACEMENT_LIMIT + 1))
375372

@@ -417,7 +414,7 @@ def test_too_many_replacements(self):
417414

418415
def test_opt_in(self):
419416
"""Replacing should only work if orig tx opted in"""
420-
tx0_outpoint = make_utxo(self.nodes[0], int(1.1 * COIN))
417+
tx0_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))
421418

422419
# Create a non-opting in transaction
423420
tx1a = CTransaction()
@@ -438,7 +435,7 @@ def test_opt_in(self):
438435
# This will raise an exception
439436
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx1b_hex, 0)
440437

441-
tx1_outpoint = make_utxo(self.nodes[0], int(1.1 * COIN))
438+
tx1_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))
442439

443440
# Create a different non-opting in transaction
444441
tx2a = CTransaction()
@@ -494,7 +491,7 @@ def test_prioritised_transactions(self):
494491
# correctly used by replacement logic
495492

496493
# 1. Check that feeperkb uses modified fees
497-
tx0_outpoint = make_utxo(self.nodes[0], int(1.1 * COIN))
494+
tx0_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))
498495

499496
tx1a = CTransaction()
500497
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
@@ -520,7 +517,7 @@ def test_prioritised_transactions(self):
520517
assert tx1b_txid in self.nodes[0].getrawmempool()
521518

522519
# 2. Check that absolute fee checks use modified fee.
523-
tx1_outpoint = make_utxo(self.nodes[0], int(1.1 * COIN))
520+
tx1_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))
524521

525522
tx2a = CTransaction()
526523
tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0)]

test/functional/interface_zmq.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class ZMQTestSetupBlock:
8282
raw transaction data.
8383
"""
8484

85-
def __init__(self, node):
86-
self.block_hash = node.generate(1)[0]
85+
def __init__(self, test_framework, node):
86+
self.block_hash = test_framework.generate(node, 1)[0]
8787
coinbase = node.getblock(self.block_hash, 2)['tx'][0]
8888
self.tx_hash = coinbase['txid']
8989
self.raw_tx = coinbase['hex']
@@ -147,7 +147,7 @@ def setup_zmq_test(self, services, *, recv_timeout=60, sync_blocks=True):
147147
for sub in subscribers:
148148
sub.socket.set(zmq.RCVTIMEO, 1000)
149149
while True:
150-
test_block = ZMQTestSetupBlock(self.nodes[0])
150+
test_block = ZMQTestSetupBlock(self, self.nodes[0])
151151
recv_failed = False
152152
for sub in subscribers:
153153
try:

test/functional/mempool_limit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def run_test(self):
3232
assert_equal(self.nodes[0].getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
3333

3434
txids = []
35-
utxos = create_confirmed_utxos(relayfee, self.nodes[0], 91)
35+
utxos = create_confirmed_utxos(self, relayfee, self.nodes[0], 91)
3636

3737
self.log.info('Create a mempool tx that will be evicted')
3838
us0 = utxos.pop()

test/functional/mempool_unbroadcast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_broadcast(self):
3232
node = self.nodes[0]
3333

3434
min_relay_fee = node.getnetworkinfo()["relayfee"]
35-
utxos = create_confirmed_utxos(min_relay_fee, node, 10)
35+
utxos = create_confirmed_utxos(self, min_relay_fee, node, 10)
3636

3737
self.disconnect_nodes(0, 1)
3838

test/functional/mining_getblocktemplate_longpoll.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ def run_test(self):
4848
thr.join(5) # wait 5 seconds or until thread exits
4949
assert thr.is_alive()
5050

51-
miniwallets = [ MiniWallet(node) for node in self.nodes ]
51+
miniwallets = [MiniWallet(node) for node in self.nodes]
5252
self.log.info("Test that longpoll will terminate if another node generates a block")
53-
miniwallets[1].generate(1) # generate a block on another node
53+
self.generate(miniwallets[1], 1) # generate a block on another node
5454
# check that thread will exit now that new transaction entered mempool
5555
thr.join(5) # wait 5 seconds or until thread exits
5656
assert not thr.is_alive()
5757

5858
self.log.info("Test that longpoll will terminate if we generate a block ourselves")
5959
thr = LongpollThread(self.nodes[0])
6060
thr.start()
61-
miniwallets[0].generate(1) # generate a block on own node
61+
self.generate(miniwallets[0], 1) # generate a block on own node
6262
thr.join(5) # wait 5 seconds or until thread exits
6363
assert not thr.is_alive()
6464

test/functional/mining_prioritisetransaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def run_test(self):
4848
self.relayfee = self.nodes[0].getnetworkinfo()['relayfee']
4949

5050
utxo_count = 90
51-
utxos = create_confirmed_utxos(self.relayfee, self.nodes[0], utxo_count)
51+
utxos = create_confirmed_utxos(self, self.relayfee, self.nodes[0], utxo_count)
5252
base_fee = self.relayfee*100 # our transactions are smaller than 100kb
5353
txids = []
5454

test/functional/p2p_unrequested_blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run_test(self):
7777
min_work_node = self.nodes[1].add_p2p_connection(P2PInterface())
7878

7979
# 1. Have nodes mine a block (leave IBD)
80-
[n.generatetoaddress(1, n.get_deterministic_priv_key().address) for n in self.nodes]
80+
[self.generatetoaddress(n, 1, n.get_deterministic_priv_key().address) for n in self.nodes]
8181
tips = [int("0x" + n.getbestblockhash(), 0) for n in self.nodes]
8282

8383
# 2. Send one block that builds on each tip.

test/functional/rpc_signrawtransaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_signing_with_csv(self):
274274
getcontext().prec = 8
275275

276276
# Make sure CSV is active
277-
generate_to_height(self.nodes[0], CSV_ACTIVATION_HEIGHT)
277+
generate_to_height(self, self.nodes[0], CSV_ACTIVATION_HEIGHT)
278278
assert self.nodes[0].getblockchaininfo()['softforks']['csv']['active']
279279

280280
# Create a P2WSH script with CSV
@@ -310,7 +310,7 @@ def test_signing_with_cltv(self):
310310
getcontext().prec = 8
311311

312312
# Make sure CLTV is active
313-
generate_to_height(self.nodes[0], CLTV_HEIGHT)
313+
generate_to_height(self, self.nodes[0], CLTV_HEIGHT)
314314
assert self.nodes[0].getblockchaininfo()['softforks']['bip65']['active']
315315

316316
# Create a P2WSH script with CLTV

0 commit comments

Comments
 (0)