예제 #1
0
        /// <summary>
        /// Given an IP address, will create a handshake to the distant node for
        /// further communications.
        /// </summary>
        /// <returns>The created peer</returns>
        public async Task <GrpcPeer> DialPeerAsync(DnsEndPoint remoteEndpoint)
        {
            var client = await CreateClientAsync(remoteEndpoint);

            if (client == null)
            {
                return(null);
            }

            var handshake = await _handshakeProvider.GetHandshakeAsync();

            var handshakeReply = await CallDoHandshakeAsync(client, remoteEndpoint, handshake);

            // verify handshake
            if (handshakeReply.Error != HandshakeError.HandshakeOk)
            {
                Logger.LogWarning($"Handshake error: {remoteEndpoint} {handshakeReply.Error}.");
                await client.Channel.ShutdownAsync();

                return(null);
            }

            if (await _handshakeProvider.ValidateHandshakeAsync(handshakeReply.Handshake) !=
                HandshakeValidationResult.Ok)
            {
                Logger.LogWarning($"Connect error: {remoteEndpoint} {handshakeReply}.");
                await client.Channel.ShutdownAsync();

                return(null);
            }

            var peer = new GrpcPeer(client, remoteEndpoint, new PeerConnectionInfo
            {
                Pubkey          = handshakeReply.Handshake.HandshakeData.Pubkey.ToHex(),
                ConnectionTime  = TimestampHelper.GetUtcNow(),
                ProtocolVersion = handshakeReply.Handshake.HandshakeData.Version,
                SessionId       = handshakeReply.Handshake.SessionId.ToByteArray(),
                IsInbound       = false
            });

            peer.UpdateLastReceivedHandshake(handshakeReply.Handshake);

            peer.InboundSessionId = handshake.SessionId.ToByteArray();
            peer.UpdateLastSentHandshake(handshake);

            return(peer);
        }
예제 #2
0
        public async Task <GrpcPeer> DialBackPeerAsync(DnsEndPoint remoteEndpoint, Handshake handshake)
        {
            var client = CreateClient(remoteEndpoint);

            await PingNodeAsync(client, remoteEndpoint);

            var peer = new GrpcPeer(client, remoteEndpoint, new PeerConnectionInfo
            {
                Pubkey          = handshake.HandshakeData.Pubkey.ToHex(),
                ConnectionTime  = TimestampHelper.GetUtcNow(),
                SessionId       = handshake.SessionId.ToByteArray(),
                ProtocolVersion = handshake.HandshakeData.Version,
                IsInbound       = true
            });

            peer.UpdateLastReceivedHandshake(handshake);

            return(peer);
        }