Use C++ language binding for ZMQ #13957

issue domob1812 opened this issue on August 13, 2018
  1. domob1812 commented at 4:07 PM on August 13, 2018: contributor

    ZMQ has a native language binding for C++, which among other things takes advantage of RAII for the context, sockets and messages.

    Is there any particular reason why Bitcoin Core's ZMQ interface does not use the C++ language bindings? That would most likely simplify the code a lot and make it more robust against subtle bugs.

    I'm aware that the doc/zmq.md file explicitly says that "the C++ language bindings are not necessary" - however, at least on my Debian system, it seems that the default ZMQ package libzmq3-dev already includes the C++ language bindings; so it is not really a separate dependency.

  2. fanquake added the label RPC/REST/ZMQ on Aug 13, 2018
  3. practicalswift commented at 4:23 PM on August 15, 2018: contributor

    I believe @jmcorgan did the initial ZeroMQ work back in 2014. He might be able to give the history to this :-)

  4. jmcorgan commented at 3:08 PM on August 17, 2018: contributor

    At the time, the C++ wrapper (which is a single, inline C++ header file) was sometimes included in the C package, sometimes not (on different distributions), and the least effort imposed on the build was to do it in C and not worry about it. I don't know if this is still true.

    Can you elaborate on the simplifications and bug resilience you're referring to? I've used both the native C API and the C++ wrapper, and there's not much difference.

  5. domob1812 commented at 4:29 PM on August 18, 2018: contributor

    @jmcorgan: The main point where I think the C++ wrapper improves readability and robustness is through type-safety and RAII. Instead of passing around void* for the ZMQ context and socket, you have a typed object (e.g. zmq::socket_t). And instead of manually calling an initialiser and a destructor for it, this is done by the language - including a guarantee that the destructor is called, which might be forgotten when using the pure C API (especially with things like multiple returns in a function or exceptions).

    If there is no longer a big reason not to depend on the C++ wrapper, then I can simply prepare a PR that changes to it and then we can judge based on that how much it simplifies the code (or not).

  6. promag commented at 5:52 PM on August 18, 2018: member

    A PR would be great to test builds.

  7. practicalswift commented at 6:11 PM on August 18, 2018: contributor

    @domob1812 Sounds great!

  8. domob1812 referenced this in commit 4216ed36a0 on Aug 21, 2018
  9. domob1812 commented at 6:59 PM on August 21, 2018: contributor

    Demo PR is in #14017.

  10. domob1812 referenced this in commit 5b263399a0 on Aug 22, 2018
  11. promag commented at 12:29 AM on August 28, 2018: member

    Can close?

  12. domob1812 commented at 10:37 AM on August 28, 2018: contributor

    Yes, I think we can close this since the C++ bindings seem to require separate dependencies on non-Debian systems (which I can't test).

  13. domob1812 closed this on Aug 28, 2018

  14. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me