示例#1
0
        public async Task OneTimeSetUp()
        {
            InitDecoders();
            SetUpSubstitutes();

            logManager  = NoErrorLimboLogs.Instance;
            timestamper = new Timestamper();

            config   = new NdmConfig();
            configId = config.Id;

            var configDb         = new MemDb(NdmDbNames.Configs);
            var configRepository = new ConfigRocksRepository(configDb, new NdmConfigDecoder());
            await configRepository.AddAsync(config);

            configManager = new ConfigManager(config, configRepository);

            config.ProviderAddress           = "c7f8522f15c189e00d2f895b4528b4f84516cd7b";
            config.ProviderColdWalletAddress = "c7f8522f15c189e00d2f895b4528b4f84516cd7b";

            config.ConsumerAddress = "a238812fb5c199ea051f89200028b4f08513cd6a";

            providerAddress   = new Address(config.ProviderAddress);
            coldWalletAddress = new Address(config.ProviderColdWalletAddress);

            var sessionDb = new MemDb(ProviderDbNames.ProviderSessions);

            sessionRepository = new ProviderSessionRocksRepository(sessionDb, new ProviderSessionDecoder());
            sessionManager    = new SessionManager(sessionRepository, timestamper, logManager);

            var consumersDb = new MemDb(ProviderDbNames.Consumers);

            consumerRepository = new ConsumerRocksRepository(consumersDb, new ConsumerDecoder());

            paymentClaimDb         = new MemDb(ProviderDbNames.PaymentClaims);
            paymentClaimRepository = new PaymentClaimRocksRepository(paymentClaimDb, new PaymentClaimDecoder());

            var dataAssetDb = new MemDb(ProviderDbNames.DataAssets);

            dataAssetRepository = new DataAssetRocksRepository(dataAssetDb, new DataAssetDecoder());

            ecdsa                     = new Ecdsa();
            abiEncoder                = new AbiEncoder();
            ndmDataPublisher          = new NdmDataPublisher();
            dataAvaliabilityValidator = new DataAvailabilityValidator();

            nodeId       = new PublicKey(new CryptoRandom().GenerateRandomBytes(64));
            providerName = "test";
            filesPath    = config.FilesPath;
            fileMaxSize  = config.FileMaxSize;
            requiredBlockConfirmations = config.BlockConfirmations;
            paymentGasLimit            = 70000;
            skipDepositVerification    = true;
            backgroundServicesDisabled = false;

            DataAsset dataAsset = new DataAsset(new Keccak(Keccak.OfAnEmptyString.ToString()), "TestDataAsset", "Testing", 1, DataAssetUnitType.Unit, 1, 10, new DataAssetRules(new DataAssetRule(10)), new DataAssetProvider(providerAddress, "provider"), null, QueryType.Query);

            await dataAssetRepository.AddAsync(dataAsset);

            paymentClaimProcessor.SendTransactionAsync(Arg.Any <PaymentClaim>(), Arg.Any <UInt256>()).Returns(new Keccak("0x77a0e79f851c097f81210d88eb59ed8f933336f65e88b0bde6506f2c6556c2b6"));

            gasPriceService.GetCurrentPaymentClaimGasPriceAsync().Returns(Task.FromResult(new UInt256(10)));

            providerService = new ProviderService(configManager, configId, consumerRepository, dataAssetRepository, providerDepositApprovalRepository, paymentClaimRepository, paymentClaimProcessor, sessionRepository, timestamper, ecdsa, abiEncoder, ndmDataPublisher, gasPriceService, dataAvaliabilityValidator, sessionManager, transactionVerifier, depositManager, refundPolicy, depositService, wallet, blockchainBridge, providerAddress, coldWalletAddress, nodeId, providerName, filesPath, fileMaxSize, requiredBlockConfirmations, paymentGasLimit, logManager, skipDepositVerification, backgroundServicesDisabled);
        }
示例#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);
        }