Exemplo n.º 1
0
        // 获取最新高度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);
        }
Exemplo n.º 2
0
        public static double GetNonRollbackWeight(Consensus consensus, long height)
        {
            double t_1max = consensus.GetRuleCount(height - 1);
            double t_2max = consensus.GetRuleCount(height + 1);

            double fDiff = 0;

            double weight = Get2F1(t_1max) + fDiff + (t_1max * Get2F1(t_2max));

            return(weight);
        }
Exemplo n.º 3
0
        public static bool IsIrreversible(Consensus consensus, Block blk, List <Block> blks2 = null)
        {
            double t_1max = consensus.GetRuleCount(blk.height - 1);
            double t_2max = consensus.GetRuleCount(blk.height + 1);

            double t_1num = consensus.GetBlockLinkCount(blk);
            double t_2num = consensus.GetBlockBeLinkCount(blk, blks2);

            double weight1 = (Math.Min(Get2F1(t_1max), t_1num) + (t_1max * Math.Min(Get2F1(t_2max), t_2num)));

            double weight2 = Get2F1(t_1max) + (t_1max * Get2F1(t_2max));

            return(weight1 >= weight2);
        }
Exemplo n.º 4
0
        // Block权重公式: T2_weight = MIN(2/3*T1max,T1num) + 0.5f*(diff) + T1max*MIN(2/3*T3max,T3num)
        // 权重最大的块胜出为主块
        static public double GetBlockWeight(Consensus consensus, Block blk, List <Block> blks2 = null, bool belink = true)
        {
            double t_1max = consensus.GetRuleCount(blk.height - 1);
            double t_2max = consensus.GetRuleCount(blk.height + 1);

            double t_1num = consensus.GetBlockLinkCount(blk);
            double t_2num = belink ? consensus.GetBlockBeLinkCount(blk, blks2) : 0;

            double fDiff = blk.GetDiff();

            double weight = (Math.Min(Get2F1(t_1max), t_1num) + fDiff + (t_1max * Math.Min(Get2F1(t_2max), t_2num)));

            return(weight);
        }
Exemplo n.º 5
0
        // 获取最新高度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);
        }
Exemplo n.º 6
0
        // Block权重公式: T2_weight = MIN(2/3*T1max,T1num) + 0.5f*(diff) + T1max*MIN(2/3*T3max,T3num)
        // 权重最大的块胜出为主块
        static public double GetBlockWeight(Consensus consensus, Block blk, List <Block> blks2 = null, bool belink = true)
        {
            double t_1max = consensus.GetRuleCount(blk.height - 1);
            double t_2max = consensus.GetRuleCount(blk.height + 1);

            // GetBlockLinkCount这个如果缺块的链接块判断为主块 但是最后发现他无法拉取到这个块该怎么办? by sxh
            double t_1num = consensus.GetBlockLinkCount(blk);
            double t_2num = belink ? consensus.GetBlockBeLinkCount(blk, blks2) : 0;

            double fDiff = blk.GetDiff();

            // 被引用是,是引用为主块的    标记
            double weight = (Math.Min(Get2F1(t_1max), t_1num) + fDiff + (t_1max * Math.Min(Get2F1(t_2max), t_2num)));

            return(weight);
        }
Exemplo n.º 7
0
        public static bool IsIrreversible(Consensus consensus, Block blk, List <Block> blks2 = null)
        {
            // 假如有2个抵押节点 2
            double t_1max = consensus.GetRuleCount(blk.height - 1);
            // 假如有2个抵押节点 2
            double t_2max = consensus.GetRuleCount(blk.height + 1);

            // 获取块链接别人的链接数 假设有两个节点 2
            double t_1num = consensus.GetBlockLinkCount(blk);

            // 获取块被链接为主块的链接数 2
            double t_2num = consensus.GetBlockBeLinkCount(blk, blks2);

            // T周期块的权重
            double weight1 = (Math.Min(Get2F1(t_1max), t_1num) + (t_1max * Math.Min(Get2F1(t_2max), t_2num)));

            // T周期-满足2F+1块最小权重
            double weight2 = Get2F1(t_1max) + (t_1max * Get2F1(t_2max));

            return(weight1 >= weight2);
        }
Exemplo n.º 8
0
        // Block权重公式: T2_weight = MIN(2/3*T1max,T1num) + 0.5f*(diff) + T1max*MIN(2/3*T3max,T3num)
        // 权重最大的块胜出为主块
        static public double GetBlockWeight(Consensus consensus, Block blk, List <Block> blks2 = null, bool belink = true)
        {
            if (blk.height >= 88380) // Smartx 3.1.1 Fix
            {
                if (!consensus.IsRule(blk.height, blk.Address))
                {
                    return(0);
                }
            }

            double t_1max = consensus.GetRuleCount(blk.height - 1);
            double t_2max = consensus.GetRuleCount(blk.height + 1);

            double t_1num = consensus.GetBlockLinkCount(blk);
            double t_2num = belink ? consensus.GetBlockBeLinkCount(blk, blks2) : 0;

            double fDiff = blk.GetDiff();

            double weight = (Math.Min(Get2F1(t_1max), t_1num) + fDiff + (t_1max * Math.Min(Get2F1(t_2max), t_2num)));

            return(weight);
        }
Exemplo n.º 9
0
        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);
        }