Beispiel #1
0
        static public BlockChain GetMcBlockNext2F1(this BlockChain chain, BlockMgr bm = null, Consensus cos = null)
        {
            var          blockMgr  = bm ?? Entity.Root.GetComponent <BlockMgr>();
            var          consensus = cos ?? Entity.Root.GetComponent <Consensus>();
            List <Block> blks      = blockMgr.GetBlock(chain.height + 1);
            List <Block> blks2     = blockMgr.GetBlock(chain.height + 2);

            for (int ii = blks.Count - 1; ii >= 0; ii--)
            {
                if (chain.hash != blks[ii].prehash || !BlockChainHelper.IsIrreversible(consensus, blks[ii], blks2))
                {
                    blks.RemoveAt(ii);
                }
            }

            if (blks.Count == 0)
            {
                return(null);
            }

            Block  mcBlk    = blks[0];
            double mcWeight = GetBlockWeight(consensus, mcBlk, blks2);

            for (int ii = 1; ii < blks.Count; ii++)
            {
                Block  blk    = blks[ii];
                double weight = GetBlockWeight(consensus, blk, blks2);
                if (weight > mcWeight)
                {
                    mcBlk    = blk;
                    mcWeight = weight;
                }
                else
                if (weight == mcWeight)
                {
                    if (blk.hash.CompareTo(mcBlk.hash) > 0)
                    {
                        mcBlk    = blk;
                        mcWeight = weight;
                    }
                }
            }

            return(new BlockChain()
            {
                hash = mcBlk.hash, height = mcBlk.height
            });
        }
Beispiel #2
0
        public bool ApplyHeight(BlockChain blockChain)
        {
            if (blockChain == null)
            {
                return(false);
            }

            // 应用高度不是下一高度
            long.TryParse(levelDBStore.Get("UndoHeight"), out transferHeight);
            if (blockChain.height - 1 != transferHeight)
            {
                return(false);
            }

            Block mcblk = blockChain.GetMcBlock();

            if (mcblk == null)
            {
                return(false);
            }

            Block preblk = BlockChainHelper.GetMcBlock(transferHeight);

            if (mcblk.prehash != preblk.hash)
            {
                return(false);
            }

            LuaVMEnv luaVMEnv = Entity.Root.GetComponent <LuaVMEnv>();

            ruleInfos = luaVMEnv.GetRules(consAddress, transferHeight);

            // --------------------------------------------------
            calculatePower.Insert(mcblk);
            //Log.Debug($"ApplyHeight={blockChain.height}, {calculatePower.GetPower()}, {mcblk.GetDiff()}");

            using (DbSnapshot dbSnapshot = levelDBStore.GetSnapshotUndo(blockChain.height))
            {
                blockChain.Apply(dbSnapshot);
                ApplyReward(dbSnapshot, mcblk);

                if (GetRuleCount(mcblk.height - 1, 2) >= 2 && BlockChainHelper.IsIrreversible(this, mcblk))
                {
                    dbSnapshot.Add("2F1Height", mcblk.height.ToString());
                }

                // 连接块交易
                for (int ii = 0; ii < mcblk.linksblk.Count; ii++)
                {
                    Block linkblk = blockMgr.GetBlock(mcblk.linksblk[ii]);
                    if (linkblk == null)
                    {
                        return(false);
                    }
                    if (linkblk.height != 1)
                    {
                        for (int jj = 0; jj < linkblk.linkstran.Count; jj++)
                        {
                            if (!ApplyTransfer(dbSnapshot, linkblk.linkstran[jj], linkblk.height))
                            {
                                return(false);
                            }
                            if (!ApplyContract(dbSnapshot, linkblk.linkstran[jj], linkblk.height))
                            {
                                return(false);
                            }
                        }
                    }
                }

                dbSnapshot.Commit();
            }

            ruleInfos = luaVMEnv.GetRules(consAddress, mcblk.height, true);
            return(true);
        }