public async Task DialBackPeer_Test() { AElfPeerEndpointHelper.TryParse("127.0.0.1:2000", out var endpoint); var handshake = await _handshakeProvider.GetHandshakeAsync(); var grpcPeer = await _peerDialer.DialBackPeerAsync(endpoint, handshake); grpcPeer.ShouldNotBeNull(); grpcPeer.CurrentBlockHash.ShouldBe(handshake.HandshakeData.BestChainHash); grpcPeer.CurrentBlockHeight.ShouldBe(handshake.HandshakeData.BestChainHeight); grpcPeer.LastKnownLibHeight.ShouldBe(handshake.HandshakeData.LastIrreversibleBlockHeight); grpcPeer.Info.Pubkey.ShouldBe(handshake.HandshakeData.Pubkey.ToHex()); grpcPeer.Info.ProtocolVersion.ShouldBe(handshake.HandshakeData.Version); }
public async Task <HandshakeReply> DoHandshakeAsync(DnsEndPoint endpoint, Handshake handshake) { // validate the handshake (signature, chain id...) var handshakeValidationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); if (handshakeValidationResult != HandshakeValidationResult.Ok) { var handshakeError = GetHandshakeError(handshakeValidationResult); return(new HandshakeReply { Error = handshakeError }); } var pubkey = handshake.HandshakeData.Pubkey.ToHex(); // remove any remaining connection to the peer (before the check // that we have room for more connections) var currentPeer = _peerPool.FindPeerByPublicKey(pubkey); if (currentPeer != null) { _peerPool.RemovePeer(pubkey); await currentPeer.DisconnectAsync(false); } try { // mark the (IP; pubkey) pair as currently handshaking if (!_peerPool.AddHandshakingPeer(endpoint.Host, pubkey)) { return new HandshakeReply { Error = HandshakeError.ConnectionRefused } } ; // create the connection to the peer var peerEndpoint = new AElfPeerEndpoint(endpoint.Host, handshake.HandshakeData.ListeningPort); var grpcPeer = await _peerDialer.DialBackPeerAsync(peerEndpoint, handshake); // add the new peer to the pool if (!_peerPool.TryAddPeer(grpcPeer)) { Logger.LogWarning($"Stopping connection, peer already in the pool {grpcPeer.Info.Pubkey}."); await grpcPeer.DisconnectAsync(false); return(new HandshakeReply { Error = HandshakeError.RepeatedConnection }); } Logger.LogDebug($"Added to pool {grpcPeer.RemoteEndpoint} - {grpcPeer.Info.Pubkey}."); // send back our handshake var replyHandshake = await _handshakeProvider.GetHandshakeAsync(); grpcPeer.InboundSessionId = replyHandshake.SessionId.ToByteArray(); grpcPeer.UpdateLastSentHandshake(replyHandshake); return(new HandshakeReply { Handshake = replyHandshake, Error = HandshakeError.HandshakeOk }); } finally { // remove the handshaking mark (IP; pubkey) _peerPool.RemoveHandshakingPeer(endpoint.Host, pubkey); } }