Exemplo n.º 1
0
        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));
        }