From 3ad56f7293b1bf63778dac3e7a55307c776b6ff0 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Sun, 30 Jun 2019 23:39:13 -0300 Subject: [PATCH] CreateActor --- neo.UnitTests/UT_RemoteNode.cs | 6 ++++-- neo/Consensus/ConsensusService.cs | 2 +- neo/Ledger/Blockchain.cs | 2 +- neo/NeoSystem.cs | 22 ++++++++++++++++++---- neo/Network/P2P/LocalNode.cs | 4 ++-- neo/Network/P2P/Peer.cs | 8 +++++--- neo/Network/P2P/ProtocolHandler.cs | 2 +- neo/Network/P2P/RemoteNode.cs | 7 +++---- neo/Network/P2P/TaskManager.cs | 2 +- 9 files changed, 36 insertions(+), 19 deletions(-) diff --git a/neo.UnitTests/UT_RemoteNode.cs b/neo.UnitTests/UT_RemoteNode.cs index 3b89785142..57aaf1ed83 100644 --- a/neo.UnitTests/UT_RemoteNode.cs +++ b/neo.UnitTests/UT_RemoteNode.cs @@ -32,7 +32,8 @@ public void RemoteNode_Test_Abort_DifferentMagic() var testProbe = CreateTestProbe(); var connectionTestProbe = CreateTestProbe(); var protocolActor = ActorOfAsTestActorRef(() => new ProtocolHandler(testBlockchain)); - var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolActor)); + // TODO: setup 'testBlockchain' mock CreateActor to return desired 'protocolActor' (using ActorOfAsTestActorRef) + var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null)); connectionTestProbe.ExpectMsg(); @@ -60,7 +61,8 @@ public void RemoteNode_Test_Accept_IfSameMagic() var testProbe = CreateTestProbe(); var connectionTestProbe = CreateTestProbe(); var protocolActor = ActorOfAsTestActorRef(() => new ProtocolHandler(testBlockchain)); - var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolActor)); + // TODO: setup 'testBlockchain' mock CreateActor to return desired 'protocolActor' (using ActorOfAsTestActorRef) + var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null)); connectionTestProbe.ExpectMsg(); diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 459a9e055b..09f5de80b2 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -572,7 +572,7 @@ protected override void PostStop() public static Props Props(IActorRef localNode, IActorRef taskManager, Store store, Wallet wallet) { - return Akka.Actor.Props.Create(() => new ConsensusService(localNode, taskManager, store, wallet)).WithMailbox("consensus-service-mailbox"); + return Akka.Actor.Props.Create(() => new ConsensusService(localNode, taskManager, store, wallet)); } private void RequestChangeView() diff --git a/neo/Ledger/Blockchain.cs b/neo/Ledger/Blockchain.cs index 181421cc3f..d26b37d77b 100644 --- a/neo/Ledger/Blockchain.cs +++ b/neo/Ledger/Blockchain.cs @@ -495,7 +495,7 @@ protected override void PostStop() public static Props Props(NeoSystem system, Store store) { - return Akka.Actor.Props.Create(() => new Blockchain(system, store)).WithMailbox("blockchain-mailbox"); + return Akka.Actor.Props.Create(() => new Blockchain(system, store)); } private void SaveHeaderHashList(Snapshot snapshot = null) diff --git a/neo/NeoSystem.cs b/neo/NeoSystem.cs index f53d6efa76..c21271c2da 100644 --- a/neo/NeoSystem.cs +++ b/neo/NeoSystem.cs @@ -1,4 +1,6 @@ using Akka.Actor; +using Akka.Configuration; +using Akka.IO; using Neo.Consensus; using Neo.Ledger; using Neo.Network.P2P; @@ -8,6 +10,8 @@ using Neo.Wallets; using System; using System.Net; +using System.Linq; +using System.Linq.Expressions; namespace Neo { @@ -26,6 +30,16 @@ public class NeoSystem : IDisposable public IActorRef Consensus { get; private set; } public RpcServer RpcServer { get; private set; } + public IActorRef CreateActor(Props props, string mailbox = null, string actorName = null) + { + if(mailbox != null) + props = props.WithMailbox(mailbox); + if(actorName != null) + return this.ActorSystem.ActorOf(props, actorName); + else + return this.ActorSystem.ActorOf(props); + } + private readonly Store store; private ChannelsConfig start_message = null; private bool suspend = false; @@ -34,9 +48,9 @@ public NeoSystem(Store store) { this.store = store; Plugin.LoadPlugins(this); - this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this, store)); - this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this)); - this.TaskManager = ActorSystem.ActorOf(Network.P2P.TaskManager.Props(this)); + this.Blockchain = CreateActor(Ledger.Blockchain.Props(this, store), "blockchain-mailbox"); + this.LocalNode = CreateActor(Network.P2P.LocalNode.Props(this)); + this.TaskManager = CreateActor(Network.P2P.TaskManager.Props(this), "task-manager-mailbox"); Plugin.NotifyPluginsLoadedAfterSystemConstructed(); } @@ -69,7 +83,7 @@ internal void ResumeNodeStartup() public void StartConsensus(Wallet wallet, Store consensus_store = null, bool ignoreRecoveryLogs = false) { - Consensus = ActorSystem.ActorOf(ConsensusService.Props(this.LocalNode, this.TaskManager, consensus_store ?? store, wallet)); + Consensus = CreateActor(ConsensusService.Props(this.LocalNode, this.TaskManager, consensus_store ?? store, wallet), "consensus-service-mailbox"); Consensus.Tell(new ConsensusService.Start { IgnoreRecoveryLogs = ignoreRecoveryLogs }, Blockchain); } diff --git a/neo/Network/P2P/LocalNode.cs b/neo/Network/P2P/LocalNode.cs index 6f30ed50cb..48755e2bd0 100644 --- a/neo/Network/P2P/LocalNode.cs +++ b/neo/Network/P2P/LocalNode.cs @@ -178,9 +178,9 @@ public static Props Props(NeoSystem system) return Akka.Actor.Props.Create(() => new LocalNode(system)); } - protected override Props ProtocolProps(object connection, IPEndPoint remote, IPEndPoint local) + protected override IActorRef ProtocolActor(NeoSystem system, string actorName, object connection, IPEndPoint remote, IPEndPoint local) { - return RemoteNode.Props(system, connection, remote, local); + return system.CreateActor(RemoteNode.Props(system, connection, remote, local), "remote-node-mailbox", actorName); } } } diff --git a/neo/Network/P2P/Peer.cs b/neo/Network/P2P/Peer.cs index eb21095685..b348d15eb6 100644 --- a/neo/Network/P2P/Peer.cs +++ b/neo/Network/P2P/Peer.cs @@ -24,6 +24,8 @@ public class Connect { public IPEndPoint EndPoint; public bool IsTrusted = false private class Timer { } private class WsConnected { public WebSocket Socket; public IPEndPoint Remote; public IPEndPoint Local; } + public NeoSystem neoSystem; // TODO: initialize + public const int DefaultMinDesiredConnections = 10; public const int DefaultMaxConnections = DefaultMinDesiredConnections * 4; @@ -191,7 +193,7 @@ private void OnTcpConnected(IPEndPoint remote, IPEndPoint local) else { ConnectedAddresses[remote.Address] = count + 1; - IActorRef connection = Context.ActorOf(ProtocolProps(Sender, remote, local), $"connection_{Guid.NewGuid()}"); + IActorRef connection = ProtocolActor(neoSystem, $"connection_{Guid.NewGuid()}", Sender, remote, local); Context.Watch(connection); Sender.Tell(new Tcp.Register(connection)); ConnectedPeers.TryAdd(connection, remote); @@ -244,7 +246,7 @@ private void OnWsConnected(WebSocket ws, IPEndPoint remote, IPEndPoint local) else { ConnectedAddresses[remote.Address] = count + 1; - Context.ActorOf(ProtocolProps(ws, remote, local), $"connection_{Guid.NewGuid()}"); + ProtocolActor(neoSystem, $"connection_{Guid.NewGuid()}", ws, remote, local); } } @@ -268,6 +270,6 @@ private async Task ProcessWebSocketAsync(HttpContext context) }); } - protected abstract Props ProtocolProps(object connection, IPEndPoint remote, IPEndPoint local); + protected abstract IActorRef ProtocolActor(NeoSystem system, string actorName, object connection, IPEndPoint remote, IPEndPoint local); } } diff --git a/neo/Network/P2P/ProtocolHandler.cs b/neo/Network/P2P/ProtocolHandler.cs index 7f9821709b..27c75112f9 100644 --- a/neo/Network/P2P/ProtocolHandler.cs +++ b/neo/Network/P2P/ProtocolHandler.cs @@ -296,7 +296,7 @@ private void OnVersionMessageReceived(VersionPayload payload) public static Props Props(NeoSystem system) { - return Akka.Actor.Props.Create(() => new ProtocolHandler(system)).WithMailbox("protocol-handler-mailbox"); + return Akka.Actor.Props.Create(() => new ProtocolHandler(system)); } } diff --git a/neo/Network/P2P/RemoteNode.cs b/neo/Network/P2P/RemoteNode.cs index 32288ed9a4..936d57135c 100644 --- a/neo/Network/P2P/RemoteNode.cs +++ b/neo/Network/P2P/RemoteNode.cs @@ -32,11 +32,11 @@ internal class Relay { public IInventory Inventory; } public uint LastBlockIndex { get; private set; } = 0; public bool IsFullNode { get; private set; } = false; - public RemoteNode(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local, IActorRef protocolHandler) + public RemoteNode(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local) : base(connection, remote, local) { this.system = system; - this.protocol = protocolHandler; + this.protocol = system.CreateActor(ProtocolHandler.Props(system), "protocol-handler-mailbox"); LocalNode.Singleton.RemoteNodes.TryAdd(Self, this); var capabilities = new List @@ -224,8 +224,7 @@ protected override void PostStop() internal static Props Props(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local) { - var protocol = system.ActorSystem.ActorOf(ProtocolHandler.Props(system)); - return Akka.Actor.Props.Create(() => new RemoteNode(system, connection, remote, local, protocol)).WithMailbox("remote-node-mailbox"); + return Akka.Actor.Props.Create(() => new RemoteNode(system, connection, remote, local)); } private void SendMessage(Message message) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index cd96547df8..49b6cf8e6b 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -195,7 +195,7 @@ protected override void PostStop() public static Props Props(NeoSystem system) { - return Akka.Actor.Props.Create(() => new TaskManager(system)).WithMailbox("task-manager-mailbox"); + return Akka.Actor.Props.Create(() => new TaskManager(system)); } private void RequestTasks(TaskSession session)