public void Dispose() { currentDag?.Dispose(); currentDag = null; foreach (var value in caches.Values) { value.Dispose(); } }
public async Task <Dag> GetDagAsync(ulong block) { Dag result; var epoch = block / EthereumConstants.EpochLength; lock (dagLock) { if (currentDag != null && currentDag.Epoch == epoch) { result = currentDag; } else { result = new Dag(epoch); currentDag = result; } } await result.GenerateAsync(); return(result); }
public async Task <Dag> GetDagAsync(ulong block) { var epoch = block / EthereumConstants.EpochLength; Dag result; lock (cacheLock) { if (numCaches == 0) { numCaches = 3; } if (!caches.TryGetValue(epoch, out result)) { // No cached DAG, evict the oldest if the cache limit was reached while (caches.Count >= numCaches) { var toEvict = caches.Values.OrderBy(x => x.LastUsed).First(); var key = caches.First(pair => pair.Value == toEvict).Key; var epochToEvict = toEvict.Epoch; logger.Debug(() => $"Evicting DAG for epoch {epochToEvict} in favour of epoch {epoch}"); toEvict.Dispose(); caches.Remove(key); } // If we have the new DAG pre-generated, use that, otherwise create a new one if (future != null && future.Epoch == epoch) { logger.Debug(() => $"Using pre-generated DAG for epoch {epoch}"); result = future; future = null; } else { logger.Debug(() => $"No pre-generated DAG available, creating new for epoch {epoch}"); result = new Dag(epoch); } caches[epoch] = result; // If we just used up the future cache, or need a refresh, regenerate if ((future == null || future.Epoch <= epoch) && !noFutureDag) { logger.Debug(() => $"Pre-generating DAG for epoch {epoch + 1}"); future = new Dag(epoch + 1); #pragma warning disable 4014 future.GenerateAsync(dagDir); #pragma warning restore 4014 } } result.LastUsed = DateTime.Now; } await result.GenerateAsync(dagDir); return(result); }