|
| 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() |
0 commit comments