コード例 #1
0
        // 解决分叉链
        public BlockChain ApplyBlockChainMost()
        {
            long.TryParse(levelDBStore.Get("UndoHeight"), out transferHeight);
            var chain1 = BlockChainHelper.GetBlockChain(transferHeight);
            var chain2 = BlockChainHelper.FindtChainMost(chain1, blockMgr, this);

            if (chain2 != null)
            {
                List <BlockChain> list = new List <BlockChain>();

                var chainStart = chain2;
                var chainEnd   = chain1;
                while (chainStart.height != chainEnd.height)
                {
                    list.Insert(0, chainStart);
                    var blk = chainStart.GetMcBlock();
                    chainStart = new BlockChain()
                    {
                        hash = blk.prehash, height = blk.height - 1
                    };
                }

                if (list.Count >= 2)
                {
                    if (ApplyHeight(list[0]))
                    {
                        return(list[0]);
                    }
                }
                return(null);
            }
            else
            if (chain1 != null)
            {
                // 应用账本到T-1周期
                chain1 = chain1.GetMcBlockNext(blockMgr, this);
                chain2 = chain1 == null ? null : chain1.GetMcBlockNext(blockMgr, this);
                if (chain1 != null && chain2 != null)
                {
                    if (ApplyHeight(chain1))
                    {
                        return(chain1);
                    }
                }
            }
            return(null);
        }