Пример #1
0
        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);
            }
        }
Пример #2
0
 public void Dispose()
 {
     if (handle != IntPtr.Zero)
     {
         LibMultihash.ethash_light_delete(handle);
         handle = IntPtr.Zero;
     }
 }
Пример #3
0
        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();
                    }
                });
            }
        }
Пример #4
0
        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);
                            }
                        }
                    }
                }
            });
        }
Пример #5
0
        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);
                            }
                        }
                    }
                }
            });
        }