rpc_misc.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2019-2022 The Limenka 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 RPC misc output."""
   6  import xml.etree.ElementTree as ET
   7  
   8  from test_framework.test_framework import LimenkaTestFramework
   9  from test_framework.util import (
  10      assert_raises_rpc_error,
  11      assert_equal,
  12      assert_greater_than,
  13      assert_greater_than_or_equal,
  14  )
  15  
  16  from test_framework.authproxy import JSONRPCException
  17  
  18  
  19  class RpcMiscTest(LimenkaTestFramework):
  20      def set_test_params(self):
  21          self.num_nodes = 1
  22          self.supports_cli = False
  23  
  24      def run_test(self):
  25          node = self.nodes[0]
  26  
  27          self.log.info("test CHECK_NONFATAL")
  28          assert_raises_rpc_error(
  29              -1,
  30              'Internal bug detected: request.params[9].get_str() != "trigger_internal_bug"',
  31              lambda: node.echo(arg9='trigger_internal_bug'),
  32          )
  33  
  34          self.log.info("test getmemoryinfo")
  35          memory = node.getmemoryinfo()['locked']
  36          assert_greater_than(memory['used'], 0)
  37          assert_greater_than(memory['free'], 0)
  38          assert_greater_than(memory['total'], 0)
  39          # assert_greater_than_or_equal() for locked in case locking pages failed at some point
  40          assert_greater_than_or_equal(memory['locked'], 0)
  41          assert_greater_than(memory['chunks_used'], 0)
  42          assert_greater_than(memory['chunks_free'], 0)
  43          assert_equal(memory['used'] + memory['free'], memory['total'])
  44  
  45          self.log.info("test mallocinfo")
  46          try:
  47              mallocinfo = node.getmemoryinfo(mode="mallocinfo")
  48              self.log.info('getmemoryinfo(mode="mallocinfo") call succeeded')
  49              tree = ET.fromstring(mallocinfo)
  50              assert_equal(tree.tag, 'malloc')
  51          except JSONRPCException:
  52              self.log.info('getmemoryinfo(mode="mallocinfo") not available')
  53              assert_raises_rpc_error(-8, 'mallocinfo mode not available', node.getmemoryinfo, mode="mallocinfo")
  54  
  55          assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar")
  56  
  57          self.log.info("test logging rpc and help")
  58  
  59          # Test toggling a logging category on/off/on with the logging RPC.
  60          assert_equal(node.logging()['qt'], True)
  61          node.logging(exclude=['qt'])
  62          assert_equal(node.logging()['qt'], False)
  63          node.logging(include=['qt'])
  64          assert_equal(node.logging()['qt'], True)
  65  
  66          # Test logging RPC returns the logging categories in alphabetical order.
  67          sorted_logging_categories = sorted(node.logging())
  68          assert_equal(list(node.logging()), sorted_logging_categories)
  69  
  70          # Test logging help returns the logging categories string in alphabetical order.
  71          categories = ', '.join(sorted_logging_categories)
  72          logging_help = self.nodes[0].help('logging')
  73          assert f"valid logging categories are: {categories}" in logging_help
  74  
  75          self.log.info("test echoipc (testing spawned process in multiprocess build)")
  76          assert_equal(node.echoipc("hello"), "hello")
  77  
  78          self.log.info("test getindexinfo")
  79          # Without any indices running the RPC returns an empty object
  80          assert_equal(node.getindexinfo(), {})
  81  
  82          # Restart the node with indices and wait for them to sync
  83          self.restart_node(0, ["-txindex", "-blockfilterindex", "-coinstatsindex"])
  84          self.wait_until(lambda: all(i["synced"] for i in node.getindexinfo().values()))
  85  
  86          # Returns a list of all running indices by default
  87          values = {"synced": True, "best_block_height": 200}
  88          assert_equal(
  89              node.getindexinfo(),
  90              {
  91                  "txindex": values,
  92                  "basic block filter index": values,
  93                  "coinstatsindex": values,
  94              }
  95          )
  96          # Specifying an index by name returns only the status of that index
  97          for i in {"txindex", "basic block filter index", "coinstatsindex"}:
  98              assert_equal(node.getindexinfo(i), {i: values})
  99  
 100          # Specifying an unknown index name returns an empty result
 101          assert_equal(node.getindexinfo("foo"), {})
 102  
 103  
 104  if __name__ == '__main__':
 105      RpcMiscTest(__file__).main()
 106