Exemplo n.º 1
0
        static public void Mining(object data)
        {
            Miner This = data as Miner;

            while (true)
            {
                if (This.hashmining != "")
                {
                    string randomTemp;
                    //if (This.taskid != "")
                    //    randomTemp = This.taskid + System.Guid.NewGuid().ToString("N").Substring(0, 13);
                    //else
                    randomTemp = System.Guid.NewGuid().ToString("N").Substring(0, 16);

                    if (randomTemp == "0")
                    {
                        Log.Debug("random==\"0\"");
                    }

                    //string hash = CryptoHelper.Sha256(This.hashmining + randomTemp);
                    string hash = BlockDag.ToHash(This.height, This.hashmining, randomTemp);

                    double diff = Helper.GetDiff(hash);
                    if (diff > This.diff_max)
                    {
                        This.diff_max = diff;
                        This.random   = randomTemp;
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
        }
Exemplo n.º 2
0
 static public string ToHash(this Block This, string r = null)
 {
     //if (r != null && r != "")
     //    return CryptoHelper.Sha256((CryptoHelper.Sha256(This.ToString()) + r));
     //return CryptoHelper.Sha256((CryptoHelper.Sha256(This.ToString()) + This.random));
     return(BlockDag.ToHash(This.height, CryptoHelper.Sha256(This.ToString()), r ?? This.random));
 }
Exemplo n.º 3
0
        // http://127.0.0.1:8088/mining?cmd=Submit
        public void OnSubmit(HttpMessage httpMessage)
        {
            // submit
            Dictionary <string, string> map = new Dictionary <string, string>();

            try
            {
                map.Add("report", "refuse");

                long   minerHeight = long.Parse(httpMessage.map["height"]);
                string address     = httpMessage.map["address"];
                string number      = httpMessage.map["number"];
                if (minerHeight == height && hashmining != "")
                {
                    MinerTask minerTask = GetMyTaskID(minerHeight, address, number, httpMessage.map["taskid"]);
                    if (minerTask != null && TimeHelper.time - minerTask.time > 0.1)
                    {
                        minerTask.time = TimeHelper.time;
                        string random = httpMessage.map["random"];
                        string hash   = BlockDag.ToHash(height, hashmining, random);
                        double diff   = Helper.GetDiff(hash);
                        if (diff > minerTask.diff && !string.IsNullOrEmpty(random))
                        {
                            minerTask.diff          = diff;
                            minerTask.random        = random;
                            minerTask.power_average = httpMessage.map["average"];
                            map.Add("report", "accept");
                        }
                    }
                }

                if (minerHeight != height && hashmining != "")
                {
                    MinerTask minerTask = NewNextTakID(height, address, number);
                    // new task
                    map.Add("height", height.ToString());
                    map.Add("hashmining", hashmining);
                    map.Add("taskid", minerTask.taskid);
                    map.Add("power", power);
                    if (minerTask.number != number)
                    {
                        map.Add("number", minerTask.number);
                    }
                }
            }
            catch (Exception)
            {
                map.Remove("report");
                map.Add("report", "error");
            }
            httpMessage.result = JsonHelper.ToJson(map);
        }
Exemplo n.º 4
0
        public void Mining(Block blk, string hashmining)
        {
            string random = System.Guid.NewGuid().ToString("N").Substring(0, 16);
            //string hash   = CryptoHelper.Sha256(hashmining + random);
            string hash = BlockDag.ToHash(blk.height, hashmining, random);

            double diff = Helper.GetDiff(hash);

            if (diff > diff_max)
            {
                diff_max   = diff;
                blk.hash   = hash;
                blk.random = random;
            }
        }
Exemplo n.º 5
0
        static public void Mining(object data)
        {
            ThreadData This = data as ThreadData;

            while (true)
            {
                if (!string.IsNullOrEmpty(This.hashmining))
                {
                    string randomTemp;
                    if (This.miner.taskid != "")
                    {
                        randomTemp = This.miner.taskid + System.Guid.NewGuid().ToString("N").Substring(0, 13);
                    }
                    else
                    {
                        randomTemp = System.Guid.NewGuid().ToString("N").Substring(0, 16);
                    }

                    if (randomTemp == "0")
                    {
                        Log.Debug("random==\"0\"");
                    }

                    string hash = BlockDag.ToHash(This.miner.height, This.hashmining, randomTemp);
                    double diff = Helper.GetDiff(hash);
                    lock (This)
                    {
                        if (diff > This.diff_max)
                        {
                            This.diff_max = diff;
                            This.random   = randomTemp;
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
        }
Exemplo n.º 6
0
        public void TestOnSubmit(HttpMessage httpMessage)
        {
            if (httpMessage.map["cmd"].ToLower() != "submit")
            {
                return;
            }

            var resultMap = JsonHelper.FromJson <Dictionary <string, string> >(httpMessage.result);

            resultMap.TryGetValue("hashmining", out string hashmining);

            if (Pool.registerPool || string.IsNullOrEmpty(hashmining))
            {
                return;
            }

            string address = httpMessage.map["address"];
            string number  = httpMessage.map["number"];

            for (int i = 0; i < 20000; i++)
            {
                MinerTask minerTask = NewNextTakID(height, address, number);
                if (minerTask != null)
                {
                    string randomTemp;
                    randomTemp = minerTask.taskid + System.Guid.NewGuid().ToString("N").Substring(0, 13);

                    string hash  = BlockDag.ToHash(minerTask.height, hashmining, randomTemp);
                    double diff  = Helper.GetDiff(hash);
                    double power = CalculatePower.Power(diff);

                    minerTask.diff = diff;
                    minerTask.random.Add(randomTemp);
                    minerTask.power         = power;
                    minerTask.power_average = power.ToString();
                }
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
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);
                        string hash = BlockDag.ToHash(height, 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.InsertPower(CalculatePower.Power(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);
            }
        }
Exemplo n.º 9
0
        // http://127.0.0.1:8088/mining?cmd=Submit
        public void OnSubmit(HttpMessage httpMessage)
        {
            // submit
            Dictionary <string, string> map = new Dictionary <string, string>();

            try
            {
                map.Add("report", "refuse");

                // 版本检查
                string version = httpMessage.map["version"];
                if (version != Miner.version)
                {
                    map.Remove("report");
                    map.Add("report", "error");
                    map.Add("tips", $"Miner.version: {Miner.version}");
                    httpMessage.result = JsonHelper.ToJson(map);
                    return;
                }

                // 矿池登记检查
                if (Pool.registerPool && HttpPoolRelay.poolIpAddress.IndexOf(httpMessage.request.RemoteEndPoint.Address.ToString()) == -1)
                {
                    map.Add("tips", "need register Pool");
                    httpMessage.result = JsonHelper.ToJson(map);
                    return;
                }

                long      minerHeight = long.Parse(httpMessage.map["height"]);
                string    address     = httpMessage.map["address"];
                string    number      = httpMessage.map["number"];
                MinerTask minerTask   = GetMyTaskID(minerHeight, address, number, httpMessage.map["taskid"]);
                if (minerHeight == height && !string.IsNullOrEmpty(hashmining) &&
                    minerTask != null && TimeHelper.time - minerTask.time > 0.1)
                {
                    minerTask.time = TimeHelper.time;
                    string random = httpMessage.map["random"];
                    string hash   = BlockDag.ToHash(height, hashmining, random);
                    double diff   = Helper.GetDiff(hash);
                    double power  = CalculatePower.Power(diff);
                    if (power >= HttpPool.ignorePower &&
                        diff > minerTask.diff &&
                        !string.IsNullOrEmpty(random) &&
                        (Pool.registerPool || random.IndexOf(minerTask.taskid) == 0))
                    {
                        ///Log.Info($"OnSubmit: Height:{height} A:{address} P:{power} H:{hash}");

                        minerTask.diff = diff;
                        minerTask.random.Add(random);
                        minerTask.power         = power;
                        minerTask.power_average = httpMessage.map["average"];
                        map.Remove("report");
                        map.Add("report", "accept");
                    }
                }

                if (minerHeight != height && !string.IsNullOrEmpty(hashmining))
                {
                    bool limitNewNext = true;
                    // 矿池人数限制
                    if (!Miners.TryGetValue(height - 1, out Dictionary <string, MinerTask> list_1) || list_1.Count < minerLimit || list_1.TryGetValue($"{address}_{number}", out MinerTask temp))
                    {
                        limitNewNext = false;
                    }
                    if (Miners.TryGetValue(height, out Dictionary <string, MinerTask> list_0) && list_0.Count >= minerLimit)
                    {
                        limitNewNext = true;
                    }

                    if (!limitNewNext)
                    {
                        MinerTask newMinerTask = NewNextTakID(height, address, number);
                        // new task
                        map.Add("height", height.ToString());
                        map.Add("hashmining", hashmining);
                        map.Add("taskid", newMinerTask.taskid);
                        map.Add("power", minerTask?.power.ToString());
                        map.Add("nodeTime", GetNodeTime().ToString());
                        if (newMinerTask.number != number)
                        {
                            map.Add("number", newMinerTask.number);
                        }
                    }
                    else
                    {
                        map.Add("minerLimit", minerLimit.ToString());
                    }
                }
            }
            catch (Exception)
            {
                map.Remove("report");
                map.Add("report", "error");
            }
            httpMessage.result = JsonHelper.ToJson(map);
        }