This changes the ZMQ code to use the C++ language bindings instead of the plain C interface. This allows us to make use of RAII for initialisation and destruction, making the code easier to read and less error-prone.
See #13957.
<!--e57a25ab6845829454e8d69fc972939a-->Note to reviewers: This pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
108 | @@ -119,17 +109,11 @@ bool CZMQNotificationInterface::Initialize() 109 | void CZMQNotificationInterface::Shutdown() 110 | { 111 | LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n"); 112 | - if (pcontext) 113 | + for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
Nit: Make this and line 86 match
120 | - } 121 | - zmq_ctx_destroy(pcontext); 122 | - 123 | - pcontext = nullptr; 124 | + CZMQAbstractNotifier *notifier = *i; 125 | + LogPrint(BCLog::ZMQ, " Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
Nit zmq: is missing as a prefix here. Or, it could be removed everywhere besides zmqError.
I didn't change the existing code here (see my previous comment about the intent of this PR). I think we can make it consistent nevertheless, but I'd rather do this in a separate commit/PR (perhaps as part of #13686). Also, from a quick grep it seems that the styles with and without prefix are pretty 50/50 at the moment.
160 | @@ -177,7 +161,7 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex) 161 | CBlock block; 162 | if(!ReadBlockFromDisk(block, pindex, consensusParams)) 163 | { 164 | - zmqError("Can't read block from disk"); 165 | + LogPrint(BCLog::ZMQ, "zmq: Can't read block from disk\n");
(Related Nit: zmq: is inconsistent)
See my other comment.
33 | @@ -34,8 +34,8 @@ buffering or reassembly. 34 | ## Prerequisites 35 | 36 | The ZeroMQ feature in Bitcoin Core requires ZeroMQ API version 4.x or 37 | -newer. Typically, it is packaged by distributions as something like 38 | -*libzmq3-dev*. The C++ wrapper for ZeroMQ is *not* needed. 39 | +newer and the C++ langauge bindings. Typically, it is packaged by
Nit: language
What do you think is wrong? Feel free to suggest an alternative wording (I'm not a native speaker).
There is a typo in language, look carefully.
Ah, my bad - fixed! (I interpreted that "language" to mean that something is wrong/bad related to grammar or style.)
utACK
Thanks for the review!
It seems the build fails on Windows in CI due to missing the ZMQ C++ language bindings. I don't know anything about either the CI system or Windows; so not sure if this just means we have to reconfigure it somehow or if that indicates that we need another dependency that is potentially unwanted.
This changes the ZMQ code to use the C++ language bindings instead of the
plain C interface. This allows us to make use of RAII for initialisation
and destruction, making the code easier to read and less error-prone.
See https://github.com/bitcoin/bitcoin/issues/13957.
I can't see zmq.hpp here: https://github.com/zeromq/libzmq/tree/master/include
Does this require extra dependencies?
@ken2812221: This is the C++ language binding for ZMQ. It is included in the same Debian package as the core library, libzmq3-dev. I don't know where in the sources it is, though.
~0 on this if this doesn't require extra dependencies on any platform, it would be slightly preferable to use the C++ bindings from C++, but if it does require extra packages on any distro, I don't think it's worth it
This seems to be at https://github.com/zeromq/cppzmq/blob/master/zmq.hpp, so it requires an extra dependency. (For depends build)
Ok, closing in that case.