Example #1
0
        static public string ToHashMerkle(this Block This, BlockMgr blockMgr)
        {
            //List<string> list1 = new List<string>();
            //for (int ii = 0; ii < This.linksblk.Count; ii++)
            //{
            //    Block linkblk = blockMgr.GetBlock(This.linksblk[ii]);
            //    for (int jj = 0; ii < linkblk.linkstran.Count; jj++)
            //    {
            //        list1.Add(linkblk.linkstran[jj].hash);
            //    }
            //}

            //List<string> list2 = new List<string>();
            //while (list1.Count>1)
            //{
            //    if (list1.Count % 2 == 1)
            //        list1.Add(list1[list1.Count - 1]);

            //    var num = list1.Count / 2;
            //    for (int ii = 0; ii < num; ii+=2)
            //    {
            //        list2.Add(CryptoHelper.Sha256( $"{list1[ii+0]}_{list1[ii+1]}" ));
            //    }
            //    List<string> temp = list1;
            //    list1 = list2;
            //    list2 = temp;
            //    list2.Clear();
            //}

            //if (list1.Count == 1)
            //    return list1[0];

            return("");
        }
Example #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);
        }
Example #3
0
        public override void Start()
        {
            consensus   = Entity.Root.GetComponent <Consensus>();
            blockMgr    = Entity.Root.GetComponent <BlockMgr>();
            httpPool    = Entity.Root.GetComponentInChild <HttpPool>();
            nodeManager = Entity.Root.GetComponent <NodeManager>();

            if (bRun)
            {
                Run();
            }
        }
        public async Task <bool> Sync(Block otherMcBlk, string ipEndPoint)
        {
            nodeManager  = nodeManager ?? Entity.Root.GetComponent <NodeManager>();
            blockMgr     = blockMgr ?? Entity.Root.GetComponent <BlockMgr>();
            cons         = cons ?? Entity.Root.GetComponent <Consensus>();
            levelDBStore = levelDBStore ?? Entity.Root.GetComponent <LevelDBStore>();

            if (cons.transferHeight >= otherMcBlk.height)
            {
                return(false);
            }

            // 接收广播过来的主块
            // 检查链是否一致,不一致表示前一高度有数据缺失
            // 获取对方此高度主块linksblk列表,对方非主块的linksblk列表忽略不用拉取
            // 没有的块逐个拉取,校验数据是否正确,添加到数据库
            // 拉取到的块重复此过程
            // UndoTransfers到拉去到的块高度 , 有新块添加需要重新判断主块把漏掉的账本应用
            // GetMcBlock 重新去主块 ,  ApplyTransfers 应用账本
            long syncHeight = otherMcBlk.height;
            long currHeight = (int)(cons.transferHeight / 10) * 10;

            var nodes = nodeManager.GetNode(NodeManager.EnumState.openSyncFast);

            if (nodes.Length != 0)
            {
                long spacing = 10;

                int ii     = 0;
                int random = RandomHelper.Random() % nodes.Length;
                while (GetFunCount <= nodes.Length)
                {
                    long h = currHeight + ii * spacing;
                    if (h > cons.transferHeight + 300)
                    {
                        break;
                    }

                    if (record.IndexOf(h) == -1)
                    {
                        Get(h, spacing, nodes[(ii + random) % nodes.Length].ipEndPoint);
                    }
                    ii++;
                }
            }

            record.RemoveAll(x => x < currHeight);

            return(await cons.SyncHeightNear(otherMcBlk, nodeManager.GetRandomNode(NodeManager.EnumState.openSyncFast), 1f));
        }
Example #5
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
            });
        }
Example #6
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);
        }
Example #7
0
        static public BlockChain GetMcBlockNext(this BlockChain chain, BlockMgr bm = null, Consensus cos = null)
        {
            var blockMgr  = bm ?? Entity.Root.GetComponent <BlockMgr>();
            var consensus = cos ?? Entity.Root.GetComponent <Consensus>();

            var chinanext = GetMcBlockNext2F1(chain, blockMgr, consensus);

            if (chinanext != null)
            {
                return(chinanext);
            }

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

            List <Block> blks1 = blockMgr.GetBlock(chain.height + 1);
            List <Block> blks2 = blockMgr.GetBlock(chain.height + 2);

            FindtChain(chain, blks1, blks2, ref list1, blockMgr, consensus);
            if (list1.Count == 1)
            {
                if (list1[0].checkWeight >= 1)
                {
                    return(list1[0]);
                }
            }

            //var t_2max = consensus.GetRuleCount(chain.height + 1);
            //List<Block> blks = blockMgr.GetBlock(chain.height + 1);
            //var blksRule = BlockChainHelper.GetRuleBlk(consensus, blks, chain.hash);
            //var blkAuxiliary = blksRule.Find((x) => { return x.Address == consensus.auxiliaryAddress; });
            //if (blkAuxiliary != null && blkAuxiliary.Address == consensus.auxiliaryAddress && blksRule.Count >= Math.Max(2, (BlockChainHelper.Get2F1(t_2max) / 2)))
            //{
            //    List<Block> blksTemp = blockMgr.GetBlock(chain.height + 2);
            //    if(blksTemp.Exists( (x) => { return x.prehash == blkAuxiliary.hash; } )) {
            //        return new BlockChain() { hash = blkAuxiliary.hash, height = blkAuxiliary.height };
            //    }
            //}

            return(null);
        }
Example #8
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);
        }
Example #9
0
        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);
        }