-
Notifications
You must be signed in to change notification settings - Fork 36.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: move net_processing implementation details out of header #20811
Conversation
This is the first step of the refactoring proposed in #20758 and just does encapsulation, it doesn't remove any of the globals. EDIT: Rationale for moving things from the header into cpp files is to:
More specifically, #20758 aims to reduce the number of net_processing globals by moving them into the PeerManager class, which will involve less churn if that class isn't exposed as a header file; and #19398 aims to move things from net to net_processing which also involves less churn if it doesn't need to go into the net_processing header file. |
62ac65d
to
56423c5
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, 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. |
56423c5
to
13a7fec
Compare
Concept ACK. I like this kind of encapsulation a lot. I'm not sure why the TSAN build is failing with "WARNING: ThreadSanitizer: double lock of a mutex". It seems like a false alarm to me. I think it's choking on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 13a7fec assuming the tsan suppression is fixed.
13a7fec
to
fa6b227
Compare
utACK fa6b227 |
fa6b227
to
7bd4517
Compare
utACK 7bd4517 |
Would it make sense to move |
7bd4517
to
abeecaa
Compare
utACK abeecaa Thanks for being so responsive to review comments! |
I'm not sure if adding the comments to utACK 02f6776. |
02f6776
to
3bcde8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 3bcde8b
Did a git range-diff to verify that only the comments in NetEventsInterface
have changed.
@@ -769,11 +769,28 @@ class CNode | |||
class NetEventsInterface | |||
{ | |||
public: | |||
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0; | |||
virtual bool SendMessages(CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_sendProcessing) = 0; | |||
/** Initialize a peer (setup state, queue any initial messages) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's a big deal, but if you're going to add these comments to the interface class, I think there are some potential improvements:
- "queue any initial messages" is only true for the version message for outbound peers. Since it's not universally true, it probably doesn't belong in the description of the interface.
SendMessages()
return value is not used to indicate if more work is done. The function always returnstrue
and that value is discarded by the caller. A future commit could change the function to returnvoid
.ProcessMessages()
does return a bool that indicates whether there's more work.
Eventually, these functions could all be updated to just take a single CNode&
argument, which would be a very clean interface (#20228 removes the update_connection_time
arg from FinalizeNode()
, and I have another branch that eliminates interrupt
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SendMessages
lost its new lock annotation in the rebase, so I've added the doc from ProcessMessages
return value.
I think "any initial messages" covers nothing, just a version message, or anything else we might think up fine. The return value of SendMessages
is "used" in the unit tests, so haven't removed that in this PR. Looking at the CNode&
thing in #20758
3bcde8b
to
c97f70c
Compare
ACK c97f70c |
ACK c97f70c
That sounds good, because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c97f70c
Code review ok (some minor questions below), compiles locally with clang 12 without warnings, unit and functional tests pass.
using PeerRef = std::shared_ptr<Peer>; | ||
|
||
class PeerManager final : public CValidationInterface, public NetEventsInterface { | ||
class PeerManager : public CValidationInterface, public NetEventsInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe consider renaming
PeerManager
-> PeerManagerInterface
PeerManagerImpl
-> PeerManager
for consistency with CValidationInterface
and NetEventsInterface
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I look at it is that Interface
is for defining a class where some other module is going to implement the methods that do the actual work; but the work to fulfill the PeerManager
API really is done by net_processing; it's just an implementation detail that the methods are virtual and inheritance is used -- the unique_ptr<Impl> m_impl;
approach used in txrequest could equally well have been used too. So I think PeerManager*
is the right name for what is being exposed to other modules here.
} // namespace | ||
|
||
namespace { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
} // namespace | |
namespace { | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for below L462-464. I don't think this needs to be fixed in this PR. When it is, everything in the second anonymous namespace (L351-462) should be unindented - our style guide says that namespace blocks shouldn't be indented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #20758 -- that has patches that move the entries from those namespaces into PeerManager (and thus skips over unindenting them). Certainly could remove the redundant close/open, but it seemed a useful indicator of where the work's up to to me, and it doesn't seem harmful.
Reviewing this really soon if you're looking for more eyes on it before merge. |
Nice, good thing to have less implementation details in (commonly included) header files. Code review ACK c97f70c
Don't let it being merged keep you from reviewing it please 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review ACK c97f70c
Good code restructuring direction, only minors.
src/net_processing.h
Outdated
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override; | ||
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing); | ||
|
||
/** Implement PeerManager */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think comment here and above should have been updated as "Overriden from..." now we have a final class.
/** | ||
* Potentially mark a node discouraged based on the contents of a BlockValidationState object | ||
* | ||
* @param[in] via_compact_block this bool is passed in because net_processing should |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This param name could be more precise, according to the BIP, the waiver around invalid transactions is restrained to high-bandwidth only, so could be via_hb_compact_block
.
void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req); | ||
|
||
/** Register with TxRequestTracker that an INV has been received from a | ||
* peer. The announcement parameters are decided in PeerManager and then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should say PeerManagerImpl now ?
void SetBestHeight(int height) { m_best_height = height; }; | ||
/** | ||
* Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. | ||
* Public for unit testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could have been better to comment in commit message the re-ordering logic followed "Move method exposed for test-only at the end of public method space declaration" :) ?
Summary: Move only, comment only. Partial backport of [[bitcoin/bitcoin#20811 | core#20811]]: bitcoin/bitcoin@0d246a5 Ref T1696. Depends on D10881. Test Plan: Read the comments Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10882
Summary: Partial backport of [[bitcoin/bitcoin#20811 | core#20811]]: bitcoin/bitcoin@0df3d3f Ref T1696. Test Plan: ninja all check-all ninja bitcoin-fuzzers Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10886
…lasses Summary: Partial backport of [[bitcoin/bitcoin#20811 | core#20811]]: bitcoin/bitcoin@a568b82 Note that the cpp file deserves a good reordering, but that's out of scope for this diff. Depends on D10886. Ref T1696. Test Plan: ninja all check-all ninja bitcoin-fuzzers Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10887
Summary: Partial backport of [[bitcoin/bitcoin#20811 | core#20811]]: bitcoin/bitcoin@e0f2e6d This is a move only change. The double namespace is kept to limit later merge conflicts, it's harmless. Depends on D10887. Ref T1696. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10890
Summary: Completes backport of [[bitcoin/bitcoin#20811 | core#20811]]: bitcoin/bitcoin@c97f70c Depends on D10890. Ref T1696. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10892
Moves the implementation details of
PeerManager
and all ofstruct Peer
into net_processing.cpp.