private EthereumBlockTemplate AssembleBlockTemplate(Block block, string[] work)
        {
            if (work.Length < 4)
            {
                logger.Error(() => $"[{LogCat}] Error(s) refreshing blocktemplate: getWork did not return blockheight. Are you really connected to a Parity daemon?");
                return(null);
            }


            var height = work[3].IntegralFromHex <ulong>();

            if (height != block.Height)
            {
                logger.Debug(() => $"[{LogCat}] Discarding block template update as getWork result is not related to pending block");
                return(null);
            }

            var result = new EthereumBlockTemplate
            {
                Header     = work[0],
                Seed       = work[1],
                Target     = work[2],
                Difficulty = block.Difficulty.IntegralFromHex <ulong>(),
                Height     = block.Height.Value,
                ParentHash = block.ParentHash,
            };

            return(result);
        }
        protected bool UpdateJob(EthereumBlockTemplate blockTemplate)
        {
            logger.LogInvoke(LogCat);

            try
            {
                if (blockTemplate == null || blockTemplate.Header?.Length == 0)
                {
                    return(false);
                }

                var job   = currentJob;
                var isNew = currentJob == null || job.BlockTemplate.Header != blockTemplate.Header;

                if (isNew)
                {
                    var jobId = NextJobId("x8");


                    job = new EthereumJob(jobId, blockTemplate, logger);

                    lock (jobLock)
                    {
                        validJobs[jobId] = job;


                        var obsoleteKeys = validJobs.Keys
                                           .Where(key => validJobs[key].BlockTemplate.Height < job.BlockTemplate.Height - MaxBlockBacklog).ToArray();

                        foreach (var key in obsoleteKeys)
                        {
                            validJobs.Remove(key);
                        }
                    }

                    currentJob = job;


                    BlockchainStats.LastNetworkBlockTime = clock.Now;
                    BlockchainStats.BlockHeight          = (long)job.BlockTemplate.Height;
                    BlockchainStats.NetworkDifficulty    = job.BlockTemplate.Difficulty;
                }

                return(isNew);
            }

            catch (Exception ex)
            {
                logger.Error(ex, () => $"[{LogCat}] Error during {nameof(UpdateJob)}");
            }

            return(false);
        }
Example #3
0
        public EthereumJob(string id, EthereumBlockTemplate blockTemplate, ILogger logger)
        {
            Id            = id;
            BlockTemplate = blockTemplate;
            this.logger   = logger;

            var target = blockTemplate.Target;

            if (target.StartsWith("0x"))
            {
                target = target.Substring(2);
            }

            blockTarget = new uint256(target.HexToByteArray().ReverseArray());
        }