예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #5
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);
        }
예제 #6
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);
        }