[RFC] ipc: add Events interface for chain and mempool notifications #36985

pull ViniciusCestarii wants to merge 4 commits into bitcoin:master from ViniciusCestarii:ipc-events changing 20 files +843 −7
  1. ViniciusCestarii commented at 8:48 PM on November 25, 2026: contributor

    This adds an Events IPC interface that streams block and mempool events in one ordered stream, addressing gaps in ZMQ notifications and providing a more modular interface. It supports the following subscriptions:

    • blockConnected: fires for every block connected to the active chain, including during IBD (blocks connected by a background assumeutxo chainstate are skipped). Carries hash, height and optionally the serialized block. ZMQ hashblock/rawblock are driven by UpdatedBlockTip, so they skip intermediate blocks when several connect at once and don't fire during IBD.
    • blockDisconnected: fires for every block disconnected during a reorg, with hash, height and optionally the serialized block.
    • txAddedToMempool: fires only for mempool acceptance. Carries txid, wtxid, mempool sequence, fee, vsize and optionally the serialized tx. ZMQ hashtx/rawtx also fire for every tx in connected and disconnected blocks, so a subscriber can't tell whether a tx entered the mempool (pain point of #19462 (comment), #23624#issue-1065790580 and #7753).
    • txRemovedFromMempool: fires for every removal, including txs removed because they were mined (reason block), except during IBD, with txid, wtxid, mempool sequence, removal reason and optionally the serialized tx.

    ZMQ sequence covers all four of these, but only carries hashes (plus the mempool sequence for "A"/"R"), and "R" doesn't include block-inclusion removals or a reason.

    Why not just do it with ZMQ?

    • One ordered stream that includes the data. rawblock/rawtx have their own sequence numbers and can't be ordered against sequence. Here every event can include its serialized block or tx in the same ordered stream. Events come in the same order as ZMQ sequence, plus block-inclusion removals.
    • Typed, versioned interface. Cap'n Proto generates the client code and lets the schema evolve without breaking older clients. With ZMQ, clients parse raw bytes by hand which can't evolve existing topics without breaking changes.
    • Fixing these in ZMQ would need new topics reporting on the same events as existing ones, which is confusing for users, and changing the existing topics would break current consumers.
    • Bounded buffering. Events are buffered per subscription up to a byte limit (-ipceventsbuffer), and the client pulls them in batches with waitNext. ZMQ limits are message counts per topic (-zmqpub*hwm), which don't bound memory when messages are full blocks.

    Why additional data, including derivable ones?

    • fee, vsize: neither can be derived from the tx alone, so including them saves RPC calls per tx. RPC calls can also fail if the tx was removed in the time in between.
    • mempool sequence: lets a client line events up with getrawmempool (mempool_sequence=true) to bootstrap and keep a mempool mirror in sync.
    • height: not in the serialized block, and needed to handle reorgs without an extra RPC call.
    • hash, txid, wtxid: identifiers are always included, so clients don't need to branch on include_data.

    Example of simple clients using this interface, including a mempool mirror: https://github.com/ViniciusCestarii/bitcoin-ipc-events-client

    Note: to make this interface usable in this PR, the Cap'n Proto union that this uses is mapped by hand in events-types.h. Ideally this support would be added to libmultiprocess first.

  2. DrahtBot commented at 8:48 PM on November 25, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36188 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36188.svg"></sub> (crypto: plug hardware optimized SHA256 into libsecp256k1's context by furszy)
    • #17783 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/17783.svg"></sub> (common: Disallow calling IsArgSet() on ALLOW_LIST options by ryanofsky)
    • #17580 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/17580.svg"></sub> (refactor: Add ALLOW_LIST flags and enforce usage in CheckArgFlags by ryanofsky)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. ViniciusCestarii force-pushed on Nov 25, 2026
  4. refactor: move maxDouble constant to common.capnp
    The upcoming Events interface needs the same default timeout as
    Mining. Move the constant to common.capnp so it can be shared without
    importing mining.capnp. No behavior change.
    a225dd8d4c
  5. mempool: report sequence of txs removed for block
    Transactions removed from the mempool because they were included in a
    block are only reported through MempoolTransactionsRemovedForBlock,
    which did not carry the mempool sequence consumed by each removal.
    Without it, subscribers cannot order these removals relative to other
    mempool additions and removals.
    
    Return the sequence from removeUnchecked and store it in
    RemovedMempoolTransactionInfo. It is used by the Events interface
    added in the next commit.
    b3682ac7eb
  6. ViniciusCestarii force-pushed on Nov 25, 2026
  7. interfaces: add Events interface
    Add a pull-based stream of chain and mempool events (block connected,
    block disconnected, transaction added to and removed from the
    mempool). Each subscription chooses which event types it wants and
    whether to include the serialized block or transaction.
    
    Events are buffered on the node side from the moment the interface is
    created, so a slow client never slows down the node. The buffer is
    limited by the new -ipceventsbuffer option. When it is full, the
    oldest events are dropped and the client sees a gap in the per
    subscription Event::sequence. waitNext acknowledges events up to
    "after" and returns a batch limited by max_events and MAX_BATCH_BYTES.
    
    The mutex and condition variable live in KernelNotifications so that
    Interrupt() can wake pending waitNext calls on shutdown.
    2d12d11333
  8. ipc: expose Events interface
    Add events.capnp and an Init.makeEvents method so IPC clients can
    subscribe to chain and mempool events.
    
    libmultiprocess does not map std::variant to capnp unions, so
    interfaces::Event is converted to and from the Event union by hand in
    events-types.h, reusing the generated code for each union member.
    
    Add a functional test covering the interface.
    19e212f7b9
  9. ViniciusCestarii force-pushed on Nov 26, 2026
  10. ViniciusCestarii commented at 1:30 PM on November 26, 2026: contributor

    Force-pushed 19e212f7b923fee4cb3b318d0616b61d1c0d2964 fixing a Ralph comment


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 02:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me