Ejemplo n.º 1
0
        public void TransactionSerializeTest()
        {
            TransactionPart partBob   = new TransactionPart("Bob", 100);
            TransactionPart partAlice = new TransactionPart("Alice", 100);

            Transaction test = new Transaction(partBob, partAlice);
            string      json = test.ToString();

            string expected = @"{""Currentransaction"":{""Item1"":{""Person"":""Bob"",""Amount"":100.0},""Item2"":{""Person"":""Alice"",""Amount"":100.0}}}";

            Assert.AreEqual(expected, json);
        }
Ejemplo n.º 2
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.º 3
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.º 4
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.º 5
0
        public Transaction CreateSampleTransaction()
        {
            int sign = 1;

            if (_rand.Next(0, 2) == 0)
            {
                sign = -1;
            }

            double amount    = _rand.Next(0, 10);
            double alicePays = sign * amount;
            double bobPays   = alicePays * -1;

            TransactionPart alice       = new TransactionPart("Alice", (decimal)alicePays);
            TransactionPart bob         = new TransactionPart("Bob", (decimal)bobPays);
            Transaction     transaction = new Transaction(alice, bob);

            return(transaction);
        }
Ejemplo n.º 6
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);
        }