Esempio n. 1
0
        public static Nonce Answer(Stamp stamp, uint difficulty)
        {
            int counter = 1;

            while (true)
            {
                var nonce  = new Nonce(BitConverter.GetBytes(counter));
                var digest = Hash(stamp(nonce));
                if (digest.HasLeadingZeroBits(difficulty))
                {
                    return(nonce);
                }

                counter++;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Finds a <see cref="Nonce"/> that satisfies the given
        /// <paramref name="difficulty"/>.  This process is so-called
        /// &#x0201c;<a
        /// href="https://en.wikipedia.org/wiki/Cryptocurrency#Mining"
        /// >mining</a>&#x0201d;.
        /// </summary>
        /// <param name="stamp">A callback to get a &#x0201c;stamp&#x0201d;
        /// which is a <see cref="byte"/> array determined from a given
        /// <see cref="Nonce"/> value.</param>
        /// <param name="difficulty">The minimum required number of
        /// leading zero bits that a returned answer needs to have.</param>
        /// <returns>A <see cref="Nonce"/> value which satisfies the given
        /// <paramref name="difficulty"/>.</returns>
        /// <seealso cref="Stamp"/>
        public static Nonce Answer(Stamp stamp, int difficulty)
        {
            var nonceBytes = new byte[10];
            var random     = new Random();

            while (true)
            {
                random.NextBytes(nonceBytes);
                var nonce  = new Nonce(nonceBytes);
                var digest = Hash(stamp(nonce));
                if (digest.HasLeadingZeroBits(difficulty))
                {
                    return(nonce);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Finds a <see cref="Nonce"/> that satisfies the given
        /// <paramref name="difficulty"/>.  This process is so-called
        /// &#x0201c;<a
        /// href="https://en.wikipedia.org/wiki/Cryptocurrency#Mining"
        /// >mining</a>&#x0201d;.
        /// </summary>
        /// <param name="stamp">A callback to get a &#x0201c;stamp&#x0201d;
        /// which is a <see cref="byte"/> array determined from a given
        /// <see cref="Nonce"/> value.</param>
        /// <param name="difficulty">The minimum required number of
        /// leading zero bits that a returned answer needs to have.</param>
        /// <returns>A <see cref="Nonce"/> value which satisfies the given
        /// <paramref name="difficulty"/>.</returns>
        /// <seealso cref="Stamp"/>
        public static Nonce Answer(Stamp stamp, uint difficulty)
        {
            int counter = 1;

            while (true)
            {
                // FIXME: nonce should be generated by a proper random generator
                var nonce  = new Nonce(BitConverter.GetBytes(counter));
                var digest = Hash(stamp(nonce));
                if (digest.HasLeadingZeroBits(difficulty))
                {
                    return(nonce);
                }

                counter++;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Finds a <see cref="Nonce"/> that satisfies the given
        /// <paramref name="difficulty"/>.  This process is so-called
        /// &#x0201c;<a
        /// href="https://en.wikipedia.org/wiki/Cryptocurrency#Mining"
        /// >mining</a>&#x0201d;.
        /// </summary>
        /// <param name="stamp">A callback to get a &#x0201c;stamp&#x0201d;
        /// which is a <see cref="byte"/> array determined from a given
        /// <see cref="Nonce"/> value.</param>
        /// <param name="difficulty">A number to calculate the target number
        /// for which the returned answer should be less than.</param>
        /// <returns>A <see cref="Nonce"/> value which satisfies the given
        /// <paramref name="difficulty"/>.</returns>
        /// <seealso cref="Stamp"/>
        public static Nonce Answer(Stamp stamp, long difficulty)
        {
            var nonceBytes = new byte[10];
            var random     = new Random();

            while (true)
            {
                random.NextBytes(nonceBytes);
                var nonce  = new Nonce(nonceBytes);
                var digest = Hash(stamp(nonce));
                if (difficulty == 0)
                {
                    return(nonce);
                }

                if (digest.Satisfies(difficulty))
                {
                    return(nonce);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Finds a <see cref="Nonce"/> that satisfies the given
        /// <paramref name="difficulty"/>.  This process is so-called
        /// &#x0201c;<a
        /// href="https://en.wikipedia.org/wiki/Cryptocurrency#Mining"
        /// >mining</a>&#x0201d;.
        /// </summary>
        /// <param name="stamp">A callback to get a &#x0201c;stamp&#x0201d;
        /// which is a <see cref="byte"/> array determined from a given
        /// <see cref="Nonce"/> value.</param>
        /// <param name="difficulty">A number to calculate the target number
        /// for which the returned answer should be less than.</param>
        /// <param name="cancellationToken">
        /// A cancellation token used to propagate notification that this
        /// operation should be canceled.
        /// </param>
        /// <returns>A <see cref="Nonce"/> value which satisfies the given
        /// <paramref name="difficulty"/>.</returns>
        /// <seealso cref="Stamp"/>
        public static Nonce Answer(
            Stamp stamp,
            long difficulty,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var nonceBytes = new byte[10];
            var random     = new Random();

            while (!cancellationToken.IsCancellationRequested)
            {
                random.NextBytes(nonceBytes);
                var nonce  = new Nonce(nonceBytes);
                var digest = Hash(stamp(nonce));

                if (digest.Satisfies(difficulty))
                {
                    return(nonce);
                }
            }

            throw new OperationCanceledException(cancellationToken);
        }