Esempio n. 1
0
        public string SendRequest(string request)
        {
            try
            {
                var rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(request);
                if (rpcRequest.Model == null && rpcRequest.Collection == null)
                {
                    var reponse = GetErrorResponse(ErrorType.InvalidRequest, "Invalid request", null, null);
                    return(_jsonSerializer.Serialize(reponse));
                }

                if (rpcRequest.Model != null)
                {
                    var response          = SendRequest(rpcRequest.Model, request);
                    var serializedReponse = _jsonSerializer.Serialize(response);

                    _logger.Debug($"Successfull request processing, method: {rpcRequest.Model.Method ?? "none"}, id: {rpcRequest.Model.Id ?? "none"}, result: {serializedReponse}");
                    return(serializedReponse);
                }

                var responses = rpcRequest.Collection.Select(x => SendRequest(x, request)).ToArray();
                return(_jsonSerializer.Serialize(responses));
            }
            catch (Exception ex)
            {
                _logger.Error($"Error during parsing/validation, request: {request}", ex);
                var response = GetErrorResponse(ErrorType.ParseError, "Incorrect message", null, null);
                return(_jsonSerializer.Serialize(response));
            }
        }
        public void RequestCollectionTest()
        {
            var netModule  = Substitute.For <INetModule>();
            var ethModule  = Substitute.For <IEthModule>();
            var web3Module = Substitute.For <IWeb3Module>();

            netModule.net_version().ReturnsForAnyArgs(x => new ResultWrapper <string> {
                Result = new Result {
                    ResultType = ResultType.Success
                }, Data = "1"
            });
            ethModule.eth_protocolVersion().ReturnsForAnyArgs(x => new ResultWrapper <string> {
                Result = new Result {
                    ResultType = ResultType.Success
                }, Data = "1"
            });
            var shhModule = Substitute.For <IShhModule>();

            var moduleProvider = new ModuleProvider(_configurationProvider, netModule, ethModule, web3Module, shhModule);

            _jsonSerializer = new JsonSerializer(_logManager);
            _jsonRpcService = new JsonRpcService(_jsonSerializer, moduleProvider, _configurationProvider, _logManager);

            var netRequestJson = GetJsonRequest("net_version", null);
            var ethRequestJson = GetJsonRequest("eth_protocolVersion", null);

            var jsonRequest = $"[{netRequestJson},{ethRequestJson}]";
            var rawResponse = _jsonRpcService.SendRequest(jsonRequest);
            var response    = _jsonSerializer.DeserializeObjectOrArray <JsonRpcResponse>(rawResponse);

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Collection);
            Assert.IsNull(response.Model);
        }
Esempio n. 3
0
        public async Task <JsonResult> Post()
        {
            using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var body = await reader.ReadToEndAsync();

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Received request: {body}");
                }


                (JsonRpcRequest Model, IEnumerable <JsonRpcRequest> Collection)rpcRequest;
                try
                {
                    rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(body);
                }
                catch (Exception ex)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Error during parsing/validation, request: {body}", ex);
                    }
                    var response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                    return(new JsonResult(response));
                }

                if (rpcRequest.Model != null)
                {
                    return(new JsonResult(_jsonRpcService.SendRequest(rpcRequest.Model)));
                }

                if (rpcRequest.Collection != null)
                {
                    List <JsonRpcResponse> responses = new List <JsonRpcResponse>();
                    foreach (JsonRpcRequest jsonRpcRequest in rpcRequest.Collection)
                    {
                        responses.Add(_jsonRpcService.SendRequest(jsonRpcRequest));
                    }

                    return(new JsonResult(responses));
                }

                {
                    var response = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Incorrect request");
                    return(new JsonResult(response));
                }
            }
        }
        public async Task <JsonRpcResult> ProcessAsync(string request)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace($"Received JSON RPC request: {request}");
            }

            (JsonRpcRequest Model, IEnumerable <JsonRpcRequest> Collection)rpcRequest;
            try
            {
                rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(request);
            }
            catch (Exception ex)
            {
                Metrics.JsonRpcRequestDeserializationFailures++;
                if (_logger.IsError)
                {
                    _logger.Error($"Error during parsing/validation, request: {request}", ex);
                }
                var response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Model != null)
            {
                Metrics.JsonRpcRequests++;
                var response = await _jsonRpcService.SendRequestAsync(rpcRequest.Model);

                if (response.Error != null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Failed to respond to {rpcRequest.Model.Method} {response.Error.Message}");
                    }
                    Metrics.JsonRpcErrors++;
                }
                else
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Responded to {rpcRequest.Model.Method}");
                    }
                    Metrics.JsonRpcSuccesses++;
                }

                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Collection != null)
            {
                var responses = new List <JsonRpcResponse>();
                foreach (var jsonRpcRequest in rpcRequest.Collection)
                {
                    Metrics.JsonRpcRequests++;
                    var response = await _jsonRpcService.SendRequestAsync(jsonRpcRequest);

                    if (response.Error != null)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Failed to respond to {jsonRpcRequest.Method} {response.Error.Message}");
                        }
                        Metrics.JsonRpcErrors++;
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Responded to {jsonRpcRequest.Method}");
                        }
                        Metrics.JsonRpcSuccesses++;
                    }

                    responses.Add(response);
                }

                TraceResult(responses.ToArray());
                return(JsonRpcResult.Collection(responses));
            }

            Metrics.JsonRpcInvalidRequests++;
            var errorResponse = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Invalid request");

            TraceResult(errorResponse);
            return(JsonRpcResult.Single(errorResponse));
        }
