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.HexToReverseByteArray());
        }
예제 #2
0
        private EthereumBlockTemplate AssembleBlockTemplate(string[] work)
        {
            if (work.Length < 4)
            {
                logger.Error(() => $"Error(s) refreshing blocktemplate: getWork did not return blockheight. Are you really connected to a Parity daemon?");
                return(null);
            }

            // extract values
            var height       = work[3].IntegralFromHex <ulong>();
            var targetString = work[2];
            var target       = BigInteger.Parse(targetString.Substring(2), NumberStyles.HexNumber);

            var result = new EthereumBlockTemplate
            {
                Header     = work[0],
                Seed       = work[1],
                Target     = targetString,
                Difficulty = (ulong)BigInteger.Divide(EthereumConstants.BigMaxValue, target),
                Height     = height,
            };

            return(result);
        }
예제 #3
0
        protected bool UpdateJob(EthereumBlockTemplate blockTemplate)
        {
            logger.LogInvoke();

            try
            {
                // may happen if daemon is currently not connected to peers
                if (blockTemplate == null || blockTemplate.Header?.Length == 0)
                {
                    return(false);
                }

                // logger.Info(() => $"Blocktemplate {blockTemplate.Height}-{blockTemplate.Header}");

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

                if (isNew)
                {
                    messageBus.NotifyChainHeight(poolConfig.Id, blockTemplate.Height, poolConfig.Template);

                    var jobId = NextJobId("x8");

                    // update template
                    job = new EthereumJob(jobId, blockTemplate, logger);

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

                        // remove old ones
                        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;

                    // update stats
                    BlockchainStats.LastNetworkBlockTime = clock.Now;
                    BlockchainStats.BlockHeight          = job.BlockTemplate.Height;
                    BlockchainStats.NetworkDifficulty    = job.BlockTemplate.Difficulty;
                    BlockchainStats.NextNetworkTarget    = job.BlockTemplate.Target;
                    BlockchainStats.NextNetworkBits      = "";
                }

                return(isNew);
            }

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

            return(false);
        }