Esempio n. 1
0
        // 算力统计
        static public void SetDT(Block myblk, Block preblk, HttpPool httpRule)
        {
            if (httpRule == null || myblk == null || preblk == null)
            {
                return;
            }

            Dictionary <string, MinerTask> miners = httpRule?.GetMiner(preblk.height);

            if (miners == null)
            {
                return;
            }

            //double difftotal = 0;
            //foreach (var miner in miners.Values)
            //{
            //    if (double.TryParse(miner.power_average, out double diff)) {
            //        difftotal += diff;
            //    }
            //}

            double difftotal = miners.Values.Sum(x => x.power);

            if (difftotal != 0)
            {
                if (myblk.extend == null)
                {
                    myblk.extend = new Dictionary <int, string>();
                }
                myblk.extend.Add(1, "" + difftotal);
            }
        }
Esempio n. 2
0
        public override void Get_Random_diff_max()
        {
            Dictionary <string, MinerTask> miners = httpPool.GetMiner(height);

            if (miners != null)
            {
                double diff  = miners.Values.Max(t => t.diff);
                var    miner = miners.Values.FirstOrDefault(c => c.diff == diff);
                if (miner != null && miner.random != null && miner.random.Count > 0)
                {
                    diff_max = diff;
                    random   = miner.random[miner.random.Count - 1];
                }
            }
        }
Esempio n. 3
0
        public async void Run()
        {
            //Entity.Root.GetComponent<LevelDBStore>().Undo(100);
            await Task.Delay(3000);

            long.TryParse(levelDBStore.Get("UndoHeight"), out height);

            Log.Debug($"Rule.Run at height {height}");

            Wallet       wallet       = Wallet.GetWallet();
            P2P_NewBlock p2p_NewBlock = new P2P_NewBlock()
            {
                networkID = NodeManager.networkIDBase
            };
            Block    blk        = null;
            Block    preblk     = null;
            TimePass timePass   = new TimePass(1);
            bool     bBlkMining = true;

            if (httpRule == null)
            {
                broadcasttime = pooltime * 5 / 15; // 不挖矿就提前广播块
            }
            state = 0xff;
            while (true)
            {
                try
                {
                    if (state < 0xff)
                    {
                        await Task.Delay(1000);
                    }

                    long time = (nodeManager.GetNodeTime() / 1000) % pooltime;
                    if (blk == null && time < broadcasttime && timePass.IsPassSet())
                    {
                        preblk = GetLastMcBlock();
                        if (consensus.IsRule(preblk.height + 1, wallet.GetCurWallet().ToAddress()))
                        {
                            blk = CreateBlock(preblk, ref bBlkMining);
                            if (blk != null && bBlkMining)
                            {
                                diff_max   = 0;
                                hashmining = blk.ToHashMining();
                                httpRule?.SetMinging(blk.height, hashmining, consensus.calculatePower.GetPower());
                            }
                        }
                        else
                        {
                            long.TryParse(levelDBStore.Get("UndoHeight"), out height);
                            await Task.Delay(1000);
                        }
                    }

                    if (blk != null && httpRule == null)
                    {
                        // 挖矿
                        if (httpRule == null && bBlkMining)
                        {
                            Mining(blk, hashmining);
                        }
                    }

                    // 广播块
                    if (blk != null && time >= broadcasttime)
                    {
                        if (bBlkMining)
                        {
                            if (httpRule != null)
                            {
                                Dictionary <string, MinerTask> miners = httpRule.GetMiner(blk.height);
                                if (miners != null)
                                {
                                    double diff  = miners.Values.Max(t => t.diff);
                                    var    miner = miners.Values.FirstOrDefault(c => c.diff == diff);
                                    if (miner != null && miner.random != null && miner.random.Count > 0)
                                    {
                                        blk.random = miner.random[miner.random.Count - 1];
                                    }
                                    httpRule.SetMinging(blk.height, "", consensus.calculatePower.GetPower());
                                }
                            }
                            height   = blk.height;
                            blk.hash = blk.ToHash();
                            blk.sign = blk.ToSign(Wallet.GetWallet().GetCurWallet());
                            blockMgr.AddBlock(blk);
                        }

                        p2p_NewBlock.block      = JsonHelper.ToJson(blk);
                        p2p_NewBlock.ipEndPoint = networkInner.ipEndPoint.ToString();
                        nodeManager.Broadcast(p2p_NewBlock, blk);
                        relayNetwork?.Broadcast(p2p_NewBlock);

                        Log.Debug($"Rule.Broadcast H:{blk.height} Mining:{bBlkMining} hash:{blk.hash} T:{blk.linkstran.Count}");

                        calculatePower.Insert(blk);
                        hashmining = "";
                        diff_max   = 0;
                        blk        = null;
                    }

                    await Task.Delay(10);
                }
                catch (Exception)
                {
                    await Task.Delay(1000);
                }
            }
        }