async void Get(long height, long spacing, string ipEndPoint)
        {
            GetFunCount++;

            try
            {
                record.Add(height);
                //
                Dictionary <long, string> blockChains = new Dictionary <long, string>();
                bool error           = false;
                var  q2p_Sync_Height = new Q2P_Sync_Height();
                q2p_Sync_Height.spacing = spacing;
                q2p_Sync_Height.height  = height;
                var reply = await cons.QuerySync_Height(q2p_Sync_Height, ipEndPoint, 30);

                if (reply == null)
                {
                    ipEndPoint = nodeManager.GetRandomNode().ipEndPoint;
                    reply      = await cons.QuerySync_Height(q2p_Sync_Height, ipEndPoint, 30);
                }
                if (reply != null && !string.IsNullOrEmpty(reply.blockChains))
                {
                    Log.Info($"SyncHeightFast.Get AddBlock {height} {ipEndPoint}");
                    blockChains = JsonHelper.FromJson <Dictionary <long, string> >(reply.blockChains);
                    do
                    {
                        for (int kk = 0; kk < reply.blocks.Count; kk++)
                        {
                            var blk = JsonHelper.FromJson <Block>(reply.blocks[kk]);
                            if (!blockMgr.AddBlock(blk))
                            {
                                error = true;
                                break;
                            }
                        }
                        if (!error && reply.height != -1)
                        {
                            q2p_Sync_Height.height  = reply.height;
                            q2p_Sync_Height.spacing = Math.Max(1, spacing - (reply.height - height));

                            reply = await cons.QuerySync_Height(q2p_Sync_Height, ipEndPoint, 30);
                        }
                    }while (!error && reply != null && reply.height != -1);
                }
                else
                {
                    Log.Info($"SyncHeightFast.Get Remove {height} {ipEndPoint}");
                    record.Remove(height);
                }
            }
            catch (Exception)
            {
            }

            GetFunCount--;
        }
Exemple #2
0
        public override void Start()
        {
            rule = Entity.Root.GetComponent <Rule>();
            ComponentNetMsg componentNetMsg = Entity.Root.GetComponent <ComponentNetMsg>();

            componentNetMsg.registerMsg(NetOpcode.P2P_NewBlock, P2P_NewBlock_Handle);

            string genesisText = File.ReadAllText("./Data/genesisBlock.dat");
            Block  blk         = JsonHelper.FromJson <Block>(genesisText);

            superAddress = blk.Address;

            long.TryParse(Entity.Root.GetComponent <LevelDBStore>().Get("UndoHeight"), out long UndoHeight);
            if (UndoHeight == 0)
            {
                if (true)
                {
                    blockMgr.AddBlock(blk);
                    ApplyGenesis(blk);
                }
            }

            string consData = Base58.Encode(FileHelper.GetFileData("./Data/Contract/RuleContract_v1.0.lua").ToByteArray());

            consAddress = Wallet.ToAddress(CryptoHelper.Sha256(Encoding.UTF8.GetBytes(consData)));

            using (DbSnapshot snapshot = levelDBStore.GetSnapshot())
            {
                LuaVMScript luaVMScript = new LuaVMScript()
                {
                    script = FileHelper.GetFileData("./Data/Contract/RuleContract_curr.lua").ToByteArray()
                };
                snapshot.Contracts.Add(consAddress, luaVMScript);
                snapshot.Commit();
            }
            ruleInfos = luaVMEnv.GetRules(consAddress, UndoHeight);

            if (bRun)
            {
                Run();
            }
        }
Exemple #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);
                }
            }
        }
Exemple #4
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();
            Block        blk          = null;
            Block        preblk       = null;
            TimePass     timePass     = new TimePass(1);
            var          httpRule     = Entity.Root.GetComponent <HttpPool>();

            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())
                    {
                        // 找到最新的Mc块
                        preblk = GetLastMcBlock();
                        // 是否在裁决节点列表中
                        if (consensus.IsRule(preblk.height + 1, wallet.GetCurWallet().ToAddress()))
                        {
                            // 生成主块
                            blk        = CreateBlock(preblk);
                            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)
                    {
                        // 挖矿 这里是提交算力 by sxh
                        Mining(blk, hashmining);
                    }

                    // 广播块
                    if (blk != null && time >= broadcasttime)
                    {
                        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.First(c => c.diff == diff);
                                blk.random = miner.random;
                                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);
                        diff_max = 0;

                        Log.Debug($"Rule.Broadcast {blk.height} {blk.hash}");
                        calculatePower.Insert(blk);
                        blk        = null;
                        hashmining = "";
                    }

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