示例#1
0
        public void ChainTest()
        {
            SimpleBlockchain <Transaction> chain1 = new SimpleBlockchain <Transaction>();

            chain1.add(new Transaction("A")).add(new Transaction("B")).add(new Transaction("C"));

            SimpleBlockchain <Transaction> chain2 = chain1.Clone();

            chain1.add(new Transaction("D"));

            Console.WriteLine(string.Format("Chain 1 Hash: {0}", chain1.getHead().getHash()));
            Console.WriteLine(string.Format("Chain 2 Hash: {0}", chain2.getHead().getHash()));
            Console.WriteLine(
                string.Format("Chains Are In Sync: {0}", chain1.getHead().getHash().Equals(chain2.getHead().getHash())));

            chain2.add(new Transaction("D"));

            Console.WriteLine(string.Format("Chain 1 Hash: {0}", chain1.getHead().getHash()));
            Console.WriteLine(string.Format("Chain 2 Hash: {0}", chain2.getHead().getHash()));
            Console.WriteLine(
                string.Format("Chains Are In Sync: {0}", chain1.getHead().getHash().Equals(chain2.getHead().getHash())));

            Assert.IsTrue(chain1.blockChainHash().Equals(chain2.blockChainHash()));

            Console.WriteLine("Current Chain Head Transactions: ");
            foreach (Block <Transaction> block in chain1.chain)
            {
                block.getTransactions().ForEach(i => Console.WriteLine(i.toString()));
            }

            // Block Merkle root should equal root hash in Merkle Tree computed from block transactions
            Block <Transaction> headBlock  = chain1.getHead();
            List <string>       merkleTree = headBlock.merkleTree();

            Assert.IsTrue(headBlock.getMerkleRoot().Equals(merkleTree[merkleTree.Count - 1]));
        }