public async Task Can_update_async()
        {
            IMongoDatabase  database = MongoForTest.TempDb.GetDatabase();
            var             repo     = new ConsumerSessionMongoRepository(database);
            ConsumerSession session  = BuildDummySession();
            await repo.AddAsync(session);

            session.Start(1);
            await repo.UpdateAsync(session);

            var result = await repo.GetAsync(session.Id);

            result.Should().BeEquivalentTo(session);
        }
Пример #2
0
        public async Task StartSessionAsync(Session session, INdmPeer provider)
        {
            DepositDetails?deposit = await _depositProvider.GetAsync(session.DepositId);

            if (deposit is null)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Cannot start the session: '{session.Id}', deposit: '{session.DepositId}' was not found.");
                }
                return;
            }

            if (session.StartTimestamp < deposit.ConfirmationTimestamp)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Cannot start the session: '{session.Id}', session timestamp {session.StartTimestamp} is before deposit confirmation timestamp {deposit.ConfirmationTimestamp}.");
                }
                return;
            }

            Keccak dataAssetId = deposit.DataAsset.Id;

            if (dataAssetId != session.DataAssetId)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Inconsistent data - data asset ID on deposit is '{dataAssetId}' while on session is '{session.DataAssetId}'.");
                }
                return;
            }

            DataAsset?dataAsset = _dataAssetService.GetDiscovered(dataAssetId);

            if (dataAsset is null)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Available data asset: '{dataAssetId}' was not found.");
                }
                return;
            }

            if (!_dataAssetService.IsAvailable(dataAsset))
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Data asset: '{dataAssetId}' is unavailable, state: {dataAsset.State}.");
                }
                return;
            }

            if (session.ProviderAddress == null)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Session: '{session.Id}' for '{session.DepositId}' cannot be started because of the unknown provider address.");
                }
                return;
            }

            if (!provider.ProviderAddress !.Equals(deposit.DataAsset.Provider.Address))
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Cannot start the session: '{session.Id}' for deposit: '{session.DepositId}', provider address (peer): '{provider.ProviderAddress}' doesn't equal the address from data asset: '{deposit.DataAsset.Provider.Address}'.");
                }
                return;
            }

            PagedResult <ConsumerSession> sessions = await _sessionRepository.BrowseAsync(new GetConsumerSessions
            {
                DepositId = session.DepositId,
                Results   = int.MaxValue
            });

            uint consumedUnits = sessions.Items.Any() ? (uint)sessions.Items.Sum(s => s.ConsumedUnits) : 0;

            if (_logger.IsInfo)
            {
                _logger.Info($"Starting the session: '{session.Id}' for deposit: '{session.DepositId}'. Settings consumed units - provider: {session.StartUnitsFromProvider}, consumer: {consumedUnits}.");
            }
            ConsumerSession consumerSession = ConsumerSession.From(session);

            consumerSession.Start(session.StartTimestamp);
            ConsumerSession?previousSession = await _sessionRepository.GetPreviousAsync(consumerSession);

            uint upfrontUnits = (uint)(deposit.DataAsset.Rules.UpfrontPayment?.Value ?? 0);

            if (upfrontUnits > 0 && previousSession is null)
            {
                consumerSession.AddUnpaidUnits(upfrontUnits);
                if (_logger.IsInfo)
                {
                    _logger.Info($"Unpaid units: {upfrontUnits} for session: '{session.Id}' based on upfront payment.");
                }
            }

            uint unpaidUnits = previousSession?.UnpaidUnits ?? 0;

            if (unpaidUnits > 0 && !(previousSession is null))
            {
                consumerSession.AddUnpaidUnits(unpaidUnits);
                if (_logger.IsInfo)
                {
                    _logger.Info($"Unpaid units: {unpaidUnits} for session: '{session.Id}' from previous session: '{previousSession.Id}'.");
                }
            }

            if (deposit.DataAsset.UnitType == DataAssetUnitType.Time)
            {
                uint unpaidTimeUnits = (uint)consumerSession.StartTimestamp - deposit.ConfirmationTimestamp;
                consumerSession.AddUnpaidUnits(unpaidTimeUnits);
                if (_logger.IsInfo)
                {
                    _logger.Info($"Unpaid units: '{unpaidTimeUnits}' for deposit: '{session.DepositId}' based on time.");
                }
            }

            SetActiveSession(consumerSession);
            await _sessionRepository.AddAsync(consumerSession);

            await _consumerNotifier.SendSessionStartedAsync(session.DepositId, session.Id);

            if (_logger.IsInfo)
            {
                _logger.Info($"Started a session with id: '{session.Id}' for deposit: '{session.DepositId}', address: '{deposit.Consumer}'.");
            }
        }