コード例 #1
0
        /// <summary>
        /// leader节点分发区块
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <HandOutResponse> BlockHandOut(HandOutRequest request)
        {
            var response = new HandOutResponse();
            //获取所有的节点
            var peers = _configProviders.GetAllPeer();
            //获取需求远程请求服务的节点
            var notself = peers.Where(p => p.Id != CurrentState.Id).ToList();

            //如果没有其他节点 本地提交区块 则返回结果
            if (notself.Count() == 0)
            {
                await _blockDataManager.PutOnChainBlockAsync(request.Block);

                response.Success = true;
                return(response);
            }
            //分发区块
            List <Task <HandOutResponse> > taskList = new List <Task <HandOutResponse> >();
            var handOutRequest = new HandOutRequest
            {
                Block     = request.Block,
                Type      = HandOutType.CheckSave,
                ChannelId = request.ChannelId
            };

            foreach (var item in notself)
            {
                taskList.Add(item.BlockHandOut(handOutRequest));
            }
            var rightcount = 1;
            var badcount   = 0;
            //区块分发的校验成功的数量是否大于二分之一
            var checkback = false;

            while (true)
            {
                for (var i = 0; i < taskList.Count; i++)
                {
                    if (taskList[i].IsCompleted)
                    {
                        var rs = await taskList[i];
                        taskList.RemoveAt(i);
                        i--;
                        if (rs.Success)
                        {
                            rightcount++;
                            //如果投票数量大于一半
                            //这里获取的peer不包括本节点
                            if (rightcount >= ((peers.Count / 2) + 1))
                            {
                                checkback = true;
                                break;
                            }
                        }
                        else
                        {
                            badcount++;
                            if (badcount >= ((peers.Count / 2) + 1))
                            {
                                checkback = false;
                                break;
                            }
                        }
                    }
                }
                if (taskList.Count == 0)
                {
                    break;
                }
                System.Threading.Thread.Sleep(10);
            }

            //如过校验区块成功
            if (checkback)
            {
                //本地保存区块
                //通知节点本地保存区块
                var commitHandOutRequest = new HandOutRequest
                {
                    Block     = request.Block,
                    ChannelId = _node.GetChannelId(),
                    Type      = HandOutType.Commit
                };
                foreach (var item in notself)
                {
                    taskList.Add(item.BlockHandOut(commitHandOutRequest));
                }
                //返回
                var putonRS = await _blockDataManager.PutOnChainBlockAsync(request.Block);

                if (putonRS)
                {
                    response.Success = true;
                    response.Message = "block save successful";
                }
                else
                {
                    response.Success = false;
                    response.Message = "上链失败";
                }
                return(response);
            }
            else
            {
                response.Success = false;
                response.Message = "follower check block err";
                return(response);
            }
        }