mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:29:40 +00:00
WIP: [RFC] ipc: add Events interface for chain and mempool notifications #36985
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36985
Loading…
Reference in a new issue
No description provided.
Delete branch "ipc-events"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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. ZMQhashblock/rawblockare 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. ZMQhashtx/rawtxalso fire for every tx in connected and disconnected blocks, so a subscriber can't tell whether a tx entered the mempool (pain point of https://github.com/bitcoin/bitcoin/pull/19462#issuecomment-658304417, https://github.com/bitcoin/bitcoin/pull/23624#issue-1065790580 and https://github.com/bitcoin/bitcoin/pull/7753).txRemovedFromMempool: fires for every removal, including txs removed because they were mined (reasonblock), except during IBD, with txid, wtxid, mempool sequence, removal reason and optionally the serialized tx.ZMQ
sequencecovers 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?
rawblock/rawtxhave their own sequence numbers and can't be ordered againstsequence. Here every event can include its serialized block or tx in the same ordered stream. Events come in the same order as ZMQsequence, plus block-inclusion removals.-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?
getrawmempool(mempool_sequence=true) to bootstrap and keep a mempool mirror in sync.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
unionthat this uses is mapped by hand inevents-types.h. Ideally this support would be added to libmultiprocess first.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
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.
This is a draft pull request, so Ralph reviewed only its concept. Code review runs on the first push after it is marked ready for review.
Earlier concerns neither republished nor resolved: 1 major finding · 4 minor findings · 11 suggestions. See the Run archive linked from the Review report for their assessments.
Design and approach
Continue concept review of the submitted approach. The decisive reason is the concrete event-time information benefit over the already capable ZMQ sequence baseline: applications can receive payloads, acceptance metadata, and explicit reasoned removals without racing follow-up queries. Reusing the existing IPC architecture is a plausible way to deliver that benefit while preserving legacy feeds. Do not justify it as introducing ordering for the first time, as a durable journal, or as automatically stable because it uses Cap'n Proto. I would change to reworking the approach if representative workloads show unacceptable validation or aggregate queue costs, or if intended consumers require a least-privilege notification endpoint or cross-version guarantees that this IPC contract cannot reasonably provide. A tested bootstrap/recovery procedure, explicit compatibility policy, and representative resource measurements would strengthen the decision. The PR delivers concrete information that the ZMQ sequence feed lacks: payloads, acceptance-time fee and vsize, reasoned removals, and mined removals with their mempool sequence numbers. The open questions are about the intended consumers, the compatibility policy, access boundaries and resource cost at expected fanout. None of them undermines the PR's purpose. 1 2 3 4 5 6 7 8
Review coverage was incomplete.
Reviewed:
de3bedfd354a· Review report · Previous reportspermalink
Force-pushed
19e212f7b9fixing a Ralph commentcc @0xB10C @instagibbs @ryanofsky I believe you might be interested in this :)
I think the zmq interface is quite light. Have you had the chance to measure the overhead this adds to the node operation and how it compares to zmq? Also, would be nice to have a latency comparison between them. By latency, I mean the time from the actual state transition to the notification becoming available and the time until the client receives it.
Conceptually, I don't see people talking about deprecating the zmq interface, there are probably many clients out there that depend on it. You are not proposing that here, but that's something that I anticipate from this RFC, better to think better about how a change like this would co-exist with zmq. An obvious advantage is to have more room for upgrading the interface without breaking downstream. But zmq is read-only, contrary to the potential of an interface like this.
Which makes me wonder if there's room to abstract an event interface from the "transport" mechanism used to communicate them to external consumers. I think a comparison with zmq in this regard could also be useful long-term. May also be part of a broader monitoring facility (reminds me of #35142).
That's something I would like to see empirically.
Some potential scenarios:
Agreed, these are good cases to measure. I'll test them and share the results. I'll also rewrite the comment to be more precise saying the node never waits for a slow client instead of that a slow client never slows down the node.
Performance isn't the motivation for this PR but I agree it's worth knowing. I'll measure that and the latency compared to ZMQ.
Yes, I don't intend this to replace or deprecate ZMQ. I see it like the Mining IPC interface and getblocktemplate where the new interface covers needs the older one doesn't serve well, while existing clients can keep using the older one.
The internal event notifications are already abstracted from the transport by
CValidationInterface, which both ZMQ and this PR build on. What differs is what each exposes on top of it ZMQ publishes raw bytes per topic, while this provides typed events in a single ordered stream that clients pull from. Sharing more than that would mean refactoring ZMQ without a clear benefit, while risking behavior changes for existing clients.Concept ACK (assuming there are a few consumers of this interface, I'm certainly interested!).
In 2020, I stood up a Bitcoin Core source code fork for coinmetrics.io (now called Talos) called Bitcoin ZMCE (ZMQ Mempool and Chain Events) that does something very similar. They use this in their mempool+chain data collection and monitoring. The current patch sets can be found in https://github.com/coinmetrics/fullnode/tree/master/fullnodes/bitcoin-zmce/patches - I'll ping them, maybe they have input on this PR.
Here's an LLM assisted comparison of your proposal and what I implemented back then. I think you cover most of it, but if there's an easy and cheap way of getting slightly more information out of such interface, why not? I haven't looked into any details.
chainheaderadded: hash, height and 80-byte header for any accepted header, including ones off the best chainAcceptBlockHeaderfor this. This might be useful to have, but might also be a lot of extra code & work here?mempoolreplaced: replaced and replacement tx paired, with both feestxRemovedFromMempoolwith reasonreplacedplus a separatetxAddedToMempool, with nothing linking the two. Having data on replacements has been very interesting, but also probably a bit more workmempoolconfirmed: tx with block height, block hash and headertxRemovedFromMempoolwith reasonblockhas no block hash or height, and is sent before theblockConnectedevents.chaintipchanged,chainconnected)BlockEventhas hash and height; the header and prev hash are only available inside the full block viainclude_data.chaintipchanged: one event per tip updateEventshasblockConnectedper block but no tip event.Mining.waitTipChangedreturns hash and height on a different interface.mempooladded: txid, raw tx, feemempoolremovedwith removal reason, including blockchainconnected: hash, height, raw blockblockDisconnected, which ZMCE has no new topic for.-ipceventsbuffer, default 128 MiB).Thank you for working on this!
Concept ACK
Currently, I'm working on Cluster Mempool Observer as part of https://github.com/0xB10C/project-ideas/issues/15. It does a similar thing as Vinicius' mempool mirror, but by polling the current txids on mempool via RPC and based on this, watch how clusters are being handled (via RPC). All this data is persisted to a SQL database for later analysis.
It has worked well so far, but as I'm aiming for a good correctness rate over time (as polling can leave gaps), I'd probably change to ZMQ and let the RPC polling just as a backfiller.
Your "Why not just do it with ZMQ?" and "Why additional data, including derivable ones?" have some really great points on why I would change to IPC in my case.
block(Dis)Connectednot losing any batch of blocks coming or dealing with reorg, would make my life easier for sure.txAddedToMempoolwould save me some RPC calls andtxRemovedFromMempoolincluding why they were removed it's useful data.It makes me wonder whether Core should always prefer extending it over ZMQ (idk the current project thoughts around it), having more freedom to change the interface (versioning), sounds like a strong pro to keep developing IPC as a long-term tooling.
A few thoughts from my first read through. I had something similar in mind a few months ago but didnt take it further because of the overlap with #29409. That PR would also let a client follow block and mempool changes through
UnstableChain. If I’m following it correctly, the node waits for the client to return from each callback. When there are more than 10 callbacks pending, it looks likeActivateBestChaincan end up waiting too.With the pull model here, the client can fall behind and still tell from a gap in the stream that it missed something. Was the wait in #29409 part of why you went with a separate interface? Perhaps worth mentioning that PR in the description. It would help me understand where you see each one being used. Small thing I noticed too is that both PRs use
@5for a new method onInit.I generally like the idea of using IPC outside of mining. Sjors put together a list of projects using ZMQ a while back. Have you had a chance to try this with any of those? A block explorer came to mind since it could get the tx and its fee together, and outside IBD it could tell which mempool txs got mined without having to work that out from the block. Saw your reply about measuring latency against ZMQ. I’d be curious how the numbers look if you include the RPC call a client would otherwise make to get the fee.
One thing I’m still unsure about is access. With ZMQ a client can just listen, but if I give it access to the
ipcbindsocket, it looks like it can also getRpcand call any enabled RPC by default, wallet ones included.rpcwhitelistdoes seem to be checked, though from what I can tell the client supplies the username. Is there already a way to expose justEventson a socket? Perhaps that could be an option alongside the connection limit if there isn’t one already.