コード例 #1
0
        public void TestMethodBlockchain()
        {
            var keys = CommonHelpers.GetPublicPrivateKeys();

            VotingsUser.PublicKey       = "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEhWfjbJW2Xd+f0Gls8bi2pzrV5av+R7eG6H8ysQXKNY99mL5j+fUSoJRDaZz9dxhPq3+zmRiewNy0BesJljUl1Q==";
            VotingsUser.PrivateKey      = "MHQCAQEEICUx8Nb2dysdxxbGHvw6yEWLgqw6PW+YLd74Z0PVxBfMoAcGBSuBBAAKoUQDQgAEhWfjbJW2Xd+f0Gls8bi2pzrV5av+R7eG6H8ysQXKNY99mL5j+fUSoJRDaZz9dxhPq3+zmRiewNy0BesJljUl1Q==";
            VotingsUser.CreateOwnBlocks = true;
            VotingsUser.PeerDiscovery   = true;

            var db = new VotingsDB();

            db.ConnectToDBAsync();
            db.Clear();

            var blockchain = new BlockChainVotings.BlockChainVotings();

            blockchain.Start();

            Assert.IsTrue(blockchain.Started == true);

            //проверка корня
            blockchain.CheckRoot();
            Assert.IsTrue(db.GetUserCreation(VotingsUser.RootPublicKey).Signature == VotingsUser.RootCreationSignature);
            Assert.IsTrue(db.GetVoting(0).Signature == VotingsUser.FirstVotingSignature);
            Assert.IsTrue(db.GetBlock(0).Signature == VotingsUser.FirstBlockSignature);

            //создание пользователя
            var newUser = keys[0];

            blockchain.CreateUser(newUser, "somename", "12345");
            Assert.IsNotNull(db.GetUserCreation(newUser));

            //голосования
            var list = new List <String>();

            list.Add(newUser);
            list.Add(VotingsUser.RootPublicKey);

            blockchain.CreateVoting(list, "voting");
            Assert.IsTrue(JObject.Parse(db.GetVoting(1).Info)["name"].Value <string>() == "voting");

            //голоса
            blockchain.CreateVote(newUser, db.GetVoting(1).Hash);
            Assert.IsTrue(db.GetUserVote(VotingsUser.PublicKey, 1).RecieverHash == newUser);

            //бан
            blockchain.BanUser(newUser, "cause");
            Assert.IsTrue(db.GetUserBan(newUser).RecieverHash == newUser);


            //создание блока
            for (int i = 0; i < 6; i++)
            {
                blockchain.CreateUser(newUser + i, "somename", "12345");
            }

            Assert.IsTrue(db.GetLastBlock().Transactions.Contains(db.GetUserCreation(newUser).Hash));
            Assert.IsTrue(db.GetFreeTransactions(100).Count == 0);
        }
コード例 #2
0
        public void TestMethodTransactions()
        {
            //проверка поиска транзакций
            db0.DeleteAsync(ban).Wait();
            Assert.IsTrue(db.SearchUsers(userName).Count == 1);
            Assert.IsTrue(db.SearchUsers(VotingsUser.PublicKey).Count == 1);
            Assert.IsTrue(db.SearchUsers(userId).Count == 1);
            db0.InsertAsync(ban).Wait();
            Thread.Sleep(1000);


            Assert.IsTrue(db.GetTransaction(user.Hash).Hash == user.Hash);
            Assert.IsTrue(db.TransactionsCount() == 4);
            Assert.IsTrue(db.UsersCount() == 1);
            Assert.IsTrue(db.UserAsCandidateCount(VotingsUser.PublicKey) == 1);
            Assert.IsTrue(db.GetVoting(votingNum).Hash == voting.Hash);
            Assert.IsTrue(db.GetLastVoting().Hash == voting.Hash);
            Assert.IsTrue(db.GetLastVoting().Hash == voting.Hash);
            Assert.IsTrue(db.GetUserCreation(VotingsUser.PublicKey).Hash == user.Hash);

            Assert.IsTrue(db.GetLastUserCreation().Hash == user.Hash);
            Assert.IsTrue(db.GetLastTransaction().Hash == ban.Hash);
            Assert.IsTrue(db.GetFreeTransactionsFromDate(new DateTime(2015, 1, 1), 10).Count == 4);
            Assert.IsTrue(db.GetSameUser(user) == null);
            Assert.IsTrue(db.GetUserBan(VotingsUser.PublicKey).Hash == ban.Hash);
            Assert.IsTrue(db.GetUserVote(VotingsUser.PublicKey, votingNum).Hash == vote.Hash);
            Assert.IsTrue(db.GetVotings().Count == 1);

            var results = db.GetCandidatesResults(voting);

            Assert.IsTrue(results.Count == 1 && results.Keys.First().Hash == user.Hash && results.Values.First() == 1);

            var results2 = db.GetCandidates(voting);

            Assert.IsTrue(results2.Count == 1 && results2[0].Hash == user.Hash);

            var results3 = db.GetUserVotesAndVotings(VotingsUser.PublicKey);

            Assert.IsTrue(results3.Count == 1 && results3.Keys.First().Hash == voting.Hash && results3.Values.First().Hash == vote.Hash);

            Assert.IsTrue(db.GetUserOpenedVotings().Count == 0);
            Assert.IsTrue(db.GetUserVotes(VotingsUser.PublicKey).Count == 1);
            Assert.IsTrue(db.GetUserVotes(VotingsUser.PublicKey).Count == 1);
            Assert.IsTrue(db.GetFreeTransactions(10).Count == 4);

            //проверка пометки статуса транзакций
            ban.Status = TransactionStatus.InPendingBlock;
            db0.UpdateAsync(ban);
            db.MarkAsFreePendingTransactions(true);
            Assert.IsTrue(db.GetTransaction(ban.Hash).Status == TransactionStatus.Free);

            ban.Status = TransactionStatus.InBlock;
            db0.UpdateAsync(ban);
            db.MarkAsFreePendingTransactions(false);
            Assert.IsTrue(db.GetTransaction(ban.Hash).Status == TransactionStatus.InBlock);
        }