public async Task try_confirm_should_confirm_deposit_if_timestamp_is_greater_than_0_and_required_number_of_confirmations_is_achieved()
        {
            const int  latestBlockNumber     = 3;
            const uint confirmationTimestamp = 1;
            var        block       = GetBlock();
            var        parentBlock = GetBlock();
            var        deposit     = GetDepositDetails();
            var        transaction = GetTransaction();

            _blockchainBridge.GetTransactionAsync(deposit.Transaction.Hash).Returns(transaction);
            _blockchainBridge.GetLatestBlockNumberAsync().Returns(latestBlockNumber);
            _blockchainBridge.FindBlockAsync(latestBlockNumber).Returns(block);
            _blockchainBridge.FindBlockAsync(block.ParentHash).Returns(parentBlock);
            _depositService.VerifyDepositAsync(deposit.Consumer, deposit.Id, block.Header.Number)
            .Returns(confirmationTimestamp);
            await _depositConfirmationService.TryConfirmAsync(deposit);

            deposit.Confirmed.Should().BeTrue();
            deposit.Confirmations.Should().Be(_requiredBlockConfirmations);
            await _depositRepository.Received().UpdateAsync(deposit);

            await _blockchainBridge.Received(2).GetLatestBlockNumberAsync();

            await _consumerNotifier.Received().SendDepositConfirmationsStatusAsync(deposit.Id, deposit.DataAsset.Name,
                                                                                   _requiredBlockConfirmations, _requiredBlockConfirmations, deposit.ConfirmationTimestamp, true);
        }
Exemplo n.º 2
0
        public async Task set_enabled_data_stream_should_succeed_for_existing_session()
        {
            var depositId = Keccak.Zero;
            var client    = "test";
            var args      = Array.Empty <string>();
            var session   = GetConsumerSession();

            _sessionService.GetActive(depositId).Returns(session);

            await _dataStreamService.SetEnabledDataStreamAsync(depositId, client, args);

            session.Clients.Single().Should().Be(new SessionClient(client, true, args));
            await _sessionRepository.Received(1).UpdateAsync(session);

            await _consumerNotifier.Received(1).SendDataStreamEnabledAsync(depositId, session.Id);
        }
Exemplo n.º 3
0
        // If deposits are eligable to refund (deposit is expired / consumer hasn't consumed all units yet etc.)
        // background proccessor will try to refund them every single block proccesed.
        public void Will_try_to_refund_deposit_while_expired_and_not_consumed()
        {
            _processor.Init();
            var blockProccesed = Build.A.Block.TestObject;

            _accountService.GetAddress().Returns(TestItem.AddressB);

            var dataAsset = new DataAsset(Keccak.OfAnEmptyString, "test", "test", 1,
                                          DataAssetUnitType.Unit, 0, 10, new DataAssetRules(new DataAssetRule(1)),
                                          new DataAssetProvider(Address.Zero, "test"));

            _refundClaimant.TryClaimEarlyRefundAsync(Arg.Any <DepositDetails>(), TestItem.AddressB)
            .Returns(new RefundClaimStatus(Keccak.Zero, true));

            var consumedDeposit        = new Deposit(Keccak.Zero, 10, 1, 1);
            var consumedDepositDetails = new DepositDetails(consumedDeposit, dataAsset, null, null, 1, null);

            consumedDepositDetails.SetConsumedUnits(9);

            var refundsResult = PagedResult <DepositDetails> .Create(
                new List <DepositDetails> {
                consumedDepositDetails
            },
                1,
                1,
                1,
                1);

            _depositRepository.BrowseAsync(Arg.Any <GetDeposits>()).Returns(Task.FromResult(refundsResult));
            _blockProcessor.BlockProcessed += Raise.EventWith(new BlockProcessedEventArgs(blockProccesed, Array.Empty <TxReceipt>()));
            _consumerNotifier.Received().SendClaimedEarlyRefundAsync(Arg.Any <Keccak>(), Arg.Any <string>(), Arg.Any <Keccak>());
        }
        public async Task data_availability_should_be_updated_when_session_exists()
        {
            var depositId        = Keccak.Zero;
            var dataAvailability = DataAvailability.Available;
            var session          = GetConsumerSession();

            _sessionService.GetActive(depositId).Returns(session);

            await _dataConsumerService.SetDataAvailabilityAsync(depositId, dataAvailability);

            session.DataAvailability.Should().Be(dataAvailability);
            _sessionService.Received(1).GetActive(depositId);
            await _sessionRepository.Received(1).UpdateAsync(session);

            await _consumerNotifier.Received(1)
            .SendDataAvailabilityChangedAsync(depositId, session.Id, dataAvailability);
        }
        public async Task send_data_request_should_succeed_for_valid_deposit()
        {
            var depositId = Keccak.Zero;
            var deposit   = GetDepositDetails(DepositConfirmationTimestamp, DepositExpiryTime);

            _depositProvider.GetAsync(depositId).Returns(deposit);
            _wallet.IsUnlocked(deposit.Consumer).Returns(true);
            var provider = Substitute.For <INdmPeer>();

            _providerService.GetPeer(deposit.DataAsset.Provider.Address).Returns(provider);
            var sessions = new List <ConsumerSession>
            {
                GetConsumerSession()
            };
            var dataRequest = new DataRequest(Keccak.OfAnEmptyString, 1, 1, 1, Array.Empty <byte>(), Address.Zero,
                                              Address.Zero, null);
            var sessionsPagedResult = PagedResult <ConsumerSession> .Create(sessions, 1, 1, 1, 1);

            _sessionRepository.BrowseAsync(Arg.Any <GetConsumerSessions>()).Returns(sessionsPagedResult);
            _dataRequestFactory.Create(deposit.Deposit, deposit.DataAsset.Id, deposit.DataAsset.Provider.Address,
                                       deposit.Consumer, deposit.Pepper).Returns(dataRequest);
            var consumedUnits = (uint)sessions.Sum(s => s.ConsumedUnits);

            provider.SendDataRequestAsync(dataRequest, consumedUnits).Returns(DataRequestResult.DepositVerified);

            var result = await _dataRequestService.SendAsync(depositId);

            result.Should().Be(DataRequestResult.DepositVerified);
            await _depositProvider.Received(1).GetAsync(depositId);

            _wallet.Received(1).IsUnlocked(deposit.Consumer);
            await _kycVerifier.DidNotReceive().IsVerifiedAsync(deposit.DataAsset.Id, deposit.Consumer);

            _providerService.Received(1).GetPeer(deposit.DataAsset.Provider.Address);
            await _sessionRepository.Received(1).BrowseAsync(Arg.Any <GetConsumerSessions>());

            _dataRequestFactory.Received(1).Create(deposit.Deposit, deposit.DataAsset.Id,
                                                   deposit.DataAsset.Provider.Address, deposit.Consumer, deposit.Pepper);
            await provider.Received(1).SendDataRequestAsync(dataRequest, consumedUnits);

            await _consumerNotifier.Received(1).SendDataRequestResultAsync(depositId, result);
        }