Ejemplo n.º 1
0
        public BlockHeader MineBlockHeader(BlockHeader blockHeader, UInt256 hashTarget)
        {
            var blockHeaderBytes = DataEncoder.EncodeBlockHeader(blockHeader);

            var hashTargetBytes = hashTarget.ToByteArray();

            var start      = 0;
            var finish     = UInt32.MaxValue;
            var total      = 0L;
            var nonceIndex = 76;
            var minedNonce = (UInt32?)null;

            this.logger.Debug("Starting mining: {0}".Format2(DateTime.Now.ToString("hh:mm:ss")));

            var stopwatch = Stopwatch.StartNew();

            Parallel.For(
                start, finish,
                new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount * 2
            },
                () => new LocalMinerState(blockHeaderBytes),
                (nonceLong, loopState, localState) =>
            {
                localState.total++;

                var nonce      = (UInt32)nonceLong;
                var nonceBytes = Bits.GetBytes(nonce);
                Buffer.BlockCopy(nonceBytes, 0, localState.headerBytes, nonceIndex, 4);

                var headerBytes = localState.headerBytes;
                var sha256      = localState.sha256;
                var hashBytes   = sha256.ComputeDoubleHash(headerBytes);

                if (BytesCompareLE(hashBytes, hashTargetBytes) < 0)
                {
                    minedNonce = nonce;
                    loopState.Stop();
                }

                return(localState);
            },
                localState => { Interlocked.Add(ref total, localState.total); });

            stopwatch.Stop();

            var hashRate = ((float)total / 1000 / 1000) / ((float)stopwatch.ElapsedMilliseconds / 1000);

            if (minedNonce != null)
            {
                this.logger.Debug("Found block in {0} hh:mm:ss at Nonce {1}, Hash Rate: {2} mHash/s, Total Hash Attempts: {3}, Found Hash: {4}".Format2(stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), minedNonce, hashRate, total.ToString("#,##0"), blockHeader.With(Nonce: minedNonce).Hash));
                return(blockHeader.With(Nonce: minedNonce));
            }
            else
            {
                this.logger.Debug("No block found in {0} hh:mm:ss, Hash Rate: {1} mHash/s, Total Hash Attempts: {2}, Found Hash: {3}".Format2(stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), hashRate, total.ToString("#,##0"), blockHeader.With(Nonce: minedNonce).Hash));
                return(null);
            }
        }
Ejemplo n.º 2
0
        public BlockHeader MineBlockHeader(BlockHeader blockHeader, UInt256 hashTarget)
        {
            var blockHeaderBytes = DataEncoder.EncodeBlockHeader(blockHeader);

            var hashTargetBytes = hashTarget.ToByteArray();

            var start      = 0;
            var finish     = UInt32.MaxValue;
            var total      = 0L;
            var nonceIndex = 76;
            var minedNonce = (UInt32?)null;

            var stopwatch = Stopwatch.StartNew();

            Parallel.For(
                start, finish,
                () => new LocalMinerState(blockHeaderBytes),
                (nonceLong, loopState, localState) =>
            {
                localState.total++;

                var nonce      = (UInt32)nonceLong;
                var nonceBytes = Bits.GetBytes(nonce);
                Buffer.BlockCopy(nonceBytes, 0, localState.headerBytes, nonceIndex, 4);

                var headerBytes = localState.headerBytes;
                var hashBytes   = SHA256Static.ComputeDoubleHash(headerBytes);

                if (BytesCompareLE(hashBytes, hashTargetBytes) < 0)
                {
                    minedNonce = nonce;
                    loopState.Stop();
                }

                return(localState);
            },
                localState => { Interlocked.Add(ref total, localState.total); });

            stopwatch.Stop();

            var hashRate = total / stopwatch.Elapsed.TotalSeconds;

            if (minedNonce == null)
            {
                throw new InvalidOperationException();
            }

            var minedHeader = blockHeader.With(Nonce: minedNonce);

            logger.Debug($"Found block in {stopwatch.Elapsed.TotalMilliseconds:N3}ms at Nonce {minedNonce}, Hash Rate: {hashRate / 1.MILLION()} mHash/s, Total Hash Attempts: {total:N0}, Found Hash: {minedHeader.Hash}");
            return(minedHeader);
        }
Ejemplo n.º 3
0
        public async Task SendHeaders(ImmutableArray <BlockHeader> blockHeaders)
        {
            await Task.Yield();

            using (var payloadStream = new MemoryStream())
                using (var payloadWriter = new BinaryWriter(payloadStream))
                {
                    payloadWriter.WriteVarInt((UInt64)blockHeaders.Length);
                    foreach (var blockHeader in blockHeaders)
                    {
                        DataEncoder.EncodeBlockHeader(payloadWriter, blockHeader);
                        payloadWriter.WriteVarInt(0);
                    }

                    await SendMessageAsync(Messaging.ConstructMessage("headers", payloadStream.ToArray()));
                }
        }
Ejemplo n.º 4
0
 public BlockHeaderStorage(string baseDirectory)
     : base(baseDirectory, "BlockHeaders",
            blockHeader => DataEncoder.EncodeBlockHeader(blockHeader),
            (blockHash, bytes) => DataEncoder.DecodeBlockHeader(bytes, blockHash))
 {
 }
Ejemplo n.º 5
0
        public void TestWireDecodeBlockHeader()
        {
            var actual = DataEncoder.EncodeBlockHeader(DataEncoder.DecodeBlockHeader(BLOCK_HEADER_1_BYTES.ToArray()));

            CollectionAssert.AreEqual(BLOCK_HEADER_1_BYTES.ToList(), actual.ToList());
        }