Esempio n. 1
0
        private JsonRpcResponse TestRequest <T>(IModule ethModule, string method, params string[] parameters) where T : IModule
        {
            RpcModuleProvider moduleProvider = new RpcModuleProvider(_configurationProvider.GetConfig <IJsonRpcConfig>());

            moduleProvider.Register <T>(ethModule);
            _jsonRpcService = new JsonRpcService(moduleProvider, _logManager);
            JsonRpcRequest  request  = RpcTest.GetJsonRequest(method, parameters);
            JsonRpcResponse response = _jsonRpcService.SendRequest(request);

            Assert.AreEqual(request.Id, response.Id);
            return(response);
        }
Esempio n. 2
0
        public static string TestSerializedRequest <T>(T module, string method, params string[] parameters) where T : class, IModule
        {
            IJsonRpcService        service  = BuildRpcService <T>(module);
            JsonRpcRequest         request  = GetJsonRequest(method, parameters);
            JsonRpcResponse        response = service.SendRequest(request);
            JsonSerializerSettings settings = new JsonSerializerSettings();

            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.Converters       = service.Converters.ToArray();
            string serialized = JsonConvert.SerializeObject(response, settings);

            TestContext.WriteLine(serialized.Replace("\"", "\\\""));
            return(serialized);
        }
        public void IncorrectMethodNameTest()
        {
            var netModule   = Substitute.For <INetModule>();
            var ethModule   = Substitute.For <IEthModule>();
            var web3Module  = Substitute.For <IWeb3Module>();
            var shhModule   = Substitute.For <IShhModule>();
            var nethmModule = Substitute.For <INethmModule>();

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

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

            var request  = GetJsonRequest("incorrect_method", null);
            var response = _jsonRpcService.SendRequest(request);

            Assert.AreEqual(response.Id, request.Id);
            Assert.AreEqual(response.Error.Code, _configurationProvider.GetConfig <IJsonRpcConfig>().ErrorCodes[ErrorType.MethodNotFound]);
            Assert.IsNull(response.Result);
            Assert.AreEqual(response.Jsonrpc, _configurationProvider.GetConfig <IJsonRpcConfig>().JsonRpcVersion);
        }
        public void CompileSolidityTest()
        {
            var netModule   = Substitute.For <INetModule>();
            var ethModule   = Substitute.For <IEthModule>();
            var web3Module  = Substitute.For <IWeb3Module>();
            var shhModule   = Substitute.For <IShhModule>();
            var nethmModule = Substitute.For <INethmModule>();

            nethmModule.nethm_compileSolidity(Arg.Any <string>()).ReturnsForAnyArgs(r => new ResultWrapper <string>()
            {
                Result = new Result()
                {
                    ResultType = ResultType.Success
                },
                Data =
                    "608060405234801561001057600080fd5b5060bb8061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820cb09d883ac888f0961fd8d82f8dae501d09d54f4bda397e8ca0fb9c05e2ec72a0029"
            });

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

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

            var parameters = new CompilerParameters
            {
                Contract =
                    "pragma solidity ^0.4.22; contract test { function multiply(uint a) public returns(uint d) {   return a * 7;   } }",
                EvmVersion = "byzantium",
                Optimize   = false,
                Runs       = 2
            };

            var request = GetJsonRequest("nethm_compileSolidity", new[] { parameters.ToJson() });

            var response = _jsonRpcService.SendRequest(request);

            TestContext.Write(response.Result);
            Assert.IsNotNull(response);
            Assert.IsNull(response.Error);
        }
        public void Web3ShaTest()
        {
            var netModule  = Substitute.For <INetModule>();
            var ethModule  = Substitute.For <IEthModule>();
            var web3Module = Substitute.For <IWeb3Module>();

            web3Module.web3_sha3(Arg.Any <Data>()).ReturnsForAnyArgs(x => new ResultWrapper <Data> {
                Result = new Result {
                    ResultType = ResultType.Success
                }, Data = new Data("abcdef")
            });
            var shhModule   = Substitute.For <IShhModule>();
            var nethmModule = Substitute.For <INethmModule>();

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

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

            var requestJson = GetJsonRequest("web3_sha3", new[] { "0x68656c6c6f20776f726c64" });
            var response    = _jsonRpcService.SendRequest(requestJson);

            Assert.AreEqual("0xabcdef", response.Result);
        }
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));
                }
            }
        }