refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public, rm temporaries, simplify #25651

pull jonatack wants to merge 2 commits into bitcoin:master from jonatack:make-ChainImpl-m_node-data-member-private changing 2 files +16 −37
  1. jonatack commented at 4:31 PM on July 20, 2022: contributor
    • Make all NodeImpl, ChainImpl and ExternalSignerImpl class members public (and document why), to be consistent in all the *Impl classes in src/node/interfaces.cpp and src/wallet/interfaces.cpp and to help future reviewers and contributors.

    • Remove unneeded temporaries in NodeImpl and ChainImpl methods in src/node/interfaces.cpp and simplify, to make the code easier to read and understand and to improve performance by avoiding unnecessary move operations.

  2. MarcoFalke commented at 4:52 PM on July 20, 2022: member

    Some notes:

    • The whole file is in an unnamed namespace, so everything in it is already "private" for all other files.
    • I can't imagine code that instantiates this class in this file and calls methods on it, so I don't think private/public makes any difference in practise.
    • As no methods are called (and will ever be called directly), except the constructor, everything else could be private as well. Though, as this makes no difference in practise, I am not sure if this even makes sense conceptually.
  3. jonatack commented at 5:02 PM on July 20, 2022: contributor

    Thanks for having a look, Marco. It seems some of those things could change in the future, so this change looks preferable to me.

    In addition to the motivations in the OP, having m_node next to the already private ChainstateManager& chainman() { return *Assert(m_node.chainman); } method seems easier to reason about. This is what made me look for m_node in the first place and wonder why it was placed there.

  4. MarcoFalke commented at 5:12 PM on July 20, 2022: member

    It seems some of those things could change in the future

    I don't think any of the things could change in the future, unless I am missing something or you have examples where it would change.

    So to me this mostly seems like a stylistic change, as I can't imagine a real-world example where this could make any difference.

  5. ryanofsky commented at 5:41 PM on July 20, 2022: contributor

    I guess I have a mild preference for making everything public, but this change seems fine.

    By design everything in ChainImpl NodeImpl WalletImpl has been public since the classes were written because the classes themselves are private. IMO it is only meaningful to distinguish between overridden and non-overridden members and private/public seems like noise in this context.

    Whatever style you land on though I do think would be good to consistently apply it to all the *Impl classes in src/node/interfaces.cpp and src/wallet/interfaces.cpp

  6. DrahtBot added the label Refactoring on Jul 20, 2022
  7. jonatack commented at 6:10 PM on July 20, 2022: contributor

    Thanks, I'm convinced. Nothing in src/wallet/interfaces.cpp is private. Making everything in src/node/interfaces.cpp public (and maybe documenting why) makes sense.

  8. jonatack renamed this:
    refactor: make ChainImpl::m_node data member private
    refactor: make all NodeImpl and ChainImpl members public and document why
    on Jul 20, 2022
  9. jonatack force-pushed on Jul 20, 2022
  10. jonatack commented at 7:08 PM on July 20, 2022: contributor

    Updated per review feedback (thank you).

  11. jonatack renamed this:
    refactor: make all NodeImpl and ChainImpl members public and document why
    refactor: make all NodeImpl/ChainImpl members public, rm unneeded temporaries, simplify
    on Jul 27, 2022
  12. jonatack force-pushed on Jul 27, 2022
  13. jonatack commented at 1:42 PM on July 27, 2022: contributor

    Added a second commit removing unnecessary and verbose temporaries in NodeImpl and ChainImpl methods in src/node/interfaces.cpp to make the code easier to read and understand and avoid unnecessary move operations. Updated the pull title and description. Refactoring only, no change in behavior; should be easy to review.

  14. DrahtBot commented at 10:16 PM on July 27, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #25673 (refactor: make member functions const when applicable by aureleoules)

    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.

  15. in src/node/interfaces.cpp:386 in b9a715ea03 outdated
     382 | @@ -384,12 +383,13 @@ class NodeImpl : public Node
     383 |                      /* verification progress is unused when a header was received */ 0);
     384 |              }));
     385 |      }
     386 | +    NodeContext* m_context{nullptr};
    


    ryanofsky commented at 5:47 PM on July 28, 2022:

    In commit "refactor: make all NodeImpl and ChainImpl members public" (b9a715ea03506bc6aa647d3df1cd177221d30dd0)

    Would encourage putting class member variables all together and not interspersing them with methods. If I'm trying to understand what a class does and what its role is in the application, knowing what data it contains is maybe the single most important piece of information I could have, and it's harder to see if the data fields aren't declared in a single place.

    #21525 messed up the style a bit but the original style and style I'd recommend would be:

    class NodeImpl : public Node
    {
    public:
      // ... virtual methods first ...
      // ... nonvirtual helper methods next ...
      // ... data fields last ...
    };
    

    jonatack commented at 5:25 PM on July 29, 2022:

    Done, and it's a smaller diff too. Thanks for clarifying!

  16. in src/node/interfaces.cpp:504 in d742ebbc91 outdated
     506 | -        if (height >= 0) {
     507 | -            return height;
     508 | -        }
     509 | -        return std::nullopt;
     510 | +        const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
     511 | +        return height >= 0 ? std::optional{height} : std::nullopt;
    


    ryanofsky commented at 5:52 PM on July 28, 2022:

    In commit "refactor: remove unneeded temporaries in node/interfaces, simplify code" (d742ebbc91e5ca40389a3923bffba3e24911cc6f)

    Nice simplification!

  17. ryanofsky approved
  18. ryanofsky commented at 5:55 PM on July 28, 2022: contributor

    Code review ACK d742ebbc91e5ca40389a3923bffba3e24911cc6f. Suggested a change but is already an improvement as-is and the second commit does a nice job of modernizing some of this code

  19. jonatack renamed this:
    refactor: make all NodeImpl/ChainImpl members public, rm unneeded temporaries, simplify
    refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public, rm temporaries, simplify
    on Jul 29, 2022
  20. refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public
    as the classes themselves are private, and to be consistent within all the
    *Impl classes in src/node/interfaces.cpp and src/wallet/interfaces.cpp
    following this order:
    
    public:
      // ... virtual methods ...
      // ... nonvirtual helper methods ...
      // ... data members ...
    
    and add documentation in src/node/interfaces.cpp and src/wallet/interfaces.cpp
    to help future reviewers and contributors.
    b27ba169eb
  21. jonatack force-pushed on Jul 29, 2022
  22. refactor: remove unneeded temporaries in node/interfaces, simplify code
    - make the code easier to read and understand
    
    - improve performance by avoiding unnecessary move operations
    
    - the cleaner, simpler, and easier to read the code is, the
      better chance the compiler has at implementing it well
    4bedfd702a
  23. jonatack force-pushed on Jul 29, 2022
  24. jonatack commented at 5:47 PM on July 29, 2022: contributor

    Updated the first commit with @ryanofsky's suggestion (thanks!) and the second commit with a simplification.

    <details><summary><code>git diff d742ebb 4bedfd7</code></summary><p>

    diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
    index bd819cf53e..699dca0a73 100644
    --- a/src/node/interfaces.cpp
    +++ b/src/node/interfaces.cpp
    @@ -73,9 +73,9 @@ namespace {
     class ExternalSignerImpl : public interfaces::ExternalSigner
     {
     public:
    -    ::ExternalSigner m_signer;
         ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
         std::string getName() override { return m_signer.m_name; }
    +    ::ExternalSigner m_signer;
     };
     #endif
     
    @@ -378,13 +378,13 @@ public:
                         /* verification progress is unused when a header was received */ 0);
                 }));
         }
    -    NodeContext* m_context{nullptr};
         NodeContext* context() override { return m_context; }
         void setContext(NodeContext* context) override
         {
             m_context = context;
         }
         ChainstateManager& chainman() { return *Assert(m_context->chainman); }
    +    NodeContext* m_context{nullptr};
     };
     
     bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)
    @@ -702,10 +702,7 @@ public:
         }
         void waitForNotificationsIfTipChanged(const uint256& old_tip) override
         {
    -        if (!old_tip.IsNull()) {
    -            LOCK(::cs_main);
    -            if (old_tip == chainman().ActiveChain().Tip()->GetBlockHash()) return;
    -        }
    +        if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
             SyncWithValidationInterfaceQueue();
         }
         std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
    @@ -760,9 +757,9 @@ public:
             return chainman().IsSnapshotActive();
         }
     
    -    NodeContext& m_node;
         NodeContext* context() override { return &m_node; }
         ChainstateManager& chainman() { return *Assert(m_node.chainman); }
    +    NodeContext& m_node;
     };
    

    </p></details>

  25. ryanofsky approved
  26. ryanofsky commented at 10:26 PM on July 29, 2022: contributor

    Code review ACK 4bedfd702ad878645c51bea6ee8ce40d8c0bd3da. Changes since last review, applying suggested style & simplifiying first commit. Also avoiding another lock in second commit.

  27. MarcoFalke merged this on Aug 1, 2022
  28. MarcoFalke closed this on Aug 1, 2022

  29. jonatack deleted the branch on Aug 1, 2022
  30. sidhujag referenced this in commit fb0bbd8e26 on Aug 1, 2022
  31. bitcoin locked this on Aug 1, 2023

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-14 21:13 UTC

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