Skip to content

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

Merged
maflcko merged 2 commits into
bitcoin:masterfrom
jonatack:make-ChainImpl-m_node-data-member-private
Aug 1, 2022
Merged

maflcko merged 2 commits into
bitcoin:masterfrom
jonatack:make-ChainImpl-m_node-data-member-private

Conversation

@jonatack

@jonatack jonatack commented Jul 20, 2022 •

Copy link
Copy Markdown
Member
  • 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.

@maflcko

maflcko commented Jul 20, 2022

Copy link
Copy Markdown
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.

@jonatack

Copy link
Copy Markdown
Member Author

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.

@maflcko

maflcko commented Jul 20, 2022

Copy link
Copy Markdown
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.

@ryanofsky

Copy link
Copy Markdown
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

@jonatack

Copy link
Copy Markdown
Member Author

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.

@jonatack jonatack changed the title refactor: make ChainImpl::m_node data member private refactor: make all NodeImpl and ChainImpl members public and document why Jul 20, 2022
@jonatack
jonatack force-pushed the make-ChainImpl-m_node-data-member-private branch from 02f5160 to fd528bc Compare July 20, 2022 19:06
@jonatack

Copy link
Copy Markdown
Member Author

Updated per review feedback (thank you).

@jonatack jonatack changed the title refactor: make all NodeImpl and ChainImpl members public and document why refactor: make all NodeImpl/ChainImpl members public, rm unneeded temporaries, simplify Jul 27, 2022
@jonatack
jonatack force-pushed the make-ChainImpl-m_node-data-member-private branch from fd528bc to d742ebb Compare July 27, 2022 13:31
@jonatack

Copy link
Copy Markdown
Member Author

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.

@DrahtBot

Copy link
Copy Markdown
Contributor

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

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.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/node/interfaces.cpp Outdated

@ryanofsky ryanofsky Jul 28, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...
};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/node/interfaces.cpp Outdated
Comment on lines 503 to 504

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Nice simplification!

@jonatack jonatack changed the title refactor: make all NodeImpl/ChainImpl members public, rm unneeded temporaries, simplify refactor: make all NodeImpl/ChainImpl/ExternalSignerImpl members public, rm temporaries, simplify Jul 29, 2022
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.
@jonatack
jonatack force-pushed the make-ChainImpl-m_node-data-member-private branch from d742ebb to e7ffe54 Compare July 29, 2022 17:30
- 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
@jonatack
jonatack force-pushed the make-ChainImpl-m_node-data-member-private branch from e7ffe54 to 4bedfd7 Compare July 29, 2022 17:42
@jonatack

Copy link
Copy Markdown
Member Author

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

git diff d742ebb 4bedfd7

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;
 };

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@maflcko
maflcko merged commit da23320 into bitcoin:master Aug 1, 2022
@jonatack
jonatack deleted the make-ChainImpl-m_node-data-member-private branch August 1, 2022 09:37
@bitcoin bitcoin locked and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants