コード例 #1
0
ファイル: Rule.cs プロジェクト: mangodager/smartx-dotnet
        // 获取最新高度MC块
        public Block GetLastMcBlock()
        {
            var levelDBStore = Entity.Root.GetComponent <LevelDBStore>();

            // 取最新高度
            long.TryParse(levelDBStore.Get("UndoHeight"), out long transferHeight);
            var chain1 = BlockChainHelper.GetBlockChain(transferHeight);
            var chain2 = chain1.GetMcBlockNext();

            while (chain2 != null)
            {
                chain1 = chain2;
                if (chain1.height >= transferHeight + 10)
                {
                    chain2 = null;
                }
                else
                {
                    chain2 = chain1.GetMcBlockNext();
                }
            }

            if (DelBlockWithHeightTime.IsPassSet())
            {
                blockMgr.DelBlockWithHeight(consensus, chain1.height);
            }

            Block blk1 = chain1.GetMcBlock();

            // 2F1
            double       t_2max = consensus.GetRuleCount(blk1.height + 1);
            List <Block> blks   = blockMgr.GetBlock(blk1.height + 1);

            blks = BlockChainHelper.GetRuleBlk(consensus, blks, chain1.hash);
            if (blks.Count >= BlockChainHelper.Get2F1(t_2max))
            {
                chain2 = BlockChainHelper.GetMcBlockNextNotBeLink(chain1);
                if (chain2 != null)
                {
                    var    blk2   = chain2.GetMcBlock();
                    double t_1max = consensus.GetRuleCount(blk2.height - 1);
                    if (blk2.linksblk.Count >= BlockChainHelper.Get2F1(t_1max))
                    {
                        return(blk2);
                    }
                }
            }

            // Auxiliary Address
            var blkAuxiliary = blks.Find((x) => { return(x.Address == consensus.auxiliaryAddress); });

            //if (blkAuxiliary != null && blkAuxiliary.Address == consensus.auxiliaryAddress && blks.Count >= Math.Min(2, consensus.GetRuleCount(blk1.height+1)) )
            if (blkAuxiliary != null && blkAuxiliary.Address == consensus.auxiliaryAddress && blks.Count >= Math.Max(2, (BlockChainHelper.Get2F1(t_2max) / 2)))
            {
                return(blkAuxiliary);
            }

            return(blk1);
        }
コード例 #2
0
        public static BlockChain FindtChainMost(BlockChain chainfrist, BlockMgr blockMgr, Consensus consensus = null)
        {
            blockMgr  = blockMgr ?? Entity.Root.GetComponent <BlockMgr>();
            consensus = consensus ?? Entity.Root.GetComponent <Consensus>();
            BlockChain chain = chainfrist;

            List <BlockChain> list1 = new List <BlockChain>();
            List <BlockChain> list2 = new List <BlockChain>();
            List <BlockChain> listT = null;

            // 得到T+1周期所有主块
            List <Block> blks1 = blockMgr.GetBlock(chain.height + 1);
            // 得到T+2周期所有主块
            List <Block> blks2 = blockMgr.GetBlock(chain.height + 2);

            FindtChain(chain, blks1, blks2, ref list1, blockMgr, consensus);
            if (list1.Count <= 1)
            {
                return(null);
            }

            long height = chain.height + 1;

            while (list1.Count >= 2)
            {
                blks1 = blks2;
                blks2 = blockMgr.GetBlock(height + 2);

                for (int ii = list1.Count - 1; ii >= 0; ii--)
                {
                    FindtChain(list1[ii], blks1, blks2, ref list2, blockMgr, consensus);
                }

                var exist = list2.Exists((x) => { return(x.checkWeight >= 2); });
                if (exist) // 如果存在2F+1 block就删除 super block
                {
                    list2.RemoveAll((x) => { return(x.checkWeight == 1); });
                }

                listT = list2;
                list2 = list1;
                list1 = listT;
                list2.Clear();

                height++;
            }

            if (list1.Count == 1)
            {
                return(list1[0]);
            }
            return(null);
        }
コード例 #3
0
ファイル: Rule.cs プロジェクト: mangodager/corlas
        // 获取最新高度MC块
        public Block GetLastMcBlock()
        {
            var levelDBStore = Entity.Root.GetComponent <LevelDBStore>();

            // 取最新高度 去TopBlock
            long.TryParse(levelDBStore.Get("UndoHeight"), out long transferHeight);
            var chain1 = BlockChainHelper.GetBlockChain(transferHeight);
            var chain2 = chain1.GetMcBlockNext();

            while (chain2 != null)
            {
                chain1 = chain2;
                chain2 = chain2.GetMcBlockNext();
            }

            blockMgr.DelBlockWithWeight(consensus, chain1.hash, chain1.height);
            Block blk1 = chain1.GetMcBlock();

            // 2F1
            float        timestamp = TimeHelper.NowSeconds();
            double       t_2max    = consensus.GetRuleCount(blk1.height + 1);
            List <Block> blks      = blockMgr.GetBlock(blk1.height + 1);

            blks = BlockChainHelper.GetRuleBlk(consensus, blks, chain1.hash);
            if (blks.Count >= BlockChainHelper.Get2F1(t_2max))
            {
                chain2 = BlockChainHelper.GetMcBlockNextNotBeLink(chain1, timestamp, pooltime);
                if (chain2 != null)
                {
                    var    blk2   = chain2.GetMcBlock();
                    double t_1max = consensus.GetRuleCount(blk2.height - 1);
                    if (blk2.linksblk.Count >= BlockChainHelper.Get2F1(t_1max))
                    {
                        return(blk2);
                    }
                }
            }

            // Super Address
            var blkSuper = blks.Find((x) => { return(x.Address == consensus.superAddress); });

            //if (blkSuper != null && blkSuper.Address == consensus.superAddress && blks.Count >= Math.Min(2, consensus.GetRuleCount(blk1.height+1)) )
            if (blkSuper != null && blkSuper.Address == consensus.superAddress && blks.Count >= Math.Max(2, (BlockChainHelper.Get2F1(t_2max) / 2)))
            {
                return(blkSuper);
            }

            return(blk1);
        }
コード例 #4
0
        // 获取某个块链接别人的链接数
        public int GetBlockLinkCount(Block target)
        {
            int count = 0;

            for (int i = 0; i < target.linksblk.Count; i++)
            {
                Block blk = blockMgr.GetBlock(target.linksblk[i]);
                //if (blk != null && CheckSign(blk) && IsRule(blk.height, blk.Address))
                if (blk != null && IsRule(blk.height, blk.Address))
                {
                    count++;
                }
            }
            return(count);
        }
コード例 #5
0
ファイル: BlockChain.cs プロジェクト: yangbaitu/smartx-dotnet
        public static int CheckChain(BlockChain chain, List <Block> blks2, BlockMgr blockMgr, Consensus consensus)
        {
            blks2 = blks2 ?? blockMgr.GetBlock(chain.height + 1);

            int rel = IsIrreversible(consensus, chain.GetMcBlock(), blks2) ? 2 : 0;

            var blksRule     = BlockChainHelper.GetRuleBlk(consensus, blks2, chain.hash);
            var blkAuxiliary = blksRule.Find((x) => { return(x.Address == consensus.auxiliaryAddress); });
            //if (blkAuxiliary != null && blkAuxiliary.Address == consensus.auxiliaryAddress && blksRule.Count >= Math.Min(2, consensus.GetRuleCount(chain.height + 1)))
            var t_2max = consensus.GetRuleCount(chain.height + 1);

            if (blkAuxiliary != null && blkAuxiliary.Address == consensus.auxiliaryAddress && blksRule.Count >= Math.Max(2, (BlockChainHelper.Get2F1(t_2max) / 2)))
            {
                rel = rel + 1;
            }
            chain.checkWeight = rel;
            return(rel);
        }
コード例 #6
0
ファイル: BlockChain.cs プロジェクト: yangbaitu/smartx-dotnet
        public static List <BlockChain> FindtChain(BlockChain chainfrist, List <Block> blks, List <Block> blks2, ref List <BlockChain> list, BlockMgr blockMgr, Consensus consensus)
        {
            blks = blks ?? blockMgr.GetBlock(chainfrist.height + 1);

            for (int ii = blks.Count - 1; ii >= 0; ii--)
            {
                if (chainfrist.hash == blks[ii].prehash)
                {
                    var cc = new BlockChain()
                    {
                        hash = blks[ii].hash, height = blks[ii].height
                    };
                    if (CheckChain(cc, blks2, blockMgr, consensus) > 0)
                    {
                        list.Add(cc);
                    }
                }
            }
            return(list);
        }
コード例 #7
0
        public void InsertLink(Block mcblk, BlockMgr blockMgr)
        {
            if (mcblk == null)
            {
                return;
            }

            diffWhole = 0;

            for (int ii = 0; ii < mcblk.linksblk.Count; ii++)
            {
                Block blk = blockMgr.GetBlock(mcblk.linksblk[ii]);
                if (blk != null && blk.extend != null && blk.extend.Count >= 1)
                {
                    double.TryParse(blk.extend[1], out double dt);
                    diffWhole += dt;
                }
            }

            InsertPower(diffWhole);
        }