Skip to content
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

Merged
merged 5 commits into from
Jan 13, 2021

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Dec 31, 2020

Moves the implementation details of PeerManager and all of struct Peer into net_processing.cpp.

@ajtowns
Copy link
Contributor Author

ajtowns commented Dec 31, 2020

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:

  • reduce compilation times (the code only gets compiled for a single cpp, rather than every cpp that includes the header)
  • reduce churn when updating the implementation (you only need to touch the cpp, not the header; and don't need to recompile other modules)
  • makes it easier to see what's interface and what's implementation details

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 31, 2020

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

Conflicts

Reviewers, 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.

@jnewbery
Copy link
Contributor

jnewbery commented Jan 1, 2021

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 std::condition_variable condMsgProc/Mutex mutexMsgProc so maybe it's something similar to google/sanitizers#1259?

src/net_processing.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@jnewbery jnewbery left a 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.

src/net_processing.h Outdated Show resolved Hide resolved
src/net_processing.cpp Outdated Show resolved Hide resolved
src/net_processing.cpp Show resolved Hide resolved
src/net_processing.cpp Outdated Show resolved Hide resolved
src/net_processing.h Show resolved Hide resolved
@jnewbery
Copy link
Contributor

jnewbery commented Jan 2, 2021

utACK fa6b227

@jnewbery
Copy link
Contributor

jnewbery commented Jan 3, 2021

utACK 7bd4517

@Sjors
Copy link
Member

Sjors commented Jan 3, 2021

Would it make sense to move PeerManager to its own file (given how much this PR is moving stuff around anyway)?

@jnewbery
Copy link
Contributor

jnewbery commented Jan 3, 2021

utACK abeecaa

Thanks for being so responsive to review comments!

@ajtowns
Copy link
Contributor Author

ajtowns commented Jan 3, 2021

Bumped to fix missed indentation.

@Sjors After the further commits in #20758 almost all of net_processing is moved into PeerManager, so its effectively a dedicated file anyway. Splitting out sub-components (like orphan handling) into their own modules would be a good next step afterwards, I think.

src/net.h Outdated Show resolved Hide resolved
@jnewbery
Copy link
Contributor

jnewbery commented Jan 7, 2021

I'm not sure if adding the comments to NetEventsInterface adds much.

utACK 02f6776.

Copy link
Contributor

@jnewbery jnewbery left a 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) */
Copy link
Contributor

@jnewbery jnewbery Jan 9, 2021

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 returns true and that value is discarded by the caller. A future commit could change the function to return void.
  • 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).

Copy link
Contributor Author

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

@jnewbery
Copy link
Contributor

jnewbery commented Jan 9, 2021

ACK c97f70c

@Sjors
Copy link
Member

Sjors commented Jan 11, 2021

ACK c97f70c

After the further commits in #20758 almost all of net_processing is moved into PeerManager, so its effectively a dedicated file anyway. Splitting out sub-components (like orphan handling) into their own modules would be a good next step afterwards, I think.

That sounds good, because net_processing.cpp is almost 5000 lines now, with a whole bunch of classes and structs.

Copy link
Contributor

@vasild vasild left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

src/net_processing.cpp Show resolved Hide resolved
Comment on lines +349 to 351
} // namespace

namespace {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
} // namespace
namespace {

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@ariard
Copy link
Member

ariard commented Jan 13, 2021

Reviewing this really soon if you're looking for more eyes on it before merge.

@laanwj
Copy link
Member

laanwj commented Jan 13, 2021

Nice, good thing to have less implementation details in (commonly included) header files.

Code review ACK c97f70c

Reviewing this really soon if you're looking for more eyes on it before merge.

Don't let it being merged keep you from reviewing it please 😄

@laanwj laanwj merged commit 60427ee into bitcoin:master Jan 13, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 13, 2021
Copy link
Member

@ariard ariard left a 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.

bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);

/** Implement PeerManager */
Copy link
Member

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

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

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

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" :) ?

Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 25, 2022
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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 25, 2022
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
Fabcien added a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 25, 2022
…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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 25, 2022
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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 25, 2022
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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
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.

None yet

9 participants