Merge bitcoin/bitcoin#36618: test: Wait for index sync in all tests
What changed, and why it matters
This commit only changes Bitcoin Core's internal functional test framework. It makes the test harness automatically wait for optional indexes (like transaction index, block filter index, and coin stats index) to finish syncing before continuing tests. It does not change the production Bitcoin node software that users run, so it cannot directly affect real wallets, funds, or network security.
No security action needed. This is a test-only reliability improvement. Reviewers may verify that the new default wait behavior does not hide real product bugs or excessively slow CI, but that is a testing quality concern, not a security issue.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The merge commit refactors the functional test framework so that wait_for_index_sync is called by default inside wait_for_rpc_connection and sync_all. It removes many manual wait_until calls for getindexinfo from individual tests and adds a wait_for_full_init parameter to control whether the RPC connection setup should block on full initialization. The changes are confined to test/functional/ files and the test framework; no consensus, wallet, P2P, or RPC server code is modified.
Changed components
test/functional/test_framework/test_framework.pytest/functional/test_framework/test_node.pytest/functional/test_framework/util.pyvarious functional test scriptsInspect captured patch +41 / −71
### test/functional/feature_assumeutxo.py
@@ -758,7 +758,7 @@ def check_tx_counts(final: bool) -> None:
# Upon restart, the node must stay in 'limited' mode until the background
# chain sync completes.
- self.restart_node(2, extra_args=self.extra_args[2])
+ self.restart_node(2, extra_args=self.extra_args[2], wait_for_full_init=False)
self.assert_only_network_limited_service(n2)
self.connect_nodes(0, 2)
### test/functional/feature_coinstatsindex.py
@@ -62,7 +62,7 @@ def block_sanity_check(self, block_info):
)
def sync_index_node(self):
- self.wait_until(lambda: self.nodes[1].getindexinfo()['coinstatsindex']['synced'] is True)
+ self.wait_for_index_sync([self.nodes[1]])
def _test_coin_stats_index(self):
node = self.nodes[0]
@@ -230,14 +230,12 @@ def _test_coin_stats_index(self):
self.log.info("Test that the index works with -reindex")
self.restart_node(1, extra_args=["-coinstatsindex", "-reindex"])
- self.sync_index_node()
res11 = index_node.gettxoutsetinfo('muhash')
assert_equal(res11, res10)
self.log.info("Test that the index works with -reindex-chainstate")
self.restart_node(1, extra_args=["-coinstatsindex", "-reindex-chainstate"])
- self.sync_index_node()
res12 = index_node.gettxoutsetinfo('muhash')
assert_equal(res12, res10)
@@ -261,7 +259,6 @@ def _test_reorg_index(self):
index_node = self.nodes[1]
reorg_blocks = self.generatetoaddress(index_node, 2, getnewdestination()[2])
reorg_block = reorg_blocks[1]
- self.sync_index_node()
res_invalid = index_node.gettxoutsetinfo('muhash')
index_node.invalidateblock(reorg_blocks[0])
assert_equal(index_node.gettxoutsetinfo('muhash')['height'], 110)
@@ -317,7 +314,6 @@ def _test_init_index_after_reorg(self):
# Restart with index that still has its best block on the old chain
self.restart_node(1, extra_args=self.extra_args[1])
- self.sync_index_node()
res1 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=None, use_index=True)
assert_equal(res["muhash"], res1["muhash"])
@@ -332,7 +328,6 @@ def _test_init_index_after_reorg(self):
self.sync_index_node()
index_node.kill_process()
self.start_node(1, extra_args=self.extra_args[1])
- self.sync_index_node()
# Because of the unclean shutdown above, indexes reset to the point we last committed them to disk.
assert_equal(index_node.getindexinfo()['coinstatsindex']['best_block_height'], committed_height)
### test/functional/feature_coinstatsindex_compatibility.py
@@ -38,8 +38,6 @@ def run_test(self):
def _test_coin_stats_index_compatibility(self):
node = self.nodes[0]
legacy_node = self.nodes[1]
- for n in self.nodes:
- self.wait_until(lambda: n.getindexinfo()['coinstatsindex']['synced'] is True)
self.log.info("Test that gettxoutsetinfo() output is consistent between the different index versions")
res0 = node.gettxoutsetinfo('muhash')
@@ -54,7 +52,6 @@ def _test_coin_stats_index_compatibility(self):
msg = f'[warning] Old version of coinstatsindex found at {old_version_path}. This folder can be safely deleted unless you plan to downgrade your node to version 29 or lower.'
with node.assert_debug_log(expected_msgs=[msg]):
self.start_node(0, ['-coinstatsindex'])
- self.wait_until(lambda: node.getindexinfo()['coinstatsindex']['synced'] is True)
res2 = node.gettxoutsetinfo('muhash')
assert_equal(res2, res0)
### test/functional/feature_index_prune.py
@@ -50,24 +50,11 @@ def linear_sync(self, node_from, *, height_from=None):
for i in range(height_from, to_height + 1):
b = node_from.getblock(blockhash=node_from.getblockhash(i), verbosity=0)
list(rpc_threads.map(lambda n: n.submitblock(b), self.nodes))
+ self.wait_for_index_sync(timeout=150)
def generate(self, node, num_blocks, sync_fun=None):
return super().generate(node, num_blocks, sync_fun=sync_fun or (lambda: self.linear_sync(node)))
- def sync_index(self, height):
- expected_filter = {
- 'basic block filter index': {'synced': True, 'best_block_height': height},
- }
- self.wait_until(lambda: self.nodes[0].getindexinfo() == expected_filter)
-
- expected_stats = {
- 'coinstatsindex': {'synced': True, 'best_block_height': height}
- }
- self.wait_until(lambda: self.nodes[1].getindexinfo() == expected_stats, timeout=150)
-
- expected = {**expected_filter, **expected_stats}
- self.wait_until(lambda: self.nodes[2].getindexinfo() == expected)
-
def restart_without_indices(self):
for i in range(3):
self.restart_node(i, extra_args=["-fastprune", "-prune=1"])
@@ -84,15 +71,13 @@ def run_test(self):
stats_nodes = [self.nodes[1], self.nodes[2]]
self.log.info("check if we can access blockfilters and coinstats when pruning is enabled but no blocks are actually pruned")
- self.sync_index(height=200)
tip = self.nodes[0].getbestblockhash()
for node in filter_nodes:
assert_greater_than(len(node.getblockfilter(tip)['filter']), 0)
for node in stats_nodes:
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=tip)['muhash']
self.generate(self.nodes[0], 500)
- self.sync_index(height=700)
self.log.info("prune some blocks")
for node in self.nodes[:2]:
@@ -118,7 +103,6 @@ def run_test(self):
# mine and sync index up to a height that will later be the pruneheight
self.generate(self.nodes[0], 54)
- self.sync_index(height=754)
self.restart_without_indices()
@@ -140,7 +124,6 @@ def run_test(self):
self.restart_node(i, extra_args=self.extra_args[i])
self.log.info("make sure that we can continue with the partially synced indices after having pruned up to the index height")
- self.sync_index(height=1500)
self.log.info("prune further than the indices best blocks while the indices are disabled")
self.restart_without_indices()
@@ -190,7 +173,6 @@ def run_test(self):
self.restart_node(i, extra_args=restart_args)
self.linear_sync(self.nodes[3])
- self.sync_index(height=2500)
for node in self.nodes[:2]:
with node.assert_debug_log(['limited pruning to height 2489']):
### test/functional/feature_init.py
@@ -61,7 +61,6 @@ def check_clean_start(self, node, extra_args):
node.wait_for_rpc_connection()
height = node.getblockcount()
assert_equal(200, height)
- self.wait_until(lambda: all(i["synced"] and i["best_block_height"] == height for i in node.getindexinfo().values()))
def init_stress_test_interrupt(self):
"""
### test/functional/feature_reindex.py
@@ -88,14 +88,14 @@ def continue_reindex_after_shutdown(self):
node.stop_node()
with node.busy_wait_for_debug_log([b'initload thread start']):
node.start(['-blockfilterindex', '-reindex'])
- node.wait_for_rpc_connection(wait_for_import=False)
+ node.wait_for_rpc_connection(wait_for_full_init=False)
node.stop_node()
# Start node without the reindex flag and verify it does not wipe the indexes data again
db_path = node.chain_path / 'indexes' / 'blockfilter' / 'basic' / 'db'
with node.assert_debug_log(expected_msgs=[f'Opening LevelDB in {db_path}'], unexpected_msgs=[f'Wiping LevelDB in {db_path}']):
node.start(['-blockfilterindex'])
- node.wait_for_rpc_connection(wait_for_import=False)
+ node.wait_for_rpc_connection(wait_for_full_init=False)
node.stop_node()
def run_test(self):
### test/functional/feature_txindex_compatibility.py
@@ -42,9 +42,6 @@ def _test_txindex_compatibility(self):
self.generate(self.nodes[0], 1)
txId1 = tx1['txid']
- for n in self.nodes:
- self.wait_until(lambda: n.getindexinfo()['txindex']['synced'])
-
self.log.info("Ensure that queries to the txindex are consistent between the different index versions")
assert_equal(node.getrawtransaction(txId1), tx1['hex'])
assert_equal(legacy_node.getrawtransaction(txId1), tx1['hex'])
@@ -56,14 +53,13 @@ def _test_txindex_compatibility(self):
msg = "txindex contains entries in the legacy format"
with node.assert_debug_log(expected_msgs=[msg]):
self.start_node(0)
- self.wait_until(lambda: node.getindexinfo()['txindex']['synced'])
assert_equal(node.getrawtransaction(txId1), tx1['hex'])
self.log.info("Test that looking up a newly added transaction in a mixed-format db is possible")
tx2 = self.wallet.send_self_transfer(from_node=self.nodes[0])
self.generate(self.nodes[0], 1, sync_fun=self.no_op)
txId2 = tx2['txid']
- self.wait_until(lambda: node.getindexinfo()['txindex']['synced'])
+ self.wait_for_index_sync([node])
assert_equal(node.getrawtransaction(txId1), tx1['hex'])
assert_equal(node.getrawtransaction(txId2), tx2['hex'])
### test/functional/interface_rest.py
@@ -24,7 +24,6 @@
assert_greater_than,
assert_greater_than_or_equal,
assert_not_equal,
- sync_txindex,
)
from test_framework.wallet import (
MiniWallet,
@@ -303,10 +302,6 @@ def run_test(self):
# See if we can get 5 headers in one response
self.generate(self.nodes[1], 5)
- expected_filter = {
- 'basic block filter index': {'synced': True, 'best_block_height': 208},
- }
- self.wait_until(lambda: self.nodes[0].getindexinfo("basic block filter index") == expected_filter)
json_obj = self.test_rest_request(f"/headers/{bb_hash}", query_params={"count": 5})
assert_equal(len(json_obj), 5) # now we should have 5 header objects
json_obj = self.test_rest_request(f"/blockfilterheaders/basic/{bb_hash}", query_params={"count": 5})
@@ -594,7 +589,6 @@ def assert_cache_control(
assert_cache_control(f"/tx/{mempool_txid}", no_store, req_type=ReqType.HEX)
self.generate(self.nodes[0], 1)
- sync_txindex(self, self.nodes[0])
self.log.info("Test Cache-Control response behavior across a newly mined block")
assert_equal(immutable_block, assert_cache_control(f"/block/{blockhash}", immutable, req_type=ReqType.BIN))
### test/functional/mempool_accept.py
@@ -50,7 +50,6 @@
assert_equal,
assert_greater_than,
assert_raises_rpc_error,
- sync_txindex,
)
from test_framework.wallet import MiniWallet
from test_framework.wallet_util import generate_keypair
@@ -283,7 +282,6 @@ def run_test(self):
self.log.info('A coinbase transaction')
# Pick the input of the first tx we created, so it has to be a coinbase tx
- sync_txindex(self, node)
raw_tx_coinbase_spent = node.getrawtransaction(txid=node.decoderawtransaction(hexstring=raw_tx_in_block)['vin'][0]['txid'])
tx = tx_from_hex(raw_tx_coinbase_spent)
self.check_mempool_result(
### test/functional/rpc_gettxspendingprevout.py
@@ -77,8 +77,6 @@ def create_tx(**kwargs):
txidA, txidB, txidC, txidD, txidE, txidF, txidG, txidH = [tx["txid"] for tx in txs]
self.sync_mempools()
- self.wait_until(lambda: node0.getindexinfo()["txospenderindex"]["synced"])
- self.wait_until(lambda: node1.getindexinfo()["txospenderindex"]["synced"])
mempool = node0.getrawmempool()
assert_equal(len(mempool), 8)
for tx in txs:
### test/functional/rpc_misc.py
@@ -99,9 +99,8 @@ def run_test(self):
# Without any indices running the RPC returns an empty object
assert_equal(node.getindexinfo(), {})
- # Restart the node with indices and wait for them to sync
+ # Restart the node with indices.
self.restart_node(0, ["-txindex", "-blockfilterindex", "-coinstatsindex", "-txospenderindex"])
- self.wait_until(lambda: all(i["synced"] for i in node.getindexinfo().values()))
# Returns a list of all running indices by default
values = {"synced": True, "best_block_height": 200}
### test/functional/rpc_rawtransaction.py
@@ -36,7 +36,6 @@
assert_equal,
assert_greater_than,
assert_raises_rpc_error,
- sync_txindex,
)
from test_framework.wallet import (
getnewdestination,
@@ -104,7 +103,6 @@ def getrawtransaction_tests(self):
self.log.info(f"Test getrawtransaction {'with' if txindex_enabled else 'without'} -txindex")
if txindex_enabled:
- sync_txindex(self, node)
# With -txindex.
# 1. valid parameters - only supply txid
assert_equal(node.getrawtransaction(txId), tx['hex'])
### test/functional/rpc_scanblocks.py
@@ -43,7 +43,6 @@ def run_test(self):
# mine a block and assure that the mined blockhash is in the filterresult
blockhash = self.generate(node, 1)[0]
height = node.getblockheader(blockhash)['height']
- self.wait_until(lambda: all(i["synced"] for i in node.getindexinfo().values()))
out = node.scanblocks("start", [f"addr({addr_1})"])
assert blockhash in out['relevant_blocks']
### test/functional/rpc_txoutproof.py
@@ -12,7 +12,6 @@
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
- sync_txindex,
)
from test_framework.wallet import MiniWallet
@@ -78,7 +77,6 @@ def run_test(self):
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist))
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist))
# We can always get a proof if we have a -txindex
- sync_txindex(self, self.nodes[1])
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent])
# We can't get a proof if we specify transactions from different blocks
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
### test/functional/rpc_whitelist.py
@@ -98,7 +98,7 @@ def run_test(self):
# Replace file configurations
self.nodes[0].replace_in_config([("rpcwhitelistdefault=0", "rpcwhitelistdefault=1")])
with open(self.nodes[0].datadir_path / "bitcoin.conf", 'a') as f:
- f.write("rpcwhitelist=__cookie__:getblockcount,getblockchaininfo,getmempoolinfo,stop\n")
+ f.write("rpcwhitelist=__cookie__:getblockcount,getblockchaininfo,getmempoolinfo,getindexinfo,stop\n")
self.restart_node(0)
self.test_users_permissions()
### test/functional/test_framework/test_framework.py
@@ -495,18 +495,18 @@ def bin_dir_from_version(version):
# adjust conf for pre 17
test_node_i.replace_in_config([('[regtest]', '')])
- def start_node(self, i, *args, **kwargs):
+ def start_node(self, i, *args, wait_for_full_init=True, **kwargs):
"""Start a bitcoind"""
node = self.nodes[i]
node.start(*args, **kwargs)
- node.wait_for_rpc_connection()
+ node.wait_for_rpc_connection(wait_for_full_init=wait_for_full_init)
if self.options.coveragedir is not None:
coverage.write_all_rpc_commands(self.options.coveragedir, node._rpc)
- def start_nodes(self, extra_args=None, *args, **kwargs):
+ def start_nodes(self, extra_args=None, *args, wait_for_full_init=True, **kwargs):
"""Start multiple bitcoinds"""
if extra_args is None:
@@ -515,7 +515,7 @@ def start_nodes(self, extra_args=None, *args, **kwargs):
for i, node in enumerate(self.nodes):
node.start(extra_args[i], *args, **kwargs)
for node in self.nodes:
- node.wait_for_rpc_connection()
+ node.wait_for_rpc_connection(wait_for_full_init=wait_for_full_init)
if self.options.coveragedir is not None:
for node in self.nodes:
@@ -556,16 +556,16 @@ def cleanup_partially_started_nodes(self):
node.stderr.close()
node.running = False
- def restart_node(self, i, extra_args=None, clear_addrman=False, *, expected_stderr=''):
+ def restart_node(self, i, extra_args=None, clear_addrman=False, *, expected_stderr='', wait_for_full_init=True):
"""Stop and start a test node"""
self.stop_node(i, expected_stderr=expected_stderr)
if clear_addrman:
peers_dat = self.nodes[i].chain_path / "peers.dat"
os.remove(peers_dat)
with self.nodes[i].assert_debug_log(expected_msgs=[f'Creating peers.dat because the file was not found ("{peers_dat}")']):
- self.start_node(i, extra_args)
+ self.start_node(i, extra_args, wait_for_full_init=wait_for_full_init)
else:
- self.start_node(i, extra_args)
+ self.start_node(i, extra_args, wait_for_full_init=wait_for_full_init)
def wait_for_node_exit(self, i, timeout):
self.nodes[i].process.wait(timeout)
@@ -744,9 +744,17 @@ def sync_mempools(self, nodes=None, *, wait=0.05, timeout=60, flush_scheduler=Tr
"".join("\n {!r}".format(m) for m in pool),
))
+ def wait_for_index_sync(self, nodes=None, **kwargs):
+ """Wait for every enabled index to reach each node's chain tip."""
+ nodes = nodes or self.nodes
+
+ for node in nodes:
+ node.wait_for_index_sync(**kwargs)
+
def sync_all(self, nodes=None):
self.sync_blocks(nodes)
self.sync_mempools(nodes)
+ self.wait_for_index_sync(nodes)
def wait_until(self, test_function, *, timeout=60, check_interval=0.05):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.options.timeout_factor, check_interval=check_interval)
### test/functional/test_framework/test_node.py
@@ -353,7 +353,7 @@ def create_new_rpc_connection(self, *, mode="AUTO", client_timeout=None):
*extra_args
)
- def wait_for_rpc_connection(self, *, wait_for_import=True):
+ def wait_for_rpc_connection(self, *, wait_for_full_init=True):
"""Sets up an RPC connection to the bitcoind process. Returns False if unable to connect."""
# Poll at a rate of twenty times per second
poll_per_s = 20
@@ -378,12 +378,13 @@ def suppress_error(category: str, e: Exception):
rpc = self.create_new_rpc_connection(mode="AUTHPROXY")
rpc.getblockcount()
# If the call to getblockcount() succeeds then the RPC connection is up
- if self.version_is_at_least(190000) and wait_for_import:
+ if self.version_is_at_least(190000) and wait_for_full_init:
# getmempoolinfo.loaded is available since commit
# bb8ae2c (version 0.19.0)
self.wait_until(lambda: rpc.getmempoolinfo()['loaded'])
+ self.wait_for_index_sync(rpc=rpc)
# Wait for the node to finish reindex, block import, and
- # loading the mempool. Usually importing happens fast or
+ # loading the mempool and indexes. Usually importing happens fast or
# even "immediate" when the node is started. However, there
# is no guarantee and sometimes ImportBlocks might finish
# later. This is going to cause intermittent test failures,
@@ -915,6 +916,20 @@ def bumpmocktime(self, seconds):
def wait_until(self, test_function, *, timeout=60, check_interval=0.05):
return wait_until_helper_internal(test_function, timeout=timeout, timeout_factor=self.timeout_factor, check_interval=check_interval)
+ def wait_for_index_sync(self, *, rpc=None, **kwargs):
+ """Wait for every enabled index to reach this node's chain tip."""
+ if not self.version_is_at_least(210000):
+ return # getindexinfo is not available in earlier versions
+
+ rpc = rpc or self
+
+ def indexes_are_synced():
+ indexes = rpc.getindexinfo().values()
+ height = rpc.getblockcount()
+ return all(index["synced"] and index["best_block_height"] == height for index in indexes)
+
+ self.wait_until(indexes_are_synced, **kwargs)
+
class TestNodeCLIAttr:
def __init__(self, cli, command):
self.cli = cli
### test/functional/test_framework/util.py
@@ -721,12 +721,6 @@ def dumb_sync_blocks(*, src, dst, height=None):
assert_equal(dst.getblockcount(), height)
-def sync_txindex(test_framework, node):
- test_framework.log.debug("Waiting for node txindex to sync")
- sync_start = int(time.time())
- test_framework.wait_until(lambda: node.getindexinfo("txindex")["txindex"]["synced"])
- test_framework.log.debug(f"Synced in {time.time() - sync_start} seconds")
-
def wallet_importprivkey(wallet_rpc, privkey, timestamp, *, label=""):
desc = descsum_create("combo(" + privkey + ")")
req = [{Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.