Skip to content

Commit 2059d32

Browse files
committed
Merge #21200: test: Speed up rpc_blockchain.py by removing miniwallet.generate()
faa137e test: Speed up rpc_blockchain.py by removing miniwallet.generate() (MarcoFalke) fa1fe80 test: Change address type from P2PKH to P2WSH in rpc_blockchain (MarcoFalke) fa4d8f3 test: Cache 25 mature coins for ADDRESS_BCRT1_P2WSH_OP_TRUE (MarcoFalke) fad2515 test: Remove unused bug workaround (MarcoFalke) faabce7 test: Start only the number of nodes that are needed (MarcoFalke) Pull request description: Speed up various tests: * Remove unused nodes, which only consume time on start/stop * Remove unused "bug workarounds" * Remove the need for `miniwallet.generate()` by adding `miniwallet.scan_blocks()`. (On my system, with valgrind, generating 105 blocks takes 3.31 seconds. Rescanning 5 blocks takes 0.11 seconds.) ACKs for top commit: laanwj: Code review ACK faa137e Tree-SHA512: ead1988d5aaa748ef9f8520af1e0bf812cf1d72e281ad22fbd172b7306d850053040526f8adbcec0b9a971c697a0ee7ee8962684644d65b791663eedd505a025
2 parents 8ca6bd0 + faa137e commit 2059d32

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

test/functional/rpc_blockchain.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import subprocess
2525

26+
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
2627
from test_framework.blocktools import (
2728
create_block,
2829
create_coinbase,
@@ -71,11 +72,10 @@ def run_test(self):
7172

7273
def mine_chain(self):
7374
self.log.info('Create some old blocks')
74-
address = self.nodes[0].get_deterministic_priv_key().address
7575
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
7676
# ten-minute steps from genesis block time
7777
self.nodes[0].setmocktime(t)
78-
self.nodes[0].generatetoaddress(1, address)
78+
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_P2WSH_OP_TRUE)
7979
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
8080

8181
def _test_getblockchaininfo(self):
@@ -227,7 +227,7 @@ def _test_gettxoutsetinfo(self):
227227
assert_equal(res['transactions'], 200)
228228
assert_equal(res['height'], 200)
229229
assert_equal(res['txouts'], 200)
230-
assert_equal(res['bogosize'], 15000),
230+
assert_equal(res['bogosize'], 16800),
231231
assert_equal(res['bestblock'], node.getblockhash(200))
232232
size = res['disk_size']
233233
assert size > 6400
@@ -332,12 +332,12 @@ def _test_getnetworkhashps(self):
332332

333333
def _test_stopatheight(self):
334334
assert_equal(self.nodes[0].getblockcount(), 200)
335-
self.nodes[0].generatetoaddress(6, self.nodes[0].get_deterministic_priv_key().address)
335+
self.nodes[0].generatetoaddress(6, ADDRESS_BCRT1_P2WSH_OP_TRUE)
336336
assert_equal(self.nodes[0].getblockcount(), 206)
337337
self.log.debug('Node should not stop at this height')
338338
assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
339339
try:
340-
self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
340+
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_P2WSH_OP_TRUE)
341341
except (ConnectionError, http.client.BadStatusLine):
342342
pass # The node already shut down before response
343343
self.log.debug('Node should stop at this height...')
@@ -387,8 +387,7 @@ def _test_getblock(self):
387387
node = self.nodes[0]
388388

389389
miniwallet = MiniWallet(node)
390-
miniwallet.generate(5)
391-
node.generate(100)
390+
miniwallet.scan_blocks(num=5)
392391

393392
fee_per_byte = Decimal('0.00000010')
394393
fee_per_kb = 1000 * fee_per_byte

test/functional/test_framework/test_framework.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020

2121
from typing import List
22+
from .address import ADDRESS_BCRT1_P2WSH_OP_TRUE
2223
from .authproxy import JSONRPCException
2324
from . import coverage
2425
from .p2p import NetworkThread
@@ -732,16 +733,17 @@ def _initialize_chain(self):
732733
# Set a time in the past, so that blocks don't end up in the future
733734
cache_node.setmocktime(cache_node.getblockheader(cache_node.getbestblockhash())['time'])
734735

