public override async Task <INdmCapabilityConnector> InitAsync(INdmApi ndmApi)
        {
            INdmConfig ndmConfig = ndmApi.ConfigProvider.GetConfig <INdmConfig>();

            await PreInitAsync(ndmApi);

            if (!ndmConfig.Enabled)
            {
                return(NullNdmCapabilityConnector.Instance);
            }

            providersModule = new NdmProvidersModule(ndmApi);
            await _consumerModule.Init();

            await providersModule.InitAsync();

            IProviderService providerService = providersModule.GetProviderService();

            var subprotocolFactory = new NdmProviderSubprotocolFactory(ndmApi.MessageSerializationService, ndmApi.NodeStatsManager,
                                                                       ndmApi.LogManager, ndmApi.AccountService, ndmApi.ConsumerService, providerService, ndmApi.NdmConsumerChannelManager, ndmApi.EthereumEcdsa,
                                                                       ndmApi.Wallet, ndmApi.NdmFaucet, ndmApi.Enode.PublicKey, ndmApi.ProviderAddress, ndmApi.ConsumerAddress, ndmConfig.VerifyP2PSignature);
            var protocolHandlerFactory = new ProtocolHandlerFactory(subprotocolFactory, ndmApi.ProtocolValidator,
                                                                    ndmApi.EthRequestService, ndmApi.LogManager);
            var capabilityConnector = new NdmCapabilityConnector(ndmApi.ProtocolsManager, protocolHandlerFactory,
                                                                 ndmApi.AccountService, ndmApi.LogManager, ndmApi.ProviderAddress);

            providerService.AddressChanged += (_, e) =>
            {
                if (!(e.OldAddress is null) && e.OldAddress != Address.Zero)
                {
                    return;
                }

                capabilityConnector.AddCapability();
            };

            var pluginBuilder = new YamlNdmPluginBuilder();
            var pluginLoader  = new YamlNdmPluginLoader("ndm-plugins", pluginBuilder, ndmApi.LogManager);
            var plugins       = pluginLoader.Load();

            foreach (var plugin in plugins)
            {
                await providerService.InitPluginAsync(plugin);
            }

            ndmApi.MonitoringService?.RegisterMetrics(typeof(Metrics));

            return(capabilityConnector);
        }
示例#2
0
        protected async Task PreInitAsync(INdmApi ndmApi)
        {
            if (ndmApi == null)
            {
                throw new ArgumentNullException(nameof(ndmApi));
            }

            IDbProvider?dbProvider = ndmApi.DbProvider;

            if (dbProvider == null)
            {
                throw new ArgumentNullException(nameof(dbProvider));
            }

            IConfigProvider configProvider = ndmApi.ConfigProvider;
            ILogManager     logManager     = ndmApi.LogManager;

            if (!(configProvider.GetConfig <INdmConfig>() is NdmConfig defaultConfig))
            {
                return;
            }

            if (!defaultConfig.Enabled)
            {
                return;
            }

            if (defaultConfig.StoreConfigInDatabase && string.IsNullOrWhiteSpace(defaultConfig.Id))
            {
                throw new ArgumentException("NDM config stored in database requires an id.", nameof(defaultConfig.Id));
            }

            IConfigRepository     configRepository;
            IEthRequestRepository ethRequestRepository;

            switch (defaultConfig.Persistence?.ToLowerInvariant())
            {
            case "mongo":
                ndmApi.MongoProvider = new MongoProvider(configProvider.GetConfig <INdmMongoConfig>(), logManager);
                IMongoDatabase?database = ndmApi.MongoProvider.GetDatabase();
                if (database == null)
                {
                    throw new ApplicationException("Failed to initialize Mongo database");
                }

                configRepository     = new ConfigMongoRepository(database);
                ethRequestRepository = new EthRequestMongoRepository(database);
                break;

            default:
                ndmApi.MongoProvider = NullMongoProvider.Instance;
                var ndmDbProvider = new NdmDbInitializer(defaultConfig, ndmApi.DbProvider, ndmApi.RocksDbFactory, ndmApi.MemDbFactory);
                await ndmDbProvider.Init();

                configRepository     = new ConfigRocksRepository(ndmApi.DbProvider.GetDb <IDb>(NdmDbNames.Configs), new NdmConfigDecoder());
                ethRequestRepository = new EthRequestRocksRepository(ndmApi.DbProvider.GetDb <IDb>(NdmDbNames.EthRequests),
                                                                     new EthRequestDecoder());
                break;
            }

            ndmApi.ConfigManager = new ConfigManager(defaultConfig, configRepository);
            ndmApi.NdmConfig     = await ndmApi.ConfigManager.GetAsync(defaultConfig.Id);

            if (ndmApi.NdmConfig is null)
            {
                ndmApi.NdmConfig = defaultConfig;
                if (defaultConfig.StoreConfigInDatabase)
                {
                    await ndmApi.ConfigManager.UpdateAsync((NdmConfig)ndmApi.NdmConfig);
                }
            }

            IWebSocketsModule webSocketsModule = ndmApi.WebSocketsManager !.GetModule("ndm");

            ndmApi.NdmNotifier = new NdmNotifier(webSocketsModule);

            ndmApi.EthRequestService = new EthRequestService(ndmApi.NdmConfig.FaucetHost, logManager);

            string baseDbPath = configProvider.GetConfig <IInitConfig>().BaseDbPath;

            ndmApi.BaseDbPath = DbPath = Path.Combine(baseDbPath, ndmApi.NdmConfig.DatabasePath);

            await _ndmModule.InitAsync();

            if (ndmApi.NdmConfig.FaucetEnabled)
            {
                // faucet should be separate from NDM? but it uses MongoDB?
                // so maybe we could extract Mongo DB beyond NDM? why would it be related?
                if (string.IsNullOrWhiteSpace(ndmApi.NdmConfig.FaucetAddress))
                {
                    ndmApi.NdmFaucet = EmptyFaucet.Instance;
                    _logger.Warn("Faucet cannot be started due to missing faucet address configuration.");
                }
                else
                {
                    Address faucetAddress = new(ndmApi.NdmConfig.FaucetAddress);
                    ndmApi.NdmFaucet = new NdmFaucet(
                        ndmApi.BlockchainBridge,
                        ethRequestRepository,
                        faucetAddress,
                        ndmApi.NdmConfig.FaucetWeiRequestMaxValue,
                        ndmApi.NdmConfig.FaucetEthDailyRequestsTotalValue,
                        ndmApi.NdmConfig.FaucetEnabled,
                        ndmApi.Timestamper,
                        ndmApi.Wallet,
                        logManager);
                }
            }
            else
            {
                ndmApi.NdmFaucet = EmptyFaucet.Instance;
            }

            ndmApi.ConsumerAddress = string.IsNullOrWhiteSpace(ndmApi.NdmConfig.ConsumerAddress)
                ? Address.Zero
                : new Address(ndmApi.NdmConfig.ConsumerAddress);
            ndmApi.ProviderAddress = string.IsNullOrWhiteSpace(ndmApi.NdmConfig.ProviderAddress)
                ? Address.Zero
                : new Address(ndmApi.NdmConfig.ProviderAddress);

            await _ndmConsumersModule.Init();
        }
