Example #1
0
        public DataAsset Decode(RlpStream rlpStream, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
        {
            try
            {
                rlpStream.ReadSequenceLength();
                Keccak            id                 = rlpStream.DecodeKeccak();
                string            name               = rlpStream.DecodeString();
                string            description        = rlpStream.DecodeString();
                UInt256           unitPrice          = rlpStream.DecodeUInt256();
                DataAssetUnitType unitType           = (DataAssetUnitType)rlpStream.DecodeInt();
                uint              minUnits           = rlpStream.DecodeUInt();
                uint              maxUnits           = rlpStream.DecodeUInt();
                DataAssetRules    rules              = Serialization.Rlp.Rlp.Decode <DataAssetRules>(rlpStream);
                DataAssetProvider provider           = Serialization.Rlp.Rlp.Decode <DataAssetProvider>(rlpStream);
                string            file               = rlpStream.DecodeString();
                QueryType         queryType          = (QueryType)rlpStream.DecodeInt();
                DataAssetState    state              = (DataAssetState)rlpStream.DecodeInt();
                string            termsAndConditions = rlpStream.DecodeString();
                bool              kycRequired        = rlpStream.DecodeBool();
                string            plugin             = rlpStream.DecodeString();

                return(new DataAsset(id, name, description, unitPrice, unitType, minUnits, maxUnits,
                                     rules, provider, file, queryType, state, termsAndConditions, kycRequired, plugin));
            }
            catch (Exception e)
            {
                throw new RlpException($"{nameof(DataAsset)} could not be deserialized", e);
            }
        }
        public InMemoryDepositNodesHandler(Keccak depositId, Address consumer, DataAssetUnitType unitType,
                                           uint verificationTimestamp, uint purchasedUnits, UInt256 unitPrice, uint consumedUnits,
                                           uint unpaidUnits, uint unmergedUnits, uint unclaimedUnits, uint graceUnits, uint expiryTime,
                                           PaymentClaim latestPaymentClaim, IEnumerable <DataDeliveryReceiptDetails> receipts,
                                           uint latestReceiptRequestNumber)
        {
            DepositId             = depositId;
            Consumer              = consumer;
            UnitType              = unitType;
            VerificationTimestamp = verificationTimestamp;
            PurchasedUnits        = purchasedUnits;
            UnitPrice             = unitPrice;
            _consumedUnits        = consumedUnits;
            _unpaidUnits          = unpaidUnits;
            _unmergedUnits        = unmergedUnits;
            _unclaimedUnits       = unclaimedUnits;
            _graceUnits           = graceUnits;
            ExpiryTime            = expiryTime;
            _latestPaymentClaim   = latestPaymentClaim;
            _receipts             = new ConcurrentStack <DataDeliveryReceiptDetails>(
                receipts ?? Enumerable.Empty <DataDeliveryReceiptDetails>());
            _latestReceiptRequestNumber = latestReceiptRequestNumber;
            var latestReceipt       = _receipts.Where(r => !r.IsMerged).OrderBy(r => r.Number).LastOrDefault();
            var latestMergedReceipt = _receipts.Where(r => r.IsMerged).OrderBy(r => r.Number).LastOrDefault();

            SetLatestReceipt(latestReceipt);
            SetLatestReceipt(latestMergedReceipt);
        }
Example #3
0
        private void Test(DataAssetUnitType unitType, uint depositedUnits,
                          long consumedUnits, uint startTimestamp, ulong nowSeconds, DataAvailability availability)
        {
            var dataAvailability = _validator.GetAvailability(unitType, depositedUnits, consumedUnits,
                                                              startTimestamp, nowSeconds);

            dataAvailability.Should().Be(availability);
        }
Example #4
0
            public Builder(Keccak depositId, DataAssetUnitType unitType)
            {
                _depositId = depositId;
                var dataAsset   = unitType == DataAssetUnitType.Unit ? CreateDataAsset() : CreateTimeDataAsset();
                var dataRequest = CreateDataRequest(dataAsset.Id);

                _consumer.DepositId = depositId;
                _consumer.Consumer  = new Consumer(depositId, 0, dataRequest, dataAsset);
            }
        private DepositDetails CreateDeposit(DataAssetUnitType type)
        {
            var deposit = new Deposit(Keccak.Zero, units: 100, expiryTime: 100, value: 10);

            var dataAsset = new DataAsset(Keccak.OfAnEmptyString,
                                          name: "TestAsset",
                                          description: "Test",
                                          unitPrice: 10,
                                          unitType: type,
                                          minUnits: 1,
                                          maxUnits: 100,
                                          rules: new DataAssetRules(new DataAssetRule(1)),
                                          provider: new DataAssetProvider(TestItem.AddressA, "provider"));

            return(new DepositDetails(deposit,
                                      dataAsset,
                                      consumer: TestItem.AddressB,
                                      pepper: Array.Empty <byte>(),
                                      timestamp: 50,
                                      transactions: Array.Empty <TransactionInfo>()));
        }
        private async Task VerifyUnitsAsync(DataAssetUnitType unitType, uint paidUnits,
                                            Func <DepositDetails, Session, uint> expectedConsumedUnits,
                                            Func <DepositDetails, Session, uint> expectedUnpaidUnits)
        {
            var depositId = Keccak.Zero;
            var consumedUnitsFromProvider = 10u;
            var session = GetConsumerSession(paidUnits);
            var deposit = GetDepositDetails(unitType);

            _sessionService.GetActive(depositId).Returns(session);
            _depositProvider.GetAsync(session.DepositId).Returns(deposit);

            await _dataConsumerService.SetUnitsAsync(depositId, consumedUnitsFromProvider);

            session.ConsumedUnitsFromProvider.Should().Be(consumedUnitsFromProvider);
            session.ConsumedUnits.Should().Be(expectedConsumedUnits(deposit, session));
            session.UnpaidUnits.Should().Be(expectedUnpaidUnits(deposit, session));
            await _depositProvider.Received(1).GetAsync(depositId);

            _sessionService.Received(1).GetActive(depositId);
            await _sessionRepository.Received(1).UpdateAsync(session);
        }
        public DataAvailability GetAvailability(DataAssetUnitType unitType, uint purchasedUnits,
                                                long consumedUnits, uint verificationTimestamp, ulong nowSeconds)
        {
            switch (unitType)
            {
            case DataAssetUnitType.Time:
                if (verificationTimestamp + purchasedUnits > nowSeconds)
                {
                    return(DataAvailability.Available);
                }

                return(DataAvailability.SubscriptionEnded);

            case DataAssetUnitType.Unit:
                if (consumedUnits < purchasedUnits)
                {
                    return(DataAvailability.Available);
                }

                return(DataAvailability.UnitsExceeded);

            default: return(DataAvailability.Available);
            }
        }
Example #8
0
        public DataAsset(Keccak id, string name, string description, UInt256 unitPrice,
                         DataAssetUnitType unitType, uint minUnits, uint maxUnits, DataAssetRules rules,
                         DataAssetProvider provider, string file = null, QueryType queryType          = QueryType.Stream,
                         DataAssetState state = DataAssetState.Unpublished, string termsAndConditions = null,
                         bool kycRequired     = false, string plugin = null)
        {
            if (provider == null || string.IsNullOrWhiteSpace(provider.Name) || provider.Address == null)
            {
                throw new ArgumentException("Invalid data asset provider.", nameof(provider));
            }

            if (id == Keccak.Zero)
            {
                throw new ArgumentException("Invalid data asset id.", nameof(id));
            }

            if (string.IsNullOrWhiteSpace(name) || name.Length > 100)
            {
                throw new ArgumentException("Invalid data asset name.", nameof(name));
            }

            if (string.IsNullOrWhiteSpace(description) || description.Length > 256)
            {
                throw new ArgumentException("Invalid data asset description.", nameof(description));
            }

            if (termsAndConditions?.Length > 10000)
            {
                throw new ArgumentException("Invalid terms and conditions (over 10000 chars).", nameof(description));
            }

            if (rules is null)
            {
                throw new ArgumentException($"Missing rules.", nameof(rules));
            }

            if (rules.Expiry is null && rules.Expiry.Value <= 0)
            {
                throw new ArgumentException($"Invalid expiry rule value: {rules.Expiry}.", nameof(rules.Expiry));
            }

            if (!(rules.UpfrontPayment is null) && rules.UpfrontPayment.Value > minUnits)
            {
                throw new ArgumentException($"Min units ({minUnits}) cannot be lower than upfront payment value" +
                                            $" ({rules.UpfrontPayment.Value}).", nameof(minUnits));
            }

            Provider           = provider;
            State              = state;
            Id                 = id;
            Name               = name;
            Description        = description;
            UnitPrice          = unitPrice;
            UnitType           = unitType;
            QueryType          = queryType;
            MinUnits           = minUnits;
            MaxUnits           = maxUnits;
            Rules              = rules;
            Provider           = provider;
            File               = file;
            State              = state;
            TermsAndConditions = termsAndConditions;
            KycRequired        = kycRequired;
            SetState(state);
            SetPlugin(plugin);
        }
 private static DataAsset GetDataAsset(DataAssetUnitType unitType)
 => new DataAsset(Keccak.OfAnEmptyString, "test", "test", 1,
                  unitType, 0, 10, new DataAssetRules(new DataAssetRule(1)),
                  new DataAssetProvider(Address.Zero, "test"));
 private static DepositDetails GetDepositDetails(DataAssetUnitType unitType = DataAssetUnitType.Unit)
 => new DepositDetails(new Deposit(Keccak.Zero, 1, 1, 1),
                       GetDataAsset(unitType), TestItem.AddressB, Array.Empty <byte>(), 1,
                       new TransactionInfo(TestItem.KeccakA, 1, 1, 1), DepositConfirmationTimestamp);
Example #11
0
 public static Builder ForDeposit(Keccak depositId, DataAssetUnitType unitType = DataAssetUnitType.Unit) => new Builder(depositId, unitType);
 public IDepositNodesHandler CreateInMemory(Keccak depositId, Address consumer, DataAssetUnitType unitType,
                                            uint verificationTimestamp, uint purchasedUnits, UInt256 unitPrice, uint consumedUnits, uint unpaidUnits,
                                            uint unmergedUnits, uint unclaimedUnits, uint graceUnits, uint expiryTime, PaymentClaim latestPaymentClaim,
                                            IEnumerable <DataDeliveryReceiptDetails> latestReceipts, uint latestReceiptRequestNumber)
 => new InMemoryDepositNodesHandler(depositId, consumer, unitType, verificationTimestamp,
                                    purchasedUnits, unitPrice, consumedUnits, unpaidUnits, unmergedUnits, unclaimedUnits, graceUnits,
                                    expiryTime, latestPaymentClaim, latestReceipts, latestReceiptRequestNumber);