Пример #1
0
        public Task Init(INethermindApi api)
        {
            _ndmApi = new NdmApi(api);
            // TODO: load messages nicely?
            api.MessageSerializationService.Register(Assembly.GetAssembly(typeof(HiMessageSerializer)));

            ILogger    logger     = api.LogManager.GetClassLogger();
            INdmConfig ndmConfig  = api.ConfigProvider.GetConfig <INdmConfig>();
            bool       ndmEnabled = ndmConfig.Enabled;

            if (ndmEnabled)
            {
                _ndmApi.NdmDataPublisher = new NdmDataPublisher();
                INdmConsumerChannelManager?ndmConsumerChannelManager = new NdmConsumerChannelManager();
                string initializerName = ndmConfig.InitializerName;
                if (logger.IsInfo)
                {
                    logger.Info($"NDM initializer: {initializerName}");
                }
                Type?ndmInitializerType = AppDomain.CurrentDomain.GetAssemblies()
                                          .SelectMany(a => a.GetTypes())
                                          .FirstOrDefault(t =>
                                                          t.GetCustomAttribute <NdmInitializerAttribute>()?.Name == initializerName);

                if (ndmInitializerType == null)
                {
                    if (logger.IsError)
                    {
                        logger.Error(
                            $"NDM enabled but the initializer {initializerName} has not been found. Ensure that a plugin exists with the properly set {nameof(NdmInitializerAttribute)}");
                    }
                }

                NdmModule          ndmModule          = new NdmModule(_ndmApi);
                NdmConsumersModule ndmConsumersModule = new NdmConsumersModule(_ndmApi);
                _ndmInitializer =
                    new NdmInitializerFactory(ndmInitializerType, ndmModule, ndmConsumersModule, api.LogManager)
                    .CreateOrFail();

                if (api.GrpcServer != null)
                {
                    var grpcChannel = new GrpcNdmConsumerChannel(api.GrpcServer);
                    ndmConsumerChannelManager.Add(grpcChannel);
                }

                NdmWebSocketsModule ndmWebSocketsModule =
                    new NdmWebSocketsModule(
                        ndmConsumerChannelManager,
                        _ndmApi.NdmDataPublisher,
                        api.EthereumJsonSerializer);
                api.WebSocketsManager.AddModule(ndmWebSocketsModule);
            }

            return(Task.CompletedTask);
        }
Пример #2
0
 public void Setup()
 {
     _module = new NdmWebSocketsModule(_channelManager, _dataPublisher, new EthereumJsonSerializer());
 }
Пример #3
0
        protected async Task StartRunners(CancellationToken cancellationToken, IConfigProvider configProvider)
        {
            IFileSystem        fileSystem        = new FileSystem();
            IInitConfig        initConfig        = configProvider.GetConfig <IInitConfig>();
            IJsonRpcConfig     jsonRpcConfig     = configProvider.GetConfig <IJsonRpcConfig>();
            IMetricsConfig     metricsConfig     = configProvider.GetConfig <IMetricsConfig>();
            ISeqConfig         seqConfig         = configProvider.GetConfig <ISeqConfig>();
            NLogManager        logManager        = new NLogManager(initConfig.LogFileName, initConfig.LogDirectory);
            IRpcModuleProvider rpcModuleProvider = jsonRpcConfig.Enabled
                ? new RpcModuleProvider(fileSystem, configProvider.GetConfig <IJsonRpcConfig>(), logManager)
                : (IRpcModuleProvider)NullModuleProvider.Instance;
            EthereumJsonSerializer jsonSerializer    = new EthereumJsonSerializer();
            WebSocketsManager      webSocketsManager = new WebSocketsManager();

            if (seqConfig.MinLevel != "Off")
            {
                if (_logger?.IsInfo ?? false)
                {
                    _logger !.Info($"Seq Logging enabled on host: {seqConfig.ServerUrl} with level: {seqConfig.MinLevel}");
                }
                new NLogConfigurator().ConfigureSeqBufferTarget(seqConfig.ServerUrl, seqConfig.ApiKey, seqConfig.MinLevel);
            }

            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));
                _monitoringService.RegisterMetrics(typeof(Nethermind.Consensus.AuRa.Metrics));

                await _monitoringService.StartAsync().ContinueWith(x =>
                {
                    if (x.IsFaulted && (_logger?.IsError ?? false))
                    {
                        _logger !.Error("Error during starting a monitoring.", x.Exception);
                    }
                }, cancellationToken);
            }
            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(cancellationToken).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));
                }

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

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

            IAnalyticsConfig analyticsConfig = configProvider.GetConfig <IAnalyticsConfig>();

            if (analyticsConfig.PluginsEnabled ||
                analyticsConfig.StreamBlocks ||
                analyticsConfig.StreamTransactions)
            {
                webSocketsManager.AddModule(new AnalyticsWebSocketsModule(jsonSerializer), true);
            }

            await _ethereumRunner.Start(cancellationToken).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, 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(cancellationToken).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");
                }
            }
        }
Пример #4
0
 public void Setup()
 {
     _module = new NdmWebSocketsModule(Substitute.For <INdmApi>());
 }