Exemple #1
0
        public async void Run()
        {
            Program.DisbleQuickEditMode();
            Console.Clear();
            Console.CursorVisible = false;
            Console.Title         = $" address:{address},thread:{thread}, number:{number}, poolUrl:{poolUrl}";

            Log.Info($"start mining...");

            await Task.Delay(1000);

            //创建后台工作线程
            for (int ii = 0; ii < thread; ii++)
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Mining));
                thread.IsBackground = true;//设置为后台线程
                thread.Start(this);
            }

            HttpMessage quest = new HttpMessage();

            quest.map = new Dictionary <string, string>();

            while (true)
            {
                try
                {
                    if (timePassInfo.IsPassSet())
                    {
                        string hash = CryptoHelper.Sha256(hashmining + random);
                        Log.Info($"\n height:{height}, taskid:{taskid}, random:{random}, diff:{diff_max}, power:{calculatePower.GetPower()} hash:{hash}");
                    }

                    quest.map.Clear();
                    quest.map.Add("cmd", "submit");
                    quest.map.Add("height", height.ToString());
                    quest.map.Add("address", address);
                    quest.map.Add("number", number);
                    quest.map.Add("random", random);
                    quest.map.Add("taskid", taskid);
                    quest.map.Add("average", calculatePower.GetPowerDouble().ToString());
                    HttpMessage result = null;
                    try
                    {
                        result = await ComponentNetworkHttp.Query($"http://{poolUrl}/mining", quest);
                    }
                    catch (Exception)
                    {
                        if (timePassDebug.IsPassSet())
                        {
                            Log.Warning($"\n Unable to open the network connection http://{poolUrl}/mining");
                        }
                    }
                    if (result != null && result.map != null)
                    {
                        if (result.map.ContainsKey("taskid"))
                        {
                            if (result.map.ContainsKey("number"))
                            {
                                number        = result.map["number"];
                                Console.Title = $" address:{address},thread:{thread}, number:{number}, poolUrl:{poolUrl}";
                            }

                            long.TryParse(result.map["height"], out long tempheight);
                            taskid = result.map["taskid"];
                            string temphash = result.map["hashmining"];

                            if (temphash == null || temphash == "" || temphash != hashmining)
                            {
                                if (diff_max != 0)
                                {
                                    calculatePower.Insert(diff_max);
                                }

                                diff_max   = 0;
                                hashmining = temphash;
                                height     = tempheight;
                                random     = "";

                                result.map.TryGetValue("power", out poolPower);
                                changeCallback?.Invoke();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                await Task.Delay(intervalTime);
            }
        }
Exemple #2
0
        public async void Run()
        {
            SetTitle($" address:{address},thread:{thread}, number:{number}, poolUrl:{poolUrl}, version:{version}");

            Log.Info($"start mining...");

            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

            //创建后台工作线程
            ThreadDataList = new ThreadData[thread];
            for (int ii = 0; ii < thread; ii++)
            {
                ThreadDataList[ii]            = new ThreadData();
                ThreadDataList[ii].miner      = this;
                ThreadDataList[ii].index      = ii;
                ThreadDataList[ii].diff_max   = 0;
                ThreadDataList[ii].random     = "";
                ThreadDataList[ii].hashmining = "";

                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Mining));
                thread.IsBackground = true;//设置为后台线程
                thread.Priority     = System.Threading.ThreadPriority.Normal;
                thread.Start(ThreadDataList[ii]);
            }

            HttpMessage quest = new HttpMessage();

            quest.map = new Dictionary <string, string>();

            while (true)
            {
                try
                {
                    if (timePassInfo.IsPassSet())
                    {
                        Get_Random_diff_max();
                        string hash     = BlockDag.ToHash(height, hashmining_last, random);
                        var    sharePer = Math.Round(effectiveShare * 100 / submitCount, 2);
                        var    power    = CalculatePower.GetPowerCompany(calculatePower.GetPowerDouble());

                        Log.Info($"\n height:{height}, taskid:{taskid},random:{random}, diff:{diff_max_last}, share:{sharePer}%, power:{power} hash:{hash}");
                    }

                    long time = (GetNodeTime() / 1000) % pooltime;
                    if (string.IsNullOrEmpty(hashmining) && time >= 1 && time < broadcasttime && timePass1.IsPassSet())
                    {
                        Get_Random_diff_max();
                        await Submit(quest);

                        //Log.Info("Task New");
                    }

                    if (!string.IsNullOrEmpty(hashmining) && time > broadcasttime - 3 && time <= broadcasttime && timePass2.IsPassSet())
                    {
                        Get_Random_diff_max();
                        if (diff_max > diff_max_lastSubmit)
                        {
                            diff_max_last       = diff_max;
                            diff_max_lastSubmit = diff_max;
                            await Submit(quest);

                            //Log.Info($"Task Submit {height}");
                        }
                    }

                    if (!string.IsNullOrEmpty(hashmining) && time > broadcasttime)
                    {
                        hashmining = null;
                        changeCallback?.Invoke();
                    }
                }
                catch (Exception)
                {
                    await Task.Delay(15000);
                }
                await Task.Delay(10);
            }
        }