Exemple #1
0
        public async Task given_some_failing_nodes_for_the_same_deposit_receipts_requests_should_have_valid_ranges()
        {
            var depositId = Keccak.Zero;
            var consumer  = TestConsumer.ForDeposit(depositId)
                            .WithNode(1).AddSession().WithUnpaidUnits(10)
                            .And.WithNode(2).AddSession().WithUnpaidUnits(10).Node.WillNotDeliverReceipt()
                            .And.WithNode(3).AddSession().WithUnpaidUnits(20)
                            .And.WithNode(4).AddSession().WithUnpaidUnits(20).Node.WillNotDeliverReceipt()
                            .And.WithNode(5).AddSession().WithUnpaidUnits(20)
                            .And.Build();
            var receipts = _depositNodesHandler.Receipts.ToList();


            ConfigureMocks(consumer);

            await _depositManager.InitAsync(depositId);

            await _depositManager.HandleConsumedUnitAsync(depositId);

            consumer.AddReceipts(_depositNodesHandler.Receipts.ToArray());
            consumer.Node(1).ShouldDeliverReceiptWithinRange(1, 0, 9);
            consumer.Node(2).ShouldNotDeliverReceipt();
            consumer.Node(3).ShouldDeliverReceiptWithinRange(3, 10, 29);
            consumer.Node(4).ShouldNotDeliverReceipt();
            consumer.Node(5).ShouldDeliverReceiptWithinRange(5, 30, 49);
        }
Exemple #2
0
        public async Task returns_unclaimed_units_correctly()
        {
            var          depositId = Keccak.Zero;
            TestConsumer consumer  = TestConsumer.ForDeposit(depositId)
                                     .WithNode(1).AddSession().WithUnpaidUnits(10)
                                     .And.WithNode(2).AddSession().WithUnpaidUnits(20)
                                     .And.Build();

            ConfigureMocks(consumer);

            _sessionManager.GetSession(depositId, consumer.Node(1).Node.Peer)
            .Returns(consumer.Node(1).Node.Sessions.First(s => s.DepositId == depositId));

            IDepositNodesHandler depositHandler = await _depositManager.InitAsync(depositId);

            AddReciptsToMerge(consumer, depositHandler);
            _receiptsPolicies.CanMergeReceipts(depositHandler.UnmergedUnits, depositHandler.UnitPrice).Returns(true);

            depositHandler.SetConsumedUnits(80);
            depositHandler.SetUnmergedUnits(50);

            await _depositManager.HandleUnpaidUnitsAsync(depositId, consumer.Node(1).Node.Peer);

            var unclaimedUnits = _depositManager.GetUnclaimedUnits(depositId);

            Assert.AreEqual(10, unclaimedUnits);
        }
Exemple #3
0
        // There is no need to match merge threshold while all of the units has been consumed
        public async Task will_merge_if_consumed_all_but_did_not_match_threshold()
        {
            var          depositId = Keccak.Zero;
            TestConsumer consumer  = TestConsumer.ForDeposit(depositId)
                                     .WithNode(1).AddSession().WithUnpaidUnits(10)
                                     .And.WithNode(2).AddSession().WithUnpaidUnits(20)
                                     .And.Build();

            ConfigureMocks(consumer);

            _sessionManager.GetSession(depositId, consumer.Node(1).Node.Peer)
            .Returns(consumer.Node(1).Node.Sessions.First(s => s.DepositId == depositId));

            IDepositNodesHandler depositHandler = await _depositManager.InitAsync(depositId);

            AddReciptsToMerge(consumer, depositHandler);
            _receiptsPolicies.CanMergeReceipts(depositHandler.UnmergedUnits, depositHandler.UnitPrice).Returns(false);

            depositHandler.SetConsumedUnits(100);
            depositHandler.SetUnmergedUnits(50);

            await _depositManager.HandleUnpaidUnitsAsync(depositId, consumer.Node(1).Node.Peer);

            Assert.IsTrue(depositHandler.UnmergedUnits == 0);
        }
Exemple #4
0
        public async Task will_calculate_units_correctly_when_unit_type_is_time()
        {
            var          depositId = Keccak.Zero;
            TestConsumer consumer  = TestConsumer.ForDeposit(depositId, DataAssetUnitType.Time)
                                     .WithNode(1).AddSession().WithUnpaidUnits(10).WithConsumedUnits(30)
                                     .And.Build();

            ConfigureMocks(consumer);

            var depositNodesHandler = new InMemoryDepositNodesHandler(Keccak.Zero,
                                                                      _consumer,
                                                                      DataAssetUnitType.Time,
                                                                      1,
                                                                      100,
                                                                      1,
                                                                      60,
                                                                      50,
                                                                      30,
                                                                      50,
                                                                      0,
                                                                      0,
                                                                      null,
                                                                      null,
                                                                      0);

            _depositNodesHandlerFactory.CreateInMemory(depositId,
                                                       Arg.Any <Address>(),
                                                       DataAssetUnitType.Time, Arg.Any <uint>(),
                                                       Arg.Any <uint>(), Arg.Any <UInt256>(), Arg.Any <uint>(),
                                                       Arg.Any <uint>(),
                                                       Arg.Any <uint>(),
                                                       Arg.Any <uint>(),
                                                       Arg.Any <uint>(),
                                                       Arg.Any <uint>(),
                                                       Arg.Any <PaymentClaim>(),
                                                       Arg.Any <IEnumerable <DataDeliveryReceiptDetails> >(),
                                                       Arg.Any <uint>())
            .Returns(depositNodesHandler);

            IDepositNodesHandler depositHandler = await _depositManager.InitAsync(depositId);

            await _depositManager.HandleConsumedUnitAsync(depositId);

            Assert.IsTrue(depositHandler.ConsumedUnits == 99);
            Assert.IsTrue(depositHandler.UnpaidUnits == 79);
            Assert.IsTrue(depositHandler.UnclaimedUnits == 89);
            Assert.IsTrue(depositHandler.UnmergedUnits == 69);
        }
Exemple #5
0
        private void AddReciptsToMerge(TestConsumer consumer, IDepositNodesHandler depositHandler)
        {
            DataDeliveryReceiptRequest request = new DataDeliveryReceiptRequest(1, consumer.DepositId, new UnitsRange(0, 5), false, new List <DataDeliveryReceiptToMerge> {
                new DataDeliveryReceiptToMerge(new UnitsRange(0, 1), new Signature(1, 2, 37))
            });
            DataDeliveryReceipt        receipt        = new DataDeliveryReceipt(StatusCodes.Ok, 50, 0, new Signature(1, 2, 37));
            DataDeliveryReceiptDetails receiptDetails = new DataDeliveryReceiptDetails(Keccak.OfAnEmptyString, consumer.Sessions.First().Id, consumer.DataAsset.Id, null, request, receipt, 10, true);

            depositHandler.AddReceipt(receiptDetails);

            DataDeliveryReceiptRequest request2        = new DataDeliveryReceiptRequest(1, consumer.DepositId, new UnitsRange(6, 49));
            DataDeliveryReceipt        receipt2        = new DataDeliveryReceipt(StatusCodes.Ok, 50, 0, new Signature(1, 2, 37));
            DataDeliveryReceiptDetails receiptDetails2 = new DataDeliveryReceiptDetails(Keccak.OfAnEmptyString, consumer.Sessions.First().Id, consumer.DataAsset.Id, null, request2, receipt2, 10, false);

            depositHandler.AddReceipt(receiptDetails2);
        }
Exemple #6
0
        // While running HandleUnpaidUnitsAsync() DepositManager will try to claim payment for
        // unpaid units.
        // The method starts with previous session and checks whether there are any unpaid units and then
        // moves to the current one.
        public async Task can_handle_unpaid_units()
        {
            var          depositId = Keccak.Zero;
            TestConsumer consumer  = TestConsumer.ForDeposit(depositId)
                                     .WithNode(1).AddSession().WithUnpaidUnits(10)
                                     .And.WithNode(2).AddSession().WithUnpaidUnits(20)
                                     .And.Build();

            ConfigureMocks(consumer);

            _sessionManager.GetSession(depositId, consumer.Node(1).Node.Peer)
            .Returns(consumer.Node(1).Node.Sessions.First(s => s.DepositId == depositId));

            IDepositNodesHandler depositHandler = await _depositManager.InitAsync(depositId);

            await _depositManager.HandleUnpaidUnitsAsync(depositId, consumer.Node(1).Node.Peer);

            Assert.IsTrue(depositHandler.UnpaidUnits == 0);
        }
Exemple #7
0
        public async Task given_next_iteration_for_receipts_requests_calculated_ranges_should_be_still_valid()
        {
            var depositId = Keccak.Zero;
            var consumer  = TestConsumer.ForDeposit(depositId)
                            .WithNode(1).AddSession().WithUnpaidUnits(10)
                            .And.WithNode(2).AddSession().WithUnpaidUnits(10)
                            .And.WithNode(3).AddSession().WithUnpaidUnits(20)
                            .And.Build();

            ConfigureMocks(consumer);

            await _depositManager.InitAsync(depositId);

            await _depositManager.HandleConsumedUnitAsync(depositId);

            consumer.AddReceipts(_depositNodesHandler.Receipts.ToArray());
            consumer.Node(1).ShouldDeliverReceiptWithinRange(1, 0, 9);
            consumer.Node(2).ShouldDeliverReceiptWithinRange(2, 10, 19);
            consumer.Node(3).ShouldDeliverReceiptWithinRange(3, 20, 39);
        }
Exemple #8
0
        private void ConfigureMocks(TestConsumer consumer)
        {
            var depositId = consumer.DepositId;

            _consumerRepository.GetAsync(depositId).Returns(consumer.Consumer);

            _paymentClaimRepository.BrowseAsync(Arg.Any <GetPaymentClaims>())
            .Returns(PagedResult <PaymentClaim> .Empty);

            _sessionRepository.BrowseAsync(new GetProviderSessions
            {
                DepositId = depositId
            })
            .Returns(PagedResult <ProviderSession> .Create(new List <ProviderSession>(consumer.Sessions), 1, 1, 1, 1));

            _receiptsPolicies.CanRequestReceipts(Arg.Any <long>(), Arg.Any <UInt256>()).Returns(true);

            _sessionManager.GetConsumerNodes(depositId).Returns(consumer.Nodes.Select(n => n.Node).ToArray());

            _receiptProcessor.TryProcessAsync(Arg.Any <ProviderSession>(), _consumer, Arg.Any <INdmProviderPeer>(),
                                              Arg.Any <DataDeliveryReceiptRequest>(), Arg.Any <DataDeliveryReceipt>()).Returns(true);
        }