Esempio n. 1
0
        public void ThatTransactionGetCorrectAmountOfResults()
        {
            var validator = new TransactionValidator();

            validator.Validate(new Transaction());

            Assert.AreEqual(7, validator.GetResults().Count);
        }
        public void Test_Validate_NoFundAssociated_ResultsInError()
        {
            var transaction = new Transaction();
            var validator   = new TransactionValidator();

            validator.Validate(transaction);

            Assert.IsTrue(validator.HasErrors);
            Assert.Contains(TransactionValidator.NO_SAVINGS_FUND_ERROR_MESSAGE, validator.ErrorMessages);
        }
Esempio n. 3
0
        public List <ErrorModel> ValidateTransactions(List <TransactionItem> list)
        {
            var validator = new TransactionValidator();
            var errors    = new List <ErrorModel>();

            foreach (var item in list)
            {
                var transaction = validator.Validate(item);
                if (!transaction.IsValid)
                {
                    errors.Add(FormateErrorMessage(item, transaction));
                }
            }

            return(errors);
        }
        public void Test_Validate_NormalWithdrawal_NoErrors()
        {
            var fund = new SavingsFund {
                Balance = 50
            };
            var transaction = new Transaction
            {
                SavingsFund = fund,
                Type        = Transaction.TransactionType.Withdraw,
                Amount      = 50
            };
            var validator = new TransactionValidator();

            validator.Validate(transaction);

            Assert.IsFalse(validator.HasErrors);
        }
        public void Test_Validate_WithdrawMoreThanBalance_ResultsInError()
        {
            var fund = new SavingsFund {
                Balance = 50
            };
            var transaction = new Transaction
            {
                SavingsFund = fund,
                Type        = Transaction.TransactionType.Withdraw,
                Amount      = 100
            };
            var validator = new TransactionValidator();

            validator.Validate(transaction);

            Assert.IsTrue(validator.HasErrors);
            Assert.Contains(TransactionValidator.INSUFFICIENT_FUNDS_ERROR_MESSAGE, validator.ErrorMessages);
        }
        public void ShouldValidateThatTheAccountDoesHaveEnoughMoney()
        {
            //Arrange
            var accountService = GivenAnAccount("EUR", 101M);

            var transactionValidator = new TransactionValidator(accountService);
            var transaction          = new Transaction
            {
                Amount   = 100,
                UsedRate = 1,
                DestinationCurrencyCode = "EUR"
            };

            //Act
            var result = transactionValidator.Validate(transaction);

            //Assert
            result.Succeded.Should().BeTrue();
        }
        public void ShouldValidateThatTheAccountDoesHaveEnoughMoneyTakingIntoAccountTheRateExchange()
        {
            //Arrange
            var accountService = GivenAnAccount("USD", 101M);

            var transactionValidator = new TransactionValidator(accountService);
            var transaction          = new Transaction
            {
                Amount   = 100,
                UsedRate = 1.18M,
                DestinationCurrencyCode = "EUR",
                IsBaseCurrencySameAsTo  = true
            };

            //Act
            var result = transactionValidator.Validate(transaction);

            //Assert
            result.Succeded.Should().BeTrue();
        }
Esempio n. 8
0
        private List <string> AttachToTangleStatement(
            Hash trunkTransaction, Hash branchTransaction,
            int minWeightMagnitude, List <string> trytes)
        {
            lock (_syncRoot)
            {
                List <TransactionViewModel> transactionViewModels = new List <TransactionViewModel>();

                Hash prevTransaction = null;
                _pearlDiver = new PearlDiver();

                int[] transactionTrits = Converter.AllocateTritsForTrytes(TrytesSize);

                foreach (string tryte in trytes)
                {
                    var  startTime = DateTime.Now;
                    long timestamp = TimeStamp.Now();
                    try
                    {
                        Converter.Trits(tryte, transactionTrits, 0);
                        //branch and trunk
                        Array.Copy((prevTransaction ?? trunkTransaction).Trits(), 0,
                                   transactionTrits, TransactionViewModel.TrunkTransactionTrinaryOffset,
                                   TransactionViewModel.TrunkTransactionTrinarySize);
                        Array.Copy((prevTransaction == null ? branchTransaction : trunkTransaction).Trits(), 0,
                                   transactionTrits, TransactionViewModel.BranchTransactionTrinaryOffset,
                                   TransactionViewModel.BranchTransactionTrinarySize);

                        //attachment fields: tag and timestamps
                        //tag - copy the obsolete tag to the attachment tag field only if tag isn't set.
                        var tagTrits = ArrayUtils.SubArray(transactionTrits,
                                                           TransactionViewModel.TagTrinaryOffset,
                                                           TransactionViewModel.TagTrinarySize);
                        if (Array.TrueForAll(tagTrits, s => s == 0))
                        {
                            Array.Copy(transactionTrits, TransactionViewModel.ObsoleteTagTrinaryOffset,
                                       transactionTrits, TransactionViewModel.TagTrinaryOffset,
                                       TransactionViewModel.TagTrinarySize);
                        }

                        Converter.CopyTrits(timestamp, transactionTrits,
                                            TransactionViewModel.AttachmentTimestampTrinaryOffset,
                                            TransactionViewModel.AttachmentTimestampTrinarySize);
                        Converter.CopyTrits(0, transactionTrits,
                                            TransactionViewModel.AttachmentTimestampLowerBoundTrinaryOffset,
                                            TransactionViewModel.AttachmentTimestampLowerBoundTrinarySize);
                        Converter.CopyTrits(MaxTimestampValue, transactionTrits,
                                            TransactionViewModel.AttachmentTimestampUpperBoundTrinaryOffset,
                                            TransactionViewModel.AttachmentTimestampUpperBoundTrinarySize);

                        if (!_pearlDiver.Search(transactionTrits, minWeightMagnitude, 0))
                        {
                            transactionViewModels.Clear();
                            break;
                        }

                        //validate PoW - throws exception if invalid
                        TransactionViewModel transactionViewModel =
                            TransactionValidator.Validate(transactionTrits,
                                                          _instance.TransactionValidator.MinWeightMagnitude);

                        transactionViewModels.Add(transactionViewModel);
                        prevTransaction = transactionViewModel.Hash;
                    }
                    finally
                    {
                        IncreaseEllapsedTimePoW(DateTime.Now - startTime);
                        IncreaseCounterPoW();
                        if ((GetCounterPoW() % 100) == 0)
                        {
                            string sb = $"Last 100 PoW consumed {GetEllapsedTimePoW().TotalSeconds:F3} seconds processing time.";

                            Log.Info(sb);
                            ResetCounterPow();
                            ResetEllapsedTimePoW();
                        }
                    }
                }

                List <string> elements = new List <string>();
                for (int i = transactionViewModels.Count; i-- > 0;)
                {
                    elements.Add(Converter.Trytes(transactionViewModels[i].Trits()));
                }

                return(elements);
            }
        }
Esempio n. 9
0
 private bool Validate(SignedTransaction signed)
 {
     return(TransactionValidator.Validate(signed));
 }