Ejemplo n.º 1
0
        public void TransactionsHistory_ValidValues()
        {
            bool workProccess = false;

            IBlockChainRepository blockChainRepository = new BlockChainRepository();

            ModelStateDictionary modelState = new ModelStateDictionary();

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), blockChainRepository);

            BlockChain blockChain = BlockChain.Instance(blockChainService);

            User user = new User
            {
                Email          = "*****@*****.**",
                FirstName      = "Amir",
                LastName       = "Akobirovich",
                MiddleName     = "Rofeev",
                Login          = "******",
                Money          = 0,
                Password       = "******",
                RepeatPassword = "******",
                WalletID       = "WalletID"
            };

            blockChain.AddTransaction(new Transactions {
                Amount = 1231, DateTime = DateTime.Now, FromUser = user.Login, ToUser = "******", TransactionID = 13
            });

            blockChain.AddTransaction(new Transactions {
                Amount = 32, DateTime = DateTime.Now, FromUser = "******", ToUser = user.Login, TransactionID = 14
            });

            MoneySend moneySend = new MoneySend {
                Amount = 100, UserName = user.Login, WalletId = user.WalletID
            };

            blockChain.BuyCoins(moneySend);

            Thread.Sleep(35000);

            IEnumerable <Transactions> transactionsHistory = blockChain.TransactionsHistory(user);

            foreach (var transaction in transactionsHistory)
            {
                workProccess = Equals(transaction.FromUser, user.Login) || Equals(transaction.ToUser, user.Login);
            }

            Assert.IsTrue(workProccess);
        }
Ejemplo n.º 2
0
        public void GetBalanceOfUser_MultyValidValue()
        {
            ModelStateDictionary modelState = new ModelStateDictionary();

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), new BlockChainRepository());

            BlockChain blockChain = BlockChain.Instance(blockChainService);

            var users = new User[100];

            for (int i = 0; i < users.Length; i++)
            {
                users[i] = new User
                {
                    Email          = "*****@*****.**",
                    FirstName      = "Amir",
                    LastName       = "Akobirovich",
                    MiddleName     = "Rofeev",
                    Login          = new Random().GetHashCode().ToString(),
                    Money          = 0,
                    Password       = "******",
                    RepeatPassword = "******",
                    WalletID       = "WalletID"
                };
            }

            var usersActions = new Task[100];

            for (int i = 0; i < users.Length; i++)
            {
                Task.Run(() =>
                {
                    MoneySend moneySend = new MoneySend {
                        Amount = 100, UserName = users[i].Login, WalletId = users[i].WalletID
                    };

                    blockChain.BuyCoins(moneySend);

                    blockChain.AddTransaction(new Transactions {
                        Amount = 50, DateTime = DateTime.Now, FromUser = users[i].Login, ToUser = "******", TransactionID = 12
                    });
                }).Wait();
            }

            Thread.Sleep(35000);

            foreach (var user in users)
            {
                Assert.AreNotEqual(blockChain.GetBalanceOfUser(user), 0);
            }
        }
