Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 5
0
        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>();
        }
Ejemplo n.º 6
0
        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));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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"));
        }
Ejemplo n.º 13
0
        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"));
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 16
0
        public async Task Can_rent_and_return(bool canBeShared)
        {
            IEthModule ethModule = await _modulePool.GetModule(canBeShared);

            _modulePool.ReturnModule(ethModule);
        }
Ejemplo n.º 17
0
 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);
        }