Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2015-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Utilities for doing coverage analysis on the RPC interface.7Provides a way to track which RPC commands are exercised during8testing.9"""11import os13from .authproxy import AuthServiceProxy14from typing import Optional16REFERENCE_FILENAME = 'rpc_interface.txt'19class AuthServiceProxyWrapper():20"""21An object that wraps AuthServiceProxy to record specific RPC calls.23"""24def __init__(self, auth_service_proxy_instance: AuthServiceProxy, rpc_url: str, coverage_logfile: Optional[str]=None):25"""26Kwargs:27auth_service_proxy_instance: the instance being wrapped.28rpc_url: url of the RPC instance being wrapped29coverage_logfile: if specified, write each service_name30out to a file when called.32"""33self.auth_service_proxy_instance = auth_service_proxy_instance34self.rpc_url = rpc_url35self.coverage_logfile = coverage_logfile37def __getattr__(self, name):38return_val = getattr(self.auth_service_proxy_instance, name)39if not isinstance(return_val, type(self.auth_service_proxy_instance)):40# If proxy getattr returned an unwrapped value, do the same here.41return return_val42return AuthServiceProxyWrapper(return_val, self.rpc_url, self.coverage_logfile)44def __call__(self, *args, **kwargs):45"""46Delegates to AuthServiceProxy, then writes the particular RPC method47called to a file.49"""50return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs)51self._log_call()52return return_val54def _log_call(self):55rpc_method = self.auth_service_proxy_instance._service_name57if self.coverage_logfile:58with open(self.coverage_logfile, 'a+') as f:59f.write("%s\n" % rpc_method)61def __truediv__(self, relative_uri):62return AuthServiceProxyWrapper(self.auth_service_proxy_instance / relative_uri,63self.rpc_url,64self.coverage_logfile)66def get_request(self, *args, **kwargs):67self._log_call()68return self.auth_service_proxy_instance.get_request(*args, **kwargs)70def get_filename(dirname, n_node):71"""72Get a filename unique to the test process ID and node.74This file will contain a list of RPC commands covered.75"""76pid = str(os.getpid())77return os.path.join(78dirname, "coverage.pid%s.node%s.txt" % (pid, str(n_node)))81def write_all_rpc_commands(dirname: str, node: AuthServiceProxy) -> bool:82"""83Write out a list of all RPC functions available in `bitcoin-cli` for84coverage comparison. This will only happen once per coverage85directory.87Args:88dirname: temporary test dir89node: client91Returns:92if the RPC interface file was written.94"""95filename = os.path.join(dirname, REFERENCE_FILENAME)97if os.path.isfile(filename):98return False100help_output = node.help().split('\n')101commands = set()103for line in help_output:104line = line.strip()106# Ignore blanks and headers107if line and not line.startswith('='):108commands.add("%s\n" % line.split()[0])110with open(filename, 'w') as f:111f.writelines(list(commands))113return True
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.