public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { IEthModule ethModule = (IEthModule)await _rpcModuleProvider.Rent("eth_syncing", false); INetModule netModule = (INetModule)await _rpcModuleProvider.Rent("net_peerCount", false); try { long netPeerCount = (long)netModule.net_peerCount().GetData(); SyncingResult ethSyncing = (SyncingResult)ethModule.eth_syncing().GetData(); if (ethSyncing.IsSyncing == false && netPeerCount > 0) { return(HealthCheckResult.Healthy(description: $"The node is now fully synced with a network, number of peers: {netPeerCount}")); } else if (ethSyncing.IsSyncing == false && netPeerCount == 0) { return(HealthCheckResult.Unhealthy(description: $"The node has 0 peers connected")); } return(HealthCheckResult.Unhealthy(description: $"The node is still syncing, CurrentBlock: {ethSyncing.CurrentBlock}, HighestBlock: {ethSyncing.HighestBlock}, Peers: {netPeerCount}")); } catch (Exception ex) { return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); } finally { _rpcModuleProvider.Return("eth_syncing", ethModule); _rpcModuleProvider.Return("net_peerCount", netModule); } }
public void GetNewFilterTest() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_newFilter(Arg.Any <Filter>()).ReturnsForAnyArgs(x => ResultWrapper <UInt256?> .Success(1)); var parameters = new { fromBlock = "0x1", toBlock = "latest", address = "0x1f88f1f195afa192cfee860698584c030f4c9db2", topics = new List <object> { "0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b", null, new[] { "0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc" } } }; JsonRpcSuccessResponse response = TestRequest(ethModule, "eth_newFilter", JsonConvert.SerializeObject(parameters)) as JsonRpcSuccessResponse; Assert.AreEqual(UInt256.One, response?.Result); }
public void Can_rent_and_return_in_a_loop(bool canBeShared) { for (int i = 0; i < 1000; i++) { IEthModule ethModule = _modulePool.GetModule(canBeShared); _modulePool.ReturnModule(ethModule); } }
public void Ensure_returning_shared_does_not_change_concurrency() { IEthModule shared = _modulePool.GetModule(true); _modulePool.ReturnModule(shared); _modulePool.GetModule(false); Assert.Throws <TimeoutException>(() => _modulePool.GetModule(false)); }
public void Case_sensitivity_test() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_chainId().ReturnsForAnyArgs(ResultWrapper <ulong> .Success(1ul)); TestRequest(ethModule, "eth_chainID").Should().BeOfType <JsonRpcErrorResponse>(); TestRequest(ethModule, "eth_chainId").Should().BeOfType <JsonRpcSuccessResponse>(); }
public async Task Ensure_returning_shared_does_not_change_concurrency() { IEthModule shared = await _modulePool.GetModule(true); _modulePool.ReturnModule(shared); await _modulePool.GetModule(false); Assert.ThrowsAsync <ModuleRentalTimeoutException>(() => _modulePool.GetModule(false)); }
public void Eth_module_populates_size_when_returning_block_data() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_getBlockByNumber(Arg.Any <BlockParameter>(), true).ReturnsForAnyArgs(x => ResultWrapper <BlockForRpc> .Success(new BlockForRpc(Build.A.Block.WithNumber(2).TestObject, true))); JsonRpcSuccessResponse response = TestRequest(ethModule, "eth_getBlockByNumber", "0x1b4", "true") as JsonRpcSuccessResponse; Assert.AreEqual(513L, (response?.Result as BlockForRpc)?.Size); }
public void GetBlockByNumberTest() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_getBlockByNumber(Arg.Any <BlockParameter>(), true).ReturnsForAnyArgs(x => ResultWrapper <BlockForRpc> .Success(new BlockForRpc(Build.A.Block.WithNumber(2).TestObject, true))); JsonRpcResponse response = TestRequest <IEthModule>(ethModule, "eth_getBlockByNumber", "0x1b4", "true"); Assert.AreEqual(2L, (response.Result as BlockForRpc)?.Number); }
public void GetWorkTest() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_getWork().ReturnsForAnyArgs(x => ResultWrapper <IEnumerable <Data> > .Success(new[] { new Data("aa"), new Data("01") })); JsonRpcResponse response = TestRequest <IEthModule>(ethModule, "eth_getWork"); Assert.Contains("0xaa", (List <object>)response.Result); Assert.Contains("0x01", (List <object>)response.Result); }
public void CanHandleOptionalArguments() { EthereumJsonSerializer serializer = new EthereumJsonSerializer(); string serialized = serializer.Serialize(new TransactionForRpc()); IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_call(Arg.Any <TransactionForRpc>()).ReturnsForAnyArgs(x => ResultWrapper <byte[]> .Success(new byte[] { 1 })); JsonRpcResponse response = TestRequest <IEthModule>(ethModule, "eth_call", serialized); Assert.AreEqual(1, (response.Result as byte[]).Length); }
public void CanHandleOptionalArguments() { EthereumJsonSerializer serializer = new EthereumJsonSerializer(); string serialized = serializer.Serialize(new TransactionForRpc()); IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_call(Arg.Any <TransactionForRpc>()).ReturnsForAnyArgs(x => ResultWrapper <string> .Success("0x1")); JsonRpcSuccessResponse response = TestRequest(ethModule, "eth_call", serialized) as JsonRpcSuccessResponse; Assert.AreEqual("0x1", response?.Result); }
public void GetWorkTest() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_getWork().ReturnsForAnyArgs(x => ResultWrapper <IEnumerable <byte[]> > .Success(new[] { Bytes.FromHexString("aa"), Bytes.FromHexString("01") })); JsonRpcSuccessResponse response = TestRequest(ethModule, "eth_getWork") as JsonRpcSuccessResponse; byte[][] dataList = response?.Result as byte[][]; Assert.NotNull(dataList?.SingleOrDefault(d => d.ToHexString(true) == "0xaa")); Assert.NotNull(dataList?.SingleOrDefault(d => d.ToHexString(true) == "0x01")); }
public void GetBlockByNumberTest() { IEthModule ethModule = Substitute.For <IEthModule>(); ethModule.eth_getBlockByNumber(Arg.Any <BlockParameter>(), true).ReturnsForAnyArgs(x => ResultWrapper <Block> .Success(new Block { Number = new Quantity(2) })); JsonRpcResponse response = TestRequest <IEthModule>(ethModule, "eth_getBlockByNumber", "0x1b4", "true"); Assert.IsTrue(response.Result.ToString().Contains("0x02")); }
private void Initialize(INetModule netModule, IEthModule ethModule, IWeb3Module web3Module, IShhModule shhModule) { _modules = new[] { new ModuleInfo(ModuleType.Net, typeof(INetModule), netModule), new ModuleInfo(ModuleType.Eth, typeof(IEthModule), ethModule), new ModuleInfo(ModuleType.Web3, typeof(IWeb3Module), web3Module), new ModuleInfo(ModuleType.Shh, typeof(IShhModule), shhModule) }; _enabledModules = _modules.Where(x => _configurationProvider.EnabledModules.Contains(x.ModuleType)).ToArray(); }
public async Task Ensure_that_shared_is_never_returned_as_exclusive() { IEthModule sharedModule = _modulePool.GetModule(true); _modulePool.ReturnModule(sharedModule); const int iterations = 1000; Action rentReturnShared = () => { for (int i = 0; i < iterations; i++) { TestContext.Out.WriteLine($"Rent shared {i}"); IEthModule ethModule = _modulePool.GetModule(true); Assert.AreSame(sharedModule, ethModule); _modulePool.ReturnModule(ethModule); TestContext.Out.WriteLine($"Return shared {i}"); } }; Action rentReturnExclusive = () => { for (int i = 0; i < iterations; i++) { TestContext.Out.WriteLine($"Rent exclusive {i}"); IEthModule ethModule = _modulePool.GetModule(false); Assert.AreNotSame(sharedModule, ethModule); _modulePool.ReturnModule(ethModule); TestContext.Out.WriteLine($"Return exclusive {i}"); } }; Task a = new Task(rentReturnExclusive); Task b = new Task(rentReturnExclusive); Task c = new Task(rentReturnShared); Task d = new Task(rentReturnShared); a.Start(); b.Start(); c.Start(); d.Start(); await Task.WhenAll(a, b, c, d); }
public async Task Can_rent_and_return(bool canBeShared) { IEthModule ethModule = await _modulePool.GetModule(canBeShared); _modulePool.ReturnModule(ethModule); }
public ModuleProvider(IConfigProvider configurationProvider, INetModule netModule, IEthModule ethModule, IWeb3Module web3Module, IShhModule shhModule) { _configurationProvider = configurationProvider.GetConfig <JsonRpcConfig>(); Initialize(netModule, ethModule, web3Module, shhModule); }
public void Can_rent_and_return(bool canBeShared) { IEthModule ethModule = _modulePool.GetModule(canBeShared); _modulePool.ReturnModule(ethModule); }