3

Let's say I want to build a database of transactions that is continuously being updated from a locally running Bitcoin Core node using JSON-RPC and ZeroMQ. My concern is how to make it robust against chain reorganizations and other events where transactions might get dropped.

If I restrict myself only to confirmed transactions, it seems simple enough to just listen for new blocks, and in the event of a reorganization, rollback all transactions confirmed at a specific height and reapply the new chaintip.

However, it gets more complicated if I also want to include unconfirmed transactions. Then for every new transaction (whether in the mempool or in a block) I need to check if it conflicts with one or more unconfirmed transactions and remove those. Bitcoin Core already does this, but other than publishing the new transaction in a rawtx ZeroMQ event it gives no indication that an unconfirmed transaction was dropped.

So, is there a simple way to maintain a mirror of Bitcoin Core's mempool?

Some ideas:

  • Call the getrawmempool RPC every 30 seconds or so to get the current state of the mempool. This has the advantage of being very simple, but it's inefficient and it means the mirror will lag behind.
  • Maintain an index of all outputs that are spent by transactions currently in the mempool, and check every new transaction against it. However, I'm not sure this covers every case where a transaction needs to be dropped, and it certainly doesn't cover transactions dropped due to -maxmempool and -mempoolexpiry.
Vojtěch Strnad
  • 5,623
  • 1
  • 8
  • 31

1 Answers1

3

There are two methods to do this: ZMQ and Tracepoints.

The zmqpubsequence can be used to notifications for all mempool events. See the PR and test for more information. It was designed specifically for the mempool mirroring use case.

The other method is tracepoints, but this requires applying a currently draft PR. Tracepoints work similarly to ZMQ in that your program will use a tracepoints interface and wait for notifications.

Andrew Chow
  • 67,209
  • 5
  • 76
  • 149
  • Isn't `R` printed as a notification when a transaction is removed from mempool if using sequenece? https://github.com/bitcoin/bitcoin/blob/1abbae65eb9c99df5d8941008068d83ad99bf117/src/zmq/zmqpublishnotifier.cpp#L298 –  Jul 28 '22 at 23:06
  • @1440000bytes Good point, forgot that `zmqpubsequence` was added. I've updated the answer. – Andrew Chow Jul 29 '22 at 19:35
  • I you want a project example which keeps a continuous updated copy of a mempool you can check https://github.com/mempoolexplorer/mempoolexplorer – dev7ba Feb 16 '23 at 11:46