示例#1
0
        public async Task Execute(CancellationToken cancellationToken)
        {
            IGrpcConfig grpcConfig = _api.Config <IGrpcConfig>();

            if (grpcConfig.Enabled)
            {
                ILogger    logger     = _api.LogManager.GetClassLogger();
                GrpcServer grpcServer = new GrpcServer(_api.EthereumJsonSerializer, _api.LogManager);
                var        grpcRunner = new GrpcRunner(grpcServer, grpcConfig, _api.LogManager);
                await grpcRunner.Start(cancellationToken).ContinueWith(x =>
                {
                    if (x.IsFaulted && logger.IsError)
                    {
                        logger.Error("Error during GRPC runner start", x.Exception);
                    }
                });

                _api.GrpcServer = grpcServer;

                GrpcPublisher grpcPublisher = new GrpcPublisher(_api.GrpcServer);
                _api.Publishers.Add(grpcPublisher);

                _api.DisposeStack.Push(grpcPublisher);
                _api.DisposeStack.Push(Disposable.Create(() => grpcRunner.StopAsync())); // do not await
            }
        }
示例#2
0
        public async Task Execute(CancellationToken cancellationToken)
        {
            IGrpcConfig grpcConfig = _api.Config <IGrpcConfig>();

            if (grpcConfig.Enabled)
            {
                ILogger    logger     = _api.LogManager.GetClassLogger();
                GrpcServer grpcServer = new(_api.EthereumJsonSerializer, _api.LogManager);
                GrpcRunner grpcRunner = new(grpcServer, grpcConfig, _api.LogManager);
                await grpcRunner.Start(cancellationToken).ContinueWith(x =>
                {
                    if (x.IsFaulted && logger.IsError)
                    {
                        logger.Error("Error during GRPC runner start", x.Exception);
                    }
                }, cancellationToken);

                _api.GrpcServer = grpcServer;

                GrpcPublisher grpcPublisher = new(_api.GrpcServer);
                _api.Publishers.Add(grpcPublisher);

                _api.DisposeStack.Push(grpcPublisher);

#pragma warning disable 4014
                _api.DisposeStack.Push(new Reactive.AnonymousDisposable(() => grpcRunner.StopAsync())); // do not await
#pragma warning restore 4014
            }
        }
示例#3
0
        public void Grpc_enabled_for_ndm(string configFile)
        {
            ConfigProvider configProvider = GetConfigProviderFromFile(configFile);
            IGrpcConfig    config         = configProvider.GetConfig <IGrpcConfig>();

            Assert.AreEqual(true, config.Enabled);
            Assert.AreEqual(false, config.ProducerEnabled);
        }
示例#4
0
        public void Grpc_disabled_by_default(string configFile)
        {
            ConfigProvider configProvider = GetConfigProviderFromFile(configFile);
            IGrpcConfig    config         = configProvider.GetConfig <IGrpcConfig>();

            Assert.AreEqual(false, config.Enabled);
            Assert.AreEqual(false, config.ProducerEnabled);
        }
示例#5
0
        public Task Execute(CancellationToken _)
        {
            IGrpcConfig grpcConfig = _context.Config <IGrpcConfig>();

            if (grpcConfig.Enabled)
            {
                GrpcProducer grpcProducer = new GrpcProducer(_context.GrpcServer);
                _context.Producers.Add(grpcProducer);
            }

            return(Task.CompletedTask);
        }
        public Task Execute(CancellationToken _)
        {
            IGrpcConfig grpcConfig = _context.Config <IGrpcConfig>();

            if (grpcConfig.Enabled)
            {
                SubsystemStateChanged?.Invoke(this, new SubsystemStateEventArgs(EthereumSubsystemState.Initializing));

                GrpcProducer grpcProducer = new GrpcProducer(_context.GrpcServer);
                _context.Producers.Add(grpcProducer);

                SubsystemStateChanged?.Invoke(this, new SubsystemStateEventArgs(EthereumSubsystemState.Running));
            }

            return(Task.CompletedTask);
        }
