Ejemplo n.º 1
0
        public async Task should_return_0_consumed_units_for_unconfirmed_deposit()
        {
            var deposit       = GetDepositDetails(confirmationTimestamp: 0);
            var consumedUnits = await _calculator.GetConsumedAsync(deposit);

            consumedUnits.Should().Be(0);

            await _sessionRepository.DidNotReceive().BrowseAsync(Arg.Any <GetConsumerSessions>());
        }
Ejemplo n.º 2
0
        public async Task <DepositDetails?> GetAsync(Keccak depositId)
        {
            DepositDetails?deposit = await _depositRepository.GetAsync(depositId);

            if (deposit is null)
            {
                return(null);
            }

            uint consumedUnits = await _depositUnitsCalculator.GetConsumedAsync(deposit);

            deposit.SetConsumedUnits(consumedUnits);

            return(deposit);
        }
        public async Task <PagedResult <DepositDetails> > BrowseAsync(GetDeposits query)
        {
            if (query is null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var depositsBytes = _database.GetAllValues().ToArray();

            if (depositsBytes.Length == 0)
            {
                return(PagedResult <DepositDetails> .Empty);
            }

            DepositDetails[] deposits = new DepositDetails[depositsBytes.Length];
            for (var i = 0; i < depositsBytes.Length; i++)
            {
                deposits[i] = Decode(depositsBytes[i]);
            }

            IEnumerable <DepositDetails> filteredDeposits = deposits.AsEnumerable();

            if (query.OnlyPending)
            {
                filteredDeposits = filteredDeposits.Where(d => !d.Rejected && !d.RefundClaimed &&
                                                          (d.Transaction?.State == TransactionState.Pending ||
                                                           d.ClaimedRefundTransaction?.State ==
                                                           TransactionState.Pending));
            }

            if (query.OnlyUnconfirmed)
            {
                filteredDeposits = filteredDeposits.Where(d => d.ConfirmationTimestamp == 0 ||
                                                          d.Confirmations < d.RequiredConfirmations);
            }

            if (query.OnlyNotRejected)
            {
                filteredDeposits = filteredDeposits.Where(d => !d.Rejected);
            }

            if (query.EligibleToRefund)
            {
                foreach (var deposit in deposits)
                {
                    uint consumedUnits = await _depositUnitsCalculator.GetConsumedAsync(deposit);

                    deposit.SetConsumedUnits(consumedUnits);
                }

                filteredDeposits = GetEligibleToRefund(filteredDeposits, query.CurrentBlockTimestamp);
            }

            return(filteredDeposits.OrderByDescending(d => d.Timestamp).ToArray().Paginate(query));
        }
        public async Task <PagedResult <DepositDetails> > BrowseAsync(GetDeposits query)
        {
            if (query is null)
            {
                return(PagedResult <DepositDetails> .Empty);
            }

            var deposits = Deposits.AsQueryable();

            if (query.OnlyPending || query.OnlyUnconfirmed || query.OnlyNotRejected || query.EligibleToRefund)
            {
                //MongoDB unsupported predicate: (d.Confirmations < d.RequiredConfirmations) - maybe due to uint type?
                var allDeposits = await deposits.ToListAsync();

                var filteredDeposits = allDeposits.AsEnumerable();
                if (query.OnlyPending)
                {
                    filteredDeposits = filteredDeposits.Where(d => !d.Rejected && !d.RefundClaimed &&
                                                              (d.Transaction?.State == TransactionState.Pending ||
                                                               d.ClaimedRefundTransaction?.State ==
                                                               TransactionState.Pending));
                }

                if (query.OnlyUnconfirmed)
                {
                    filteredDeposits = filteredDeposits.Where(d => d.ConfirmationTimestamp == 0 ||
                                                              d.Confirmations < d.RequiredConfirmations);
                }

                if (query.OnlyNotRejected)
                {
                    filteredDeposits = filteredDeposits.Where(d => !d.Rejected);
                }

                if (query.EligibleToRefund)
                {
                    foreach (var deposit in deposits)
                    {
                        uint consumedUnits = await _depositUnitsCalculator.GetConsumedAsync(deposit);

                        deposit.SetConsumedUnits(consumedUnits);
                    }

                    filteredDeposits = filteredDeposits.Where(d => !d.RefundClaimed && (d.ConsumedUnits < d.Deposit.Units) &&
                                                              (!(d.EarlyRefundTicket is null) ||
                                                               query.CurrentBlockTimestamp >= d.Deposit.ExpiryTime
                                                              ));
                }

                return(filteredDeposits.OrderByDescending(d => d.Timestamp).ToArray().Paginate(query));
            }

            return(await deposits.OrderByDescending(d => d.Timestamp).PaginateAsync(query));
        }
Ejemplo n.º 5
0
        private async Task <DepositDetails> FetchAsync(Keccak depositId)
        {
            var deposit = await _depositRepository.GetAsync(depositId);

            if (deposit is null)
            {
                return(null);
            }

            var consumedUnits = await _depositUnitsCalculator.GetConsumedAsync(deposit);

            deposit.SetConsumedUnits(consumedUnits);

            return(deposit);
        }
Ejemplo n.º 6
0
        public async Task <DepositsReport> GetAsync(GetDepositsReport query)
        {
            PagedResult <DepositDetails> deposits;

            if (query.DepositId == null)
            {
                deposits =
                    await _depositRepository.BrowseAsync(new GetDeposits
                {
                    Results = int.MaxValue
                });
            }
            else
            {
                DepositDetails?detailsOfOne = await _depositRepository.GetAsync(query.DepositId);

                if (detailsOfOne is null)
                {
                    return(DepositsReport.Empty);
                }

                deposits = PagedResult <DepositDetails> .Create(new[] { detailsOfOne },
                                                                1, 1, 1, 1);
            }

            if (deposits.Items.Count == 0)
            {
                return(DepositsReport.Empty);
            }

            if (!deposits.Items.Any() || deposits.Items.Any(d => d is null))
            {
                return(DepositsReport.Empty);
            }

            Dictionary <Keccak, DepositDetails> foundDeposits = deposits.Items
                                                                .Where(d => (query.Provider is null || d.DataAsset.Provider.Address == query.Provider) &&
                                                                       (query.AssetId is null || d.DataAsset.Id == query.AssetId))
                                                                .ToDictionary(d => d.Id, d => d);

            if (!foundDeposits.Any())
            {
                return(DepositsReport.Empty);
            }

            IEnumerable <Keccak> assetIds = foundDeposits.Select(d => d.Value.DataAsset.Id);
            IReadOnlyList <DataDeliveryReceiptDetails> receipts = await _receiptRepository.BrowseAsync(query.DepositId, query.AssetId);

            Dictionary <Keccak, IEnumerable <DataDeliveryReceiptDetails> > depositsReceipts = receipts.Where(r => assetIds.Contains(r.DataAssetId))
                                                                                              .GroupBy(r => r.DepositId).ToDictionary(r => r.Key, r => r.AsEnumerable());

            int page = query.Page;

            if (page <= 0)
            {
                page = 1;
            }

            int results = query.Results;

            if (results <= 0)
            {
                results = 10;
            }

            uint timestamp = (uint)_timestamper.EpochSeconds;
            int  skip      = (page - 1) * results;
            List <DepositReportItem> items = new List <DepositReportItem>();

            foreach ((Keccak _, DepositDetails deposit) in foundDeposits.OrderByDescending(d => d.Value.Timestamp).Skip(skip)
                     .Take(results))
            {
                depositsReceipts.TryGetValue(deposit.Id, out IEnumerable <DataDeliveryReceiptDetails>?depositReceipts);
                bool expired = deposit.IsExpired(timestamp);
                IEnumerable <DataDeliveryReceiptReportItem>?receiptItems = depositReceipts?.Select(r => new DataDeliveryReceiptReportItem(r.Id, r.Number,
                                                                                                                                          r.SessionId, r.ConsumerNodeId, r.Request, r.Receipt, r.Timestamp, r.IsMerged, r.IsClaimed));
                PagedResult <ConsumerSession> sessions = await _sessionRepository.BrowseAsync(new GetConsumerSessions
                {
                    DepositId = deposit.Id,
                    Results   = int.MaxValue
                });

                uint consumedUnits = await _depositUnitsCalculator.GetConsumedAsync(deposit);

                items.Add(ToReportItem(deposit, expired, consumedUnits, receiptItems ?? Enumerable.Empty <DataDeliveryReceiptReportItem>()));
            }

            (UInt256 total, UInt256 claimed, UInt256 refunded) = CalculateValues(foundDeposits, depositsReceipts);
            int totalResults = foundDeposits.Count;
            int totalPages   = (int)Math.Ceiling((double)totalResults / query.Results);

            return(new DepositsReport(total, claimed, refunded,
                                      PagedResult <DepositReportItem> .Create(items.OrderByDescending(i => i.Timestamp).ToList(),
                                                                              query.Page, query.Results, totalPages, totalResults)));
        }
        public async Task Browse_by_eligible_to_refund()
        {
            depositUnitsCalculator.GetConsumedAsync(Arg.Is <DepositDetails>(d => d.Timestamp == 1000)).Returns(Task.FromResult((uint)200));
            foreach (DepositDetails details in _cases)
            {
                await repository.AddAsync(details);
            }

            PagedResult <DepositDetails> result = await repository.BrowseAsync(new GetDeposits { EligibleToRefund = true, CurrentBlockTimestamp = 200 });

            result.Items.Should().HaveCount(1);
        }