From 3301d2cbe8c3b76c97285d75fa59637cb6952d0b Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Mon, 10 Mar 2025 15:24:16 +0100 Subject: [PATCH] qa: Wait for txindex to avoid race condition Can be verified to be necessary through adding std::this_thread::sleep_for(0.5s) at the beginning of TxIndex::CustomAppend. --- test/functional/mempool_accept.py | 2 ++ test/functional/rpc_rawtransaction.py | 2 ++ test/functional/rpc_txoutproof.py | 2 ++ test/functional/test_framework/util.py | 7 +++++++ 4 files changed, 13 insertions(+) diff --git a/test/functional/mempool_accept.py b/test/functional/mempool_accept.py index 27ecc3b4a84..9014ab260e1 100755 --- a/test/functional/mempool_accept.py +++ b/test/functional/mempool_accept.py @@ -45,6 +45,7 @@ from test_framework.util import ( 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 @@ -270,6 +271,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework): 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( diff --git a/test/functional/rpc_rawtransaction.py b/test/functional/rpc_rawtransaction.py index b8870bd8139..3c3eb9fcb68 100755 --- a/test/functional/rpc_rawtransaction.py +++ b/test/functional/rpc_rawtransaction.py @@ -34,6 +34,7 @@ from test_framework.util import ( assert_equal, assert_greater_than, assert_raises_rpc_error, + sync_txindex, ) from test_framework.wallet import ( getnewdestination, @@ -109,6 +110,7 @@ class RawTransactionsTest(BitcoinTestFramework): self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex") if n == 0: + sync_txindex(self, self.nodes[n]) # With -txindex. # 1. valid parameters - only supply txid assert_equal(self.nodes[n].getrawtransaction(txId), tx['hex']) diff --git a/test/functional/rpc_txoutproof.py b/test/functional/rpc_txoutproof.py index 90572245d60..d430f47f663 100755 --- a/test/functional/rpc_txoutproof.py +++ b/test/functional/rpc_txoutproof.py @@ -12,6 +12,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, + sync_txindex, ) from test_framework.wallet import MiniWallet @@ -77,6 +78,7 @@ class MerkleBlockTest(BitcoinTestFramework): 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]) diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index 14930ef6719..ceca0d3b19b 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -592,3 +592,10 @@ def find_vout_for_address(node, txid, addr): if addr == tx["vout"][i]["scriptPubKey"]["address"]: return i raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr)) + + +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")