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

Fix duplicate connection #1685

Merged
merged 10 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ internal static IPEndPoint GetIpEndPoint(string hostAndPort)
return null;
}

/// <summary>
/// Check the new connection <br/>
/// If it is equal to the Nonce of local or any remote node, it'll return false, else we'll return true and update the Listener address of the connected remote node.
/// </summary>
/// <param name="remoteActor">Remote node actor</param>
/// <param name="remoteNode">Remote node</param>
public bool AllowNewConnection(IActorRef remoteActor, RemoteNode remoteNode)
{
var version = remoteNode.Version;
var remote = remoteNode.Remote;

if (remote is null) return true;
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (version.Nonce == Nonce) return false;

foreach (var pair in RemoteNodes)
{
var otherNode = pair.Value;
if (otherNode != remoteNode && otherNode.Remote.Address.Equals(remote.Address) && otherNode.Version?.Nonce == version.Nonce)
{// filter duplicate connections
return false;
}
}
if (remote.Port != remoteNode.ListenerTcpPort && remoteNode.ListenerTcpPort != 0)
{
ConnectedPeers.TryUpdate(remoteActor, remoteNode.Listener, remote);
}
return true;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
{
return RemoteNodes.Values;
Expand Down
7 changes: 1 addition & 6 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,7 @@ private void OnVersionMessageReceived(VersionPayload payload)
break;
}
}
if (payload.Nonce == LocalNode.Nonce || payload.Magic != ProtocolSettings.Default.Magic)
{
Disconnect(true);
return;
}
if (LocalNode.Singleton.RemoteNodes.Values.Where(p => p != this).Any(p => p.Remote.Address.Equals(Remote.Address) && p.Version?.Nonce == payload.Nonce))
if (payload.Magic != ProtocolSettings.Default.Magic || !LocalNode.Singleton.AllowNewConnection(Self, this))
{
Disconnect(true);
return;
Expand Down