Exemple #1
0
        private void TrimmingLoop()
        {
            while (!Calcelled)
            {
                GrinConeeect conn = GetCurrentConn();

                if (conn.lastComm.AddMinutes(30) < DateTime.Now)
                {
                    conn.WaitForJob = true;
                }

                if (conn.WaitForJob | !conn.IsConnected)
                {
                    Task.Delay(100).Wait();
                    Console.Write(".");
                    continue;
                }

                JobTemplate job = conn.CurrentJob;

                UInt64 hnonce = (UInt64)(long)rnd.Next() | ((UInt64)(long)rnd.Next() << 32);
                var    bytes  = BitConverter.GetBytes(hnonce).Reverse().ToArray();
                header = header.Concat(bytes).ToArray();
                var    hash   = new Crypto.Blake2B(256);
                byte[] blaked = hash.ComputeHash(header);
                //blaked = hash.ComputeHash(blaked); -- testnet2 bug

                k0 = BitConverter.ToUInt64(blaked, 0);
                k1 = BitConverter.ToUInt64(blaked, 8);
                k2 = BitConverter.ToUInt64(blaked, 16);
                k3 = BitConverter.ToUInt64(blaked, 24);
            }
        }
Exemple #2
0
        private void Listen()
        {
            try
            {
                while (client.Connected)
                {
                    if ((DateTime.Now - lastComm) > TimeSpan.FromMinutes(30) || BadPacketCnt > 10)
                    {
                        break;
                    }

                    string message = reader.ReadLine();

                    if (string.IsNullOrEmpty(message) || string.IsNullOrWhiteSpace(message))
                    {
                        Console.WriteLine("Bad TCP packet!");
                        BadPacketCnt++;
                        continue;
                    }

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine();
                    Console.WriteLine("TCP IN: " + message + Environment.NewLine);
                    Console.ResetColor();



                    try
                    {
                        JObject msg = JsonConvert.DeserializeObject(message, new JsonSerializerSettings()
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        }) as JObject;

                        var    method = ((JObject)msg)["method"].ToString();
                        string para   = "";
                        if (msg.ContainsKey("params"))
                        {
                            para = msg["params"].ToString() /*.Replace("\\", "")*/;
                        }

                        BadPacketCnt = 0;

                        switch (method)
                        {
                        case "getjobtemplate":
                            if (msg.ContainsKey("result"))
                            {
                                CurrentJob = JsonConvert.DeserializeObject <JobTemplate>(msg["result"].ToString(), new JsonSerializerSettings()
                                {
                                    NullValueHandling = NullValueHandling.Ignore
                                });
                                if (CurrentJob != null && CurrentJob.pre_pow != null && CurrentJob.pre_pow != "")
                                {
                                    WaitForJob = false;
                                    lastComm   = DateTime.Now;
                                }
                            }
                            break;

                        case "job":
                            CurrentJob = JsonConvert.DeserializeObject <JobTemplate>(para, new JsonSerializerSettings()
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            });
                            WaitForJob = false;
                            lastComm   = DateTime.Now;
                            break;

                        case "submit":
                            if (msg.ContainsKey("result") && msg["result"].ToString() == "ok")
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine("Share accepted");
                                Console.ResetColor();
                            }
                            else if (msg.ContainsKey("result") && msg["result"].ToString().StartsWith("blockfound"))
                            {
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine("###################################");
                                Console.WriteLine("######  Block mined!  #" + (++mined).ToString("D4") + "  ######");     // 8 chars
                                Console.WriteLine("###################################");
                                Console.ResetColor();
                                statistics.mined++;
                            }
                            break;

                        default:
                            Console.WriteLine(para);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                IsConnected = false;
                // TODO REMOVE when recconect is added
                WaitForJob = false;
                Console.WriteLine("Connection dropped.");
                // reconnect
                if (!terminated)
                {
                    Console.WriteLine("Reconnecting");
                    Connect();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }