コード例 #1
0
 public NdmWebSocketsModule(INdmApi api)
 {
     _consumerChannelManager = api.NdmConsumerChannelManager;
     _dataPublisher          = api.NdmDataPublisher;
     _jsonSerializer         = api.EthereumJsonSerializer;
     _logManager             = api.LogManager;
 }
コード例 #2
0
ファイル: NdmModuleTests.cs プロジェクト: Semenka/nethermind
 public void Setup()
 {
     _baseDbPath = "db";
     _enableUnsecuredDevWallet = false;
     _ndmApi    = new NdmApi(Substitute.For <INethermindApi>());
     _ndmModule = new NdmModule(_ndmApi);
 }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void Init(INdmApi api)
        {
            AddDecoders();
            var config = api.NdmConfig;

            api.ConsumerAddress = string.IsNullOrWhiteSpace(config.ConsumerAddress)
                ? Address.Zero
                : new Address(config.ConsumerAddress);
            api.ContractAddress = string.IsNullOrWhiteSpace(config.ContractAddress)
                ? Address.Zero
                : new Address(config.ContractAddress);

            if (config.ProxyEnabled)
            {
                if (config.JsonRpcUrlProxies == null || api.EthJsonRpcClientProxy == null)
                {
                    throw new InvalidDataException("JSON RPC proxy is enabled but the proxies were not initialized properly.");
                }

                api.JsonRpcClientProxy !.SetUrls(config.JsonRpcUrlProxies !);
                api.BlockchainBridge = new NdmBlockchainBridgeProxy(
                    api.EthJsonRpcClientProxy);
            }
            else
            {
                api.BlockchainBridge = new NdmBlockchainBridge(
                    api.CreateBlockchainBridge(),
                    api.BlockTree,
                    api.StateReader,
                    api.TxSender);
            }

            api.GasPriceService
                = new GasPriceService(api.HttpClient, api.ConfigManager, config.Id, api.Timestamper, api.LogManager);
            api.TransactionService
                = new TransactionService(api.BlockchainBridge, api.Wallet, api.ConfigManager, config.Id, api.LogManager);
            api.DepositService
                = new DepositService(api.BlockchainBridge, api.AbiEncoder, api.Wallet, api.ContractAddress);
            api.JsonRpcNdmConsumerChannel
                = new JsonRpcNdmConsumerChannel(api.LogManager);

            if (config.JsonRpcDataChannelEnabled)
            {
                api.NdmConsumerChannelManager.Add(api.JsonRpcNdmConsumerChannel);
            }
        }
コード例 #5
0
        public virtual async Task <INdmCapabilityConnector> InitAsync(INdmApi api)
        {
            _logger = api.LogManager.GetClassLogger() ?? throw new ArgumentNullException(nameof(api.LogManager));
            INdmConfig ndmConfig = api.Config <INdmConfig>();

            if (!ndmConfig.Enabled)
            {
                // can we not even call it here? // can be step and use the subsystems
                return(NullNdmCapabilityConnector.Instance);
            }

            await PreInitAsync(api);

            NdmSubprotocolFactory subprotocolFactory = new NdmSubprotocolFactory(
                api.MessageSerializationService,
                api.NodeStatsManager,
                api.LogManager,
                api.AccountService,
                api.ConsumerService,
                api.NdmConsumerChannelManager,
                api.EthereumEcdsa,
                api.Wallet,
                api.NdmFaucet,
                api.Enode.PublicKey,
                api.ProviderAddress,
                api.ConsumerAddress,
                api.Config <INdmConfig>().VerifyP2PSignature);

            ProtocolHandlerFactory protocolHandlerFactory = new(
                subprotocolFactory,
                api.ProtocolValidator,
                api.EthRequestService,
                api.LogManager);

            NdmCapabilityConnector capabilityConnector = new(
                api.ProtocolsManager,
                protocolHandlerFactory,
                api.AccountService,
                api.LogManager,
                ndmConfig.ProviderAddress == null ? Address.Zero : new Address(ndmConfig.ProviderAddress));

            return(capabilityConnector);
        }