Ejemplo n.º 3
0
        private void loadTransactions(List <Tuple <NodeConnection, List <string> > > resultRemoteChainStates)
        {
            var tasks = new List <Task <List <Transaction> > >();

            foreach (var connection in resultRemoteChainStates)
            {
                tasks.Add(connection.Item1.GetTransactions(connection.Item2));
            }
            Task.WaitAll(tasks.ToArray());
            foreach (var taskResult in tasks.Select(t => t.Result))
            {
                if (taskResult != null)
                {
                    foreach (var transaction in taskResult)
                    {
                        _blockChain.AddTransaction(transaction);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void GetBalanceOfUser_ValidValue()
        {
            ModelStateDictionary modelState = new ModelStateDictionary();

            User user = new User
            {
                Email          = "*****@*****.**",
                FirstName      = "Amir",
                LastName       = "Akobirovich",
                MiddleName     = "Rofeev",
                Login          = "******",
                Money          = 0,
                Password       = "******",
                RepeatPassword = "******",
                WalletID       = "WalletID"
            };

            double predictableBalanceOfUser = 0;

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), new BlockChainRepository());

            BlockChain blockChain = BlockChain.Instance(blockChainService);

            MoneySend moneySend = new MoneySend {
                Amount = 100, UserName = user.Login, WalletId = user.WalletID
            };

            blockChain.BuyCoins(moneySend);

            predictableBalanceOfUser = 100;

            blockChain.AddTransaction(new Transactions {
                Amount = 50, DateTime = DateTime.Now, FromUser = user.Login, ToUser = "******", TransactionID = 12
            });

            predictableBalanceOfUser -= 50;

            Thread.Sleep(35000);

            Assert.AreEqual(blockChain.GetBalanceOfUser(user), predictableBalanceOfUser);
        }
Ejemplo n.º 5
0
        public ActionResult SendMoney(MoneySend moneySend)
        {
            CurrentUser currentUser = new CurrentUser(new ModelStateWrapper(ModelState));
            User        FromUser    = currentUser.GetCurrentUser(User.Identity.Name);
            User        ToUser      = _serviceUsers.ListUsers.Where(u => u.WalletID == moneySend.WalletId).FirstOrDefault();

            if (ToUser != null && ToUser.WalletID != FromUser.WalletID)
            {
                FromUser.Money = _blockChain.GetBalanceOfUser(FromUser);

                if (FromUser.Money >= moneySend.Amount)
                {
                    Transactions transactions = new Transactions
                    {
                        DateTime = DateTime.Now,
                        Amount   = moneySend.Amount,
                        FromUser = FromUser.Login,
                        ToUser   = ToUser.Login
                    };

                    _blockChain.AddTransaction(transactions);
                    moneySend = new MoneySend {
                        UserName = FromUser.FirstName, Amount = _blockChain.GetBalanceOfUser(FromUser)
                    };
                }
                else
                {
                    ModelState.AddModelError("WalletId", "У вас на счету недостаточно денег!");
                }
            }
            else
            {
                ModelState.AddModelError("WalletId", "К сожелению такого аккаунта нет в базе или вы указали хеш своего же аккаунта.");
            }

            return(View(moneySend));
        }
Ejemplo n.º 6
0
        public void AddTransaction_MultyValidAdding()
        {
            WebWalletDbContext webWalletDbContext = new WebWalletDbContext();

            int previouseTransactionAmount = webWalletDbContext.Transactions.Count(), afterTransactionAmount;

            ModelStateDictionary modelState = new ModelStateDictionary();

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), new BlockChainRepository());

            var addingTransactions = new Task[100];

            for (int i = 0; i < addingTransactions.Length; i++)
            {
                addingTransactions[i] = new Task(new Action(
                                                     () =>
                {
                    BlockChain blockChain = BlockChain.Instance(blockChainService);

                    Transactions transactions = new Transactions {
                        Amount = 122, DateTime = DateTime.Now, FromUser = "******", ToUser = "******", TransactionID = 12
                    };

                    blockChain.AddTransaction(transactions);
                }
                                                     ));

                addingTransactions[i].Start();
            }

            Thread.Sleep(35000);

            afterTransactionAmount = webWalletDbContext.Transactions.Count();

            Assert.AreNotEqual(previouseTransactionAmount, afterTransactionAmount);
        }
Ejemplo n.º 7
0
        public void AddTransaction_ValidAdding()
        {
            WebWalletDbContext webWalletDbContext = new WebWalletDbContext();

            int previouseTransactionAmount = webWalletDbContext.Transactions.Count(), afterTransactionAmount;

            ModelStateDictionary modelState = new ModelStateDictionary();

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), new BlockChainRepository());

            BlockChain blockChain = BlockChain.Instance(blockChainService);

            Transactions transactions = new Transactions {
                Amount = 122, DateTime = DateTime.Now, FromUser = "******", ToUser = "******", TransactionID = 12
            };

            blockChain.AddTransaction(transactions);

            Thread.Sleep(35000);

            afterTransactionAmount = webWalletDbContext.Transactions.Count();

            Assert.AreNotEqual(previouseTransactionAmount, afterTransactionAmount);
        }
Ejemplo n.º 8
0
        public void TransactionsHistory_MultyValidValues()
        {
            bool workProccess = false;

            IBlockChainRepository blockChainRepository = new BlockChainRepository();

            ModelStateDictionary modelState = new ModelStateDictionary();

            IBlockChainService blockChainService = new BlockChainService(new ModelStateWrapper(modelState), blockChainRepository);

            BlockChain blockChain = BlockChain.Instance(blockChainService);

            var users = new User[100];

            for (int i = 0; i < users.Length; i++)
            {
                users[i] = new User
                {
                    Email          = "*****@*****.**",
                    FirstName      = "Amir",
                    LastName       = "Akobirovich",
                    MiddleName     = "Rofeev",
                    Login          = new Random().GetHashCode().ToString(),
                    Money          = 0,
                    Password       = "******",
                    RepeatPassword = "******",
                    WalletID       = "WalletID"
                };
            }

            var usersActions = new Task[100];

            for (int i = 0; i < users.Length; i++)
            {
                Task.Run(() =>
                {
                    blockChain.AddTransaction(new Transactions {
                        Amount = 1231, DateTime = DateTime.Now, FromUser = users[i].Login, ToUser = "******", TransactionID = 13
                    });

                    blockChain.AddTransaction(new Transactions {
                        Amount = 32, DateTime = DateTime.Now, FromUser = "******", ToUser = users[i].Login, TransactionID = 14
                    });

                    MoneySend moneySend = new MoneySend {
                        Amount = 100, UserName = users[i].Login, WalletId = users[i].WalletID
                    };

                    blockChain.BuyCoins(moneySend);
                }).Wait();
            }

            Thread.Sleep(35000);

            foreach (var user in users)
            {
                IEnumerable <Transactions> transactionsHistory = blockChain.TransactionsHistory(user);

                foreach (var transaction in transactionsHistory)
                {
                    workProccess = Equals(transaction.FromUser, user.Login) || Equals(transaction.ToUser, user.Login);
                }
            }

            Assert.IsTrue(workProccess);
        }