Ejemplo n.º 1
0
        private HashTarget FindHashTarget(HashBits initialNounce, ulong maxItterations, CancellationToken cancellationToken)
        {
            return(_feedback.Execute("FindHashTarget",
                                     () =>
            {
                var cryptography = new Cryptography();
                ulong itterations = 0;
                try
                {
                    var nounce = initialNounce.ToHash();
                    do
                    {
                        var hash = cryptography.CalculateHash(_signedBlockBytes, nounce);
                        if (hash.Compare(_hashTarget) < 0)
                        {
                            return new HashTarget(nounce, hash);
                        }
                        nounce.Increment(1);
                    } while (++itterations < maxItterations && !cancellationToken.IsCancellationRequested);

                    return null;
                }
                finally
                {
                    _feedback.MiningHashProgress(itterations);
                }
            },
                                     () => $"{nameof(initialNounce)}: {initialNounce}, {nameof(maxItterations)}: {maxItterations}"));
        }
Ejemplo n.º 2
0
        public Miner(
            HashBits hashTargetBits,
            BlockSigned signedBlock,
            Hash signedBlockHash,
            Cryptography cryptography,
            IFeedback feedback)
        {
            _hashTarget       = hashTargetBits.ToHash();
            _signedBlock      = signedBlock;
            _signedBlockBytes = signedBlockHash.ToBinary();
            _cryptography     = cryptography;
            _feedback         = feedback;

            _taskManagerWork = new CancellationTokenSource();
            InitializeTaskManagerWait();
            _taskManager = new Task <HashTarget>(ManageTasks, _taskManagerWork.Token);
        }