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 2 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 duplicated duplicated Nonce. Usually it occurs when a new remote connection is established, which checks its counterpart's Nonce value. <br/>
/// If it is equal to the Nonce of other RemoteNode, we just return true, else we'll return false 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 CheckDuplicateNonce(IActorRef remoteActor, RemoteNode remoteNode)
{
var version = remoteNode.Version;
var remote = remoteNode.Remote;

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

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 true;
}
}
if (remote.Port != remoteNode.ListenerTcpPort && remoteNode.ListenerTcpPort != 0)
{
ConnectedPeers.TryUpdate(remoteActor, remoteNode.Listener, remote);
}
return false;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
{
return RemoteNodes.Values;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private void OnVersionMessageReceived(VersionPayload payload)
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 (LocalNode.Singleton.CheckDuplicateNonce(Self, this))
{
Disconnect(true);
return;
Expand Down