public void Can_add_various_channel_types()
        {
            NdmConsumerChannelManager manager = new NdmConsumerChannelManager();

            manager.Add(new JsonRpcNdmConsumerChannel(LimboLogs.Instance));
            manager.Add(new GrpcNdmConsumerChannel(Substitute.For <IGrpcServer>()));
            manager.Add(new NdmWebSocketsConsumerChannel(Substitute.For <ISocketsClient>()));
        }
示例#2
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);
        }
        public async Task Can_publish_on_various_channel_types()
        {
            NdmConsumerChannelManager manager = new NdmConsumerChannelManager();

            ISocketsClient client = Substitute.For <ISocketsClient>();

            INdmConsumerChannel[] channels = new INdmConsumerChannel[]
            {
                new JsonRpcNdmConsumerChannel(LimboLogs.Instance),
                new GrpcNdmConsumerChannel(Substitute.For <IGrpcServer>()),
                new NdmWebSocketsConsumerChannel(client),
            };

            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().BeNull();

            for (int i = 0; i < 3; i++)
            {
                manager.Add(channels[i]);
            }

            channels[0].Type.Should().Be(NdmConsumerChannelType.JsonRpc);
            channels[1].Type.Should().Be(NdmConsumerChannelType.Grpc);

            await manager.PublishAsync(Keccak.Zero, "client1", "data1");

            await manager.PublishAsync(Keccak.Zero, "client2", "data2");

            for (int i = 0; i < 3; i++)
            {
                manager.Remove(channels[i]);
            }

            await manager.PublishAsync(Keccak.Zero, "client3", "data3");

            for (int i = 0; i < 3; i++)
            {
                await client.Received().SendAsync(Arg.Is <SocketsMessage>(wm => wm.Client == "client1"));

                await client.Received().SendAsync(Arg.Is <SocketsMessage>(wm => wm.Client == "client2"));

                await client.DidNotReceive().SendAsync(Arg.Is <SocketsMessage>(wm => wm.Client == "client3"));
            }

            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().NotBeNull();
            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().NotBeNull();
            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().BeNull();
        }
示例#4
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");
                }
            }
        }