Esempio n. 1
0
 private void HandleHeaders(Message msg)
 {
     try
     {
         BlockHeaderList blockHeaders = BlockHeaderList.Parser.ParseFrom(msg.Payload);
         MessageHub.Instance.Publish(new HeadersReceived(blockHeaders.Headers.ToList()));
     }
     catch (Exception e)
     {
         _logger?.Error(e, "Error while handling header list.");
     }
 }
Esempio n. 2
0
        public async Task <BlockHeaderList> GetBlockHeaderList(ulong index, int count)
        {
            var blockHeaderList = new BlockHeaderList();

            for (var i = index; i > index - (ulong)count; i--)
            {
                var block = await BlockChain.GetBlockByHeightAsync(i);

                blockHeaderList.Headers.Add(block.Header);
            }

            return(blockHeaderList);
        }
Esempio n. 3
0
// -- Methods ------------------------------------------------------------
        public void GetWork()
        {
            if (MiningProtocolType == PROTCOL_TEST)
            {
                BlockHeader = "0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A29AB5F49FFFF001D1DAC2B7C";
                Target      = Utils.GetTargetFromBlockHeader(BlockHeader);
                MidState    = "BC909A336358BFF090CCAC7D1E59CAA8C3C8D8E94F0103C896B187364719F91B";
            }
            else if (MiningProtocolType == PROTCOL_FROM_FILE)
            {
                // Keep getting data from list until empty
                if (BlockHeaderList.Count > 0)
                {
                    BlockHeader  = BlockHeaderList[0];
                    Target       = Utils.GetTargetFromBlockHeader(BlockHeader);
                    MidState     = Utils.GetMidStateFromBlockHeader(BlockHeader);
                    CurrentBlock = Convert.ToInt32(BlockHeightList[0]);

                    // Since we just consumed data fromt he list remove that record.
                    BlockHeaderList.RemoveAt(0);
                    BlockHeightList.RemoveAt(0);
                }
                else
                {
                    ContinueMining = false;
                }
            }
            else if ((MiningProtocolType == PROTCOL_GETWORK) || (MiningProtocolType == PROTCOL_GETWORK_STRATUM))
            {
                String reply = "";
                String data  = "";

                // Call Getwork and retrieve block header data information
                // Retrieve Block header
                reply = InvokeMethod("getwork");

                //If this was from a Stratum proxy then there's extra spaces within message. Remove them before parsing
                //if (MiningProtocolType == PROTCOL_GETWORK_STRATUM)
                //reply = reply.Replace(" ", "");

                Match match = Regex.Match(reply, "\"data\":\"([A-Fa-f0-9]+)");
                if (match.Success)
                {
                    data        = Utils.RemovePadding(match.Groups[1].Value);
                    data        = Utils.EndianFlip32BitChunks(data);
                    BlockHeader = data;
                }
                else
                {
                    throw new Exception("Didn't find valid 'data' in Server Response");
                }

                // Retrieve Midstate
                match = Regex.Match(reply, "\"midstate\":\"([A-Fa-f0-9]+)");
                if (match.Success)
                {
                    data     = match.Groups[1].Value;
                    data     = Utils.EndianFlip32BitChunks(data); //<-- is this needed??
                    MidState = data;
                }
                else
                {
                    throw new Exception("Didn't find valid 'midstate' in Server Response");
                }

                // Retrieve target
                match = Regex.Match(reply, "\"target\":\"([A-Fa-f0-9]+)");
                if (match.Success)
                {
                    data = match.Groups[1].Value;
                    //data = EndianFlip32BitChunks(data); //<-- is this needed??
                    Target = data;
                }
                else
                {
                    throw new Exception("Didn't find valid 'target' in Server Response");
                }


                if (!isPool)
                {
                    // Add a small delay so we are not oversaturating server
                    Thread.Sleep(100);

                    // Find out what block number we are working on
                    reply = InvokeMethod("getblockcount");
                    match = Regex.Match(reply, "\"result\":([0-9]+)");
                    if (match.Success)
                    {
                        data         = match.Groups[1].Value;
                        CurrentBlock = Convert.ToInt32(data) + 1; // Increment by 1 since we are working on future block
                    }
                    else
                    {
                        throw new Exception("Didn't find valid 'getblockcount' in Server Response");
                    }
                }
            }
        }