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

P2P handshake handling #2306

Merged
merged 14 commits into from
Apr 27, 2019
Merged

P2P handshake handling #2306

merged 14 commits into from
Apr 27, 2019

Conversation

prestonvanloon
Copy link
Member

@prestonvanloon prestonvanloon commented Apr 19, 2019

The basic idea of this PR is to implement the following:

On new peer connection, send a "handshake" message to the peer. The peer should also respond with a "handshake" message. If the two peers do not agree on some of the information in the handshake, they should disconnect and not try to reconnect with each other.

Banning peers is blocked by libp2p/go-libp2p#274

@codecov
Copy link

codecov bot commented Apr 19, 2019

Codecov Report

Merging #2306 into master will decrease coverage by 0.18%.
The diff coverage is 52.88%.

@@            Coverage Diff             @@
##           master    #2306      +/-   ##
==========================================
- Coverage   65.81%   65.63%   -0.19%     
==========================================
  Files         119      122       +3     
  Lines        9446     9532      +86     
==========================================
+ Hits         6217     6256      +39     
- Misses       2538     2575      +37     
- Partials      691      701      +10

@raulk
Copy link

raulk commented Apr 24, 2019

Hey @prestonvanloon, this usage of the libp2p API is not correct, that's why you're observing weird effects. The job of the connection manager is to keep the connection count in check to avoid killing commercial routers.

What you want to do is attach your notifiee directly to the host's network:

host.Network().Notify(...)

You can:

  1. pass in a custom struct that implements the inet.Notifiee interface, or
  2. use the convenience struct NotifyBundle; it wrongly resides in go-libp2p-connmgr right now, but is being hoisted to go-libp2p-core soon as part of Consolidate libp2p abstractions and core types libp2p/go-libp2p#602.

Once you do that, here's the key: you want to open the stream via the host itself (host.NewStream(peer, protocol)), not through the conn.

The conn doesn't know about multistream negotiation, and will open a raw stream for you but will not negotiate the protocol. The call to SetProtocol(string) won't kick off that negotiation either, that's actually an internal API that has, unfortunately, leaked to the user API.

So the result is that you have a raw stream with no protocol pinned on it on the wire, therefore the other end doesn't notify the stream handler for that protocol.

I'm sorry our APIs are a bit messy here. I'm taking this issue as an experience report and once we settle the core refactor, we'll embark on a mission to make our APIs more cohesive and intuitive.

ConnectedF: func(net inet.Network, conn inet.Conn) {
log.Debug("Checking connection to peer")

s, err := h.NewStream(context.Background(), conn.RemotePeer(), handshakeProtocol)
Copy link
Member Author

Choose a reason for hiding this comment

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

@raulk, this is blocking / never opens the stream. Am I missing something?

Copy link

Choose a reason for hiding this comment

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

Could you try to create the stream in a goroutine? "never block the callback" is the problem I think you're seeing here.

Copy link
Member Author

Choose a reason for hiding this comment

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

That works, thanks!

@prestonvanloon
Copy link
Member Author

Thanks for the feedback @raulk. I'm still unable to create a stream between the peers. I'm seeing that host.NewStream is blocking indefinitely on both sides.

@prestonvanloon prestonvanloon marked this pull request as ready for review April 27, 2019 16:02
@prestonvanloon prestonvanloon added the Ready For Review A pull request ready for code review label Apr 27, 2019
Co-Authored-By: prestonvanloon <preston@prysmaticlabs.com>
@rauljordan rauljordan merged commit 210edfc into master Apr 27, 2019
@rauljordan rauljordan deleted the handshake branch April 27, 2019 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants