Skip to content

Commit b218f12

Browse files
committed
merge bitcoin#23046: Add txindex migration test
1 parent ebae59e commit b218f12

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test that legacy txindex will be disabled on upgrade.
6+
7+
Previous releases are required by this test, see test/README.md.
8+
"""
9+
10+
import os
11+
import shutil
12+
13+
from test_framework.test_framework import BitcoinTestFramework
14+
from test_framework.wallet import MiniWallet
15+
16+
17+
class MempoolCompatibilityTest(BitcoinTestFramework):
18+
def set_test_params(self):
19+
self.num_nodes = 3
20+
self.extra_args = [
21+
["-reindex", "-txindex"],
22+
["-txindex=0"],
23+
["-txindex=0"],
24+
]
25+
26+
def skip_test_if_missing_module(self):
27+
self.skip_if_no_previous_releases()
28+
29+
def setup_network(self):
30+
self.add_nodes(
31+
self.num_nodes,
32+
self.extra_args,
33+
versions=[
34+
170003, # Last release with legacy txindex
35+
None, # For MiniWallet, without migration code
36+
18020200, # Any release with migration code (0.18.x - 21.x)
37+
# We are using v18.2.2 to avoid MigrateDBIfNeeded(2) routines as
38+
# they don't handle a no-upgrade case correctly
39+
],
40+
)
41+
# Delete v18.2.2 cached datadir to avoid making a legacy version try to
42+
# make sense of our current database formats
43+
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain))
44+
self.start_nodes()
45+
self.connect_nodes(0, 1)
46+
self.connect_nodes(1, 2)
47+
48+
def run_test(self):
49+
mini_wallet = MiniWallet(self.nodes[1])
50+
mini_wallet.rescan_utxos()
51+
spend_utxo = mini_wallet.get_utxo()
52+
mini_wallet.send_self_transfer(from_node=self.nodes[1], utxo_to_spend=spend_utxo)
53+
self.generate(self.nodes[1], 1)
54+
55+
self.log.info("Check legacy txindex")
56+
self.nodes[0].getrawtransaction(txid=spend_utxo["txid"]) # Requires -txindex
57+
58+
self.stop_nodes()
59+
legacy_chain_dir = os.path.join(self.nodes[0].datadir, self.chain)
60+
61+
self.log.info("Migrate legacy txindex")
62+
migrate_chain_dir = os.path.join(self.nodes[2].datadir, self.chain)
63+
shutil.rmtree(migrate_chain_dir)
64+
shutil.copytree(legacy_chain_dir, migrate_chain_dir)
65+
with self.nodes[2].assert_debug_log([
66+
"Upgrading txindex database...",
67+
"txindex is enabled at height 200",
68+
]):
69+
self.start_node(2, extra_args=["-txindex"])
70+
self.nodes[2].getrawtransaction(txid=spend_utxo["txid"]) # Requires -txindex
71+
72+
self.log.info("Drop legacy txindex")
73+
drop_index_chain_dir = os.path.join(self.nodes[1].datadir, self.chain)
74+
shutil.rmtree(drop_index_chain_dir)
75+
shutil.copytree(legacy_chain_dir, drop_index_chain_dir)
76+
self.nodes[1].assert_start_raises_init_error(
77+
extra_args=["-txindex"],
78+
expected_msg="Error: The block index db contains a legacy 'txindex'. To clear the occupied disk space, run a full -reindex, otherwise ignore this error. This error message will not be displayed again.",
79+
)
80+
# Build txindex from scratch and check there is no error this time
81+
self.start_node(1, extra_args=["-txindex"])
82+
self.nodes[2].getrawtransaction(txid=spend_utxo["txid"]) # Requires -txindex
83+
84+
self.stop_nodes()
85+
86+
self.log.info("Check migrated txindex can not be read by legacy node")
87+
err_msg = f": You need to rebuild the database using -reindex to change -txindex.{os.linesep}Please restart with -reindex or -reindex-chainstate to recover."
88+
shutil.rmtree(legacy_chain_dir)
89+
shutil.copytree(migrate_chain_dir, legacy_chain_dir)
90+
self.nodes[0].assert_start_raises_init_error(extra_args=["-txindex"], expected_msg=err_msg)
91+
shutil.rmtree(legacy_chain_dir)
92+
shutil.copytree(drop_index_chain_dir, legacy_chain_dir)
93+
self.nodes[0].assert_start_raises_init_error(extra_args=["-txindex"], expected_msg=err_msg)
94+
95+
96+
if __name__ == "__main__":
97+
MempoolCompatibilityTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@
346346
'rpc_deriveaddresses.py --usecli',
347347
'p2p_ping.py',
348348
'rpc_scantxoutset.py',
349+
'feature_txindex_compatibility.py',
349350
'feature_logging.py',
350351
'feature_anchors.py',
351352
'feature_coinstatsindex.py',

0 commit comments

Comments
 (0)