diff --git a/neo/IO/Caching/FIFOSet.cs b/neo/IO/Caching/FIFOSet.cs index ae02039825..6dd52dbccf 100644 --- a/neo/IO/Caching/FIFOSet.cs +++ b/neo/IO/Caching/FIFOSet.cs @@ -1,9 +1,12 @@ using System; +using System.Collections; +using System.Collections.Generic; using System.Collections.Specialized; +using System.Linq; namespace Neo.IO.Caching { - internal class FIFOSet where T : IEquatable + internal class FIFOSet : IEnumerable where T : IEquatable { private readonly int maxCapacity; private readonly int removeCount; @@ -37,5 +40,21 @@ public bool Add(T item) dictionary.Add(item, null); return true; } + + public void ExceptWith(IEnumerable hashes) + { + foreach (var hash in hashes) + { + dictionary.Remove(hash); + } + } + + public IEnumerator GetEnumerator() + { + var entries = dictionary.Values.Cast().ToArray(); + foreach (var entry in entries) yield return entry; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 9e028f7967..56f4e12765 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -1,6 +1,7 @@ using Akka.Actor; using Akka.Configuration; using Neo.IO.Actors; +using Neo.IO.Caching; using Neo.Ledger; using Neo.Network.P2P.Payloads; using System; @@ -24,7 +25,13 @@ private class Timer { } private readonly NeoSystem system; private const int MaxConncurrentTasks = 3; - private readonly HashSet knownHashes = new HashSet(); + + /// + /// The limit `Blockchain.Singleton.MemPool.Capacity * 2` was the same value used in ProtocolHandler + /// + private static readonly int MaxCachedHashes = Blockchain.Singleton.MemPool.Capacity * 2; + private readonly FIFOSet knownHashes = new FIFOSet(MaxCachedHashes); + private readonly Dictionary globalTasks = new Dictionary(); private readonly Dictionary sessions = new Dictionary(); private readonly ICancelable timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimerInterval, TimerInterval, Context.Self, new Timer(), ActorRefs.NoSender);