Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
88 changes: 41 additions & 47 deletions neo.UnitTests/UT_ProtocolHandler.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using Akka.TestKit.Xunit2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P;
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ProtocolHandler : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism
[TestClass]
public class UT_ProtocolHandler : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

private NeoSystem testBlockchain;
private NeoSystem testBlockchain;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}
[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}
[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
var config = TestKit.DefaultConfig;
var akkaSettings = new Akka.Actor.Settings(system, config);
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void ProtocolHandler_Test_SendVersion_TellParent()
{
var senderProbe = CreateTestProbe();
var parent = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(()=> new ProtocolHandler(testBlockchain, parent));

var payload = new VersionPayload()
{
UserAgent = "".PadLeft(1024, '0'),
Nonce = 1,
Magic = 2,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};
[TestMethod]
public void ProtocolHandler_Test_SendVersion_TellParent()
{
var senderProbe = CreateTestProbe();
var parent = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain, parent));

senderProbe.Send(protocolActor, Message.Create(MessageCommand.Version, payload));
parent.ExpectMsg<VersionPayload>();
}
}
var payload = new VersionPayload()
{
UserAgent = "".PadLeft(1024, '0'),
Nonce = 1,
Magic = 2,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};


senderProbe.Send(protocolActor, Message.Create(MessageCommand.Version, payload));
parent.ExpectMsg<VersionPayload>();
}
}
}
214 changes: 105 additions & 109 deletions neo.UnitTests/UT_RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,111 @@
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;
using System;
using System.Collections.Generic;
using System.Text;

namespace Neo.UnitTests
{
[TestClass]
public class UT_RemoteNode : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

private NeoSystem testBlockchain;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void RemoteNode_Test_Abort_DifferentMagic()
{
var testProbe = CreateTestProbe();
var connectionTestProbe = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain));
var protocolFactory = new TestProtocolFactory(protocolActor);
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "".PadLeft(1024, '0'),
Nonce = 1,
Magic = 2,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};

testProbe.Send(remoteNodeActor, payload);

connectionTestProbe.ExpectMsg<Tcp.Abort>();
}

[TestMethod]
public void RemoteNode_Test_Accept_IfSameMagic()
{
var testProbe = CreateTestProbe();
var connectionTestProbe = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain));
var protocolFactory = new TestProtocolFactory(protocolActor);
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "Unit Test".PadLeft(1024, '0'),
Nonce = 1,
Magic = ProtocolSettings.Default.Magic,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};

testProbe.Send(remoteNodeActor, payload);

var verackMessage = connectionTestProbe.ExpectMsg<Tcp.Write>();

//Verack
verackMessage.Data.Count.Should().Be(3);
}
}

internal class TestProtocolFactory : IActorRefFactory
{
private IActorRef protocolRef;

public TestProtocolFactory(IActorRef protocolRef)
{
this.protocolRef = protocolRef;
}

public IActorRef ActorOf(Props props, string name = null)
{
return protocolRef;
}

public ActorSelection ActorSelection(ActorPath actorPath)
{
throw new NotImplementedException();
}

public ActorSelection ActorSelection(string actorPath)
{
throw new NotImplementedException();
}
}
}
[TestClass]
public class UT_RemoteNode : TestKit
{
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

private NeoSystem testBlockchain;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
Akka.Actor.ActorSystem system = Sys;
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void RemoteNode_Test_Abort_DifferentMagic()
{
var testProbe = CreateTestProbe();
var connectionTestProbe = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain));
var protocolFactory = new TestProtocolFactory(protocolActor);
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "".PadLeft(1024, '0'),
Nonce = 1,
Magic = 2,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};

testProbe.Send(remoteNodeActor, payload);
connectionTestProbe.ExpectMsg<Tcp.Abort>();
}

[TestMethod]
public void RemoteNode_Test_Accept_IfSameMagic()
{
var testProbe = CreateTestProbe();
var connectionTestProbe = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain));
var protocolFactory = new TestProtocolFactory(protocolActor);
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory));

connectionTestProbe.ExpectMsg<Tcp.Write>();

var payload = new VersionPayload()
{
UserAgent = "Unit Test".PadLeft(1024, '0'),
Nonce = 1,
Magic = ProtocolSettings.Default.Magic,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
};

testProbe.Send(remoteNodeActor, payload);
var verackMessage = connectionTestProbe.ExpectMsg<Tcp.Write>();

//Verack
verackMessage.Data.Count.Should().Be(3);
}
}

internal class TestProtocolFactory : IActorRefFactory
{
private IActorRef protocolRef;

public TestProtocolFactory(IActorRef protocolRef)
{
this.protocolRef = protocolRef;
}

public IActorRef ActorOf(Props props, string name = null)
{
return protocolRef;
}

public ActorSelection ActorSelection(ActorPath actorPath)
{
throw new NotImplementedException();
}

public ActorSelection ActorSelection(string actorPath)
{
throw new NotImplementedException();
}
}
}
28 changes: 14 additions & 14 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ public class SetFilter { public BloomFilter Filter; }
private VersionPayload version;
private bool verack = false;
private BloomFilter bloom_filter;
private IActorRef parent;
private IActorRef parent;



public ProtocolHandler(NeoSystem system)
{
this.system = system;
this.knownHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.sentHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.parent = Context.Parent;
}

public ProtocolHandler(NeoSystem system, IActorRef parent) : this(system)
{
this.parent = parent;
}
public ProtocolHandler(NeoSystem system)
{
this.system = system;
this.knownHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.sentHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
this.parent = Context.Parent;
}

public ProtocolHandler(NeoSystem system, IActorRef parent) : this(system)
{
this.parent = parent;
}

protected override void OnReceive(object message)
protected override void OnReceive(object message)
{
if (!(message is Message msg)) return;
foreach (IP2PPlugin plugin in Plugin.P2PPlugins)
Expand Down
Loading