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.
502# 3) Consume ZMQ backlog until we get to "now" for the mempool snapshot503while True:504if zmq_mem_seq == get_raw_seq - 1:505break506(hash_str, label, mempool_sequence) = seq.receive_sequence()507if mempool_sequence is not None:508zmq_mem_seq = mempool_sequence509if zmq_mem_seq > get_raw_seq:510raise Exception(f"We somehow jumped mempool sequence numbers! zmq_mem_seq: {zmq_mem_seq} > get_raw_seq: {get_raw_seq}")512# 4) Moving forward, we apply the delta to our local view513# remaining txs(5) + 1 rbf(A+R) + 1 block connect + 1 final tx514expected_sequence = get_raw_seq515r_gap = 0516for _ in range(num_txs + 2 + 1 + 1):517(hash_str, label, mempool_sequence) = seq.receive_sequence()518if mempool_sequence is not None:519if mempool_sequence != expected_sequence:520# Detected "R" gap, means this a conflict eviction, and mempool tx are being evicted before its521# position in the incoming block message "C"522if label == "R":523assert mempool_sequence > expected_sequence524r_gap += mempool_sequence - expected_sequence525else:526raise Exception(f"WARNING: txhash has unexpected mempool sequence value: {mempool_sequence} vs expected {expected_sequence}")527if label == "A":528assert hash_str not in mempool_view529mempool_view.add(hash_str)530expected_sequence = mempool_sequence + 1531elif label == "R":532assert hash_str in mempool_view533mempool_view.remove(hash_str)534expected_sequence = mempool_sequence + 1535elif label == "C":536# (Attempt to) remove all txids from known block connects537block_txids = self.nodes[0].getblock(hash_str)["tx"][1:]538for txid in block_txids:539if txid in mempool_view:540expected_sequence += 1541mempool_view.remove(txid)542expected_sequence -= r_gap543r_gap = 0544elif label == "D":545# Not useful for mempool tracking per se546continue547else:548raise Exception("Unexpected ZMQ sequence label!")550assert_equal(self.nodes[0].getrawmempool(), [final_txid])551assert_equal(self.nodes[0].getrawmempool(mempool_sequence=True)["mempool_sequence"], expected_sequence)553# 5) If you miss a zmq/mempool sequence number, go back to step (2)555self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE)557def test_multiple_interfaces(self):558# Set up two subscribers with different addresses559# (note that after the reorg test, syncing would fail due to different560# chain lengths on node0 and node1; for this test we only need node0, so561# we can disable syncing blocks on the setup)562subscribers = self.setup_zmq_test([563("hashblock", f"tcp://127.0.0.1:{self.zmq_port_base + 1}"),564("hashblock", f"tcp://127.0.0.1:{self.zmq_port_base + 2}"),565], sync_blocks=False)567# Generate 1 block in nodes[0] and receive all notifications568self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE, sync_fun=self.no_op)570# Should receive the same block hash on both subscribers571assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())572assert_equal(self.nodes[0].getbestblockhash(), subscribers[1].receive().hex())574def test_ipv6(self):575if not test_ipv6_local():576self.log.info("Skipping IPv6 test, because IPv6 is not supported.")577return578self.log.info("Testing IPv6")579# Set up subscriber using IPv6 loopback address580subscribers = self.setup_zmq_test([581("hashblock", f"tcp://[::1]:{self.zmq_port_base}")582], ipv6=True)584# Generate 1 block in nodes[0]585self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE)587# Should receive the same block hash588assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())591if __name__ == '__main__':592ZMQTest(__file__).main()
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.