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

Use Sock in CNode #23604

Merged
merged 4 commits into from Feb 4, 2022
Merged

Use Sock in CNode #23604

merged 4 commits into from Feb 4, 2022

Conversation

vasild
Copy link
Contributor

@vasild vasild commented Nov 26, 2021

This is a piece of #21878, chopped off to ease review.

Change CNode to use a pointer to Sock instead of a bare SOCKET.
This will help mocking / testing / fuzzing more code.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 27, 2021

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24247 (p2p: Split network logging into two categories by anshu-khare-design)
  • #24185 (refactor: only use explicit reinterpret/const casts, not implicit by PastaPastaPasta)
  • #24170 (p2p, rpc: Manual block-relay-only connections with addnode by mzumsande)
  • #23900 (rpc: p2p_v2 rpc argument for addnode by dhruv)
  • #23545 (scripted-diff: Use clang-tidy syntax for C++ named arguments by MarcoFalke)
  • #22778 (net processing: Reduce resource usage for inbound block-relay-only connections by jnewbery)
  • #21160 (net/net processing: Move tx inventory into net_processing by jnewbery)
  • #17167 (Allow whitelisting outgoing connections by luke-jr)

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.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept and light code-review ACK ba8cc5b

The first commit seems unrelated to the other two, I guess this will be useful in a later chopped off PR?

@vasild
Copy link
Contributor Author

vasild commented Nov 29, 2021

ba8cc5b4e5...846e9952cf: use FuzzedSock in ConsumeNode()

Invalidates (light) ACK from @theStack

The first commit seems unrelated to the other two...

Oh, good catch! The idea is to use FuzzedSock in ConsumeNode() which creates a CNode object. Both are defined in src/test/fuzz/util.h, but for this FuzzedSock must appear before ConsumeNode(). It was like this in a previous incarnation of #21878 but in the latest push, for some reason, I changed it to use nullptr 🤦

@vasild
Copy link
Contributor Author

vasild commented Dec 1, 2021

846e9952cf...78cda19bb8: rebase due to conflicts

Previously invalidated (light) ACK from @theStack

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Light re-ACK 78cda19

(as per git range-diff ba8cc5b4...78cda19b)

@vasild
Copy link
Contributor Author

vasild commented Dec 7, 2021

78cda19bb8...0ec56c651e: rebase due to conflicts

Invalidates (light) ACK from @theStack

@jonatack
Copy link
Contributor

jonatack commented Dec 7, 2021

Concept ACK. CI failures are unrelated.

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK 0ec56c6

src/test/net_tests.cpp Outdated Show resolved Hide resolved
src/test/fuzz/util.h Outdated Show resolved Hide resolved
@vasild
Copy link
Contributor Author

vasild commented Dec 9, 2021

0ec56c651e...d22a477542: address suggestions (only stylistic changes)

Invalidates ACK from @jonatack

Previously invalidated ACK from @theStack

@jonatack
Copy link
Contributor

jonatack commented Dec 9, 2021

ACK d22a477

Copy link
Contributor

@stratospher stratospher left a comment

Choose a reason for hiding this comment

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

ACK 1477f3a.

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

tACK 1477f3a

Great explanation of why use shared_ptr instead of std::unique_ptr for m_sock

This way it can be used in `ConsumeNode()`.
@vasild
Copy link
Contributor Author

vasild commented Jan 6, 2022

1477f3aa73...af7fc2864e: rebase due to conflicts

Invalidates ACKs from @jonatack, @theStack, @stratospher, @w0xlt

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK af7fc28 🧦

src/net.cpp Outdated Show resolved Hide resolved
src/net.cpp Outdated Show resolved Hide resolved
@laanwj laanwj added this to Blockers in High-priority for review Jan 14, 2022
break;
nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
}
nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably a silly question: LOCK(node.m_sock_mutex); applies even to node.m_sock->Send(...) operation but then here one can return contained sockets out of the function.

I understand that m_sock_mutex guards only m_sock member read & write operations but not necessarily the contained socket. Is that correct or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good question.

Before this PR the mutex protects the value of the socket variable - that is, to make sure it does not change from e.g. 7 (it is a file descriptor) to INVALID_SOCKET in the middle of constructs like:

if (socket != INVALID_SOCKET) {
    ... do something with the socket, assuming it is valid ...
}

After this PR, it is the same with the exception that the variable is not a bare integer anymore, but a shared_ptr and an empty such pointer is used to designate what was designated before by INVALID_SOCKET.

Copy link
Contributor

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

LGTM, one request

src/net.cpp Outdated Show resolved Hide resolved
Change `CNode` to use a pointer to `Sock` instead of a bare `SOCKET`.
This will help mocking / testing / fuzzing more code.
-BEGIN VERIFY SCRIPT-
sed -i -e 's/cs_hSocket/m_sock_mutex/g' $(git grep -l cs_hSocket)
-END VERIFY SCRIPT-
@vasild
Copy link
Contributor Author

vasild commented Jan 28, 2022

af7fc2864e...ef5014d256: address suggestions

Invalidates ACK from @jonatack

Previously invalidated ACKs from @theStack, @stratospher, @w0xlt

@jonatack
Copy link
Contributor

re-ACK ef5014d changes since last review are the removal of an unneeded dtor and the addition of a style commit

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

reACK ef5014d

@@ -2994,11 +2997,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer());
}

CNode::~CNode()
Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies if this is a silly question but just wanted to clarify - removing this destructor, it's implicit that the shared_ptr for Sock will be reset and the Sock's destructor will responsible for closing the socket?

Copy link
Contributor

Choose a reason for hiding this comment

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

See https://en.cppreference.com/w/cpp/language/destructor "Implicitly-declared destructor".

The default destructor for CNode will call all the destructors of all of it's members. The destructor for shared_ptr will reduce the reference counter, and then if that reference counter is zero, the destructor for Sock will be called. If the RC is not zero, then the Sock will continue to exist on the heap (presumably being used by another thread), until the owner of that shared_ptr calls the destructor.

Copy link
Contributor

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

utACK ef5014d, I have reviewed the code, and believe it makes sense to merge

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Since my last ACK was only "light" and happened weeks ago, so I decided to do a full review of this PR again instead of looking at the range-diff. Changes LGTM!

Cod-review ACK ef5014d

@laanwj laanwj merged commit e8a3882 into bitcoin:master Feb 4, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 6, 2022
@laanwj laanwj removed this from Blockers in High-priority for review Feb 10, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Feb 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet