public async Task UnaryServerHandler_Auth_Failed()
        {
            var helper = new MockServiceBuilder();

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => Task.FromResult("ok"));

            helper.ServiceDefinition = helper.ServiceDefinition.Intercept(_authInterceptor);
            _server = helper.GetServer();
            _server.Start();
            _channel = helper.GetChannel();

            await ShouldBeCancelRpcExceptionAsync(async() =>
                                                  await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), ""));

            var method = new Method <string, string>(MethodType.Unary, MockServiceBuilder.ServiceName, "Unary",
                                                     Marshallers.StringMarshaller, Marshallers.StringMarshaller);

            var peer = _peerPool.GetPeersByHost("127.0.0.1").First();

            ((GrpcPeer)peer).InboundSessionId = new byte[] { 1, 2, 3 };
            var callInvoker = helper.GetChannel().Intercept(metadata =>
            {
                metadata = new Metadata
                {
                    { GrpcConstants.PubkeyMetadataKey, peer.Info.Pubkey }
                };
                return(metadata);
            });

            await ShouldBeCancelRpcExceptionAsync(async() =>
                                                  await callInvoker.AsyncUnaryCall(method, "localhost", new CallOptions(), ""));

            callInvoker = helper.GetChannel().Intercept(metadata =>
            {
                metadata = new Metadata
                {
                    { GrpcConstants.PubkeyMetadataKey, peer.Info.Pubkey },
                    { GrpcConstants.SessionIdMetadataKey, new byte[] { 4, 5, 6 } }
                };
                return(metadata);
            });

            await ShouldBeCancelRpcExceptionAsync(async() =>
                                                  await callInvoker.AsyncUnaryCall(method, "localhost", new CallOptions(), ""));

            ((GrpcPeer)peer).InboundSessionId = null;
            await ShouldBeCancelRpcExceptionAsync(async() =>
                                                  await callInvoker.AsyncUnaryCall(method, "localhost", new CallOptions(), ""));
        }
Пример #2
0
        public void GetPeersByHost_ShouldReturnAllPeers_WithSameHost()
        {
            var    commonHost     = "12.34.56.67";
            string commonPort     = "1900";
            string commonEndpoint = commonHost + ":" + commonPort;

            _peerPool.TryAddPeer(CreatePeer(commonEndpoint));
            _peerPool.TryAddPeer(CreatePeer(commonEndpoint));
            _peerPool.TryAddPeer(CreatePeer("12.34.56.64:1900"));
            _peerPool.TryAddPeer(CreatePeer("12.34.56.61:1900"));

            var peersWithSameHost = _peerPool.GetPeersByHost(commonHost);

            peersWithSameHost.Count.ShouldBe(2);
        }
Пример #3
0
        public void QueryPeers_Test()
        {
            var    commonHost     = "12.34.56.67";
            string commonPort     = "1900";
            string commonEndpoint = commonHost + ":" + commonPort;

            var peer1 = CreatePeer(commonEndpoint);

            _peerPool.TryAddPeer(peer1);
            var peer2 = CreatePeer(commonEndpoint);

            _peerPool.TryAddPeer(peer2);
            var peer3 = CreatePeer("12.34.56.64:1900");

            _peerPool.TryAddPeer(peer3);
            var peer4 = CreatePeer("12.34.56.61:1900", isReady: false);

            _peerPool.TryAddPeer(peer4);

            var peers = _peerPool.GetPeers();

            peers.Count.ShouldBe(3);

            peers = _peerPool.GetPeers(true);
            peers.Count.ShouldBe(4);

            peers = _peerPool.GetPeersByHost(commonHost);
            peers.Count.ShouldBe(2);
            peers.ShouldContain(peer1);
            peers.ShouldContain(peer2);

            var peer = _peerPool.FindPeerByEndpoint(peer3.RemoteEndpoint);

            peer.ShouldBe(peer3);

            peer = _peerPool.FindPeerByPublicKey(peer3.Info.Pubkey);
            peer.ShouldBe(peer3);
        }
        public async Task ProcessPeerInvalidTransactionAsync(Hash transactionId)
        {
            var knowsTransactionPeers = _peerPool.GetPeers().Where(p => p.KnowsTransaction(transactionId)).ToList();
            var toRemovePeerPubkey    = new List <string>();

            foreach (var knowsTransactionPeer in knowsTransactionPeers)
            {
                var host = knowsTransactionPeer.RemoteEndpoint.Host;
                if (_peerInvalidTransactionProvider.TryMarkInvalidTransaction(host, transactionId))
                {
                    continue;
                }

                var peers = _peerPool.GetPeersByHost(host);
                toRemovePeerPubkey.AddRange(peers.Select(p => p.Info.Pubkey));

                _peerInvalidTransactionProvider.TryRemoveInvalidRecord(host);
            }

            foreach (var pubkey in toRemovePeerPubkey)
            {
                await _networkService.RemovePeerByPubkeyAsync(pubkey);
            }
        }