Exemple #1
0
        private IEthashDataSet GetOrAddCache(uint epoch, bool precompute = true)
        {
            IEthashDataSet dataSet = _cacheCache.Get(epoch);

            if (dataSet == null)
            {
                uint   cacheSize = GetCacheSize(epoch);
                Keccak seed      = GetSeedHash(epoch);
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Building cache for epoch {epoch}");
                }
                _cacheStopwatch.Restart();
                dataSet = new EthashCache(cacheSize, seed.Bytes);
                _cacheStopwatch.Stop();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Cache for epoch {epoch} built in {_cacheStopwatch.ElapsedMilliseconds}ms");
                }
                _cacheCache.Set(epoch, dataSet);
            }

            uint epochToPrecompute = epoch + 1;

            if (precompute && _epochsRequested.TryAdd(epochToPrecompute, null))
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Asking to precompute epoch {epochToPrecompute}");
                }
                PrecomputeCache(epochToPrecompute).ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Precompute failure at epoch {epochToPrecompute}");
                        }
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Epoch precompute success at {epochToPrecompute}");
                        }
                    }
                });
            }

            return(dataSet);
        }
Exemple #2
0
 private Task <IEthashDataSet> BuildCache(uint epoch)
 {
     return(Task.Run(() =>
     {
         uint cacheSize = GetCacheSize(epoch);
         Keccak seed = GetSeedHash(epoch);
         if (_logger.IsInfo)
         {
             _logger.Info($"Building ethash cache for epoch {epoch}");
         }
         _cacheStopwatch.Restart();
         IEthashDataSet dataSet = new EthashCache(cacheSize, seed.Bytes);
         _cacheStopwatch.Stop();
         if (_logger.IsInfo)
         {
             _logger.Info($"Cache for epoch {epoch} with size {cacheSize} nd seed {seed.Bytes.ToHexString()} built in {_cacheStopwatch.ElapsedMilliseconds}ms");
         }
         return dataSet;
     }));
 }
Exemple #3
0
        // TODO: in a separate thread
        private IEthashDataSet GetOrAddCache(uint epoch)
        {
            IEthashDataSet dataSet = _cacheCache.Get(epoch);

            if (dataSet == null)
            {
                uint   cacheSize = GetCacheSize(epoch);
                Keccak seed      = GetSeedHash(epoch);
                if (_logger.IsInfo)
                {
                    _logger.Info($"Building cache for epoch {epoch}");
                }
                _cacheStopwatch.Restart();
                dataSet = new EthashCache(cacheSize, seed.Bytes);
                _cacheStopwatch.Stop();
                if (_logger.IsInfo)
                {
                    _logger.Info($"Cache for epoch {epoch} built in {_cacheStopwatch.ElapsedMilliseconds}ms");
                }
                _cacheCache.Set(epoch, dataSet);
            }

            return(dataSet);
        }