public override async Task RequestIndexingFromParentChain(CrossChainRequest crossChainRequest,
                                                                  IServerStreamWriter <ParentChainBlockData> responseStream, ServerCallContext context)
        {
            Logger.LogTrace(
                $"Parent Chain Server received IndexedInfo message from chain {ChainHelper.ConvertChainIdToBase58(crossChainRequest.FromChainId)}.");
            var requestedHeight = crossChainRequest.NextHeight;
            var remoteChainId   = crossChainRequest.FromChainId;

            while (requestedHeight - crossChainRequest.NextHeight < CrossChainCommunicationConstants.MaximalIndexingCount)
            {
                var parentChainBlockData =
                    await _crossChainResponseService.ResponseParentChainBlockDataAsync(requestedHeight, remoteChainId);

                if (parentChainBlockData == null)
                {
                    break;
                }
                if (context.Status.StatusCode != Status.DefaultSuccess.StatusCode)
                {
                    Logger.LogTrace(
                        $"Disconnected with side chain {ChainHelper.ConvertChainIdToBase58(crossChainRequest.FromChainId)} node.");
                    return;
                }

                Logger.LogTrace($"Response parent chain data {parentChainBlockData.Height}");
                await responseStream.WriteAsync(parentChainBlockData);

                requestedHeight++;
            }
        }
Exemple #2
0
        public override async Task RequestIndexingFromSideChain(CrossChainRequest crossChainRequest,
                                                                IServerStreamWriter <SideChainBlockData> responseStream, ServerCallContext context)
        {
            Logger.LogDebug("Side Chain Server received IndexedInfo message.");
            var requestedHeight = crossChainRequest.NextHeight;

            while (requestedHeight - crossChainRequest.NextHeight <
                   CrossChainCommunicationConstants.MaximalIndexingCount)
            {
                var sideChainBlock = await _crossChainResponseService.ResponseSideChainBlockDataAsync(requestedHeight);

                if (sideChainBlock == null)
                {
                    break;
                }
                try
                {
                    await responseStream.WriteAsync(sideChainBlock);

                    requestedHeight++;
                }
                catch (InvalidOperationException)
                {
                    Logger.LogWarning("Failed to write into server side stream.");
                    return;
                }
            }
        }
        /// <summary>
        /// Request target chain for cross chain data from target height.
        /// </summary>
        /// <param name="targetHeight"></param>
        /// <param name="crossChainBlockDataEntityHandler"></param>
        /// <returns></returns>
        public Task RequestCrossChainDataAsync(long targetHeight, Func <IBlockCacheEntity, bool> crossChainBlockDataEntityHandler)
        {
            var requestData = new CrossChainRequest
            {
                FromChainId = _localChainId,
                NextHeight  = targetHeight
            };

            return(RequestAsync(() => RequestCrossChainDataAsync(requestData, crossChainBlockDataEntityHandler)));
        }
Exemple #4
0
        public async Task RequestIndexingParentChain_EmptyResponse_Test()
        {
            var requestData = new CrossChainRequest
            {
                ChainId    = ChainHelper.GetChainId(1),
                NextHeight = 101
            };

            var responseResults = new List <ParentChainBlockData>();
            IServerStreamWriter <ParentChainBlockData> responseStream = MockServerStreamWriter(responseResults);
            var context = BuildServerCallContext();
            await ParentChainGrpcServerBase.RequestIndexingFromParentChain(requestData, responseStream, context);

            Assert.Empty(responseResults);
        }
