예제 #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);
            }
        }
        public void IsSyncing_ReturnsExpectedResult(long bestHeader, long currentHead, bool expectedResult)
        {
            IBlockFinder blockFinder = Substitute.For <IBlockFinder>();

            blockFinder.FindBestSuggestedHeader().Returns(Build.A.BlockHeader.WithNumber(bestHeader).TestObject);
            blockFinder.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(currentHead).TestObject).TestObject);
            EthSyncingInfo ethSyncingInfo = new(blockFinder);
            SyncingResult  syncingResult  = ethSyncingInfo.GetFullInfo();

            Assert.AreEqual(expectedResult, syncingResult.IsSyncing);
        }
        public void GetFullInfo_WhenSyncing()
        {
            IBlockFinder blockFinder = Substitute.For <IBlockFinder>();

            blockFinder.FindBestSuggestedHeader().Returns(Build.A.BlockHeader.WithNumber(6178010L).TestObject);
            blockFinder.Head.Returns(Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(6178000L).TestObject).TestObject);
            EthSyncingInfo ethSyncingInfo = new(blockFinder);
            SyncingResult  syncingResult  = ethSyncingInfo.GetFullInfo();

            Assert.AreEqual(true, syncingResult.IsSyncing);
            Assert.AreEqual(6178000L, syncingResult.CurrentBlock);
            Assert.AreEqual(6178010, syncingResult.HighestBlock);
            Assert.AreEqual(0, syncingResult.StartingBlock);
        }
        private void OnConditionsChange(object?sender, BlockEventArgs e)
        {
            ScheduleAction(() =>
            {
                SyncingResult syncingResult = _ethSyncingInfo.GetFullInfo();
                bool isSyncing = syncingResult.IsSyncing;

                if (isSyncing == _lastIsSyncing)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Syncing subscription {Id} didn't changed syncing status: {_lastIsSyncing}");
                    }
                    return;
                }

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Syncing subscription {Id} changed syncing status from {_lastIsSyncing} to {isSyncing}");
                }

                _lastIsSyncing = isSyncing;
                JsonRpcResult result;

                if (isSyncing == false)
                {
                    result = CreateSubscriptionMessage(isSyncing);
                }
                else
                {
                    result = CreateSubscriptionMessage(syncingResult);
                }


                JsonRpcDuplexClient.SendJsonRpcResult(result);
                _logger.Trace($"Syncing subscription {Id} printed SyncingResult object.");
            });
        }