Esempio n. 1
0
        public async Task <bool> QueryPeerTipAsync(PeerId recipientPeerId)
        {
            try
            {
                PeerClient.SendMessage(new MessageDto(
                                           new LatestDeltaHashRequest().ToProtocolMessage(PeerId, CorrelationId.GenerateCorrelationId()),
                                           recipientPeerId
                                           ));

                using (CancellationTokenProvider.CancellationTokenSource)
                {
                    await QueryTipResponseMessageStreamer
                    .FirstAsync(a => a != null &&
                                a.PeerId.PublicKey.SequenceEqual(recipientPeerId.PublicKey) &&
                                a.PeerId.Ip.SequenceEqual(recipientPeerId.Ip))
                    .ToTask(CancellationTokenProvider.CancellationTokenSource.Token)
                    .ConfigureAwait(false);
                }
            }
            catch (OperationCanceledException)
            {
                return(false);
            }
            catch (Exception e)
            {
                Logger.Error(e, nameof(QueryPeerTipAsync));
                return(false);
            }

            return(true);
        }
        public async Task <IPeerDeltaHistoryResponse> DeltaHistoryAsync(PeerId recipientPeerId, uint height = 1, uint range = 1024)
        {
            IPeerDeltaHistoryResponse history;

            try
            {
                PeerClient.SendMessage(new MessageDto(
                                           new DeltaHistoryRequest
                {
                    Range  = range,
                    Height = height
                }.ToProtocolMessage(PeerId, CorrelationId.GenerateCorrelationId()),
                                           recipientPeerId
                                           ));

                using (CancellationTokenProvider.CancellationTokenSource)
                {
                    history = await DeltaHistoryResponseMessageStreamer
                              .FirstAsync(a => a != null &&
                                          a.PeerId.PublicKey.SequenceEqual(recipientPeerId.PublicKey) &&
                                          a.PeerId.Ip.SequenceEqual(recipientPeerId.Ip))
                              .ToTask(CancellationTokenProvider.CancellationTokenSource.Token)
                              .ConfigureAwait(false);
                }
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
            catch (Exception e)
            {
                Logger.Error(e, nameof(DeltaHistoryAsync));
                return(null);
            }

            return(history);
        }