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

net processing: Move peer_map to PeerManager #19910

Merged
merged 4 commits into from
Dec 9, 2020

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Sep 7, 2020

This moves g_peer_map from a global in net_processing.cpp's unnamed namespace to being a member m_peer_map of PeerManager.

@jnewbery
Copy link
Contributor Author

jnewbery commented Sep 7, 2020

Requested by @MarcoFalke @sdaftuar and @theuni . Review very much appreciated 🙏

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Concept ACK

src/rpc/net.cpp Outdated
@@ -156,7 +156,10 @@ static UniValue getpeerinfo(const JSONRPCRequest& request)
for (const CNodeStats& stats : vstats) {
UniValue obj(UniValue::VOBJ);
CNodeStateStats statestats;
bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
bool fStateStats{false};
if (node.peerman) {
Copy link
Member

Choose a reason for hiding this comment

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

It is currently not possible to not have node.peerman. I think an Ensure..() like EnsureMempool would be good. It doesn't really make sense to call getpeerinfo without a peerman anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done something slightly different and checked existence at the top of this function. Let me know what you think.

src/net_processing.h Outdated Show resolved Hide resolved
@jnewbery jnewbery force-pushed the 2020-08-peer-plv2 branch 2 times, most recently from e4488c3 to 322e87b Compare September 7, 2020 18:09
@hebasto
Copy link
Member

hebasto commented Sep 7, 2020

Concept ACK.

@practicalswift
Copy link
Contributor

Concept ACK: decoupling is good!

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK 322e87b, I have reviewed the code and it looks OK.

src/net_processing.h Outdated Show resolved Hide resolved
src/net_processing.h Show resolved Hide resolved
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK d350feb

Copy link
Contributor

@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.

Concept ACK

src/interfaces/node.cpp Outdated Show resolved Hide resolved
src/net_processing.cpp Show resolved Hide resolved
@promag
Copy link
Contributor

promag commented Sep 9, 2020

Concept ACK.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 19, 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.

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

jnewbery commented Dec 1, 2020

rebased

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK aedd418, added two last commits since my previous review.

Still required rebasing due to the conflict with #20530.

src/net_processing.h Outdated Show resolved Hide resolved
Comment on lines 840 to 842
PeerRef ret;
LOCK(m_peer_mutex);
auto it = m_peer_map.find(id);
return it != m_peer_map.end() ? it->second : nullptr;
if (it != m_peer_map.end()) ret = it->second;
return ret;
Copy link
Member

Choose a reason for hiding this comment

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

6bfcef3

Why are these changes required? What are benefits to return an empty shared pointer instead nullptr-constructed one? Btw, in all caller places the return value is checked for nullptr, not being empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes is clear that NRVO can be used, since there's only one return statement. I'm not sure if the previous version would have used copy elision.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for considering this!

I'd be curious to know if compilers are allowed to rearrange the code before deciding how many return statements there are. I could see this going either way.

Copy link
Member

@hebasto hebasto Dec 5, 2020

Choose a reason for hiding this comment

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

Considering the before-change code, to be precise, in C++17 in a return statement, when the operand is a prvalue (the result of the ternary conditional expression), elision of copy/move operations is mandatory.

OTOH, NRVO is non-mandatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @hebasto! Yes, I think you're right. If I'm reading it correctly, the result of the conditional operator is a prvalue (item 5 in https://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator since 4 doesn't apply), and copy elision is mandatory for returning a prvalue.

I've reverted this.

Copy link
Member

Choose a reason for hiding this comment

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

... and copy elision is mandatory for returning a prvalue.

Good to have C++17 :)

@dongcarl
Copy link
Contributor

dongcarl commented Dec 3, 2020

Code Review ACK aedd418


Private members >> Anon-namespaced statics/functions

Especially when the functionality is strongly intertwined with the class of which it's being made a member.

@jnewbery
Copy link
Contributor Author

jnewbery commented Dec 4, 2020

Rebased and addressed outstanding review comments.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Core review ACK 4fe77ae.

Could squash last commit or fix the comment in 5a1f08c.

src/net_processing.cpp Show resolved Hide resolved
Copy link
Member

@theuni theuni left a comment

Choose a reason for hiding this comment

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

ACK 4fe77ae or squashed as @promag suggested.

src/net_processing.cpp Show resolved Hide resolved
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK 4fe77ae, only rebased since my previous review and added a commit to fix comments.

@jnewbery
Copy link
Contributor Author

jnewbery commented Dec 7, 2020

Thanks for the review @hebasto @promag @theuni and @dongcarl. I've addressed all comments, so this should now be ready for re-review.

Comment on lines +843 to +853
PeerRef PeerManager::RemovePeer(NodeId id)
{
PeerRef ret;
LOCK(m_peer_mutex);
auto it = m_peer_map.find(id);
if (it != m_peer_map.end()) {
ret = std::move(it->second);
m_peer_map.erase(it);
}
return ret;
}
Copy link
Member

Choose a reason for hiding this comment

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

3025ca9
I'm curios is it possible to force the mandatory copy/move elision for the return value here as well?

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK 3025ca9, since my previous review only reverted the change that introduced NRVO in PeerManager::GetPeerRef, and comments are fixed in the proper commits.

@theuni
Copy link
Member

theuni commented Dec 8, 2020

Re-ACK 3025ca9.

@dongcarl
Copy link
Contributor

dongcarl commented Dec 9, 2020

Re-ACK 3025ca9


  1. Disambiguated between nullptr and empty shared_ptr
  2. Reverted refactoring in PeerManager::GetPeerRef
  3. Made Peer constructor explicit

@fanquake fanquake merged commit 795afe6 into bitcoin:master Dec 9, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 9, 2020
@@ -842,11 +790,9 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
LOCK(cs_main);
int misbehavior{0};
{
PeerRef peer = GetPeerRef(nodeid);
PeerRef peer = RemovePeer(nodeid);
assert(peer != nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm getting bitcoind crashing due to this assert. It appears that sometimes FinalizeNode gets called twice (from DeleteNode()). Perhaps nRefCount is being increased between the two occurances. This only seems to happen though when there's a delay between them due to UpdateTip(). It's probably a bug I've introduced somewhere, but just leaving this comment as a "heads up".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I expect if the bug was present in master we'd have seen it in CI or from a user report.

@jnewbery jnewbery deleted the 2020-08-peer-plv2 branch September 15, 2021 10:02
@bitcoin bitcoin locked and limited conversation to collaborators Sep 15, 2021
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.