예제 #1
0
        /// <summary>
        /// Attempts to mine the block with the next range of nonce values.
        /// </summary>
        /// <returns>True if the block is successfully mined.</returns>
        public bool Attempt()
        {
            int nonceRange = int.MaxValue - _startingNonce;

            if (nonceRange > _nonceRange)
            {
                nonceRange = _nonceRange;
            }

            for (int n = _startingNonce, c = _startingNonce + nonceRange; n < c; ++n)
            {
                HexUtils.AppendHexFromInt(_headerStreamWriter, n);
                _headerStreamWriter.Flush();

                var hash = Pow.Hash(_headerStream);
                if (Pow.IsValidHash(hash, _difficulty))
                {
                    Block.Nonce = n;
                    Block.Hash  = HexUtils.HexFromByteArray(hash);
                    return(true);
                }

                _headerStream.Seek(_streamOrgPosition, SeekOrigin.Begin);
            }

            _startingNonce += nonceRange;

            if (_startingNonce == int.MaxValue)
            {
                _maxNonceReached = true;
            }

            return(false);
        }