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.
252self.log.info("Hooking into mempool:replaced tracepoint...")253node = self.nodes[0]254ctx = USDT(pid=node.process.pid)255ctx.enable_probe(probe="mempool:replaced", fn_name="trace_replaced")256bpf = BPF(text=MEMPOOL_TRACEPOINTS_PROGRAM, usdt_contexts=[ctx], debug=0, cflags=bpf_cflags())258def handle_replaced_event(_, data, __):259event = ctypes.cast(data, ctypes.POINTER(MempoolReplaced)).contents260events.append(event)262bpf["replaced_events"].open_perf_buffer(handle_replaced_event)264self.log.info("Sending RBF transaction...")265utxo = self.wallet.get_utxo(mark_as_spent=True)266original_fee = Decimal(40000)267original_tx = self.wallet.send_self_transfer(268from_node=node, utxo_to_spend=utxo, fee=original_fee / COIN269)270entry_time = node.getmempoolentry(original_tx["txid"])["time"]272self.log.info("Sending replacement transaction...")273replacement_fee = Decimal(45000)274replacement_tx = self.wallet.send_self_transfer(275from_node=node, utxo_to_spend=utxo, fee=replacement_fee / COIN276)278self.log.info("Polling buffer...")279bpf.perf_buffer_poll(timeout=200)281self.log.info("Ensuring mempool:replaced event was handled successfully...")282assert_equal(1, len(events))283event = events[0]284assert_equal(bytes(event.replaced_hash)[::-1].hex(), original_tx["txid"])285assert_equal(event.replaced_vsize, original_tx["tx"].get_vsize())286assert_equal(event.replaced_fee, original_fee)287assert_equal(event.replaced_entry_time, entry_time)288assert_equal(bytes(event.replacement_hash)[::-1].hex(), replacement_tx["txid"])289assert_equal(event.replacement_vsize, replacement_tx["tx"].get_vsize())290assert_equal(event.replacement_fee, replacement_fee)291assert_equal(event.replaced_by_transaction, True)293bpf.cleanup()294self.generate(self.wallet, 1)296def rejected_test(self):297"""Create an invalid transaction and make sure the tracepoint returns298the expected txid, rejection reason, peer id, and peer address."""300events = []302self.log.info("Adding P2P connection...")303node = self.nodes[0]304node.add_p2p_connection(P2PDataStore())306self.log.info("Hooking into mempool:rejected tracepoint...")307ctx = USDT(pid=node.process.pid)308ctx.enable_probe(probe="mempool:rejected", fn_name="trace_rejected")309bpf = BPF(text=MEMPOOL_TRACEPOINTS_PROGRAM, usdt_contexts=[ctx], debug=0, cflags=bpf_cflags())311def handle_rejected_event(_, data, __):312events.append(bpf["rejected_events"].event(data))314bpf["rejected_events"].open_perf_buffer(handle_rejected_event)316self.log.info("Sending invalid transaction...")317tx = self.wallet.create_self_transfer(fee_rate=Decimal(0))318node.p2ps[0].send_txs_and_test([tx["tx"]], node, success=False)320self.log.info("Polling buffer...")321bpf.perf_buffer_poll(timeout=200)323self.log.info("Ensuring mempool:rejected event was handled successfully...")324assert_equal(1, len(events))325event = events[0]326assert_equal(bytes(event.hash)[::-1].hex(), tx["tx"].txid_hex)327assert_equal(event.reason.decode("UTF-8"), "min relay fee not met")329bpf.cleanup()330self.generate(self.wallet, 1)332def run_test(self):333"""Tests the mempool:added, mempool:removed, mempool:replaced,334and mempool:rejected tracepoints."""336# Create some coinbase transactions and mature them so they can be spent337node = self.nodes[0]338self.wallet = MiniWallet(node)339self.generate(self.wallet, 4)340self.generate(node, COINBASE_MATURITY)342# Test individual tracepoints343self.added_test()344self.removed_test()345self.replaced_test()346self.rejected_test()349if __name__ == "__main__":350MempoolTracepointTest(__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.