public void GetWorkTest() { var netModule = Substitute.For <INetModule>(); var ethModule = Substitute.For <IEthModule>(); var web3Module = Substitute.For <IWeb3Module>(); ethModule.eth_getWork().ReturnsForAnyArgs(x => new ResultWrapper <IEnumerable <Data> > { Result = new Result { ResultType = ResultType.Success }, Data = new [] { new Data("t1"), new Data("t2") } }); 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 requestJson = GetJsonRequest("eth_getWork", null); var rawResponse = _jsonRpcService.SendRequest(requestJson); var response = _jsonSerializer.Deserialize <JsonRpcResponse>(rawResponse); Assert.IsTrue(response.Result.ToString().Contains("0xt1")); Assert.IsTrue(response.Result.ToString().Contains("0xt2")); }
public void NetVersionTest() { 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" }); 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("net_version", null); var response = _jsonRpcService.SendRequest(request); Assert.AreEqual(response.Id, request.Id); Assert.AreEqual(response.Result, "1"); Assert.IsNull(response.Error); Assert.AreEqual(_configurationProvider.GetConfig <IJsonRpcConfig>().JsonRpcVersion, response.Jsonrpc); }
public void NetPeerCountTest() { var netModule = Substitute.For <INetModule>(); netModule.net_peerCount().ReturnsForAnyArgs(x => new ResultWrapper <Quantity> { Result = new Result { ResultType = ResultType.Success }, Data = new Quantity(2) }); 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 requestJson = GetJsonRequest("net_peerCount", null); var response = _jsonRpcService.SendRequest(requestJson); var quantity = new Quantity(); quantity.FromJson(response.Result.ToString()); Assert.AreEqual(quantity.GetValue(), new BigInteger(2)); }
public JsonRpcProcessor(IJsonRpcService jsonRpcService, IJsonSerializer jsonSerializer, IJsonRpcConfig jsonRpcConfig, IFileSystem fileSystem, ILogManager logManager) { _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); if (fileSystem == null) { throw new ArgumentNullException(nameof(fileSystem)); } _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig)); _jsonRpcService = jsonRpcService ?? throw new ArgumentNullException(nameof(jsonRpcService)); _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer)); if (_jsonRpcConfig.RpcRecorderEnabled) { if (_logger.IsWarn) { _logger.Warn("Enabling JSON RPC diagnostics recorder - this will affect performance and should be only used in a diagnostics mode."); } string recorderBaseFilePath = _jsonRpcConfig.RpcRecorderBaseFilePath.GetApplicationResourcePath(); _recorder = new Recorder(recorderBaseFilePath, fileSystem, _logger); } BuildTraceJsonSerializer(); _localStats = new JsonRpcLocalStats(Timestamper.Default, jsonRpcConfig, logManager); }
public void GetBlockByNumberTest() { var netModule = Substitute.For <INetModule>(); var ethModule = Substitute.For <IEthModule>(); var web3Module = Substitute.For <IWeb3Module>(); ethModule.eth_getBlockByNumber(Arg.Any <BlockParameter>(), true).ReturnsForAnyArgs(x => new ResultWrapper <Block> { Result = new Result { ResultType = ResultType.Success }, Data = new Block { Number = new Quantity(2) } }); 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("eth_getBlockByNumber", new[] { "0x1b4", "true" }); var response = _jsonRpcService.SendRequest(request); Assert.IsTrue(response.Result.ToString().Contains("0x02")); }
public void GetWorkTest() { var netModule = Substitute.For <INetModule>(); var ethModule = Substitute.For <IEthModule>(); var web3Module = Substitute.For <IWeb3Module>(); ethModule.eth_getWork().ReturnsForAnyArgs(x => new ResultWrapper <IEnumerable <Data> > { Result = new Result { ResultType = ResultType.Success }, Data = new [] { new Data("aa"), new Data("01") } }); 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("eth_getWork", null); var response = _jsonRpcService.SendRequest(request); Assert.Contains("0xaa", (List <object>)response.Result); Assert.Contains("0x01", (List <object>)response.Result); }
public static string TestSerializedRequest <T>(IReadOnlyCollection <JsonConverter> converters, T module, string method, params string[] parameters) where T : class, IModule { IJsonRpcService service = BuildRpcService(module); JsonRpcRequest request = GetJsonRequest(method, parameters); JsonRpcResponse response = service.SendRequestAsync(request).Result; EthereumJsonSerializer serializer = new EthereumJsonSerializer(); foreach (JsonConverter converter in converters) { serializer.RegisterConverter(converter); } Stream stream = new MemoryStream(); serializer.Serialize(stream, response); // for coverage (and to prove that it does not throw Stream indentedStream = new MemoryStream(); serializer.Serialize(indentedStream, response, true); stream.Seek(0, SeekOrigin.Begin); string serialized = new StreamReader(stream).ReadToEnd(); TestContext.Out?.WriteLine("Serialized:"); TestContext.Out?.WriteLine(serialized); return(serialized); }
public static JsonRpcResponse TestRequest <T>(T module, string method, params string[] parameters) where T : class, IModule { IJsonRpcService service = BuildRpcService <T>(module); JsonRpcRequest request = GetJsonRequest(method, parameters); return(service.SendRequest(request)); }
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); }
public MainController(IJsonRpcProcessor jsonRpcProcessor, IJsonRpcService jsonRpcService) { _jsonRpcProcessor = jsonRpcProcessor; if (_serializer == null) { lock (_lockObject) { if (_serializer == null) { _jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; foreach (var converter in jsonRpcService.Converters) { _jsonSettings.Converters.Add(converter); } _serializer = JsonSerializer.Create(_jsonSettings); } } } }
public static JsonRpcResponse TestRequest <T>(T module, string method, params string[] parameters) where T : class, IModule { IJsonRpcService service = BuildRpcService(module); JsonRpcRequest request = GetJsonRequest(method, parameters); return(service.SendRequestAsync(request, JsonRpcContext.Http).Result); }
public JsonRpcProcessor(IJsonRpcService jsonRpcService, IJsonSerializer jsonSerializer, ILogManager logManager) { _jsonRpcService = jsonRpcService ?? throw new ArgumentNullException(nameof(jsonRpcService)); _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer)); _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); BuildTraceJsonSerializer(); }
public void Initialize() { IJsonRpcService service = Substitute.For <IJsonRpcService>(); service.SendRequestAsync(Arg.Any <JsonRpcRequest>()).Returns(ci => new JsonRpcResponse { Id = ci.Arg <JsonRpcRequest>().Id, Result = null, JsonRpc = ci.Arg <JsonRpcRequest>().JsonRpc }); _jsonRpcProcessor = new JsonRpcProcessor(service, new EthereumJsonSerializer(), new JsonRpcConfig(), LimboLogs.Instance); }
/// <inheritdoc /> public void ReleaseService(IJsonRpcService service) { service.RequestContext = null; // Let autofac take care of the disposal if (scopeDict.TryRemove(service, out var subScope)) { subScope.Dispose(); } }
private JsonRpcResponse TestRequest<T>(T module, string method, params string[] parameters) where T : IRpcModule { RpcModuleProvider moduleProvider = new RpcModuleProvider(new FileSystem(), _configurationProvider.GetConfig<IJsonRpcConfig>(), LimboLogs.Instance); moduleProvider.Register(new SingletonModulePool<T>(new SingletonFactory<T>(module), true)); _jsonRpcService = new JsonRpcService(moduleProvider, _logManager); JsonRpcRequest request = RpcTest.GetJsonRequest(method, parameters); JsonRpcResponse response = _jsonRpcService.SendRequestAsync(request, JsonRpcContext.Http).Result; Assert.AreEqual(request.Id, response.Id); return response; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpcProcessor jsonRpcProcessor, IJsonRpcService jsonRpcService) { _jsonSerializer = new EthereumJsonSerializer(); foreach (JsonConverter converter in jsonRpcService.Converters) { _jsonSerializer.RegisterConverter(converter); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("Cors"); var initConfig = app.ApplicationServices.GetService <IConfigProvider>().GetConfig <IInitConfig>(); if (initConfig.WebSocketsEnabled) { app.UseWebSockets(); app.UseWhen(ctx => ctx.WebSockets.IsWebSocketRequest && ctx.Request.Path.HasValue && ctx.Request.Path.Value.StartsWith("/ws"), builder => builder.UseWebSocketsModules()); } app.Use(async(ctx, next) => { if (ctx.Request.Method == "GET") { await ctx.Response.WriteAsync("Nethermind JSON RPC"); return; } if (ctx.Request.Method != "POST") { return; } using var reader = new StreamReader(ctx.Request.Body, Encoding.UTF8); var request = await reader.ReadToEndAsync(); var result = await jsonRpcProcessor.ProcessAsync(request); if (result.IsCollection) { _jsonSerializer.Serialize(ctx.Response.Body, result.Responses); } else { _jsonSerializer.Serialize(ctx.Response.Body, result.Response); } await ctx.Response.CompleteAsync(); }); }
public JsonRpcServer(IJsonRpcService service, JsonRpcServerConfiguration config) { Config = config; Listener = new HttpListener(); Listener.AuthenticationSchemes = AuthenticationSchemes.Basic | AuthenticationSchemes.Anonymous; foreach (var prefix in Config.Prefixes) { Listener.Prefixes.Add(prefix); } Service = service; }
public MainController(IJsonRpcProcessor jsonRpcProcessor, IJsonRpcService jsonRpcService) { _jsonRpcProcessor = jsonRpcProcessor; _jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; foreach (var converter in jsonRpcService.Converters) { _jsonSettings.Converters.Add(converter); } }
/// <inheritdoc /> public void ReleaseService(IJsonRpcService service) { if (service == null) { throw new ArgumentNullException(nameof(service)); } // Basic cleanup. service.RequestContext = null; var disposable = service as IDisposable; disposable?.Dispose(); }
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.SendRequestAsync(request).Result; Assert.AreEqual(request.Id, response.Id); return(response); }
public MainController(ILogManager logManager, IJsonRpcService jsonRpcService, IJsonSerializer jsonSerializer) { _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); _jsonRpcService = jsonRpcService ?? throw new ArgumentNullException(nameof(jsonRpcService)); _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer)); _jsonSettings = new JsonSerializerSettings(); _jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); for (int i = 0; i < _jsonRpcService.Converters.Count; i++) { _jsonSettings.Converters.Add(_jsonRpcService.Converters[i]); } }
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 JsonRpcSocketsClient( string clientName, ISocketHandler handler, RpcEndpoint endpointType, IJsonRpcProcessor jsonRpcProcessor, IJsonRpcService jsonRpcService, IJsonRpcLocalStats jsonRpcLocalStats, IJsonSerializer jsonSerializer, JsonRpcUrl?url = null) : base(clientName, handler, jsonSerializer) { _jsonRpcProcessor = jsonRpcProcessor; _jsonRpcService = jsonRpcService; _jsonRpcLocalStats = jsonRpcLocalStats; _jsonRpcContext = new JsonRpcContext(endpointType, this, url); }
public JsonRpcProcessor(IJsonRpcService jsonRpcService, IJsonSerializer jsonSerializer, ILogManager logManager) { _jsonRpcService = jsonRpcService ?? throw new ArgumentNullException(nameof(jsonRpcService)); _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer)); var jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; foreach (var converter in _jsonRpcService.Converters) { jsonSettings.Converters.Add(converter); } _traceSerializer = JsonSerializer.Create(jsonSettings); _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); }
public static string TestSerializedRequest <T>(IReadOnlyCollection <JsonConverter> converters, T module, string method, params string[] parameters) where T : class, IModule { IJsonRpcService service = BuildRpcService(module); JsonRpcRequest request = GetJsonRequest(method, parameters); JsonRpcResponse response = service.SendRequestAsync(request).Result; var serializer = new EthereumJsonSerializer(); foreach (var converter in converters) { serializer.RegisterConverter(converter); } var serialized = serializer.Serialize(response); TestContext.Out?.WriteLine("Serialized:"); TestContext.Out?.WriteLine(serialized); 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 static string TestSerializedRequest <T>(IReadOnlyCollection <JsonConverter> converters, T module, string method, params string[] parameters) where T : class, IRpcModule { IJsonRpcService service = BuildRpcService(module, converters); JsonRpcRequest request = GetJsonRequest(method, parameters); JsonRpcContext context = new JsonRpcContext(RpcEndpoint.Http); if (module is IContextAwareRpcModule contextAwareModule && contextAwareModule.Context != null) { context = contextAwareModule.Context; } JsonRpcResponse response = service.SendRequestAsync(request, context).Result; EthereumJsonSerializer serializer = new(); foreach (JsonConverter converter in converters) { serializer.RegisterConverter(converter); } Stream stream = new MemoryStream(); long size = serializer.Serialize(stream, response); // for coverage (and to prove that it does not throw Stream indentedStream = new MemoryStream(); serializer.Serialize(indentedStream, response, true); stream.Seek(0, SeekOrigin.Begin); string serialized = new StreamReader(stream).ReadToEnd(); TestContext.Out?.WriteLine("Serialized:"); TestContext.Out?.WriteLine(serialized); size.Should().Be(serialized.Length); return(serialized); }
public void Initialize() { IJsonRpcService service = Substitute.For <IJsonRpcService>(); service.SendRequestAsync(Arg.Any <JsonRpcRequest>()).Returns(ci => _returnErrors ? (JsonRpcResponse) new JsonRpcErrorResponse { Id = ci.Arg <JsonRpcRequest>().Id } : new JsonRpcSuccessResponse { Id = ci.Arg <JsonRpcRequest>().Id }); service.GetErrorResponse(0, null).ReturnsForAnyArgs(_errorResponse); service.Converters.Returns(new JsonConverter[] { new AddressConverter() }); // just to test converter loader _fileSystem = Substitute.For <IFileSystem>(); /* we enable recorder always to have an easy smoke test for recording * and this is fine because recorder is non-critical component */ JsonRpcConfig configWithRecorder = new JsonRpcConfig(); configWithRecorder.RpcRecorderEnabled = true; _jsonRpcProcessor = new JsonRpcProcessor(service, new EthereumJsonSerializer(), configWithRecorder, _fileSystem, LimboLogs.Instance); }
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); }