Exemple #1
0
        public void DelBlockWithWeight(Consensus consensus, string prehash, long perheight)
        {
            using (DbSnapshot snapshot = levelDBStore.GetSnapshot())
            {
                List <Block> blks = GetBlock(perheight + 1);
                blks = BlockChainHelper.GetRuleBlk(consensus, blks, prehash);

                List <string> list = snapshot.Heights.Get((perheight + 1).ToString());
                if (list != null)
                {
                    for (int ii = list.Count - 1; ii >= 0; ii--)
                    {
                        if (blks.Find(x => x.hash == list[ii]) == null)
                        {
                            snapshot.Blocks.Delete(list[ii]);
                            cacheDict.Remove(list[ii]);
                            cacheList.Remove(list[ii]);
                            list.RemoveAt(ii);
                        }
                    }
                    snapshot.Heights.Add((perheight + 1).ToString(), list);
                    snapshot.Commit();
                }
            }
        }
Exemple #2
0
        public Block CreateBlock(Block preblk)
        {
            if (preblk == null)
            {
                return(null);
            }

            Block myblk = new Block();

            myblk.Address   = Wallet.GetWallet().GetCurWallet().ToAddress();
            myblk.prehash   = preblk != null ? preblk.hash : "";
            myblk.timestamp = nodeManager.GetNodeTime();
            myblk.height    = preblk != null ? preblk.height + 1 : 1;
            myblk.random    = System.Guid.NewGuid().ToString("N").Substring(0, 16);

            //引用上一周期的连接块
            List <Block> blks = blockMgr.GetBlock(preblk.height);

            blks = BlockChainHelper.GetRuleBlk(consensus, blks, preblk.prehash);
            for (int ii = 0; ii < blks.Count; ii++)
            {
                myblk.AddBlock(ii, blks[ii]);
            }
            RefTransfer(myblk);
            myblk.prehashmkl = myblk.ToHashMerkle(blockMgr);

            return(myblk);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
0
        public Block CreateBlock(Block preblk, ref bool bBlkMining)
        {
            if (preblk == null)
            {
                return(null);
            }
            string address = Wallet.GetWallet().GetCurWallet().ToAddress();

            Block myblk = new Block();

            myblk.Address   = Wallet.GetWallet().GetCurWallet().ToAddress();
            myblk.prehash   = preblk != null ? preblk.hash : "";
            myblk.timestamp = nodeManager.GetNodeTime();
            myblk.height    = preblk != null ? preblk.height + 1 : 1;
            myblk.random    = System.Guid.NewGuid().ToString("N").Substring(0, 16);

            //引用上一周期的连接块
            var blks1 = blockMgr.GetBlock(preblk.height);
            var blks2 = BlockChainHelper.GetRuleBlk(consensus, blks1, preblk.prehash);

            for (int ii = 0; ii < blks2.Count; ii++)
            {
                myblk.AddBlock(ii, blks2[ii]);
            }

            // 比较相同高度的出块
            var blks3   = blockMgr.GetBlock(myblk.height);
            var blklast = blks3.Find(x => (x.Address == address && x.prehash == myblk.prehash && x.linksblk.Count >= myblk.linksblk.Count));

            if (blklast != null)
            {
                bBlkMining = false;
                return(blklast);
            }

            CalculatePower.SetDT(myblk, preblk, httpRule);

            RefTransfer(myblk);
            myblk.prehashmkl = myblk.ToHashMerkle(blockMgr);

            bBlkMining = true;

            //Log.Debug($"Rule.CreateBlock {myblk.height} linksblk:{myblk.linksblk.Count} linkstran:{myblk.linkstran.Count} blks1:{blks1.Count} blks3:{blks3.Count}");

            return(myblk);
        }
        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);
        }
Exemple #7
0
        public static void Q2P_BeLinkHash_Handle(Session session, int opcode, object msg)
        {
            Q2P_BeLinkHash q2q_McBlockHash = msg as Q2P_BeLinkHash;
            R2P_BeLinkHash r2p_McBlockHash = new R2P_BeLinkHash();

            var consensus = Entity.Root.GetComponent <Consensus>();
            var blockMgr  = Entity.Root.GetComponent <BlockMgr>();

            Block        mcbkl  = blockMgr.GetBlock(q2q_McBlockHash.hash);
            double       t_1max = consensus.GetRuleCount(mcbkl.height + 1);
            List <Block> blks   = blockMgr.GetBlock(mcbkl.height + 1);

            blks = BlockChainHelper.GetRuleBlk(consensus, blks, mcbkl.hash);


            string[] hashs = blks.Select(a => a.hash).ToArray();
            r2p_McBlockHash.hashs = JsonHelper.ToJson(hashs);

            session.Reply(q2q_McBlockHash, r2p_McBlockHash);
        }