public async ValueTask GenerateAsync(string dagDir, ILogger logger, CancellationToken ct) { Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(dagDir), $"{nameof(dagDir)} must not be empty"); if (handle == IntPtr.Zero) { await Task.Run(() => { try { sem.WaitOne(); // re-check after obtaining lock if (handle != IntPtr.Zero) { return; } logger.Info(() => $"Generating DAG for epoch {Epoch}"); var started = DateTime.Now; var block = Epoch * EthereumConstants.EpochLength; // Generate a temporary cache var light = LibMultihash.ethash_light_new(block); try { // Generate the actual DAG handle = LibMultihash.ethash_full_new(dagDir, light, progress => { logger.Info(() => $"Generating DAG for epoch {Epoch}: {progress}%"); return(!ct.IsCancellationRequested ? 0 : 1); }); if (handle == IntPtr.Zero) { throw new OutOfMemoryException("ethash_full_new IO or memory error"); } logger.Info(() => $"Done generating DAG for epoch {Epoch} after {DateTime.Now - started}"); } finally { if (light != IntPtr.Zero) { LibMultihash.ethash_light_delete(light); } } } finally { sem.Release(); } }, ct); } }
public async Task GenerateAsync(string dagDir, ILogger logger) { Assertion.Requires <ArgumentException>(!string.IsNullOrEmpty(dagDir), $"{nameof(dagDir)} must not be empty"); if (handle == IntPtr.Zero) { await Task.Run(() => { try { sem.WaitOne(); if (handle != IntPtr.Zero) { return; } logger.Info(() => $"Generating DAG for epoch {Epoch}"); var started = DateTime.Now; var block = Epoch * EthereumConstants.EpochLength; var light = LibMultihash.ethash_light_new(block); try { handle = LibMultihash.ethash_full_new(dagDir, light, progress => { logger.Info(() => $"Generating DAG for epoch {Epoch}: {progress}%"); return(0); }); if (handle == IntPtr.Zero) { throw new OutOfMemoryException("ethash_full_new IO or memory error"); } logger.Info(() => $"Done generating DAG for epoch {Epoch} after {DateTime.Now - started}"); } finally { if (light != IntPtr.Zero) { LibMultihash.ethash_light_delete(light); } } } finally { sem.Release(); } }); } }
public async Task GenerateAsync(string dagDir) { Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(dagDir), $"{nameof(dagDir)} must not be empty"); await Task.Run(() => { lock (genLock) { if (!isGenerated) { logger.Info(() => $"Generating DAG for epoch {Epoch}"); var started = DateTime.Now; var block = Epoch * EthereumConstants.EpochLength; // Generate a temporary cache var light = LibMultihash.ethash_light_new(block); try { // Generate the actual DAG handle = LibMultihash.ethash_full_new(dagDir, light, progress => { logger.Info(() => $"Generating DAG: {progress}%"); return(0); }); if (handle == IntPtr.Zero) { throw new OutOfMemoryException("ethash_full_new IO or memory error"); } logger.Info(() => $"Done generating DAG for epoch {Epoch} after {DateTime.Now - started}"); isGenerated = true; } finally { if (light != IntPtr.Zero) { LibMultihash.ethash_light_delete(light); } } } } }); }
public async Task GenerateAsync() { await Task.Run(() => { lock (genLock) { if (!isGenerated) { logger.Debug(() => $"Generating DAG for epoch {Epoch}"); var started = DateTime.Now; var block = Epoch * EthereumConstants.EpochLength; // Generate a temporary cache var light = LibMultihash.ethash_light_new(block); try { // Generate the actual DAG handle = LibMultihash.ethash_full_new(light, progress => { logger.Debug(() => $"Generating DAG: {progress}%"); return(0); }); if (handle == IntPtr.Zero) { throw new OutOfMemoryException("ethash_full_new IO or memory error"); } logger.Debug(() => $"Done generating DAG for epoch {Epoch} after {DateTime.Now - started}"); isGenerated = true; } finally { if (light != IntPtr.Zero) { LibMultihash.ethash_light_delete(light); } } } } }); }
public async Task GenerateAsync() { await Task.Run(() => { lock (genLock) { if (!isGenerated) { var started = DateTime.Now; logger.Debug(() => $"Generating cache for epoch {Epoch}"); var block = Epoch * EthereumConstants.EpochLength; handle = LibMultihash.ethash_light_new(block); logger.Debug(() => $"Done generating cache for epoch {Epoch} after {DateTime.Now - started}"); isGenerated = true; } } }); }