rpc_getblockfilter.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2018-present 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 the getblockfilter RPC."""
   6  
   7  from test_framework.test_framework import BitcoinTestFramework
   8  from test_framework.util import (
   9      assert_equal, assert_is_hex_string, assert_raises_rpc_error,
  10      )
  11  
  12  FILTER_TYPES = ["basic"]
  13  
  14  class GetBlockFilterTest(BitcoinTestFramework):
  15      def set_test_params(self):
  16          self.setup_clean_chain = True
  17          self.num_nodes = 2
  18          self.extra_args = [["-blockfilterindex"], []]
  19  
  20      def run_test(self):
  21          # Create two chains by disconnecting nodes 0 & 1, mining, then reconnecting
  22          self.disconnect_nodes(0, 1)
  23  
  24          self.generate(self.nodes[0], 3, sync_fun=self.no_op)
  25          self.generate(self.nodes[1], 4, sync_fun=self.no_op)
  26  
  27          assert_equal(self.nodes[0].getblockcount(), 3)
  28          chain0_hashes = [self.nodes[0].getblockhash(block_height) for block_height in range(4)]
  29  
  30          # Reorg node 0 to a new chain
  31          self.connect_nodes(0, 1)
  32          self.sync_blocks()
  33  
  34          assert_equal(self.nodes[0].getblockcount(), 4)
  35          chain1_hashes = [self.nodes[0].getblockhash(block_height) for block_height in range(4)]
  36  
  37          # Test getblockfilter returns a filter for all blocks and filter types on active chain
  38          for block_hash in chain1_hashes:
  39              for filter_type in FILTER_TYPES:
  40                  result = self.nodes[0].getblockfilter(block_hash, filter_type)
  41                  assert_is_hex_string(result['filter'])
  42  
  43          # Test getblockfilter returns a filter for all blocks and filter types on stale chain
  44          for block_hash in chain0_hashes:
  45              for filter_type in FILTER_TYPES:
  46                  result = self.nodes[0].getblockfilter(block_hash, filter_type)
  47                  assert_is_hex_string(result['filter'])
  48  
  49          # Test getblockfilter with unknown block
  50          bad_block_hash = "0123456789abcdef" * 4
  51          assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblockfilter, bad_block_hash, "basic")
  52  
  53          # Test getblockfilter with undefined filter type
  54          genesis_hash = self.nodes[0].getblockhash(0)
  55          assert_raises_rpc_error(-5, "Unknown filtertype", self.nodes[0].getblockfilter, genesis_hash, "unknown")
  56  
  57          # Test getblockfilter fails on node without compact block filter index
  58          self.restart_node(0, extra_args=["-blockfilterindex=0"])
  59          for filter_type in FILTER_TYPES:
  60              assert_raises_rpc_error(-1, "Index is not enabled for filtertype {}".format(filter_type),
  61                                      self.nodes[0].getblockfilter, genesis_hash, filter_type)
  62  
  63  if __name__ == '__main__':
  64      GetBlockFilterTest(__file__).main()
  65