| 1 | # The Current/Future Status of ZMQ in Monero |
| 2 | |
| 3 | ## ZMQ Pub/Sub |
| 4 | Client `ZMQ_SUB` sockets must "subscribe" to topics before it receives any data. |
| 5 | This allows filtering on the server side, so network traffic is reduced. Monero |
| 6 | allows for filtering on: (1) format, (2) context, and (3) event. |
| 7 | |
| 8 | * **format** refers to the _wire_ format (i.e. JSON) used to send event |
| 9 | information. |
| 10 | * **context** allows for a reduction in fields for the event, so the |
| 11 | daemon doesn't waste cycles serializing fields that get ignored. |
| 12 | * **event** refers to status changes occurring within the daemon (i.e. new |
| 13 | block to main chain). |
| 14 | |
| 15 | * Formats: |
| 16 | * `json` |
| 17 | * Contexts: |
| 18 | * `full` - the entire block or transaction is transmitted (the hash can be |
| 19 | computed remotely). |
| 20 | * `minimal` - the bare minimum for a remote client to react to an event is |
| 21 | sent. |
| 22 | * Events: |
| 23 | * `chain_main` - changes to the primary/main blockchain. |
| 24 | * `txpool_add` - new _publicly visible_ transactions in the mempool. |
| 25 | Includes previously unseen transactions in a block but _not_ the |
| 26 | `miner_tx`. Does not "re-publish" after a reorg. Includes `do_not_relay` |
| 27 | transactions. |
| 28 | * `miner_data` - provides the necessary data to create a custom block template |
| 29 | Available only in the `full` context. |
| 30 | |
| 31 | The subscription topics are formatted as `format-context-event`, with prefix |
| 32 | matching supported by both Monero and ZMQ. The `format`, `context` and `event` |
| 33 | will _never_ have hyphens or colons in their name. For example, subscribing to |
| 34 | `json-minimal-chain_main` will send minimal information in JSON when changes |
| 35 | to the main/primary blockchain occur. Whereas, subscribing to `json-minimal` |
| 36 | will send minimal information in JSON on all available events supported by the |
| 37 | daemon. |
| 38 | |
| 39 | The Monero daemon will ensure that events prefixed by `chain` will be sent in |
| 40 | "chain-order" - the `prev_id` (hash) field will _always_ refer to a previous |
| 41 | block. On rollbacks/reorgs, the event will reference an earlier block in the |
| 42 | chain instead of the last block. The Monero daemon also ensures that |
| 43 | `txpool_add` events are sent before `chain_*` events - the `chain_*` messages |
| 44 | will only serialize miner transactions since the other transactions were |
| 45 | previously published via `txpool_add`. This prevents transactions from being |
| 46 | serialized twice, even when the transaction was first observed in a block. |
| 47 | |
| 48 | ZMQ Pub/Sub will drop messages if the network is congested, so the above rules |
| 49 | for send order are used for detecting lost messages. A missing gap in `height` |
| 50 | or `prev_id` for `chain_*` events indicates a lost pub message. Missing |
| 51 | `txpool_add` messages can only be detected at the next `chain_` message. |
| 52 | |
| 53 | Since blockchain events can be dropped, clients will likely want to have a |
| 54 | timeout against `chain_main` events. The `GetLastBlockHeader` RPC is useful |
| 55 | for checking the current chain state. Dropped messages should be rare in most |
| 56 | conditions. |
| 57 | |
| 58 | The Monero daemon will send a `txpool_add` pub exactly once for each |
| 59 | transaction, even after a reorg or restarts. Clients should use the |
| 60 | `GetTransactionPool` after a reorg to get all transactions that have been put |
| 61 | back into the tx pool or been invalidated due to a double-spend. |
| 62 | |
| 63 |