示例#1
0
        public Task BroadcastLibAnnounceAsync(Hash libHash, long libHeight)
        {
            var announce = new LibAnnouncement
            {
                LibHash   = libHash,
                LibHeight = libHeight
            };

            foreach (var peer in _peerPool.GetPeers())
            {
                try
                {
                    peer.EnqueueLibAnnouncement(announce, async ex =>
                    {
                        if (ex != null)
                        {
                            Logger.LogError(ex, $"Error while broadcasting lib announcement to {peer}.");
                            await HandleNetworkException(peer, ex);
                        }
                    });
                }
                catch (NetworkException ex)
                {
                    Logger.LogError(ex, $"Error while broadcasting lib announcement to {peer}.");
                }
            }

            return(Task.CompletedTask);
        }
示例#2
0
        public Task ProcessLibAnnouncement(LibAnnouncement announcement, ServerCallContext context)
        {
            if (announcement?.LibHash == null)
            {
                Logger.LogWarning($"Received null or empty announcement from {context.GetPeerInfo()}.");
                return(Task.CompletedTask);
            }

            Logger.LogDebug(
                $"Received lib announce hash: {announcement.LibHash}, height {announcement.LibHeight} from {context.GetPeerInfo()}.");

            var peer = _connectionService.GetPeerByPubkey(context.GetPublicKey());

            if (peer == null)
            {
                // if peer already removed, drop.
                return(Task.CompletedTask);
            }

            peer.UpdateLastKnownLib(announcement);

            if (peer.SyncState != SyncState.Finished)
            {
                peer.SyncState = SyncState.Finished;
            }

            return(Task.CompletedTask);
        }
        public Task BroadcastLibAnnounceAsync(Hash libHash, long libHeight)
        {
            var announce = new LibAnnouncement
            {
                LibHash   = libHash,
                LibHeight = libHeight
            };

            foreach (var peer in _peerPool.GetPeers())
            {
                try
                {
                    peer.EnqueueLibAnnouncement(announce, async ex =>
                    {
                        if (ex != null)
                        {
                            Logger.LogWarning(ex, $"Could not broadcast lib announcement to {peer} " +
                                              $"- status {peer.ConnectionStatus}.");
                            await HandleNetworkException(peer, ex);
                        }
                    });
                }
                catch (NetworkException ex)
                {
                    Logger.LogWarning(ex, $"Could not enqueue lib announcement to {peer} " +
                                      $"- status {peer.ConnectionStatus}.");
                }
            }

            return(Task.CompletedTask);
        }
        public void UpdateLastKnownLib_Test()
        {
            var libAnnouncement = new LibAnnouncement
            {
                LibHeight = 100,
                LibHash   = HashHelper.ComputeFrom(100)
            };

            _grpcPeer.UpdateLastKnownLib(libAnnouncement);
            _grpcPeer.LastKnownLibHash.ShouldBe(libAnnouncement.LibHash);
            _grpcPeer.LastKnownLibHeight.ShouldBe(libAnnouncement.LibHeight);

            libAnnouncement = new LibAnnouncement
            {
                LibHeight = 101,
                LibHash   = HashHelper.ComputeFrom(101)
            };
            _grpcPeer.UpdateLastKnownLib(libAnnouncement);
            _grpcPeer.LastKnownLibHash.ShouldBe(libAnnouncement.LibHash);
            _grpcPeer.LastKnownLibHeight.ShouldBe(libAnnouncement.LibHeight);

            var wrongLibAnnouncement = new LibAnnouncement
            {
                LibHeight = 90,
                LibHash   = HashHelper.ComputeFrom(90)
            };

            _grpcPeer.UpdateLastKnownLib(wrongLibAnnouncement);
            _grpcPeer.LastKnownLibHash.ShouldBe(libAnnouncement.LibHash);
            _grpcPeer.LastKnownLibHeight.ShouldBe(libAnnouncement.LibHeight);
        }
示例#5
0
        public void UpdateLastKnownLib(LibAnnouncement libAnnouncement)
        {
            if (libAnnouncement.LibHeight <= LastKnownLibHeight)
            {
                return;
            }

            LastKnownLibHash   = libAnnouncement.LibHash;
            LastKnownLibHeight = libAnnouncement.LibHeight;
        }
示例#6
0
        public void EnqueueLibAnnouncement(LibAnnouncement libAnnouncement, Action <NetworkException> sendCallback)
        {
            if (!IsReady)
            {
                throw new NetworkException($"Dropping lib announcement, peer is not ready - {this}.",
                                           NetworkExceptionType.NotConnected);
            }

            _sendAnnouncementJobs.Post(new StreamJob
            {
                LibAnnouncement = libAnnouncement,
                SendCallback    = sendCallback
            });
        }
示例#7
0
        public Task ProcessLibAnnouncement(LibAnnouncement announcement, ServerCallContext context)
        {
            if (announcement?.LibHash == null)
            {
                Logger.LogError($"Received null or empty announcement from {context.GetPeerInfo()}.");
                return(Task.CompletedTask);
            }

            Logger.LogDebug($"Received lib announce hash: {announcement.LibHash}, height {announcement.LibHeight} from {context.GetPeerInfo()}.");

            var peer = _connectionService.GetPeerByPubkey(context.GetPublicKey());

            peer?.UpdateLastKnownLib(announcement);

            return(Task.CompletedTask);
        }
示例#8
0
        /// <summary>
        /// Send a lib announcement to the peer using the stream call.
        /// Note: this method is not thread safe.
        /// </summary>
        public async Task SendLibAnnouncementAsync(LibAnnouncement libAnnouncement)
        {
            if (_libAnnouncementStreamCall == null)
            {
                _libAnnouncementStreamCall = _client.LibAnnouncementBroadcastStream(new Metadata {
                    { GrpcConstants.SessionIdMetadataKey, OutboundSessionId }
                });
            }

            try
            {
                await _libAnnouncementStreamCall.RequestStream.WriteAsync(libAnnouncement);
            }
            catch (RpcException)
            {
                _libAnnouncementStreamCall.Dispose();
                _libAnnouncementStreamCall = null;

                throw;
            }
        }
示例#9
0
        public Task ProcessLibAnnouncementAsync(LibAnnouncement announcement, string peerPubkey)
        {
            if (announcement?.LibHash == null)
            {
                Logger.LogWarning($"Received null or empty announcement from {peerPubkey}.");
                return(Task.CompletedTask);
            }

            Logger.LogDebug(
                $"Received lib announce hash: {announcement.LibHash}, height {announcement.LibHeight} from {peerPubkey}.");

            var peer = TryGetPeerByPubkey(peerPubkey);

            peer.UpdateLastKnownLib(announcement);

            if (peer.SyncState != SyncState.Finished)
            {
                peer.SyncState = SyncState.Finished;
            }

            return(Task.CompletedTask);
        }