コード例 #1
0
        public void AddBlacklistedPeer_ShouldReturnFalse()
        {
            var host = "12.34.56.67";

            _blackListProvider.AddHostToBlackList(host);
            _peerPool.AddHandshakingPeer(host, "somePubKey").ShouldBeFalse();
        }
コード例 #2
0
ファイル: PeerPoolTests.cs プロジェクト: xiangxingxing/AElf
        public void AddBlacklistedPeer_ShouldReturnFalse()
        {
            IPAddress ipAddress = IPAddress.Parse("12.34.56.67");

            _blackListProvider.AddIpToBlackList(ipAddress);
            _peerPool.AddHandshakingPeer(ipAddress, "somePubKey").ShouldBeFalse();
        }
コード例 #3
0
        public void AddHandshakingPeer_WithSameIp_ShouldAddToList()
        {
            var commonIp      = IPAddress.Parse("12.34.56.64");
            var mockPubkeyOne = "pub1";
            var mockPubkeyTwo = "pub2";

            _peerPool.AddHandshakingPeer(commonIp, mockPubkeyOne);
            _peerPool.AddHandshakingPeer(commonIp, mockPubkeyTwo);

            _peerPool.GetHandshakingPeers().TryGetValue(commonIp, out var peers);
            peers.Values.Count.ShouldBe(2);
            peers.Values.ShouldContain(mockPubkeyOne, mockPubkeyTwo);
        }
コード例 #4
0
        public void AddHandshakingPeer_LoopbackAddress_Test()
        {
            var handshakingPeerHost = "127.0.0.1";

            for (int i = 0; i < 3; i++)
            {
                var pubkey    = "pubkey" + i;
                var addResult = _peerPool.AddHandshakingPeer(handshakingPeerHost, pubkey);
                addResult.ShouldBeTrue();

                var handshakingPeers = _peerPool.GetHandshakingPeers();
                handshakingPeers.ShouldContainKey(handshakingPeerHost);
                handshakingPeers[handshakingPeerHost].ShouldContainKey(pubkey);
            }
        }
コード例 #5
0
        public void AddBlacklistedPeer_ShouldReturnFalse()
        {
            var host = "12.34.56.67";

            _blackListProvider.AddHostToBlackList(host, NetworkConstants.DefaultPeerRemovalSeconds);
            _peerPool.AddHandshakingPeer(host, "somePubKey").ShouldBeFalse();
        }
コード例 #6
0
        public async Task DoHandshake_AlreadyInHandshaking_Test()
        {
            AElfPeerEndpointHelper.TryParse(NetworkTestConstants.GoodPeerEndpoint, out var endpoint);
            var handshake = CreateHandshake();

            _peerPool.AddHandshakingPeer(endpoint.Host, handshake.HandshakeData.Pubkey.ToHex());

            var result = await _connectionService.DoHandshakeAsync(endpoint, handshake);

            result.Error.ShouldBe(HandshakeError.ConnectionRefused);

            _peerPool.GetHandshakingPeers().ShouldNotContainKey(endpoint.Host);
        }
コード例 #7
0
        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);
            }
        }