Ejemplo n.º 1
0
        public void CheckBlockChainTest()
        {
            DataHelper helper = new DataHelper();

            Example.Blockchain blockchain = helper.GetSampleChain();

            Miner miner = new Miner();
            Dictionary <string, decimal> result = miner.CheckBlockchain(blockchain);

            Assert.AreEqual(result.Values.Sum(), 200);
        }
Ejemplo n.º 2
0
        public void ChainHashTest()
        {
            DataHelper helper = new DataHelper();

            Example.Blockchain blockchain = helper.GetSampleChain();

            string currentHash = String.Empty;

            foreach (Block chain in blockchain.Chain)
            {
                Assert.AreEqual(chain.Content.ParentHash, currentHash);
                currentHash = Hash.CreateHash(chain.Content.ToString());
            }
        }
Ejemplo n.º 3
0
        public void ChainUniqueNumberTest()
        {
            DataHelper helper = new DataHelper();

            Example.Blockchain blockchain = helper.GetSampleChain();

            int lastnumber = 0;

            foreach (Block chain in blockchain.Chain)
            {
                Assert.AreEqual(chain.Content.BlockNumber, lastnumber);
                lastnumber++;
            }
        }
Ejemplo n.º 4
0
        public void CreateFirstBlockTest()
        {
            Example.Blockchain chain  = new Example.Blockchain();
            BlockchainHelper   helper = new BlockchainHelper();

            TransactionPart alice       = new TransactionPart("Alice", -3);
            TransactionPart bob         = new TransactionPart("Bob", 3);
            Transaction     transaction = new Transaction(alice, bob);

            Block newBlock = helper.CreateBlock(transaction, chain);

            Assert.AreEqual(newBlock.Content.BlockNumber, 1);
            Assert.AreEqual(newBlock.Content.NumberOfTransactions, 2);
            Assert.AreNotEqual(newBlock.Content.ParentHash, String.Empty);
        }
Ejemplo n.º 5
0
        public void CheckBlockHashTest()
        {
            Example.Blockchain chain  = new Example.Blockchain();
            BlockchainHelper   helper = new BlockchainHelper();

            TransactionPart alice       = new TransactionPart("Alice", -3);
            TransactionPart bob         = new TransactionPart("Bob", 3);
            Transaction     transaction = new Transaction(alice, bob);

            Block newBlock = helper.CreateBlock(transaction, chain);

            Miner miner  = new Miner();
            bool  result = miner.CheckBlockHash(newBlock);

            Assert.IsTrue(result);
        }
Ejemplo n.º 6
0
        private static void PlayBlockchain(Random rand)
        {
            Console.WriteLine(String.Format("{0}: Start", DateTime.Now));
            Console.WriteLine(String.Format("{0}: Creating Blockchain", DateTime.Now));

            Example.Blockchain chain  = new Example.Blockchain();
            BlockchainHelper   helper = new BlockchainHelper();

            for (int i = 1; i <= 1000; i++)
            {
                int sign = 1;
                if (rand.Next(0, 2) == 0)
                {
                    sign = -1;
                }

                byte[] buff = new byte[2];
                rand.NextBytes(buff);

                // ToDo: Find a better way for random number
                string number = "0" + Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) + buff[0] + buff[1];
                double amount = Convert.ToDouble(number);

                double personA = sign * amount;
                double personB = personA * -1;

                TransactionPart alice       = new TransactionPart(ALICE, (decimal)personA);
                TransactionPart bob         = new TransactionPart(BOB, (decimal)personB);
                Transaction     transaction = new Transaction(alice, bob);

                Console.WriteLine(String.Format("{0}: Creating transaction {1} - {2}", DateTime.Now, i,
                                                transaction.ToString()));

                Block innerBlock = helper.CreateBlock(transaction, chain);
                chain.Chain.Add(innerBlock);
            }

            Console.WriteLine(String.Format("{0}: Start mining", DateTime.Now));
            Miner miner = new Miner();
            Dictionary <string, decimal> result = miner.CheckBlockchain(chain);

            Console.WriteLine(String.Format("{0}: Final result is {1}:{2} and {3}:{4}", DateTime.Now, ALICE, result[ALICE], BOB,
                                            result[BOB]));

            Console.WriteLine("Press any key to start over");
            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public Example.Blockchain GetSampleChain()
        {
            const int howMany    = 25;
            string    parentHash = "";

            Example.Blockchain chain  = new Example.Blockchain();
            BlockchainHelper   helper = new BlockchainHelper();

            for (int i = 1; i <= howMany; i++)
            {
                Transaction tx         = CreateSampleTransaction();
                Block       innerBlock = helper.CreateBlock(tx, chain);
                chain.Chain.Add(innerBlock);
            }

            return(chain);
        }
Ejemplo n.º 8
0
        public void CheckBlockTest()
        {
            Example.Blockchain chain  = new Example.Blockchain();
            BlockchainHelper   helper = new BlockchainHelper();

            Dictionary <string, decimal> states = new Dictionary <string, decimal>();

            states.Add("Alice", 5);
            states.Add("Bob", 5);

            TransactionPart alice       = new TransactionPart("Alice", -3);
            TransactionPart bob         = new TransactionPart("Bob", 3);
            Transaction     transaction = new Transaction(alice, bob);

            Block newBlock    = helper.CreateBlock(transaction, chain);
            Block parentBlock = chain.Chain[0];

            Miner         miner = new Miner();
            List <string> errors;

            miner.CheckBlock(newBlock, parentBlock, states, out errors);

            Assert.AreEqual(0, errors.Count);
        }