ZMQ: Small cleanups in the ZMQ code #13686

pull domob1812 wants to merge 5 commits into bitcoin:master from domob1812:zmq-cleanup changing 10 files +103 −109
  1. domob1812 commented at 11:19 am on July 17, 2018: contributor
    This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion). The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the notifiers list after its callback function returned false (which is likely not relevant in practice but still a bug).
  2. domob1812 commented at 11:20 am on July 17, 2018: contributor
    I split the patch into three commits which I think make sense logically and for the review. But of course I’m happy to squash any of the changes, or to revert parts of them (as code readability is a matter of taste).
  3. fanquake added the label RPC/REST/ZMQ on Jul 17, 2018
  4. promag commented at 3:56 pm on July 24, 2018: member

    Concept ACK.

    From a quick view LGTM.

  5. DrahtBot commented at 12:31 pm on July 27, 2018: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #19572 (ZMQ: Create “sequence” notifier, enabling client-side mempool tracking by instagibbs)
    • #18309 (zmq: Add support to listen on multiple interfaces by n-thumann)

    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.

  6. DrahtBot added the label Needs rebase on Aug 8, 2018
  7. domob1812 force-pushed on Aug 8, 2018
  8. domob1812 commented at 4:16 pm on August 8, 2018: contributor
    Rebased
  9. DrahtBot removed the label Needs rebase on Aug 8, 2018
  10. kostyantyn commented at 10:00 pm on August 20, 2018: contributor

    What do you think about wrapping the code with zmq namespace and removing ZMQ/zmq prefix from class/function names? Basically when the location of the file is a/b/c.cpp then we have:

    0namespace a {
    1namespace b {
    2class C {}
    3}
    4}
    
  11. domob1812 commented at 5:59 am on August 21, 2018: contributor

    @kostyantyn: At least for now, there seems to be not much namespace usage at all in the Bitcoin code - so I’m not sure if we should change that lightly. Also, note that zmq is the namespace that is also used by ZMQ’s own C++ language bindings; they are currently not used, but I proposed to change that (#13957).

    Also, note that C++ namespaces are different from Java’s, so that some people advise against nesting (e.g. exact correspondence to the directory structure as you suggest).

  12. laanwj commented at 11:38 am on August 31, 2018: member
    Concept ACK @jmcorgan do you mind taking a look here?
  13. in src/zmq/zmqabstractnotifier.h:12 in 55eca083fa outdated
     4@@ -5,12 +5,13 @@
     5 #ifndef BITCOIN_ZMQ_ZMQABSTRACTNOTIFIER_H
     6 #define BITCOIN_ZMQ_ZMQABSTRACTNOTIFIER_H
     7 
     8-#include <zmq/zmqconfig.h>
     9+#include <memory>
    10 
    


    ken2812221 commented at 7:02 am on September 14, 2018:
    Missing a bunch of includes here. See appveyor CI result.

    domob1812 commented at 10:04 am on November 6, 2018:
    I missed that comment - I’ve now revised the commit to include <string>. There are still CI errors, though, which I don’t understand so far. Has anyone an insight of where they come from? I’ll try to resolve them now.
  14. DrahtBot added the label Needs rebase on Nov 5, 2018
  15. domob1812 force-pushed on Nov 5, 2018
  16. domob1812 commented at 2:24 pm on November 5, 2018: contributor
    Rebased
  17. DrahtBot removed the label Needs rebase on Nov 5, 2018
  18. domob1812 force-pushed on Nov 5, 2018
  19. domob1812 force-pushed on Nov 6, 2018
  20. in src/zmq/zmqpublishnotifier.cpp:22 in 95c442d496 outdated
    19 
    20+#include <zmq.h>
    21+
    22+#include <cstdarg>
    23+#include <cstddef>
    24+
    


    ken2812221 commented at 9:23 am on November 9, 2018:

    I would suggest adding the following code for Windows.

    0#if defined(WIN32) && defined(SendMessage)
    1#undef SendMessage
    2#endif
    

    Windows headers have defined SendMessage as SendMessageA or SendMessageW

    Or you could rename the method name SendMessage.


    domob1812 commented at 8:08 pm on November 10, 2018:
    Thanks, very interesting! From a quick look, I didn’t see what my PR changes here and why it worked before - but I guess a defensive rename of the method is not a bad idea. I did that now in a separate commit and pushed it, just to see if it fixes the CI issues. If it does, I can include this directly in one of the commits or perhaps just keep it as it is.

    domob1812 commented at 5:55 pm on November 11, 2018:
    Indeed, looks like the tests pass now.
  21. in src/zmq/zmqnotificationinterface.cpp:9 in 5d66a3be2d outdated
    3@@ -4,79 +4,69 @@
    4 
    5 #include <zmq/zmqnotificationinterface.h>
    6 #include <zmq/zmqpublishnotifier.h>
    7+#include <zmq/zmqutil.h>
    8+
    9+#include <zmq.h>
    


    hebasto commented at 3:04 pm on December 10, 2018:

    Why is a guard dropped?

    0#ifdef ENABLE_ZMQ
    1#include <zmq.h>
    2#endif
    

    domob1812 commented at 8:34 am on October 17, 2019:
    I think I’ve replied on this before, but just in case and for the record: The guard that used to be here was redundant, as there is a guard already (e.g. in init.cpp) around zmqnotificationinterface itself.
  22. hebasto changes_requested
  23. hebasto commented at 3:05 pm on December 10, 2018: member
    Concept ACK.
  24. domob1812 commented at 9:28 am on December 11, 2018: contributor
    @hebasto: For some reason, Github won’t let me reply this directly to your comment above. The guard is not necessary, since it is present already where the src/zmq files themselves are used - e.g. in src/init.cpp.
  25. domob1812 force-pushed on May 2, 2019
  26. domob1812 commented at 8:03 am on May 2, 2019: contributor
    Rebased. Is there anything missing for me to do here? I think this is a real improvement to the quality of the ZMQ code.
  27. fanquake commented at 2:20 am on June 16, 2019: member
    @jmcorgan Are you able to take a look at this?
  28. DrahtBot added the label Needs rebase on Oct 16, 2019
  29. domob1812 force-pushed on Oct 17, 2019
  30. DrahtBot removed the label Needs rebase on Oct 17, 2019
  31. domob1812 force-pushed on Oct 17, 2019
  32. domob1812 commented at 8:35 am on October 17, 2019: contributor
    Rebased. Can I do anything to speed up review / merging of this one? It should not need review by the original author or a ZMQ expert, as this is mostly just “standard” refactoring from manual memory management to the use of proper RAII (e.g. std::unique_ptr).
  33. in src/zmq/zmqnotificationinterface.cpp:118 in f18bc54cab outdated
    114@@ -136,45 +115,44 @@ void CZMQNotificationInterface::Shutdown()
    115     }
    116 }
    117 
    118-void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
    119+namespace
    


    promag commented at 8:56 am on October 17, 2019:

    f18bc54cab43ef1de8502b8f84b5585ae68af4bc

    According to developer notes put { here.


    domob1812 commented at 10:42 am on October 17, 2019:
    Done.
  34. in src/zmq/zmqnotificationinterface.cpp:118 in f18bc54cab outdated
    129-            i++;
    130-        }
    131-        else
    132-        {
    133+template <typename Function>
    134+void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
    


    promag commented at 8:57 am on October 17, 2019:

    f18bc54cab43ef1de8502b8f84b5585ae68af4bc

    Could make this refactor in a separate commit?


    domob1812 commented at 11:03 am on October 17, 2019:
    Yes sure, if you prefer. Let’s finish the review of the code first, and then I will rework the commit history as desired afterwards (else we might mess up the ongoing review with force-pushes).

    hebasto commented at 8:57 am on August 27, 2020:

    69f92ffce9f0d11d9389610e702b99d9038f3fed

    Could make this refactor in a separate commit?

    Agree with @promag.


    instagibbs commented at 2:13 pm on September 15, 2020:
    Note for reviewers: I think this is resolved?
  35. in src/zmq/zmqabstractnotifier.h:28 in d10f274a6e outdated
    22@@ -21,9 +23,9 @@ class CZMQAbstractNotifier
    23     virtual ~CZMQAbstractNotifier();
    24 
    25     template <typename T>
    26-    static CZMQAbstractNotifier* Create()
    27+    static std::unique_ptr<CZMQAbstractNotifier> Create()
    28     {
    29-        return new T();
    30+        return std::unique_ptr<CZMQAbstractNotifier>(new T());
    


    promag commented at 9:00 am on October 17, 2019:

    d10f274a6e0291e752e25b2bd99c0472d22a420f

    MakeUnique?


    domob1812 commented at 10:42 am on October 17, 2019:
    Done.
  36. promag commented at 9:07 am on October 17, 2019: member
    Code review ACK ba8b67aee28623d7fdd9b2053bc1784518a4a8ca, with some comments.
  37. domob1812 commented at 10:43 am on October 17, 2019: contributor

    Thanks for the review! I’ve addressed your comments in follow-up commits (I do not want to force-push right now and perhaps mess up your ongoing review). I will add them as fixups to the proper commits later when you are done with the review.

    I can then also split out the creation of TryForEachAndRemoveFailed into a separate commit if you prefer that.

  38. DrahtBot added the label Needs rebase on Dec 31, 2019
  39. domob1812 force-pushed on Jan 1, 2020
  40. domob1812 commented at 8:02 am on January 1, 2020: contributor
    Rebased
  41. DrahtBot removed the label Needs rebase on Jan 1, 2020
  42. in src/zmq/zmqnotificationinterface.cpp:69 in 69f92ffce9 outdated
    72+            notificationInterface.reset();
    73         }
    74     }
    75 
    76-    return notificationInterface;
    77+    return notificationInterface.release();
    


    hebasto commented at 9:00 am on August 27, 2020:

    69f92ffce9f0d11d9389610e702b99d9038f3fed

    If this code is touched to make it easier to read and maintain, may I suggest a simpler approach:

    0    if (!notifiers.empty()) {
    1        auto notificationInterface = std::unique_ptr<CZMQNotificationInterface>(new CZMQNotificationInterface());
    2        notificationInterface->notifiers = std::move(notifiers);
    3
    4        if (notificationInterface->Initialize()) {
    5            return notificationInterface.release();
    6        }
    7    }
    8
    9    return nullptr;
    

    ?


    domob1812 commented at 7:06 am on September 1, 2020:
    Indeed, looks better. I’ve updated the code accordingly (except I changed the auto notificationInterface = ... line slightly into what I consider even clearer; but let me know if you prefer your suggestion).
  43. in src/zmq/zmqpublishnotifier.cpp:18 in 49e2cb7856 outdated
    18 #include <rpc/server.h>
    19 
    20+#include <zmq.h>
    21+
    22+#include <cstdarg>
    23+#include <cstddef>
    


    hebasto commented at 9:07 am on August 27, 2020:
    0#include <cstdarg>
    1#include <cstddef>
    2#include <map>
    3#include <string>
    4#include <utility>
    

    domob1812 commented at 6:59 am on September 1, 2020:
    Indeed, I’ve pushed this change together with the two nanonits below (in a single commit).
  44. in src/zmq/zmqpublishnotifier.cpp:7 in 49e2cb7856 outdated
    1@@ -2,14 +2,25 @@
    2 // Distributed under the MIT software license, see the accompanying
    3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
    4 
    5+#if defined(HAVE_CONFIG_H)
    6+#include <config/bitcoin-config.h>
    7+#endif
    


    hebasto commented at 9:09 am on August 27, 2020:
    Is this required here as no defines from bitcoin-config.h is used here?

    domob1812 commented at 7:01 am on September 1, 2020:
    I guess not, removed.
  45. in src/zmq/zmqutil.h:8 in 49e2cb7856 outdated
    0@@ -0,0 +1,10 @@
    1+// Copyright (c) 2014-2018 The Bitcoin Core developers
    2+// Distributed under the MIT software license, see the accompanying
    3+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
    4+
    5+#ifndef BITCOIN_ZMQ_ZMQUTIL_H
    6+#define BITCOIN_ZMQ_ZMQUTIL_H
    7+
    8+void zmqError(const char *str);
    


    hebasto commented at 9:11 am on August 27, 2020:

    49e2cb78563df27ff0a5d2ad985e074ee98afcce nanonit:

    0void zmqError(const char* str);
    
  46. in src/zmq/zmqutil.cpp:11 in 49e2cb7856 outdated
     6+
     7+#include <logging.h>
     8+
     9+#include <zmq.h>
    10+
    11+void zmqError(const char *str)
    


    hebasto commented at 9:12 am on August 27, 2020:

    49e2cb78563df27ff0a5d2ad985e074ee98afcce nanonit:

    0void zmqError(const char* str)
    
  47. hebasto changes_requested
  48. hebasto commented at 9:12 am on August 27, 2020: member
    Approach ACK 14b40f1b7ce7d0b457e163dbda4e81f5105db753
  49. domob1812 commented at 7:07 am on September 1, 2020: contributor
    Thanks for the review, @hebasto, very appreciated! I’ve addressed your suggestions now with fixup commits. I will now rebase this onto current master, and then go clean up the commit history (including splitting out the refactor as suggested by @promag and you).
  50. domob1812 force-pushed on Sep 1, 2020
  51. domob1812 force-pushed on Sep 1, 2020
  52. domob1812 force-pushed on Sep 1, 2020
  53. domob1812 force-pushed on Sep 1, 2020
  54. domob1812 force-pushed on Sep 1, 2020
  55. domob1812 commented at 8:06 am on September 1, 2020: contributor
    Rebase done, and I’ve now cleaned up the commit history as well (squashed in the “fixup” commits as appropriate). Also split out the TryForEachAndRemoveFailed refactor into its own commit.
  56. hebasto commented at 8:17 am on September 6, 2020: member
    The first commit 707c4803d5edd2d5ebd6febb33e08bb031630652 is not compiled for me. Make iterator type auto?
  57. domob1812 force-pushed on Sep 7, 2020
  58. Various cleanups in zmqnotificationinterface.
    This is a pure refactoring of zmqnotificationinterface to make the
    code easier to read and maintain.  It replaces explicit iterators
    with C++11 for-each loops where appropriate and uses std::unique_ptr
    to make memory ownership more explicit.
    e15b1cfc31
  59. Simplify and fix notifier removal on error.
    This factors out the common logic to run over all ZMQ notifiers, call a
    function on them, and remove them from the list if the function fails is
    extracted to a helper method.
    
    Note that this also fixes a potential memory leak:  When a notifier was
    removed previously after its callback returned false, it would just be
    removed from the list without destructing the object.  This is now done
    correctly by std::unique_ptr behind the scenes.
    b93b9d5456
  60. Use std::unique_ptr for CZMQNotifierFactory.
    Instead of returning a raw pointer from CZMQNotifierFactory and
    implicitly requiring the caller to know that it has to take ownership,
    return a std::unique_ptr to make this explicit.
    
    This also changes the typedef for CZMQNotifierFactory to use the new
    C++11 using syntax, which makes it (a little) less cryptic.
    7f2ad1b9ac
  61. Replace zmqconfig.h by a simple zmqutil.
    zmqconfig.h is currently not really needed anywhere, except that
    it declares zmqError (which is then defined in
    zmqnotificationinterface.cpp).  Note in particular that there is
    no need to conditionally include zmq.h only if ZMQ is enabled, because
    the place in the core code where the ZMQ library itself is included
    (init.cpp) is conditional already on that.
    
    This commit removes zmqconfig.h and replaces it by a much simpler
    zmqutil.h library for zmqError.  The definition of the function is
    moved to the matching (newly created) zmqutil.cpp.
    a3ffb6ebeb
  62. domob1812 force-pushed on Sep 7, 2020
  63. domob1812 commented at 9:06 am on September 7, 2020: contributor

    The first commit 707c480 is not compiled for me. Make iterator type auto?

    Sorry for that, it is fixed now. The overall PR is not changed (except I’ve rebased it to current master), but now each individual commit builds.

  64. hebasto changes_requested
  65. hebasto commented at 11:10 am on September 14, 2020: member

    ACK 166ddcbef82ba2b6734e8f09584fb73e4f9f98a4.

    Mind making the latest commit 166ddcbef82ba2b6734e8f09584fb73e4f9f98a4 a sripted-diff with the following script:

    0sed -i 's/SendMessage/SendZmqMessage/g' src/zmq/zmqpublishnotifier.*
    

    ?

  66. fanquake requested review from instagibbs on Sep 15, 2020
  67. scripted-diff: Rename SendMessage to SendZmqMessage.
    Windows headers define SendMessage as a macro, which leads to problems
    with the method name "SendMessage".  To circumvent this, we rename the
    method to "SendZmqMessage".
    
    -BEGIN VERIFY SCRIPT-
    sed -i 's/SendMessage/SendZmqMessage/g' src/zmq/zmqpublishnotifier.*
    -END VERIFY SCRIPT-
    6fe2ef2acb
  68. domob1812 force-pushed on Sep 15, 2020
  69. domob1812 commented at 7:40 am on September 15, 2020: contributor

    Mind making the latest commit 166ddcb a sripted-diff with the following script:

    Good point, done.

  70. instagibbs commented at 2:23 pm on September 15, 2020: member

    utACK 6fe2ef2acb00b1df7f6a0c0dea1a81a1924be0e1

    I restarted one travis job that appeared to be a spurious timeout.

  71. hebasto approved
  72. hebasto commented at 2:25 pm on September 15, 2020: member
    re-ACK 6fe2ef2acb00b1df7f6a0c0dea1a81a1924be0e1, only the latest commit got a scripted-diff since my previous review.
  73. fanquake merged this on Sep 19, 2020
  74. fanquake closed this on Sep 19, 2020

  75. sidhujag referenced this in commit 99dd8bffb8 on Sep 20, 2020
  76. domob1812 deleted the branch on Sep 21, 2020
  77. domob1812 referenced this in commit 956668abec on Sep 21, 2020
  78. ftrader referenced this in commit 01f5955382 on Apr 14, 2021
  79. kittywhiskers referenced this in commit c7c5ed352e on Aug 28, 2021
  80. PastaPastaPasta referenced this in commit bbc8623245 on Aug 31, 2021
  81. deadalnix referenced this in commit 9d00dd39b5 on Oct 11, 2021
  82. deadalnix referenced this in commit 9a008904ee on Oct 11, 2021
  83. deadalnix referenced this in commit 7a143c3086 on Oct 11, 2021
  84. deadalnix referenced this in commit df5197f20d on Oct 11, 2021
  85. deadalnix referenced this in commit 5fcbf32ac0 on Oct 11, 2021
  86. DrahtBot locked this on Feb 15, 2022

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: 2024-12-19 00:12 UTC

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