public void RemoveHandshakingPeer_Last_ShouldRemoveEndpoint() { var commonIp = IPAddress.Parse("12.34.56.64"); var mockPubkeyOne = "pub1"; var mockPubkeyTwo = "pub2"; _peerPool.AddHandshakingPeer(commonIp, mockPubkeyOne); _peerPool.AddHandshakingPeer(commonIp, mockPubkeyTwo); _peerPool.RemoveHandshakingPeer(commonIp, mockPubkeyOne); _peerPool.GetHandshakingPeers().TryGetValue(commonIp, out _).ShouldBeTrue(); // remove last should remove entry _peerPool.RemoveHandshakingPeer(commonIp, mockPubkeyTwo); _peerPool.GetHandshakingPeers().TryGetValue(commonIp, out _).ShouldBeFalse(); }
public void RemoveHandshakingPeer_Test() { var commonHost = "12.34.56.64"; var mockPubkeyOne = "pub1"; var mockPubkeyTwo = "pub2"; _peerPool.RemoveHandshakingPeer(commonHost, mockPubkeyOne).ShouldBeFalse(); _peerPool.AddHandshakingPeer(commonHost, mockPubkeyOne); _peerPool.AddHandshakingPeer(commonHost, mockPubkeyTwo); _peerPool.RemoveHandshakingPeer(commonHost, "NotExist").ShouldBeFalse(); _peerPool.RemoveHandshakingPeer(commonHost, mockPubkeyOne).ShouldBeTrue(); _peerPool.GetHandshakingPeers().TryGetValue(commonHost, out _).ShouldBeTrue(); // remove last should remove entry _peerPool.RemoveHandshakingPeer(commonHost, mockPubkeyTwo).ShouldBeTrue(); _peerPool.GetHandshakingPeers().TryGetValue(commonHost, out _).ShouldBeFalse(); }
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); } }