示例#7
0
        protected async Task StartRunners(IConfigProvider configProvider)
        {
            IInitConfig        initConfig        = configProvider.GetConfig <IInitConfig>();
            IJsonRpcConfig     jsonRpcConfig     = configProvider.GetConfig <IJsonRpcConfig>();
            IMetricsConfig     metricsConfig     = configProvider.GetConfig <IMetricsConfig>();
            NLogManager        logManager        = new NLogManager(initConfig.LogFileName, initConfig.LogDirectory);
            IRpcModuleProvider rpcModuleProvider = jsonRpcConfig.Enabled
                ? new RpcModuleProvider(configProvider.GetConfig <IJsonRpcConfig>(), logManager)
                : (IRpcModuleProvider)NullModuleProvider.Instance;
            EthereumJsonSerializer jsonSerializer    = new EthereumJsonSerializer();
            WebSocketsManager      webSocketsManager = new WebSocketsManager();

            if (!string.IsNullOrEmpty(metricsConfig.NodeName))
            {
                logManager.SetGlobalVariable("nodeName", metricsConfig.NodeName);
            }

            if (metricsConfig.Enabled)
            {
                Metrics.Version = VersionToMetrics.ConvertToNumber(ClientVersion.Version);
                MetricsUpdater metricsUpdater = new MetricsUpdater(metricsConfig);
                _monitoringService = new MonitoringService(metricsUpdater, metricsConfig, logManager);
                _monitoringService.RegisterMetrics(typeof(Nethermind.Blockchain.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Db.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Evm.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.JsonRpc.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Trie.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Network.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Synchronization.Metrics));
                _monitoringService.RegisterMetrics(typeof(Nethermind.TxPool.Metrics));
                _monitoringService.RegisterMetrics(typeof(Metrics));

                await _monitoringService.StartAsync().ContinueWith(x =>
                {
                    if (x.IsFaulted && (_logger?.IsError ?? false))
                    {
                        _logger !.Error("Error during starting a monitoring.", x.Exception);
                    }
                });
            }
            else
            {
                if (_logger?.IsInfo ?? false)
                {
                    _logger !.Info("Grafana / Prometheus metrics are disabled in configuration");
                }
            }

            IGrpcConfig grpcConfig = configProvider.GetConfig <IGrpcConfig>();
            GrpcServer? grpcServer = null;

            if (grpcConfig.Enabled)
            {
                grpcServer  = new GrpcServer(jsonSerializer, logManager);
                _grpcRunner = new GrpcRunner(grpcServer, grpcConfig, logManager);
                await _grpcRunner.Start().ContinueWith(x =>
                {
                    if (x.IsFaulted && (_logger?.IsError ?? false))
                    {
                        _logger !.Error("Error during GRPC runner start", x.Exception);
                    }
                });
            }

            INdmDataPublisher?         ndmDataPublisher          = null;
            INdmConsumerChannelManager?ndmConsumerChannelManager = null;
            INdmInitializer?           ndmInitializer            = null;
            INdmConfig ndmConfig  = configProvider.GetConfig <INdmConfig>();
            bool       ndmEnabled = ndmConfig.Enabled;

            if (ndmEnabled)
            {
                ndmDataPublisher          = new NdmDataPublisher();
                ndmConsumerChannelManager = new NdmConsumerChannelManager();
                string initializerName = ndmConfig.InitializerName;
                if (_logger?.IsInfo ?? false)
                {
                    _logger !.Info($"NDM initializer: {initializerName}");
                }
                Type ndmInitializerType = AppDomain.CurrentDomain.GetAssemblies()
                                          .SelectMany(a => a.GetTypes())
                                          .FirstOrDefault(t =>
                                                          t.GetCustomAttribute <NdmInitializerAttribute>()?.Name == initializerName);
                NdmModule          ndmModule          = new NdmModule();
                NdmConsumersModule ndmConsumersModule = new NdmConsumersModule();
                ndmInitializer = new NdmInitializerFactory(ndmInitializerType, ndmModule, ndmConsumersModule, logManager).CreateOrFail();

                if (grpcServer != null)
                {
                    ndmConsumerChannelManager.Add(new GrpcNdmConsumerChannel(grpcServer));
                }

                webSocketsManager.AddModule(new NdmWebSocketsModule(ndmConsumerChannelManager, ndmDataPublisher, jsonSerializer));
            }

            _ethereumRunner = new EthereumRunner(
                rpcModuleProvider,
                configProvider,
                logManager,
                grpcServer,
                ndmConsumerChannelManager,
                ndmDataPublisher,
                ndmInitializer,
                webSocketsManager,
                jsonSerializer,
                _monitoringService);

            await _ethereumRunner.Start().ContinueWith(x =>
            {
                if (x.IsFaulted && (_logger?.IsError ?? false))
                {
                    _logger !.Error("Error during ethereum runner start", x.Exception);
                }
            });

            if (jsonRpcConfig.Enabled)
            {
                rpcModuleProvider.Register(new SingletonModulePool <IWeb3Module>(new Web3Module(logManager), true));
                JsonRpcService   jsonRpcService   = new JsonRpcService(rpcModuleProvider, logManager);
                JsonRpcProcessor jsonRpcProcessor = new JsonRpcProcessor(jsonRpcService, jsonSerializer, jsonRpcConfig, new FileSystem(), logManager);
                if (initConfig.WebSocketsEnabled)
                {
                    webSocketsManager.AddModule(new JsonRpcWebSocketsModule(jsonRpcProcessor, jsonSerializer), true);
                }

                Bootstrap.Instance.JsonRpcService = jsonRpcService;
                Bootstrap.Instance.LogManager     = logManager;
                Bootstrap.Instance.JsonSerializer = jsonSerializer;
                _jsonRpcRunner = new JsonRpcRunner(configProvider, rpcModuleProvider, logManager, jsonRpcProcessor, webSocketsManager);
                await _jsonRpcRunner.Start().ContinueWith(x =>
                {
                    if (x.IsFaulted && (_logger?.IsError ?? false))
                    {
                        _logger !.Error("Error during jsonRpc runner start", x.Exception);
                    }
                });
            }
            else
            {
                if (_logger?.IsInfo ?? false)
                {
                    _logger !.Info("Json RPC is disabled");
                }
            }
        }