示例#3
0
            PreInitAsync(IConfigProvider configProvider, IDbProvider dbProvider, string baseDbPath,
                IBlockTree blockTree, ITxPool txPool, ISpecProvider specProvider,
                IReceiptStorage receiptStorage, IWallet wallet, IFilterStore filterStore, IFilterManager filterManager,
                ITimestamper timestamper, IEthereumEcdsa ecdsa, IRpcModuleProvider rpcModuleProvider,
                IKeyStore keyStore, IJsonSerializer jsonSerializer, ICryptoRandom cryptoRandom, IEnode enode,
                INdmConsumerChannelManager consumerChannelManager, INdmDataPublisher dataPublisher,
                IGrpcServer grpcServer, bool enableUnsecuredDevWallet, IWebSocketsManager webSocketsManager,
                ILogManager logManager, IBlockProcessor blockProcessor)
        {
            if (!(configProvider.GetConfig<INdmConfig>() is NdmConfig defaultConfig))
            {
                return default;
            }

            if (!defaultConfig.Enabled)
            {
                return default;
            }

            if (defaultConfig.StoreConfigInDatabase && string.IsNullOrWhiteSpace(defaultConfig.Id))
            {
                throw new ArgumentException("NDM config stored in database requires an id.", nameof(defaultConfig.Id));
            }

            IMongoProvider mongoProvider = null;
            IConfigRepository configRepository = null;
            IEthRequestRepository ethRequestRepository = null;
            switch (defaultConfig.Persistence?.ToLowerInvariant())
            {
                case "mongo":
                    mongoProvider = new MongoProvider(configProvider.GetConfig<INdmMongoConfig>(), logManager);
                    var database = mongoProvider.GetDatabase();
                    configRepository = new ConfigMongoRepository(database);
                    ethRequestRepository = new EthRequestMongoRepository(database);
                    break;
                default:
                    configRepository = new ConfigRocksRepository(dbProvider.ConfigsDb, new NdmConfigDecoder());
                    ethRequestRepository = new EthRequestRocksRepository(dbProvider.EthRequestsDb,
                        new EthRequestDecoder());
                    break;
            }

            var configManager = new ConfigManager(defaultConfig, configRepository);
            var ndmConfig = await configManager.GetAsync(defaultConfig.Id);
            if (ndmConfig is null)
            {
                ndmConfig = defaultConfig;
                if (defaultConfig.StoreConfigInDatabase)
                {
                    await configManager.UpdateAsync(ndmConfig);
                }
            }

            var webSocketsModule = webSocketsManager.GetModule("ndm");
            var notifier = new NdmNotifier(webSocketsModule);
            var ethRequestService = new EthRequestService(ndmConfig.FaucetHost, logManager);
            var services = _ndmModule.Init(new NdmRequiredServices(configProvider, configManager, ndmConfig,
                baseDbPath, dbProvider, mongoProvider, logManager, blockTree, txPool, specProvider, receiptStorage,
                filterStore, filterManager, wallet, timestamper, ecdsa, keyStore, rpcModuleProvider, jsonSerializer,
                cryptoRandom, enode, consumerChannelManager, dataPublisher, grpcServer, ethRequestService, notifier,
                enableUnsecuredDevWallet, blockProcessor));

            var faucetAddress = string.IsNullOrWhiteSpace(ndmConfig.FaucetAddress)
                ? null
                : new Address(ndmConfig.FaucetAddress);
            var faucet = new NdmFaucet(services.CreatedServices.BlockchainBridge, ethRequestRepository, faucetAddress,
                ndmConfig.FaucetWeiRequestMaxValue, ndmConfig.FaucetEthDailyRequestsTotalValue, ndmConfig.FaucetEnabled,
                timestamper, wallet, logManager);

            var consumerAddress = string.IsNullOrWhiteSpace(ndmConfig.ConsumerAddress)
                ? Address.Zero
                : new Address(ndmConfig.ConsumerAddress);
            var providerAddress = string.IsNullOrWhiteSpace(ndmConfig.ProviderAddress)
                ? Address.Zero
                : new Address(ndmConfig.ProviderAddress);
            var consumers = _ndmConsumersModule.Init(services);

            return (ndmConfig, services, faucet, consumers.AccountService, consumers.ConsumerService, consumerAddress,
                providerAddress);
        }