public static Rlp Encode <T>(this IRlpObjectDecoder <T> decoder, T?[]?items, RlpBehaviors behaviors = RlpBehaviors.None)
        {
            if (items == null)
            {
                return(Rlp.OfEmptySequence);
            }

            Rlp[] rlpSequence = new Rlp[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                rlpSequence[i] = items[i] == null ? Rlp.OfEmptySequence : decoder.Encode(items[i], behaviors);
            }

            return(Rlp.Encode(rlpSequence));
        }
Beispiel #2
0
 public void Setup()
 {
     _gasPrice               = 20.GWei();
     _gasPriceService        = Substitute.For <IGasPriceService>();
     _consumerRepository     = Substitute.For <IConsumerRepository>();
     _paymentClaimRepository = Substitute.For <IPaymentClaimRepository>();
     _paymentService         = Substitute.For <IPaymentService>();
     _coldWalletAddress      = Address.Zero;
     _consumerRlpDecoder     = Substitute.For <IRlpObjectDecoder <Consumer> >();
     _consumerRlpDecoder.Encode(Arg.Any <Consumer>()).ReturnsForAnyArgs(new Rlp(Array.Empty <byte>()));
     _unitsRangeRlpDecoder = Substitute.For <IRlpObjectDecoder <UnitsRange> >();
     _unitsRangeRlpDecoder.Encode(Arg.Any <UnitsRange>()).ReturnsForAnyArgs(new Rlp(Array.Empty <byte>()));
     _timestamper      = Substitute.For <ITimestamper>();
     _transaction      = Build.A.Transaction.TestObject;
     _transaction.Hash = TestItem.KeccakA;
     _gasPriceService.GetCurrentPaymentClaimGasPriceAsync().Returns(_gasPrice);
     _processor = new PaymentClaimProcessor(_gasPriceService, _consumerRepository, _paymentClaimRepository,
                                            _paymentService, _coldWalletAddress, _timestamper, _unitsRangeRlpDecoder, LimboLogs.Instance);
 }
        public async Task <PaymentClaim?> ProcessAsync(DataDeliveryReceiptRequest receiptRequest, Signature signature)
        {
            Keccak   depositId = receiptRequest.DepositId;
            Consumer?consumer  = await _consumerRepository.GetAsync(depositId);

            if (consumer is null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Could not find any consumers for deposit {depositId} in the repository.");
                }
                return(null);
            }

            DataRequest  dataRequest   = consumer.DataRequest;
            UnitsRange   range         = receiptRequest.UnitsRange;
            uint         claimedUnits  = range.Units;
            BigInteger   claimedValue  = claimedUnits * (BigInteger)consumer.DataRequest.Value / consumer.DataRequest.Units;
            ulong        timestamp     = _timestamper.UnixTime.Seconds;
            Rlp          unitsRangeRlp = _unitsRangeRlpDecoder.Encode(range);
            Keccak       id            = Keccak.Compute(Rlp.Encode(Rlp.Encode(depositId), Rlp.Encode(timestamp), unitsRangeRlp).Bytes);
            PaymentClaim paymentClaim  = new PaymentClaim(id, consumer.DepositId, consumer.DataAsset.Id,
                                                          consumer.DataAsset.Name, dataRequest.Units, claimedUnits, range, dataRequest.Value,
                                                          (UInt256)claimedValue, dataRequest.ExpiryTime, dataRequest.Pepper, dataRequest.Provider,
                                                          dataRequest.Consumer, signature, timestamp, Array.Empty <TransactionInfo>(), PaymentClaimStatus.Unknown);
            await _paymentClaimRepository.AddAsync(paymentClaim);

            if (_logger.IsInfo)
            {
                _logger.Info($"Claiming a payment (id: '{paymentClaim.Id}') for deposit: '{depositId}', range: [{range.From}, {range.To}], units: {claimedUnits}.");
            }
            UInt256 gasPrice = await _gasPriceService.GetCurrentPaymentClaimGasPriceAsync();

            Keccak?transactionHash = null;

            if (_disableSendingPaymentClaimTransaction)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn("*** NDM provider sending payment claim transaction is disabled ***");
                }
            }
            else
            {
                transactionHash = await SendTransactionAsync(paymentClaim, gasPrice);

                if (transactionHash is null)
                {
                    if (_logger.IsInfo)
                    {
                        _logger.Info($"Payment claim (id: {paymentClaim.Id}) for deposit: '{paymentClaim.DepositId}' did not receive a transaction hash.");
                    }
                    return(paymentClaim);
                }
            }

            if (_logger.IsInfo)
            {
                _logger.Info($"Payment claim (id: {paymentClaim.Id}) for deposit: '{paymentClaim.DepositId}' received a transaction hash: '{transactionHash}'.");
            }
            paymentClaim.AddTransaction(TransactionInfo.Default(transactionHash, 0, gasPrice, _paymentService.GasLimit,
                                                                timestamp));
            paymentClaim.SetStatus(PaymentClaimStatus.Sent);
            await _paymentClaimRepository.UpdateAsync(paymentClaim);

            string claimedEth = ((decimal)claimedValue / Eth).ToString("0.0000");

            if (_logger.IsInfo)
            {
                _logger.Info($"Sent a payment claim (id: '{paymentClaim.Id}') for deposit: '{depositId}', range: [{range.From}, {range.To}], units: {claimedUnits}, units: {claimedUnits}, value: {claimedValue} wei ({claimedEth} ETH, transaction hash: '{transactionHash}'.");
            }

            return(paymentClaim);
        }