Esempio n. 5
0
        public async Task <JsonRpcResult> ProcessAsync(string request)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            (JsonRpcRequest Model, List <JsonRpcRequest> Collection)rpcRequest;
            try
            {
                rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(request);
            }
            catch (Exception ex)
            {
                Metrics.JsonRpcRequestDeserializationFailures++;
                if (_logger.IsError)
                {
                    _logger.Error($"Error during parsing/validation, request: {request}", ex);
                }
                JsonRpcResponse response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Model != null)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"JSON RPC request {rpcRequest.Model.Method}");
                }

                Metrics.JsonRpcRequests++;
                JsonRpcResponse response = await _jsonRpcService.SendRequestAsync(rpcRequest.Model);

                if (response.Error != null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Failed to respond to {rpcRequest.Model.Method} {response.Error.Message}");
                    }
                    Metrics.JsonRpcErrors++;
                }
                else
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Responded to {rpcRequest.Model.Method}");
                    }
                    Metrics.JsonRpcSuccesses++;
                }

                TraceResult(response);
                stopwatch.Stop();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"  {rpcRequest.Model.Method} handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
                }
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Collection != null)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"{rpcRequest.Collection.Count} JSON RPC requests");
                }

                var       responses          = new List <JsonRpcResponse>();
                int       requestIndex       = 0;
                Stopwatch singleRequestWatch = new Stopwatch();
                foreach (JsonRpcRequest jsonRpcRequest in rpcRequest.Collection)
                {
                    singleRequestWatch.Start();

                    Metrics.JsonRpcRequests++;
                    JsonRpcResponse response = await _jsonRpcService.SendRequestAsync(jsonRpcRequest);

                    if (response.Error != null)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Failed to respond to {jsonRpcRequest.Method} {response.Error.Message}");
                        }
                        Metrics.JsonRpcErrors++;
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Responded to {jsonRpcRequest.Method}");
                        }
                        Metrics.JsonRpcSuccesses++;
                    }

                    singleRequestWatch.Stop();
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"  {requestIndex++}/{rpcRequest.Collection.Count} JSON RPC request - {jsonRpcRequest.Method} handled after {singleRequestWatch.Elapsed.TotalMilliseconds}");
                    }
                    responses.Add(response);
                }

                TraceResult(responses);
                stopwatch.Stop();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"  {rpcRequest.Collection.Count} requests handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
                }
                return(JsonRpcResult.Collection(responses));
            }

            Metrics.JsonRpcInvalidRequests++;
            JsonRpcResponse errorResponse = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Invalid request");

            TraceResult(errorResponse);
            stopwatch.Stop();
            if (_logger.IsDebug)
            {
                _logger.Debug($"  Failed request handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
            return(JsonRpcResult.Single(errorResponse));
        }
Esempio n. 6
0
        public async Task <JsonResult> Post()
        {
            using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var body = await reader.ReadToEndAsync();

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Received request: {body}");
                }

                (JsonRpcRequest Model, IEnumerable <JsonRpcRequest> Collection)rpcRequest;
                try
                {
                    rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(body);
                }
                catch (Exception ex)
                {
                    Metrics.JsonRpcRequestDeserializationFailures++;
                    if (_logger.IsError)
                    {
                        _logger.Error($"Error during parsing/validation, request: {body}", ex);
                    }
                    var response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                    return(new JsonResult(response));
                }

                if (rpcRequest.Model != null)
                {
                    Metrics.JsonRpcRequests++;
                    var response = _jsonRpcService.SendRequest(rpcRequest.Model);
                    if (response.Error != null)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Failed to respond to {rpcRequest.Model.Method} {response.Error.Message}");
                        }
                        Metrics.JsonRpcErrors++;
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Responded to {rpcRequest.Model.Method}");
                        }
                        Metrics.JsonRpcSuccesses++;
                    }

                    return(new JsonResult(response, _jsonSettings));
                }

                if (rpcRequest.Collection != null)
                {
                    List <JsonRpcResponse> responses = new List <JsonRpcResponse>();
                    foreach (JsonRpcRequest jsonRpcRequest in rpcRequest.Collection)
                    {
                        Metrics.JsonRpcRequests++;
                        JsonRpcResponse response = _jsonRpcService.SendRequest(jsonRpcRequest);
                        if (response.Error != null)
                        {
                            if (_logger.IsError)
                            {
                                _logger.Error($"Failed to respond to {jsonRpcRequest.Method} {response.Error.Message}");
                            }
                            Metrics.JsonRpcErrors++;
                        }
                        else
                        {
                            if (_logger.IsDebug)
                            {
                                _logger.Debug($"Responded to {jsonRpcRequest.Method}");
                            }
                            Metrics.JsonRpcSuccesses++;
                        }

                        responses.Add(response);
                    }

                    return(new JsonResult(responses, _jsonSettings));
                }

                {
                    Metrics.JsonRpcInvalidRequests++;
                    var response = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Invalid request");
                    return(new JsonResult(response, _jsonSettings));
                }
            }
        }