コード例 #6
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();
        }
コード例 #7
0
ファイル: NdmModule.cs プロジェクト: uzbekdev1/nethermind
 public NdmModule(INdmApi api)
 {
     _api = api ?? throw new ArgumentNullException(nameof(api));
 }
コード例 #8
0
        public void Init(INdmApi api)
        {
            AddDecoders();
            ILogManager logManager = api.LogManager;
            ILogger     logger     = logManager.GetClassLogger();

            bool disableSendingDepositTransaction  = HasEnabledVariable("SENDING_DEPOSIT_TRANSACTION_DISABLED");
            bool instantDepositVerificationEnabled = HasEnabledVariable("INSTANT_DEPOSIT_VERIFICATION_ENABLED");
            bool backgroundServicesDisabled        = HasEnabledVariable("BACKGROUND_SERVICES_DISABLED");

            if (disableSendingDepositTransaction)
            {
                if (logger.IsWarn)
                {
                    logger.Warn("*** NDM sending deposit transaction is disabled ***");
                }
            }

            if (instantDepositVerificationEnabled)
            {
                if (logger.IsWarn)
                {
                    logger.Warn("*** NDM instant deposit verification is enabled ***");
                }
            }

            if (backgroundServicesDisabled)
            {
                if (logger.IsWarn)
                {
                    logger.Warn("*** NDM background services are disabled ***");
                }
            }

            INdmConfig ndmConfig       = api.NdmConfig;
            string     configId        = ndmConfig.Id;
            IDbConfig  dbConfig        = api.ConfigProvider.GetConfig <IDbConfig>();
            Address    contractAddress = string.IsNullOrWhiteSpace(ndmConfig.ContractAddress)
                ? Address.Zero
                : new Address(ndmConfig.ContractAddress);
            ConsumerRocksDbProvider rocksDbProvider = new ConsumerRocksDbProvider(api.BaseDbPath, dbConfig,
                                                                                  logManager);
            DepositDetailsDecoder             depositDetailsRlpDecoder  = new DepositDetailsDecoder();
            DepositApprovalDecoder            depositApprovalRlpDecoder = new DepositApprovalDecoder();
            DataDeliveryReceiptDetailsDecoder receiptRlpDecoder         = new DataDeliveryReceiptDetailsDecoder();
            ConsumerSessionDecoder            sessionRlpDecoder         = new ConsumerSessionDecoder();
            ReceiptRequestValidator           receiptRequestValidator   = new ReceiptRequestValidator(logManager);

            IDepositDetailsRepository          depositRepository;
            IConsumerDepositApprovalRepository depositApprovalRepository;
            IProviderRepository        providerRepository;
            IReceiptRepository         receiptRepository;
            IConsumerSessionRepository sessionRepository;

            switch (ndmConfig.Persistence?.ToLowerInvariant())
            {
            case "mongo":
                IMongoDatabase?database = api.MongoProvider.GetDatabase();
                if (database == null)
                {
                    throw new ApplicationException("Failed to initialize Mongo DB.");
                }

                depositRepository         = new DepositDetailsMongoRepository(database);
                depositApprovalRepository = new ConsumerDepositApprovalMongoRepository(database);
                providerRepository        = new ProviderMongoRepository(database);
                receiptRepository         = new ReceiptMongoRepository(database, "consumerReceipts");
                sessionRepository         = new ConsumerSessionMongoRepository(database);
                break;

            case "memory":
                if (logger.IsWarn)
                {
                    logger.Warn("*** NDM is using in memory database ***");
                }
                DepositsInMemoryDb depositsDatabase = new DepositsInMemoryDb();
                depositRepository         = new DepositDetailsInMemoryRepository(depositsDatabase);
                depositApprovalRepository = new ConsumerDepositApprovalInMemoryRepository();
                providerRepository        = new ProviderInMemoryRepository(depositsDatabase);
                receiptRepository         = new ReceiptInMemoryRepository();
                sessionRepository         = new ConsumerSessionInMemoryRepository();
                break;

            default:
                depositRepository = new DepositDetailsRocksRepository(rocksDbProvider.DepositsDb,
                                                                      depositDetailsRlpDecoder);
                depositApprovalRepository = new ConsumerDepositApprovalRocksRepository(
                    rocksDbProvider.ConsumerDepositApprovalsDb, depositApprovalRlpDecoder);
                providerRepository = new ProviderRocksRepository(rocksDbProvider.DepositsDb,
                                                                 depositDetailsRlpDecoder);
                receiptRepository = new ReceiptRocksRepository(rocksDbProvider.ConsumerReceiptsDb,
                                                               receiptRlpDecoder);
                sessionRepository = new ConsumerSessionRocksRepository(rocksDbProvider.ConsumerSessionsDb,
                                                                       sessionRlpDecoder);
                break;
            }

            uint                       requiredBlockConfirmations = ndmConfig.BlockConfirmations;
            IAbiEncoder                abiEncoder                = api.AbiEncoder;
            INdmBlockchainBridge       blockchainBridge          = api.BlockchainBridge;
            IBlockProcessor            blockProcessor            = api.MainBlockProcessor;
            IConfigManager             configManager             = api.ConfigManager;
            Address                    consumerAddress           = api.ConsumerAddress;
            ICryptoRandom              cryptoRandom              = api.CryptoRandom;
            IDepositService            depositService            = api.DepositService;
            GasPriceService            gasPriceService           = api.GasPriceService;
            IEthereumEcdsa             ecdsa                     = api.EthereumEcdsa;
            IEthRequestService         ethRequestService         = api.EthRequestService;
            IJsonRpcNdmConsumerChannel jsonRpcNdmConsumerChannel = api.JsonRpcNdmConsumerChannel;
            INdmNotifier               ndmNotifier               = api.NdmNotifier;
            PublicKey                  nodePublicKey             = api.Enode.PublicKey;
            ITimestamper               timestamper               = api.Timestamper;
            IWallet                    wallet                    = api.Wallet;
            IHttpClient                httpClient                = api.HttpClient;
            IJsonRpcClientProxy?       jsonRpcClientProxy        = api.JsonRpcClientProxy;
            IEthJsonRpcClientProxy?    ethJsonRpcClientProxy     = api.EthJsonRpcClientProxy;
            TransactionService         transactionService        = api.TransactionService;
            IMonitoringService         monitoringService         = api.MonitoringService;

            monitoringService?.RegisterMetrics(typeof(Metrics));

            DataRequestFactory     dataRequestFactory     = new DataRequestFactory(wallet, nodePublicKey);
            TransactionVerifier    transactionVerifier    = new TransactionVerifier(blockchainBridge, requiredBlockConfirmations);
            DepositUnitsCalculator depositUnitsCalculator = new DepositUnitsCalculator(sessionRepository, timestamper);
            DepositProvider        depositProvider        = new DepositProvider(depositRepository, depositUnitsCalculator, logManager);
            KycVerifier            kycVerifier            = new KycVerifier(depositApprovalRepository, logManager);
            ConsumerNotifier       consumerNotifier       = new ConsumerNotifier(ndmNotifier);

            DataAssetService   dataAssetService   = new DataAssetService(providerRepository, consumerNotifier, logManager);
            ProviderService    providerService    = new ProviderService(providerRepository, consumerNotifier, logManager);
            DataRequestService dataRequestService = new DataRequestService(dataRequestFactory, depositProvider, kycVerifier, wallet,
                                                                           providerService, timestamper, sessionRepository, consumerNotifier, logManager);

            SessionService sessionService = new SessionService(providerService, depositProvider, dataAssetService,
                                                               sessionRepository, timestamper, consumerNotifier, logManager);
            DataConsumerService dataConsumerService = new DataConsumerService(depositProvider, sessionService,
                                                                              consumerNotifier, timestamper, sessionRepository, logManager);
            DataStreamService dataStreamService = new DataStreamService(dataAssetService, depositProvider,
                                                                        providerService, sessionService, wallet, consumerNotifier, sessionRepository, logManager);
            DepositApprovalService depositApprovalService = new DepositApprovalService(dataAssetService, providerService,
                                                                                       depositApprovalRepository, timestamper, consumerNotifier, logManager);
            DepositConfirmationService depositConfirmationService = new DepositConfirmationService(blockchainBridge, consumerNotifier,
                                                                                                   depositRepository, depositService, logManager, requiredBlockConfirmations);

            IDepositManager depositManager = new DepositManager(depositService, depositUnitsCalculator,
                                                                dataAssetService, kycVerifier, providerService, abiEncoder, cryptoRandom, wallet, gasPriceService,
                                                                depositRepository, timestamper, logManager, requiredBlockConfirmations,
                                                                disableSendingDepositTransaction);

            if (instantDepositVerificationEnabled)
            {
                depositManager = new InstantDepositManager(depositManager, depositRepository, timestamper, logManager,
                                                           requiredBlockConfirmations);
            }

            DepositReportService depositReportService = new DepositReportService(depositRepository, receiptRepository, sessionRepository,
                                                                                 timestamper);
            ReceiptService receiptService = new ReceiptService(depositProvider, providerService, receiptRequestValidator,
                                                               sessionService, timestamper, receiptRepository, sessionRepository, abiEncoder, wallet, ecdsa,
                                                               nodePublicKey, logManager);
            RefundService refundService = new RefundService(blockchainBridge, abiEncoder, depositRepository,
                                                            contractAddress, logManager);
            RefundClaimant refundClaimant = new RefundClaimant(refundService, blockchainBridge, depositRepository,
                                                               transactionVerifier, gasPriceService, timestamper, logManager);

            api.AccountService = new AccountService(configManager, dataStreamService, providerService,
                                                    sessionService, consumerNotifier, wallet, configId, consumerAddress, logManager);
            ProxyService proxyService = new ProxyService(jsonRpcClientProxy, configManager, configId, logManager);

            api.ConsumerService = new ConsumerService(api.AccountService, dataAssetService, dataRequestService,
                                                      dataConsumerService, dataStreamService, depositManager, depositApprovalService, providerService,
                                                      receiptService, refundService, sessionService, proxyService);
            EthPriceService             ethPriceService             = new EthPriceService(httpClient, timestamper, logManager);
            ConsumerTransactionsService consumerTransactionsService = new ConsumerTransactionsService(transactionService, depositRepository,
                                                                                                      timestamper, logManager);
            ConsumerGasLimitsService gasLimitService = new ConsumerGasLimitsService(depositService, refundService);


            api.RpcModuleProvider.Register(
                new SingletonModulePool <INdmRpcConsumerModule>(new NdmRpcConsumerModule(api.ConsumerService,
                                                                                         depositReportService, jsonRpcNdmConsumerChannel, ethRequestService, ethPriceService,
                                                                                         gasPriceService, consumerTransactionsService, gasLimitService, wallet, timestamper), true));

            if (!backgroundServicesDisabled)
            {
                bool useDepositTimer = ndmConfig.ProxyEnabled;
                ConsumerServicesBackgroundProcessor consumerServicesBackgroundProcessor =
                    new ConsumerServicesBackgroundProcessor(
                        api.AccountService,
                        refundClaimant,
                        depositConfirmationService,
                        ethPriceService,
                        api.GasPriceService,
                        api.MainBlockProcessor,
                        depositRepository,
                        consumerNotifier,
                        logManager,
                        useDepositTimer,
                        ethJsonRpcClientProxy);
                consumerServicesBackgroundProcessor.Init();
            }
        }
コード例 #9
0
 public void Setup()
 {
     _ndmApi    = new NdmApi(Substitute.For <INethermindApi>());
     _ndmModule = new NdmModule(_ndmApi);
 }