]>
Commit | Line | Data |
---|---|---|
f200002c JG |
1 | # Block and Transaction Broadcasting With ZeroMQ |
2 | ||
3 | [ZeroMQ](http://zeromq.org/) is a lightweight wrapper around TCP | |
9540b0c6 | 4 | connections, inter-process communication, and shared-memory, |
f200002c JG |
5 | providing various message-oriented semantics such as publish/subcribe, |
6 | request/reply, and push/pull. | |
7 | ||
4fbc46c2 JG |
8 | The Zcash daemon can be configured to act as a trusted "border |
9 | router", implementing the zcash wire protocol and relay, making | |
f200002c JG |
10 | consensus decisions, maintaining the local blockchain database, |
11 | broadcasting locally generated transactions into the network, and | |
12 | providing a queryable RPC interface to interact on a polled basis for | |
13 | requesting blockchain related data. However, there exists only a | |
14 | limited service to notify external software of events like the arrival | |
15 | of new blocks or transactions. | |
16 | ||
9540b0c6 JC |
17 | The ZeroMQ facility implements a notification interface through a set |
18 | of specific notifiers. Currently there are notifiers that publish | |
f200002c | 19 | blocks and transactions. This read-only facility requires only the |
9540b0c6 | 20 | connection of a corresponding ZeroMQ subscriber port in receiving |
f200002c JG |
21 | software; it is not authenticated nor is there any two-way protocol |
22 | involvement. Therefore, subscribers should validate the received data | |
23 | since it may be out of date, incomplete or even invalid. | |
24 | ||
9540b0c6 JC |
25 | ZeroMQ sockets are self-connecting and self-healing; that is, |
26 | connections made between two endpoints will be automatically restored | |
27 | after an outage, and either end may be freely started or stopped in | |
28 | any order. | |
f200002c JG |
29 | |
30 | Because ZeroMQ is message oriented, subscribers receive transactions | |
31 | and blocks all-at-once and do not need to implement any sort of | |
32 | buffering or reassembly. | |
33 | ||
34 | ## Prerequisites | |
35 | ||
4fbc46c2 | 36 | The ZeroMQ feature in Zcash requires ZeroMQ API version 4.x or |
7c8845ed JG |
37 | newer, which you will need to install if you are not using the depends |
38 | system. Typically, it is packaged by distributions as something like | |
edcec148 | 39 | *libzmq5-dev*. The C++ wrapper for ZeroMQ is *not* needed. |
f200002c | 40 | |
9540b0c6 JC |
41 | In order to run the example Python client scripts in contrib/ one must |
42 | also install *python-zmq*, though this is not necessary for daemon | |
43 | operation. | |
f200002c JG |
44 | |
45 | ## Enabling | |
46 | ||
d5f4dc15 JC |
47 | By default, the ZeroMQ feature is automatically compiled in if the |
48 | necessary prerequisites are found. To disable, use --disable-zmq | |
4fbc46c2 | 49 | during the *configure* step of building zcashd: |
f200002c | 50 | |
d5f4dc15 | 51 | $ ./configure --disable-zmq (other options) |
f200002c | 52 | |
d5f4dc15 JC |
53 | To actually enable operation, one must set the appropriate options on |
54 | the commandline or in the configuration file. | |
f200002c JG |
55 | |
56 | ## Usage | |
57 | ||
58 | Currently, the following notifications are supported: | |
59 | ||
60 | -zmqpubhashtx=address | |
61 | -zmqpubhashblock=address | |
62 | -zmqpubrawblock=address | |
63 | -zmqpubrawtx=address | |
64 | ||
9540b0c6 JC |
65 | The socket type is PUB and the address must be a valid ZeroMQ socket |
66 | address. The same address can be used in more than one notification. | |
f200002c JG |
67 | |
68 | For instance: | |
69 | ||
4fbc46c2 JG |
70 | $ zcashd -zmqpubhashtx=tcp://127.0.0.1:28332 \ |
71 | -zmqpubrawtx=ipc:///tmp/zcashd.tx.raw | |
f200002c JG |
72 | |
73 | Each PUB notification has a topic and body, where the header | |
9540b0c6 JC |
74 | corresponds to the notification type. For instance, for the |
75 | notification `-zmqpubhashtx` the topic is `hashtx` (no null | |
76 | terminator) and the body is the hexadecimal transaction hash (32 | |
77 | bytes). | |
f200002c | 78 | |
4fbc46c2 | 79 | These options can also be provided in zcash.conf. |
f200002c JG |
80 | |
81 | ZeroMQ endpoint specifiers for TCP (and others) are documented in the | |
678b614a | 82 | [ZeroMQ API](http://api.zeromq.org/4-0:_start). |
f200002c JG |
83 | |
84 | Client side, then, the ZeroMQ subscriber socket must have the | |
9540b0c6 JC |
85 | ZMQ_SUBSCRIBE option set to one or either of these prefixes (for |
86 | instance, just `hash`); without doing so will result in no messages | |
87 | arriving. Please see `contrib/zmq/zmq_sub.py` for a working example. | |
f200002c JG |
88 | |
89 | ## Remarks | |
90 | ||
4fbc46c2 | 91 | From the perspective of zcashd, the ZeroMQ socket is write-only; PUB |
f200002c | 92 | sockets don't even have a read function. Thus, there is no state |
4fbc46c2 | 93 | introduced into zcashd directly. Furthermore, no information is |
f200002c JG |
94 | broadcast that wasn't already received from the public P2P network. |
95 | ||
96 | No authentication or authorization is done on connecting clients; it | |
97 | is assumed that the ZeroMQ port is exposed only to trusted entities, | |
98 | using other means such as firewalling. | |
99 | ||
9540b0c6 JC |
100 | Note that when the block chain tip changes, a reorganisation may occur |
101 | and just the tip will be notified. It is up to the subscriber to | |
102 | retrieve the chain from the last known block to the new tip. | |
3ba2e19e JS |
103 | |
104 | There are several possibilities that ZMQ notification can get lost | |
105 | during transmission depending on the communication type your are | |
4fbc46c2 | 106 | using. Zcashd appends an up-counting sequence number to each |
3ba2e19e | 107 | notification which allows listeners to detect lost notifications. |