Skip to content

Commit 6a4c0c0

Browse files
committed
test: Add functional test for Coinstats index
1 parent 3f166ec commit 6a4c0c0

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 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 coinstatsindex across nodes.
6+
7+
Test that the values returned by gettxoutsetinfo are consistent
8+
between a node running the coinstatsindex and a node without
9+
the index.
10+
"""
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import (
14+
assert_equal,
15+
assert_raises_rpc_error,
16+
try_rpc,
17+
)
18+
19+
class CoinStatsIndexTest(BitcoinTestFramework):
20+
def set_test_params(self):
21+
self.setup_clean_chain = True
22+
self.num_nodes = 2
23+
self.supports_cli = False
24+
self.extra_args = [
25+
[],
26+
["-coinstatsindex"]
27+
]
28+
29+
def skip_test_if_missing_module(self):
30+
self.skip_if_no_wallet()
31+
32+
def run_test(self):
33+
self._test_coin_stats_index()
34+
35+
def _test_coin_stats_index(self):
36+
node = self.nodes[0]
37+
index_node = self.nodes[1]
38+
# Both none and muhash options allow the usage of the index
39+
index_hash_options = ['none', 'muhash']
40+
41+
# Generate a normal transaction and mine it
42+
node.generate(101)
43+
address = self.nodes[0].get_deterministic_priv_key().address
44+
node.sendtoaddress(address=address, amount=10, subtractfeefromamount=True)
45+
node.generate(1)
46+
47+
self.sync_blocks(timeout=120)
48+
49+
self.log.info("Test that gettxoutsetinfo() output is consistent with or without coinstatsindex option")
50+
self.wait_until(lambda: not try_rpc(-32603, "Unable to read UTXO set", node.gettxoutsetinfo))
51+
res0 = node.gettxoutsetinfo('none')
52+
53+
# The fields 'disk_size' and 'transactions' do not exist on the index
54+
del res0['disk_size'], res0['transactions']
55+
56+
self.wait_until(lambda: not try_rpc(-32603, "Unable to read UTXO set", index_node.gettxoutsetinfo, 'muhash'))
57+
for hash_option in index_hash_options:
58+
res1 = index_node.gettxoutsetinfo(hash_option)
59+
res1.pop('muhash', None)
60+
61+
# Everything left should be the same
62+
assert_equal(res1, res0)
63+
64+
self.log.info("Test that gettxoutsetinfo() can get fetch data on specific heights with index")
65+
66+
# Generate a new tip
67+
node.generate(5)
68+
69+
self.wait_until(lambda: not try_rpc(-32603, "Unable to read UTXO set", index_node.gettxoutsetinfo, 'muhash'))
70+
for hash_option in index_hash_options:
71+
# Fetch old stats by height
72+
res2 = index_node.gettxoutsetinfo(hash_option, 102)
73+
res2.pop('muhash', None)
74+
assert_equal(res0, res2)
75+
76+
# Fetch old stats by hash
77+
res3 = index_node.gettxoutsetinfo(hash_option, res0['bestblock'])
78+
res3.pop('muhash', None)
79+
assert_equal(res0, res3)
80+
81+
# It does not work without coinstatsindex
82+
assert_raises_rpc_error(-8, "Querying specific block heights requires coinstatsindex", node.gettxoutsetinfo, hash_option, 102)
83+
84+
if __name__ == '__main__':
85+
CoinStatsIndexTest().main()

test/functional/test_framework/test_framework.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ def cache_path(*paths):
757757

758758
os.rmdir(cache_path('wallets')) # Remove empty wallets dir
759759
for entry in os.listdir(cache_path()):
760-
if entry not in ['chainstate', 'blocks']: # Only keep chainstate and blocks folder
760+
if entry not in ['chainstate', 'blocks', 'indexes']: # Only indexes, chainstate and blocks folders
761761
os.remove(cache_path(entry))
762762

763763
for i in range(self.num_nodes):

test/functional/test_runner.py

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
'rpc_scantxoutset.py',
282282
'feature_logging.py',
283283
'feature_anchors.py',
284+
'feature_coinstatsindex.py',
284285
'p2p_node_network_limited.py',
285286
'p2p_permissions.py',
286287
'feature_blocksdir.py',

0 commit comments

Comments
 (0)