2017-02-16 15:05:46 +01:00
# Block and Transaction Broadcasting with ZeroMQ
2014-11-18 18:06:32 +01:00
2020-09-11 07:12:43 +02:00
[ZeroMQ ](https://zeromq.org/ ) is a lightweight wrapper around TCP
2015-09-29 19:48:45 +02:00
connections, inter-process communication, and shared-memory,
2015-10-17 12:10:45 +02:00
providing various message-oriented semantics such as publish/subscribe,
2014-11-18 18:06:32 +01:00
request/reply, and push/pull.
2016-03-04 08:25:16 +01:00
The Dash Core daemon can be configured to act as a trusted "border
router", implementing the dash wire protocol and relay, making
2014-11-18 18:06:32 +01:00
consensus decisions, maintaining the local blockchain database,
broadcasting locally generated transactions into the network, and
providing a queryable RPC interface to interact on a polled basis for
requesting blockchain related data. However, there exists only a
limited service to notify external software of events like the arrival
of new blocks or transactions.
2015-09-29 19:48:45 +02:00
The ZeroMQ facility implements a notification interface through a set
of specific notifiers. Currently there are notifiers that publish
2014-11-18 18:06:32 +01:00
blocks and transactions. This read-only facility requires only the
2015-09-29 19:48:45 +02:00
connection of a corresponding ZeroMQ subscriber port in receiving
2014-11-18 18:06:32 +01:00
software; it is not authenticated nor is there any two-way protocol
involvement. Therefore, subscribers should validate the received data
since it may be out of date, incomplete or even invalid.
2015-09-29 19:48:45 +02:00
ZeroMQ sockets are self-connecting and self-healing; that is,
connections made between two endpoints will be automatically restored
after an outage, and either end may be freely started or stopped in
any order.
2014-11-18 18:06:32 +01:00
Because ZeroMQ is message oriented, subscribers receive transactions
and blocks all-at-once and do not need to implement any sort of
buffering or reassembly.
## Prerequisites
Merge #13578: [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (#4397)
f1bd03eb013b96ff040a8f835e4137fbd2a38cda [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (mruddy)
Pull request description:
Upgrade the ZeroMQ dependency from version 4.2.3 to the latest stable version 4.2.5.
This PR Follows the lead of https://github.com/bitcoin/bitcoin/pull/11986.
I upgraded both patch files to correspond to the version `4.2.5` libzmq files.
I assume doing so is still necessary and correct.
Without updating the patch line numbers, things appear to work, but you get extra log messages while building `depends` because things don't exactly match, e.g.:
```
/bitcoin/depends> make zeromq
Extracting zeromq...
/bitcoin/depends/sources/zeromq-4.2.5.tar.gz: OK
Preprocessing zeromq...
patching file src/windows.hpp
Hunk #1 succeeded at 58 (offset 3 lines).
patching file src/thread.cpp
Hunk #1 succeeded at 307 with fuzz 2 (offset 87 lines).
Hunk #2 succeeded at 323 with fuzz 2 (offset 90 lines).
```
Updating the patches seemed cleaner, so I did it. Note that libzmq had some whitespace changes, so that's why the updated patches do too.
More info: https://github.com/zeromq/libzmq/releases/tag/v4.2.5
tags: libzmq, zmq, 0mq
Tree-SHA512: 78659dd276b5311e40634b1bbebb802ddd6b69662ba3c84995ef1e3795c49a78b1635112c7fd72a405ea36e2cc3bdeb84e6d00d4e491a349bba1dafff50e2fa5
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
2021-09-08 18:25:20 +02:00
The ZeroMQ feature in Dash Core requires the ZeroMQ API >= 4.0.0
[libzmq ](https://github.com/zeromq/libzmq/releases ).
For version information, see [dependencies.md ](dependencies.md ).
Typically, it is packaged by distributions as something like
2015-09-29 19:48:45 +02:00
*libzmq3-dev*. The C++ wrapper for ZeroMQ is *not* needed.
2014-11-18 18:06:32 +01:00
2020-09-11 07:12:43 +02:00
In order to run the example Python client scripts in the `contrib/zmq/`
directory, one must also install [PyZMQ ](https://github.com/zeromq/pyzmq )
(generally with `pip install pyzmq` ), though this is not necessary for daemon
2015-09-29 19:48:45 +02:00
operation.
2014-11-18 18:06:32 +01:00
## Enabling
2015-10-06 05:09:04 +02:00
By default, the ZeroMQ feature is automatically compiled in if the
necessary prerequisites are found. To disable, use --disable-zmq
2020-06-11 10:39:04 +02:00
during the *configure* step of building dashd:
2014-11-18 18:06:32 +01:00
2015-10-06 05:09:04 +02:00
$ ./configure --disable-zmq (other options)
2014-11-18 18:06:32 +01:00
2015-10-06 05:09:04 +02:00
To actually enable operation, one must set the appropriate options on
2017-02-16 15:05:46 +01:00
the command line or in the configuration file.
2014-11-18 18:06:32 +01:00
## Usage
Currently, the following notifications are supported:
2019-05-08 11:12:54 +02:00
-zmqpubhashblock=address
-zmqpubhashchainlock=address
2014-11-18 18:06:32 +01:00
-zmqpubhashtx=address
2016-07-15 08:38:33 +02:00
-zmqpubhashtxlock=address
2019-05-08 11:12:54 +02:00
-zmqpubhashgovernancevote=address
-zmqpubhashgovernanceobject=address
-zmqpubhashinstantsenddoublespend=address
2020-11-17 20:41:30 +01:00
-zmqpubhashrecoveredsig=address
2014-11-18 18:06:32 +01:00
-zmqpubrawblock=address
2019-05-08 11:12:54 +02:00
-zmqpubrawchainlock=address
2019-05-23 11:13:58 +02:00
-zmqpubrawchainlocksig=address
2014-11-18 18:06:32 +01:00
-zmqpubrawtx=address
2016-07-15 08:38:33 +02:00
-zmqpubrawtxlock=address
2019-05-23 11:13:58 +02:00
-zmqpubrawtxlocksig=address
2018-07-12 11:06:30 +02:00
-zmqpubrawgovernancevote=address
2019-05-08 11:12:54 +02:00
-zmqpubrawgovernanceobject=address
2018-09-12 13:12:44 +02:00
-zmqpubrawinstantsenddoublespend=address
2020-11-17 20:41:30 +01:00
-zmqpubrawrecoveredsig=address
2024-08-26 17:35:12 +02:00
-zmqpubsequence=address
2014-11-18 18:06:32 +01:00
2015-09-29 19:48:45 +02:00
The socket type is PUB and the address must be a valid ZeroMQ socket
address. The same address can be used in more than one notification.
2020-10-01 17:41:22 +02:00
The same notification can be specified more than once.
2014-11-18 18:06:32 +01:00
2021-09-08 18:39:06 +02:00
The option to set the PUB socket's outbound message high water mark
(SNDHWM) may be set individually for each notification:
-zmqpubhashtxhwm=n
-zmqpubhashblockhwm=n
-zmqpubhashchainlockhwm=n
-zmqpubhashtxlockhwm=n
-zmqpubhashgovernancevotehwm=n
-zmqpubhashgovernanceobjecthwm=n
-zmqpubhashinstantsenddoublespendhwm=n
-zmqpubhashrecoveredsighwm=n
-zmqpubrawblockhwm=n
-zmqpubrawtxhwm=n
-zmqpubrawchainlockhwm=n
-zmqpubrawchainlocksighwm=n
-zmqpubrawtxlockhwm=n
-zmqpubrawtxlocksighwm=n
-zmqpubrawgovernancevotehwm=n
-zmqpubrawgovernanceobjecthwm=n
-zmqpubrawinstantsenddoublespendhwm=n
-zmqpubrawrecoveredsighwm=n
2024-08-26 17:35:12 +02:00
-zmqpubsequencehwm=address
2021-09-08 18:39:06 +02:00
The high water mark value must be an integer greater than or equal to 0.
2014-11-18 18:06:32 +01:00
For instance:
2016-03-06 16:26:01 +01:00
$ dashd -zmqpubhashtx=tcp://127.0.0.1:28332 \
2020-10-01 17:41:22 +02:00
-zmqpubhashtx=tcp://192.168.1.2:28332 \
2021-05-26 19:18:58 +02:00
-zmqpubhashblock="tcp://[::1]:28333" \
2021-09-08 18:39:06 +02:00
-zmqpubrawtx=ipc:///tmp/dashd.tx.raw \
-zmqpubhashtxhwm=10000
2014-11-18 18:06:32 +01:00
Each PUB notification has a topic and body, where the header
2015-09-29 19:48:45 +02:00
corresponds to the notification type. For instance, for the
notification `-zmqpubhashtx` the topic is `hashtx` (no null
2021-11-09 04:53:29 +01:00
terminator). These options can also be provided in dash.conf.
The topics are:
`sequence` : the body is structured as the following based on the type of message:
2024-08-26 17:35:12 +02:00
< 32-byte hash > C : Blockhash connected
< 32-byte hash > D : Blockhash disconnected
< 32-byte hash > R< 8-byte LE uint > : Transactionhash removed from mempool for non-block inclusion reason
< 32-byte hash > A< 8-byte LE uint > : Transactionhash added mempool
Where the 8-byte uints correspond to the mempool sequence number.
2014-11-18 18:06:32 +01:00
2021-11-09 04:53:29 +01:00
`rawtx` : Notifies about all transactions, both when they are added to mempool or when a new block arrives. This means a transaction could be published multiple times. First, when it enters the mempool and then again in each block that includes it. The messages are ZMQ multipart messages with three parts. The first part is the topic (`rawtx`), the second part is the serialized transaction, and the last part is a sequence number (representing the message count to detect lost messages).
| rawtx | < serialized transaction > | < uint32 sequence number in Little Endian >
`hashtx` : Notifies about all transactions, both when they are added to mempool or when a new block arrives. This means a transaction could be published multiple times. First, when it enters the mempool and then again in each block that includes it. The messages are ZMQ multipart messages with three parts. The first part is the topic (`hashtx`), the second part is the 32-byte transaction hash, and the last part is a sequence number (representing the message count to detect lost messages).
| hashtx | < 32-byte transaction hash in Little Endian > | < uint32 sequence number in Little Endian >
`rawblock` : Notifies when the chain tip is updated. Messages are ZMQ multipart messages with three parts. The first part is the topic (`rawblock`), the second part is the serialized block, and the last part is a sequence number (representing the message count to detect lost messages).
| rawblock | < serialized block > | < uint32 sequence number in Little Endian >
`hashblock` : Notifies when the chain tip is updated. Messages are ZMQ multipart messages with three parts. The first part is the topic (`hashblock`), the second part is the 32-byte block hash, and the last part is a sequence number (representing the message count to detect lost messages).
| hashblock | < 32-byte block hash in Little Endian > | < uint32 sequence number in Little Endian >
**_NOTE:_** Note that the 32-byte hashes are in Little Endian and not in the Big Endian format that the RPC interface and block explorers use to display transaction and block hashes.
2014-11-18 18:06:32 +01:00
ZeroMQ endpoint specifiers for TCP (and others) are documented in the
2015-10-12 22:39:47 +02:00
[ZeroMQ API ](http://api.zeromq.org/4-0:_start ).
2014-11-18 18:06:32 +01:00
Client side, then, the ZeroMQ subscriber socket must have the
2015-09-29 19:48:45 +02:00
ZMQ_SUBSCRIBE option set to one or either of these prefixes (for
instance, just `hash` ); without doing so will result in no messages
2020-05-12 10:22:06 +02:00
arriving. Please see [`contrib/zmq/zmq_sub.py` ](/contrib/zmq/zmq_sub.py ) for a working example.
2014-11-18 18:06:32 +01:00
Merge #14687: zmq: enable tcp keepalive
c276df775914e4e42993c76e172ef159e3b830d4 zmq: enable tcp keepalive (mruddy)
Pull request description:
This addresses https://github.com/bitcoin/bitcoin/issues/12754.
These changes enable node operators to address the silent dropping (by network middle boxes) of long-lived low-activity ZMQ TCP connections via further operating system level TCP keepalive configuration. For example, ZMQ sockets that publish block hashes can be affected in this way due to the length of time it sometimes takes between finding blocks (e.g.- sometimes more than an hour).
Prior to this patch, operating system level TCP keepalive configurations would not take effect since the SO_KEEPALIVE option was not enabled on the underlying socket.
There are additional ZMQ socket options related to TCP keepalive that can be set. However, I decided not to implement those options in this changeset because doing so would require adding additional bitcoin node configuration options, and would not yield a better outcome. I preferred a small, easily reviewable patch that doesn't add a bunch of new config options, with the tradeoff that the fine tuning would have to be done via well-documented operating system specific configurations.
I tested this patch by running a node with:
`./src/qt/bitcoin-qt -regtest -txindex -datadir=/tmp/node -zmqpubhashblock=tcp://127.0.0.1:28332 &`
and connecting to it with:
`python3 ./contrib/zmq/zmq_sub.py`
Without these changes, `ss -panto | grep 28332 | grep ESTAB | grep bitcoin` will report no keepalive timer information. With these changes, the output from the prior command will show keepalive timer information consistent with the configuration at the time of connection establishment, e.g.-: `timer:(keepalive,119min,0)`.
I also tested with a non-TCP transport and did not witness any adverse effects:
`./src/qt/bitcoin-qt -regtest -txindex -datadir=/tmp/node -zmqpubhashblock=ipc:///tmp/bitcoin.block &`
ACKs for top commit:
adamjonas:
Just to summarize for those looking to review - as of c276df775914e4e42993c76e172ef159e3b830d4 there are 3 tACKs (n-thumann, Haaroon, and dlogemann), 1 "looks good to me" (laanwj) with no NACKs or any show-stopping concerns raised.
jonasschnelli:
utACK c276df775914e4e42993c76e172ef159e3b830d4
Tree-SHA512: b884c2c9814e97e666546a7188c48f9de9541499a11a934bd48dd16169a900c900fa519feb3b1cb7e9915fc7539aac2829c7806b5937b4e1409b4805f3ef6cd1
2020-09-02 09:09:08 +02:00
The ZMQ_PUB socket's ZMQ_TCP_KEEPALIVE option is enabled. This means that
the underlying SO_KEEPALIVE option is enabled when using a TCP transport.
The effective TCP keepalive values are managed through the underlying
operating system configuration and must be configured prior to connection establishment.
For example, when running on GNU/Linux, one might use the following
to lower the keepalive setting to 10 minutes:
sudo sysctl -w net.ipv4.tcp_keepalive_time=600
Setting the keepalive values appropriately for your operating environment may
improve connectivity in situations where long-lived connections are silently
dropped by network middle boxes.
2021-05-26 19:18:58 +02:00
Also, the socket's ZMQ_IPV6 option is enabled to accept connections from IPv6
hosts as well. If needed, this option has to be set on the client side too.
2014-11-18 18:06:32 +01:00
## Remarks
2016-03-06 16:26:01 +01:00
From the perspective of dashd, the ZeroMQ socket is write-only; PUB
2014-11-18 18:06:32 +01:00
sockets don't even have a read function. Thus, there is no state
2016-03-06 16:26:01 +01:00
introduced into dashd directly. Furthermore, no information is
2014-11-18 18:06:32 +01:00
broadcast that wasn't already received from the public P2P network.
No authentication or authorization is done on connecting clients; it
is assumed that the ZeroMQ port is exposed only to trusted entities,
using other means such as firewalling.
2024-08-26 17:35:12 +02:00
Note that for `*block` topics, when the block chain tip changes,
a reorganisation may occur and just the tip will be notified.
It is up to the subscriber to retrieve the chain from the last known
block to the new tip. Also note that no notification will occur if the tip
was in the active chain--as would be the case after calling invalidateblock RPC.
In contrast, the `sequence` topic publishes all block connections and
disconnections.
2016-10-17 22:09:21 +02:00
There are several possibilities that ZMQ notification can get lost
2018-03-03 15:24:28 +01:00
during transmission depending on the communication type you are
2016-10-17 22:09:21 +02:00
using. Dashd appends an up-counting sequence number to each
notification which allows listeners to detect lost notifications.
2024-08-26 17:35:12 +02:00
The `sequence` topic refers specifically to the mempool sequence
number, which is also published along with all mempool events. This
is a different sequence value than in ZMQ itself in order to allow a total
ordering of mempool events to be constructed.