From a2199626242da2113b1f4f75610bede17176742a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 18 Oct 2019 03:39:04 -0300 Subject: [PATCH 01/10] Adding random hashes for OnGetDataMessageReceived --- neo/Network/P2P/ProtocolHandler.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/Network/P2P/ProtocolHandler.cs b/neo/Network/P2P/ProtocolHandler.cs index aa7f0e06f2..59ddbc424b 100644 --- a/neo/Network/P2P/ProtocolHandler.cs +++ b/neo/Network/P2P/ProtocolHandler.cs @@ -178,7 +178,8 @@ private void OnGetBlocksMessageReceived(GetBlocksPayload payload) private void OnGetDataMessageReceived(InvPayload payload) { UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray(); - foreach (UInt256 hash in hashes) + var randomHashes = hashes.OrderBy(x => random.Next()).ToArray(); + foreach (UInt256 hash in randomHashes) { switch (payload.Type) { From 9efa148a106994cb72c81100430284de7a274674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 18 Oct 2019 03:40:08 -0300 Subject: [PATCH 02/10] Adding static readonly Random --- neo/Network/P2P/ProtocolHandler.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/neo/Network/P2P/ProtocolHandler.cs b/neo/Network/P2P/ProtocolHandler.cs index 59ddbc424b..7f04b42311 100644 --- a/neo/Network/P2P/ProtocolHandler.cs +++ b/neo/Network/P2P/ProtocolHandler.cs @@ -27,6 +27,8 @@ public class SetFilter { public BloomFilter Filter; } private bool verack = false; private BloomFilter bloom_filter; + private static readonly Random random = new Random(); + public ProtocolHandler(NeoSystem system) { this.system = system; From bad8dbef898e0fd04ea188f1ac4dde8ea423b0e2 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 18 Oct 2019 08:13:42 -0300 Subject: [PATCH 03/10] Renaming consensus RestartTasks to ConsensusTxsTask --- neo/Consensus/ConsensusService.cs | 2 +- neo/Network/P2P/TaskManager.cs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index a65916b5aa..6301cf643c 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -462,7 +462,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.Transactions.Count < context.TransactionHashes.Length) { UInt256[] hashes = context.TransactionHashes.Where(i => !context.Transactions.ContainsKey(i)).ToArray(); - taskManager.Tell(new TaskManager.RestartTasks + taskManager.Tell(new TaskManager.ConsensusTxsTask { Payload = InvPayload.Create(InventoryType.TX, hashes) }); diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 2c0f7ae07b..e8e226396f 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -17,7 +17,7 @@ public class Register { public VersionPayload Version; } public class NewTasks { public InvPayload Payload; } public class TaskCompleted { public UInt256 Hash; } public class HeaderTaskCompleted { } - public class RestartTasks { public InvPayload Payload; } + public class ConsensusTxsTask { public InvPayload Payload; } private class Timer { } private static readonly TimeSpan TimerInterval = TimeSpan.FromSeconds(30); @@ -95,8 +95,8 @@ protected override void OnReceive(object message) case HeaderTaskCompleted _: OnHeaderTaskCompleted(); break; - case RestartTasks restart: - OnRestartTasks(restart.Payload); + case ConsensusTxsTask restart: + OnConsensusTxsTasks(restart.Payload); break; case Timer _: OnTimer(); @@ -115,7 +115,7 @@ private void OnRegister(VersionPayload version) RequestTasks(session); } - private void OnRestartTasks(InvPayload payload) + private void OnConsensusTxsTasks(InvPayload payload) { knownHashes.ExceptWith(payload.Hashes); foreach (UInt256 hash in payload.Hashes) @@ -256,7 +256,7 @@ internal protected override bool IsHighPriority(object message) switch (message) { case TaskManager.Register _: - case TaskManager.RestartTasks _: + case TaskManager.ConsensusTxsTask _: return true; case TaskManager.NewTasks tasks: if (tasks.Payload.Type == InventoryType.Block || tasks.Payload.Type == InventoryType.Consensus) From 8c5300287b09388d263bd3e762470c8b32ef10a1 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 18 Oct 2019 08:14:09 -0300 Subject: [PATCH 04/10] Minor s removal --- neo/Network/P2P/ProtocolHandler.cs | 2 +- neo/Network/P2P/TaskManager.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/neo/Network/P2P/ProtocolHandler.cs b/neo/Network/P2P/ProtocolHandler.cs index 7f04b42311..d7bb94baf4 100644 --- a/neo/Network/P2P/ProtocolHandler.cs +++ b/neo/Network/P2P/ProtocolHandler.cs @@ -28,7 +28,7 @@ public class SetFilter { public BloomFilter Filter; } private BloomFilter bloom_filter; private static readonly Random random = new Random(); - + public ProtocolHandler(NeoSystem system) { this.system = system; diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index e8e226396f..f0c9526357 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -96,7 +96,7 @@ protected override void OnReceive(object message) OnHeaderTaskCompleted(); break; case ConsensusTxsTask restart: - OnConsensusTxsTasks(restart.Payload); + OnConsensusTxsTask(restart.Payload); break; case Timer _: OnTimer(); @@ -115,7 +115,7 @@ private void OnRegister(VersionPayload version) RequestTasks(session); } - private void OnConsensusTxsTasks(InvPayload payload) + private void OnConsensusTxsTask(InvPayload payload) { knownHashes.ExceptWith(payload.Hashes); foreach (UInt256 hash in payload.Hashes) From 0ddc2c4c1fa028b064f8e76c496d2dc6c5e1ba70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 18 Oct 2019 08:18:13 -0300 Subject: [PATCH 05/10] removing wrong modification on protocolhandler --- neo/Network/P2P/ProtocolHandler.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/neo/Network/P2P/ProtocolHandler.cs b/neo/Network/P2P/ProtocolHandler.cs index d7bb94baf4..aa7f0e06f2 100644 --- a/neo/Network/P2P/ProtocolHandler.cs +++ b/neo/Network/P2P/ProtocolHandler.cs @@ -27,8 +27,6 @@ public class SetFilter { public BloomFilter Filter; } private bool verack = false; private BloomFilter bloom_filter; - private static readonly Random random = new Random(); - public ProtocolHandler(NeoSystem system) { this.system = system; @@ -180,8 +178,7 @@ private void OnGetBlocksMessageReceived(GetBlocksPayload payload) private void OnGetDataMessageReceived(InvPayload payload) { UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray(); - var randomHashes = hashes.OrderBy(x => random.Next()).ToArray(); - foreach (UInt256 hash in randomHashes) + foreach (UInt256 hash in hashes) { switch (payload.Type) { From ceeac0be3efb201c2ba944299e306539e4e9a0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 18 Oct 2019 08:19:21 -0300 Subject: [PATCH 06/10] Renaming restart to consensusTask --- neo/Network/P2P/TaskManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index f0c9526357..c8c97f527d 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -95,8 +95,8 @@ protected override void OnReceive(object message) case HeaderTaskCompleted _: OnHeaderTaskCompleted(); break; - case ConsensusTxsTask restart: - OnConsensusTxsTask(restart.Payload); + case ConsensusTxsTask consensusTask: + OnConsensusTxsTask(consensusTask.Payload); break; case Timer _: OnTimer(); From 52a6444defceeb99dc5a41f62360f5907f3962c9 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 18 Oct 2019 08:29:26 -0300 Subject: [PATCH 07/10] Fixing UT for protocol handler --- neo.UnitTests/Network/P2P/UT_TaskManagerMailbox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo.UnitTests/Network/P2P/UT_TaskManagerMailbox.cs b/neo.UnitTests/Network/P2P/UT_TaskManagerMailbox.cs index fca280d1eb..860166ff86 100644 --- a/neo.UnitTests/Network/P2P/UT_TaskManagerMailbox.cs +++ b/neo.UnitTests/Network/P2P/UT_TaskManagerMailbox.cs @@ -34,7 +34,7 @@ public void TaskManager_Test_IsHighPriority() { // high priority uut.IsHighPriority(new TaskManager.Register()).Should().Be(true); - uut.IsHighPriority(new TaskManager.RestartTasks()).Should().Be(true); + uut.IsHighPriority(new TaskManager.ConsensusTxsTask()).Should().Be(true); // low priority // -> NewTasks: generic InvPayload From 4d5d214bde9b28a5522bbb579a64867f43926a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 18 Oct 2019 10:15:41 -0300 Subject: [PATCH 08/10] Adding comment --- neo/Network/P2P/TaskManager.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index c8c97f527d..a303dd617c 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -115,6 +115,10 @@ private void OnRegister(VersionPayload version) RequestTasks(session); } + /// + /// Receives the InvPayload of ConsensusService actors and cleans txs hashes in order to ensure new requests + /// + /// An InvPayload payload that contains transactions that are missing in order to check a Block proposed by current Speaker private void OnConsensusTxsTask(InvPayload payload) { knownHashes.ExceptWith(payload.Hashes); From 573d145c670128a02e3c5daebbec6b614643f7f7 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 6 Nov 2019 10:29:46 -0300 Subject: [PATCH 09/10] Renaming AvailableTaks to InvBlockPendingTasks --- neo/Network/P2P/TaskManager.cs | 14 +++++++------- neo/Network/P2P/TaskSession.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 9c47e07cd9..4b649084d5 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -60,7 +60,7 @@ private void OnNewTasks(InvPayload payload) HashSet hashes = new HashSet(payload.Hashes); hashes.Remove(knownHashes); if (payload.Type == InventoryType.Block) - session.AvailableTasks.UnionWith(hashes.Where(p => globalTasks.ContainsKey(p))); + session.InvBlockPendingTasks.UnionWith(hashes.Where(p => globalTasks.ContainsKey(p))); hashes.Remove(globalTasks); if (hashes.Count == 0) @@ -133,7 +133,7 @@ private void OnTaskCompleted(UInt256 hash) knownHashes.Add(hash); globalTasks.Remove(hash); foreach (TaskSession ms in sessions.Values) - ms.AvailableTasks.Remove(hash); + ms.InvBlockPendingTasks.Remove(hash); if (sessions.TryGetValue(Sender, out TaskSession session)) { session.Tasks.Remove(hash); @@ -205,11 +205,11 @@ public static Props Props(NeoSystem system) private void RequestTasks(TaskSession session) { if (session.HasTask) return; - if (session.AvailableTasks.Count > 0) + if (session.InvBlockPendingTasks.Count > 0) { - session.AvailableTasks.Remove(knownHashes); - session.AvailableTasks.RemoveWhere(p => Blockchain.Singleton.ContainsBlock(p)); - HashSet hashes = new HashSet(session.AvailableTasks); + session.InvBlockPendingTasks.Remove(knownHashes); + session.InvBlockPendingTasks.RemoveWhere(p => Blockchain.Singleton.ContainsBlock(p)); + HashSet hashes = new HashSet(session.InvBlockPendingTasks); if (hashes.Count > 0) { foreach (UInt256 hash in hashes.ToArray()) @@ -217,7 +217,7 @@ private void RequestTasks(TaskSession session) if (!IncrementGlobalTask(hash)) hashes.Remove(hash); } - session.AvailableTasks.Remove(hashes); + session.InvBlockPendingTasks.Remove(hashes); foreach (UInt256 hash in hashes) session.Tasks[hash] = DateTime.UtcNow; foreach (InvPayload group in InvPayload.CreateGroup(InventoryType.Block, hashes.ToArray())) diff --git a/neo/Network/P2P/TaskSession.cs b/neo/Network/P2P/TaskSession.cs index a1cf8a0ffc..3d351f0a79 100644 --- a/neo/Network/P2P/TaskSession.cs +++ b/neo/Network/P2P/TaskSession.cs @@ -12,7 +12,7 @@ internal class TaskSession public readonly IActorRef RemoteNode; public readonly VersionPayload Version; public readonly Dictionary Tasks = new Dictionary(); - public readonly HashSet AvailableTasks = new HashSet(); + public readonly HashSet InvBlockPendingTasks = new HashSet(); public bool HasTask => Tasks.Count > 0; public uint StartHeight { get; } From d823d219747934c919f2a634867c6d54fa7993ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 8 Nov 2019 08:09:01 -0300 Subject: [PATCH 10/10] Adding Summary to InvBlockPendingTasks --- neo/Network/P2P/TaskSession.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/neo/Network/P2P/TaskSession.cs b/neo/Network/P2P/TaskSession.cs index 3d351f0a79..7f79290906 100644 --- a/neo/Network/P2P/TaskSession.cs +++ b/neo/Network/P2P/TaskSession.cs @@ -12,6 +12,9 @@ internal class TaskSession public readonly IActorRef RemoteNode; public readonly VersionPayload Version; public readonly Dictionary Tasks = new Dictionary(); + /// + /// Pending high priority tasks of type `InventoryType.Block` + /// public readonly HashSet InvBlockPendingTasks = new HashSet(); public bool HasTask => Tasks.Count > 0;