示例#8
0
 public GrpcRunner(NethermindService.NethermindServiceBase service, IGrpcConfig config, ILogManager logManager)
 {
     _service = service;
     _config  = config;
     _logger  = logManager.GetClassLogger();
 }
示例#9
0
        public virtual Task Execute(CancellationToken cancellationToken)
        {
            IInitConfig initConfig = _api.Config <IInitConfig>();
            IGrpcConfig grpcConfig = _api.Config <IGrpcConfig>();

            string fullPluginsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory !, initConfig.PluginsDirectory);

            if (!Directory.Exists(fullPluginsDir))
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Plugins folder {fullPluginsDir} was not found. Skipping.");
                }
                return(Task.CompletedTask);
            }

            string[] pluginFiles = Directory.GetFiles(fullPluginsDir).Where(p => p.EndsWith("dll")).ToArray();

            if (pluginFiles.Length > 0)
            {
                if (_logger.IsInfo)
                {
                    _logger.Info($"Loading {pluginFiles.Length} analytics plugins from {fullPluginsDir}");
                }
            }

            foreach (string path in pluginFiles)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                if (_logger.IsInfo)
                {
                    _logger.Warn($"Loading assembly {path}");
                }
                Assembly assembly = Assembly.LoadFile(Path.Combine(fullPluginsDir, path));
                foreach (Type type in assembly.GetTypes())
                {
                    AnalyticsLoaderAttribute?loader = type.GetCustomAttribute <AnalyticsLoaderAttribute>();
                    if (loader != null)
                    {
                        if (_logger.IsWarn)
                        {
                            _logger.Warn($"Activating plugin {type.Name} from {path} {new FileInfo(path).CreationTime}");
                        }
                        IAnalyticsPluginLoader?pluginLoader = Activator.CreateInstance(type) as IAnalyticsPluginLoader;
                        if (grpcConfig.Enabled)
                        {
                            if (_logger.IsWarn)
                            {
                                _logger.Warn($"Initializing gRPC for {type.Name}");
                            }
                            pluginLoader?.Init(_api.FileSystem, _api.TxPool, _api.BlockTree, _api.MainBlockProcessor, new GrpcPublisher(_api.GrpcServer !), _api.LogManager);
                        }
                        else
                        {
                            if (_logger.IsWarn)
                            {
                                _logger.Warn($"Initializing log publisher for {type.Name}");
                            }
                            pluginLoader?.Init(_api.FileSystem, _api.TxPool, _api.BlockTree, _api.MainBlockProcessor, new LogDataPublisher(_api.LogManager), _api.LogManager);
                        }
                    }
                }
            }

            return(Task.CompletedTask);
        }