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 akka warning #1533

Merged
merged 12 commits into from
Apr 10, 2020
6 changes: 5 additions & 1 deletion src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Akka.Actor;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using System;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -214,6 +213,11 @@ private void OnRelayDirectly(IInventory inventory)

private void OnSendDirectly(IInventory inventory) => SendToRemoteNodes(inventory);

protected override void OnTcpConnected(IActorRef connection)
{
connection.Tell(new RemoteNode.SendVersion());
}

public static Props Props(NeoSystem system)
{
return Akka.Actor.Props.Create(() => new LocalNode(system));
Expand Down
5 changes: 5 additions & 0 deletions src/neo/Network/P2P/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,14 @@ private void OnTcpConnected(IPEndPoint remote, IPEndPoint local)
Context.Watch(connection);
Sender.Tell(new Tcp.Register(connection));
ConnectedPeers.TryAdd(connection, remote);
OnTcpConnected(connection);
}
}

protected virtual void OnTcpConnected(IActorRef connection)
{
}

/// <summary>
/// Will be triggered when a Tcp.CommandFailed message is received.
/// If it's a Tcp.Connect command, remove the related endpoint from ConnectingPeers.
Expand Down
27 changes: 17 additions & 10 deletions src/neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Neo.Network.P2P
public class RemoteNode : Connection
{
internal class Relay { public IInventory Inventory; }
internal class SendVersion { }

private readonly NeoSystem system;
private readonly IActorRef protocol;
Expand All @@ -38,16 +39,6 @@ public RemoteNode(NeoSystem system, object connection, IPEndPoint remote, IPEndP
this.system = system;
this.protocol = Context.ActorOf(ProtocolHandler.Props(system));
LocalNode.Singleton.RemoteNodes.TryAdd(Self, this);

var capabilities = new List<NodeCapability>
{
new FullNodeCapability(Blockchain.Singleton.Height)
};

if (LocalNode.Singleton.ListenerTcpPort > 0) capabilities.Add(new ServerCapability(NodeCapabilityType.TcpServer, (ushort)LocalNode.Singleton.ListenerTcpPort));
if (LocalNode.Singleton.ListenerWsPort > 0) capabilities.Add(new ServerCapability(NodeCapabilityType.WsServer, (ushort)LocalNode.Singleton.ListenerWsPort));

SendMessage(Message.Create(MessageCommand.Version, VersionPayload.Create(LocalNode.Nonce, LocalNode.UserAgent, capabilities.ToArray())));
}

/// <summary>
Expand Down Expand Up @@ -153,9 +144,25 @@ protected override void OnReceive(object message)
case PingPayload payload:
OnPingPayload(payload);
break;
case SendVersion _:
OnSendVersion();
break;
}
}

private void OnSendVersion()
{
var capabilities = new List<NodeCapability>
{
new FullNodeCapability(Blockchain.Singleton.Height)
};

if (LocalNode.Singleton.ListenerTcpPort > 0) capabilities.Add(new ServerCapability(NodeCapabilityType.TcpServer, (ushort)LocalNode.Singleton.ListenerTcpPort));
if (LocalNode.Singleton.ListenerWsPort > 0) capabilities.Add(new ServerCapability(NodeCapabilityType.WsServer, (ushort)LocalNode.Singleton.ListenerWsPort));

SendMessage(Message.Create(MessageCommand.Version, VersionPayload.Create(LocalNode.Nonce, LocalNode.UserAgent, capabilities.ToArray())));
}
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

private void OnPingPayload(PingPayload payload)
{
if (payload.LastBlockIndex > LastBlockIndex)
Expand Down
4 changes: 0 additions & 4 deletions tests/neo.UnitTests/Network/P2P/UT_RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public void RemoteNode_Test_Abort_DifferentMagic()
var connectionTestProbe = CreateTestProbe();
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "".PadLeft(1024, '0'),
Expand All @@ -58,8 +56,6 @@ public void RemoteNode_Test_Accept_IfSameMagic()
var connectionTestProbe = CreateTestProbe();
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "Unit Test".PadLeft(1024, '0'),
Expand Down