Exemple #5
0
        public async Task RequestIndexingSideChain_MaximalResponse_Test()
        {
            var requestData = new CrossChainRequest
            {
                ChainId    = ChainHelper.GetChainId(1),
                NextHeight = 10
            };

            var responseResults = new List <SideChainBlockData>();
            IServerStreamWriter <SideChainBlockData> responseStream = MockServerStreamWriter(responseResults);
            var context = BuildServerCallContext();
            await SideChainGrpcServerBase.RequestIndexingFromSideChain(requestData, responseStream, context);

            Assert.Equal(CrossChainCommunicationConstants.MaximalIndexingCount, responseResults.Count);
            Assert.Equal(10, responseResults[0].Height);
        }
        private async Task RequestCrossChainDataAsync(CrossChainRequest crossChainRequest,
                                                      Func <IBlockCacheEntity, bool> crossChainBlockDataEntityHandler)
        {
            using (var serverStream = RequestIndexing(crossChainRequest))
            {
                while (await serverStream.ResponseStream.MoveNext())
                {
                    var response = serverStream.ResponseStream.Current;

                    // requestCrossChain failed or useless response
                    if (!crossChainBlockDataEntityHandler(response))
                    {
                        break;
                    }
                }
            }
        }
Exemple #7
0
        public async Task RequestIndexingParentChain_SpecificResponse_Test()
        {
            var requestData = new CrossChainRequest
            {
                ChainId    = ChainHelper.GetChainId(1),
                NextHeight = 81
            };

            var responseResults = new List <ParentChainBlockData>();
            IServerStreamWriter <ParentChainBlockData> responseStream = MockServerStreamWriter(responseResults);
            var context = BuildServerCallContext();
            await ParentChainGrpcServerBase.RequestIndexingFromParentChain(requestData, responseStream, context);

            Assert.Equal(20, responseResults.Count);
            Assert.Equal(81, responseResults.First().Height);
            Assert.Equal(100, responseResults.Last().Height);
        }
Exemple #8
0
        public override async Task RequestIndexingFromSideChain(CrossChainRequest crossChainRequest,
                                                                IServerStreamWriter <SideChainBlockData> responseStream, ServerCallContext context)
        {
            Logger.LogTrace("Side Chain Server received IndexedInfo message.");
            var requestedHeight = crossChainRequest.NextHeight;

            while (requestedHeight - crossChainRequest.NextHeight < CrossChainCommunicationConstants.MaximalIndexingCount)
            {
                var sideChainBlock = await _crossChainResponseService.ResponseSideChainBlockDataAsync(requestedHeight);

                if (sideChainBlock == null)
                {
                    break;
                }
                await responseStream.WriteAsync(sideChainBlock);

                requestedHeight++;
            }
        }
Exemple #9
0
        public override async Task RequestIndexingFromParentChain(CrossChainRequest crossChainRequest,
                                                                  IServerStreamWriter <ParentChainBlockData> responseStream, ServerCallContext context)
        {
            Logger.LogDebug(
                $"Parent Chain Server received IndexedInfo message from chain {ChainHelper.ConvertChainIdToBase58(crossChainRequest.ChainId)}.");
            var requestedHeight = crossChainRequest.NextHeight;
            var remoteChainId   = crossChainRequest.ChainId;

            while (requestedHeight - crossChainRequest.NextHeight < GrpcCrossChainConstants.MaximalIndexingCount)
            {
                var parentChainBlockData =
                    await _crossChainResponseService.ResponseParentChainBlockDataAsync(requestedHeight, remoteChainId);

                if (parentChainBlockData == null)
                {
                    break;
                }

                if (context.Status.StatusCode != Status.DefaultSuccess.StatusCode)
                {
                    Logger.LogTrace(
                        $"Disconnected with side chain {ChainHelper.ConvertChainIdToBase58(crossChainRequest.ChainId)} node.");
                    return;
                }

                try
                {
                    await responseStream.WriteAsync(parentChainBlockData);

                    requestedHeight++;
                }
                catch (InvalidOperationException)
                {
                    Logger.LogWarning("Failed to write into server side stream.");
                    return;
                }
            }
        }
 protected override AsyncServerStreamingCall <ParentChainBlockData> RequestIndexing(CrossChainRequest crossChainRequest)
 {
     return(GrpcClient.RequestIndexingFromParentChain(crossChainRequest, CreateOption()));
 }
 protected abstract AsyncServerStreamingCall <TData> RequestIndexing(CrossChainRequest crossChainRequest);