735-
# Create a 199-block-long chain; each of the 4 first nodes
736+
# Create a 199-block-long chain; each of the 3 first nodes
736737
# gets 25 mature blocks and 25 immature.
737-
# The 4th node gets only 24 immature blocks so that the very last
738+
# The 4th address gets 25 mature and only 24 immature blocks so that the very last
738739
# block in the cache does not age too much (have an old tip age).
739740
# This is needed so that we are out of IBD when the test starts,
740741
# see the tip age check in IsInitialBlockDownload().
742+
gen_addresses = [k.address for k in TestNode.PRIV_KEYS] + [ADDRESS_BCRT1_P2WSH_OP_TRUE]
741743
for i in range(8):
742744
cache_node.generatetoaddress(
743745
nblocks=25 if i != 7 else 24,
744-
address=TestNode.PRIV_KEYS[i % 4].address,
746+
address=gen_addresses[i % 4],
745747
)
746748

747749
assert_equal(cache_node.getblockchaininfo()["blocks"], 199)

test/functional/test_framework/wallet.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def __init__(self, test_node):
3232
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
3333
self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
3434

35+
def scan_blocks(self, *, start=1, num):
36+
"""Scan the blocks for self._address outputs and add them to self._utxos"""
37+
for i in range(start, start + num):
38+
block = self._test_node.getblock(blockhash=self._test_node.getblockhash(i), verbosity=2)
39+
for tx in block['tx']:
40+
for out in tx['vout']:
41+
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
42+
self._utxos.append({'txid': tx['txid'], 'vout': out['n'], 'value': out['value']})
43+
3544
def generate(self, num_blocks):
3645
"""Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list"""
3746
blocks = self._test_node.generatetoaddress(num_blocks, self._address)

test/functional/wallet_txn_clone.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
)
1212
from test_framework.messages import CTransaction, COIN
1313

14+
1415
class TxnMallTest(BitcoinTestFramework):
1516
def set_test_params(self):
16-
self.num_nodes = 4
17+
self.num_nodes = 3
1718
self.supports_cli = False
1819

1920
def skip_test_if_missing_module(self):
@@ -38,9 +39,8 @@ def run_test(self):
3839

3940
# All nodes should start with 1,250 BTC:
4041
starting_balance = 1250
41-
for i in range(4):
42+
for i in range(3):
4243
assert_equal(self.nodes[i].getbalance(), starting_balance)
43-
self.nodes[i].getnewaddress() # bug workaround, coins generated assigned to first getnewaddress!
4444

4545
self.nodes[0].settxfee(.001)
4646

@@ -139,5 +139,6 @@ def run_test(self):
139139
expected -= 50
140140
assert_equal(self.nodes[0].getbalance(), expected)
141141

142+
142143
if __name__ == '__main__':
143144
TxnMallTest().main()

test/functional/wallet_txn_doublespend.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
find_output,
1212
)
1313

14+
1415
class TxnMallTest(BitcoinTestFramework):
1516
def set_test_params(self):
16-
self.num_nodes = 4
17+
self.num_nodes = 3
1718
self.supports_cli = False
1819

1920
def skip_test_if_missing_module(self):
@@ -39,9 +40,8 @@ def run_test(self):
3940
for n in self.nodes:
4041
assert n.getblockchaininfo()["initialblockdownload"] == False
4142

42-
for i in range(4):
43+
for i in range(3):
4344
assert_equal(self.nodes[i].getbalance(), starting_balance)
44-
self.nodes[i].getnewaddress("") # bug workaround, coins generated assigned to first getnewaddress!
4545

4646
# Assign coins to foo and bar addresses:
4747
node0_address_foo = self.nodes[0].getnewaddress()
@@ -136,5 +136,6 @@ def run_test(self):
136136
# Node1's balance should be its initial balance (1250 for 25 block rewards) plus the doublespend:
137137
assert_equal(self.nodes[1].getbalance(), 1250 + 1240)
138138

139+
139140
if __name__ == '__main__':
140141
TxnMallTest().main()

0 commit comments

Comments
 (0)