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.
751CAddress.NET_IPV4: [752"20.0.0.1",753"30.0.0.1",754"40.0.0.1",755"50.0.0.1",756"60.0.0.1",757"70.0.0.1",758"80.0.0.1",759"90.0.0.1",760"100.0.0.1",761"110.0.0.1",762"120.0.0.1",763"130.0.0.1",764"140.0.0.1",765"150.0.0.1",766"160.0.0.1",767"170.0.0.1",768"180.0.0.1",769"190.0.0.1",770"200.0.0.1",771"210.0.0.1",772],773CAddress.NET_IPV6: [774"[20::1]",775"[30::1]",776"[40::1]",777"[50::1]",778"[60::1]",779"[70::1]",780"[80::1]",781"[90::1]",782"[100::1]",783"[110::1]",784"[120::1]",785"[130::1]",786"[140::1]",787"[150::1]",788"[160::1]",789"[170::1]",790"[180::1]",791"[190::1]",792"[200::1]",793"[210::1]",794],795CAddress.NET_TORV3: [796"testonlyad777777777777777777777777777777777777777775b6qd.onion",797"testonlyah77777777777777777777777777777777777777777z7ayd.onion",798"testonlyal77777777777777777777777777777777777777777vp6qd.onion",799"testonlyap77777777777777777777777777777777777777777r5qad.onion",800"testonlyat77777777777777777777777777777777777777777udsid.onion",801"testonlyax77777777777777777777777777777777777777777yciid.onion",802"testonlya777777777777777777777777777777777777777777rhgyd.onion",803"testonlybd77777777777777777777777777777777777777777rs4ad.onion",804"testonlybp77777777777777777777777777777777777777777zs2ad.onion",805"testonlybt777777777777777777777777777777777777777777x6id.onion",806"testonlybx777777777777777777777777777777777777777775styd.onion",807"testonlyb3777777777777777777777777777777777777777774ckid.onion",808"testonlycd77777777777777777777777777777777777777777733id.onion",809"testonlych77777777777777777777777777777777777777777t6kid.onion",810"testonlycl77777777777777777777777777777777777777777tt3ad.onion",811"testonlyct77777777777777777777777777777777777777777wvhyd.onion",812"testonlycx7777777777777777777777777777777777777777774bad.onion",813"testonlyc377777777777777777777777777777777777777777u6aid.onion",814"testonlydd777777777777777777777777777777777777777777u5ad.onion",815"testonlydh77777777777777777777777777777777777777777wgnyd.onion",816],817CAddress.NET_I2P: [818"testonlyad77777777777777777777777777777777777777777q.b32.i2p",819"testonlyah77777777777777777777777777777777777777777q.b32.i2p",820"testonlyap77777777777777777777777777777777777777777q.b32.i2p",821"testonlyat77777777777777777777777777777777777777777q.b32.i2p",822"testonlyax77777777777777777777777777777777777777777q.b32.i2p",823"testonlya377777777777777777777777777777777777777777q.b32.i2p",824"testonlya777777777777777777777777777777777777777777q.b32.i2p",825"testonlybd77777777777777777777777777777777777777777q.b32.i2p",826"testonlybh77777777777777777777777777777777777777777q.b32.i2p",827"testonlybl77777777777777777777777777777777777777777q.b32.i2p",828"testonlybp77777777777777777777777777777777777777777q.b32.i2p",829"testonlybt77777777777777777777777777777777777777777q.b32.i2p",830"testonlybx77777777777777777777777777777777777777777q.b32.i2p",831"testonlyb777777777777777777777777777777777777777777q.b32.i2p",832"testonlych77777777777777777777777777777777777777777q.b32.i2p",833"testonlycp77777777777777777777777777777777777777777q.b32.i2p",834"testonlyct77777777777777777777777777777777777777777q.b32.i2p",835"testonlycx77777777777777777777777777777777777777777q.b32.i2p",836"testonlyc377777777777777777777777777777777777777777q.b32.i2p",837"testonlyc777777777777777777777777777777777777777777q.b32.i2p",838],839CAddress.NET_CJDNS: [840"[fc00::1]",841"[fc00::2]",842"[fc00::3]",843"[fc00::5]",844"[fc00::6]",845"[fc00::7]",846"[fc00::8]",847"[fc00::9]",848"[fc00::10]",849"[fc00::11]",850"[fc00::12]",851"[fc00::13]",852"[fc00::15]",853"[fc00::16]",854"[fc00::17]",855"[fc00::18]",856"[fc00::19]",857"[fc00::20]",858"[fc00::22]",859"[fc00::23]",860],861}862for addr_type in address_types_to_add:863for addr in ADDRESSES[addr_type]:864res = self.nodes[node_index].addpeeraddress(address=addr, port=0 if addr.endswith(".i2p") else 8333, tried=False)865if not res["success"]:866self.log.debug(f"Could not add {addr} to nodes[{node_index}]'s addrman (collision?)")868# Private helper methods. These should not be accessed by the subclass test scripts.870def _start_logging(self):871# Add logger and logging handlers872self.log = logging.getLogger('TestFramework')873self.log.setLevel(logging.DEBUG)874# Create file handler to log all messages875fh = logging.FileHandler(self.options.tmpdir + '/test_framework.log')876fh.setLevel(logging.DEBUG)877# Create console handler to log messages to stderr. By default this logs only error messages, but can be configured with --loglevel.878ch = logging.StreamHandler(sys.stdout)879# User can provide log level as a number or string (eg DEBUG). loglevel was caught as a string, so try to convert it to an int880ll = int(self.options.loglevel) if self.options.loglevel.isdigit() else self.options.loglevel.upper()881ch.setLevel(ll)883# Format logs the same as bitcoind's debug.log with microprecision (so log files can be concatenated and sorted)884class MicrosecondFormatter(logging.Formatter):885def formatTime(self, record, _=None):886dt = datetime.fromtimestamp(record.created, timezone.utc)887return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')889formatter = MicrosecondFormatter(890fmt='%(asctime)sZ %(name)s (%(levelname)s): %(message)s',891)892fh.setFormatter(formatter)893ch.setFormatter(formatter)894# add the handlers to the logger895self.log.addHandler(fh)896self.log.addHandler(ch)898if self.options.trace_rpc:899rpc_logger = logging.getLogger("BitcoinRPC")900rpc_logger.setLevel(logging.DEBUG)901rpc_handler = logging.StreamHandler(sys.stdout)902rpc_handler.setLevel(logging.DEBUG)903rpc_logger.addHandler(rpc_handler)905def _initialize_chain(self):906"""Initialize a pre-mined blockchain for use by the test.908Create a cache of a 199-block-long chain909Afterward, create num_nodes copies from the cache."""911CACHE_NODE_ID = 0 # Use node 0 to create the cache for all other nodes912cache_node_dir = get_datadir_path(self.options.cachedir, CACHE_NODE_ID)913assert self.num_nodes <= MAX_NODES915if not os.path.isdir(cache_node_dir):916self.log.debug("Creating cache directory {}".format(cache_node_dir))918initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain, self.disable_autoconnect)919self.nodes.append(920TestNode(921CACHE_NODE_ID,922cache_node_dir,923chain=self.chain,924extra_conf=["bind=127.0.0.1"],925extra_args=[],926rpchost=None,927timewait=self.rpc_timeout,928timeout_factor=self.options.timeout_factor,929binaries=self.get_binaries(),930coverage_dir=None,931cwd=self.options.tmpdir,932uses_wallet=self.uses_wallet,933))934self.start_node(CACHE_NODE_ID)935cache_node = self.nodes[CACHE_NODE_ID]937# Wait for RPC connections to be ready938cache_node.wait_for_rpc_connection()940# Set a time in the past, so that blocks don't end up in the future941cache_node.setmocktime(cache_node.getblockheader(cache_node.getbestblockhash())['time'])943# Create a 199-block-long chain; each of the 3 first nodes944# gets 25 mature blocks and 25 immature.945# The 4th address gets 25 mature and only 24 immature blocks so that the very last946# block in the cache does not age too much (have an old tip age).947# This is needed so that we are out of IBD when the test starts,948# see the tip age check in IsInitialBlockDownload().949gen_addresses = [k.address for k in TestNode.PRIV_KEYS][:3] + [create_deterministic_address_bcrt1_p2tr_op_true()[0]]950assert_equal(len(gen_addresses), 4)951for i in range(8):952self.generatetoaddress(953cache_node,954nblocks=25 if i != 7 else 24,955address=gen_addresses[i % len(gen_addresses)],956)958assert_equal(cache_node.getblockchaininfo()["blocks"], 199)960# Shut it down, and clean up cache directories:961self.stop_nodes()962self.nodes = []964cache_path = cache_node_dir / self.chain966(cache_path / "wallets").rmdir() # Do not cache empty wallets dir967shutil.rmtree(cache_path / "fees") # Do not cache fees dat files968for entry in cache_path.iterdir():969if entry.name not in ["chainstate", "blocks", "indexes"]: # Only keep indexes, chainstate and blocks folders970entry.unlink()972for i in range(self.num_nodes):973self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i))974to_dir = get_datadir_path(self.options.tmpdir, i)975shutil.copytree(cache_node_dir, to_dir)976initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect) # Overwrite port/rpcport in bitcoin.conf978def _initialize_chain_clean(self):979"""Initialize empty blockchain for use by the test.981Create an empty blockchain and num_nodes wallets.982Useful if a test case wants complete control over initialization."""983for i in range(self.num_nodes):984initialize_datadir(self.options.tmpdir, i, self.chain, self.disable_autoconnect)986def skip_if_no_py3_zmq(self):987"""Attempt to import the zmq package and skip the test if the import fails."""988try:989import zmq # noqa990except ImportError:991raise SkipTest("python3-zmq module not available.")993def skip_if_no_py_sqlite3(self):994"""Attempt to import the sqlite3 package and skip the test if the import fails."""995try:996import sqlite3 # noqa997except ImportError:998raise SkipTest("sqlite3 module not available.")1000def skip_if_no_py_capnp(self):
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.