mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
516cfb54c5
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
163 lines
6.4 KiB
Markdown
163 lines
6.4 KiB
Markdown
# Block and Transaction Broadcasting with ZeroMQ
|
|
|
|
[ZeroMQ](https://zeromq.org/) is a lightweight wrapper around TCP
|
|
connections, inter-process communication, and shared-memory,
|
|
providing various message-oriented semantics such as publish/subscribe,
|
|
request/reply, and push/pull.
|
|
|
|
The Dash Core daemon can be configured to act as a trusted "border
|
|
router", implementing the dash wire protocol and relay, making
|
|
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.
|
|
|
|
The ZeroMQ facility implements a notification interface through a set
|
|
of specific notifiers. Currently there are notifiers that publish
|
|
blocks and transactions. This read-only facility requires only the
|
|
connection of a corresponding ZeroMQ subscriber port in receiving
|
|
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.
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
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
|
|
*libzmq3-dev*. The C++ wrapper for ZeroMQ is *not* needed.
|
|
|
|
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
|
|
operation.
|
|
|
|
## Enabling
|
|
|
|
By default, the ZeroMQ feature is automatically compiled in if the
|
|
necessary prerequisites are found. To disable, use --disable-zmq
|
|
during the *configure* step of building dashd:
|
|
|
|
$ ./configure --disable-zmq (other options)
|
|
|
|
To actually enable operation, one must set the appropriate options on
|
|
the command line or in the configuration file.
|
|
|
|
## Usage
|
|
|
|
Currently, the following notifications are supported:
|
|
|
|
-zmqpubhashblock=address
|
|
-zmqpubhashchainlock=address
|
|
-zmqpubhashtx=address
|
|
-zmqpubhashtxlock=address
|
|
-zmqpubhashgovernancevote=address
|
|
-zmqpubhashgovernanceobject=address
|
|
-zmqpubhashinstantsenddoublespend=address
|
|
-zmqpubhashrecoveredsig=address
|
|
-zmqpubrawblock=address
|
|
-zmqpubrawchainlock=address
|
|
-zmqpubrawchainlocksig=address
|
|
-zmqpubrawtx=address
|
|
-zmqpubrawtxlock=address
|
|
-zmqpubrawtxlocksig=address
|
|
-zmqpubrawgovernancevote=address
|
|
-zmqpubrawgovernanceobject=address
|
|
-zmqpubrawinstantsenddoublespend=address
|
|
-zmqpubrawrecoveredsig=address
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
The high water mark value must be an integer greater than or equal to 0.
|
|
|
|
For instance:
|
|
|
|
$ dashd -zmqpubhashtx=tcp://127.0.0.1:28332 \
|
|
-zmqpubrawtx=ipc:///tmp/dashd.tx.raw \
|
|
-zmqpubhashtxhwm=10000
|
|
|
|
Each PUB notification has a topic and body, where the header
|
|
corresponds to the notification type. For instance, for the
|
|
notification `-zmqpubhashtx` the topic is `hashtx` (no null
|
|
terminator) and the body is the transaction hash (32
|
|
bytes).
|
|
|
|
These options can also be provided in dash.conf.
|
|
|
|
ZeroMQ endpoint specifiers for TCP (and others) are documented in the
|
|
[ZeroMQ API](http://api.zeromq.org/4-0:_start).
|
|
|
|
Client side, then, the ZeroMQ subscriber socket must have the
|
|
ZMQ_SUBSCRIBE option set to one or either of these prefixes (for
|
|
instance, just `hash`); without doing so will result in no messages
|
|
arriving. Please see [`contrib/zmq/zmq_sub.py`](/contrib/zmq/zmq_sub.py) for a working example.
|
|
|
|
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.
|
|
|
|
## Remarks
|
|
|
|
From the perspective of dashd, the ZeroMQ socket is write-only; PUB
|
|
sockets don't even have a read function. Thus, there is no state
|
|
introduced into dashd directly. Furthermore, no information is
|
|
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.
|
|
|
|
Note that 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.
|
|
|
|
There are several possibilities that ZMQ notification can get lost
|
|
during transmission depending on the communication type you are
|
|
using. Dashd appends an up-counting sequence number to each
|
|
notification which allows listeners to detect lost notifications.
|