public ControllerThread(SimpleBlockchain chain, OptimizedLineage opti, BoundedInbox inbox, BoundedInbox outbox) { _chain = chain; _opti = opti; _inbox = inbox; _outbox = outbox; }
public void ReadAndWriteStreamAllUncommitted() { AddValidBlocksWithForkAllUncommitted(); var stream = new MemoryStream(); var writer = new BinaryWriter(stream); var reader = new BinaryReader(stream); BlockchainSerializer.WriteTo(writer, _chain); stream.Seek(0, 0); SimpleBlockchain newChain = BlockchainSerializer.ReadFrom(reader); Assert.Equal(_1, newChain.RetrieveAlias(_tmpId1)); Assert.Equal(_2, newChain.RetrieveAlias(_tmpId2)); Assert.Equal(_3, newChain.RetrieveAlias(_tmpId3)); Assert.Equal(_4, newChain.RetrieveAlias(_tmpId4)); Assert.True(newChain.RetrieveUncommittedBlock(_1, out var oneBlock)); Assert.True(newChain.RetrieveUncommittedBlock(_2, out var twoBlock)); Assert.True(newChain.RetrieveUncommittedBlock(_3, out var threeBlock)); Assert.True(newChain.RetrieveUncommittedBlock(_4, out var fourBlock)); Assert.Equal(_tmpId1, oneBlock.BlockId); Assert.Equal(_tmpId2, twoBlock.BlockId); Assert.Equal(_tmpId3, threeBlock.BlockId); Assert.Equal(_tmpId4, fourBlock.BlockId); Assert.Equal(2, _chain.BlockchainHeight); }
public void MerkleTreeTest() { // create chain, add transaction SimpleBlockchain <Transaction> chain1 = new SimpleBlockchain <Transaction>(); chain1.add(new Transaction("A")).add(new Transaction("B")).add(new Transaction("C")).add(new Transaction("D")); // get a block in chain Block <Transaction> block = chain1.getHead(); Console.WriteLine("Merkle Hash tree :"); block.merkleTree().ForEach(i => Console.WriteLine(i + ",")); // get a transaction from block Transaction tx = block.getTransactions()[0]; // see if hash is valid... using merkle Tree... block.isTransactionValid(tx); Assert.IsTrue(block.isTransactionValid(tx)); // mutate the transaction data tx.setValue("Z"); Assert.IsFalse(block.isTransactionValid(tx)); }
public void CommitChildOfUncommittedParent() { _chain = new SimpleBlockchain(); _chain.OpenFirstBlock(); var id = _chain.OpenBlock(_1).Alias; Assert.Throws <ArgumentException>(() => _chain.CommitBlock(id, _hash1)); }
public void AddBlockToCommittedParent() { _chain = new SimpleBlockchain(); var id = _chain.OpenFirstBlock().Alias; _chain.CommitBlock(id, BlockId.Genesis); _chain.OpenBlock(_1); }
public void AddValidBlocksWithForkAllUncommitted() { _chain = new SimpleBlockchain(); _tmpId1 = _chain.OpenFirstBlock().BlockId; _tmpId2 = _chain.OpenBlock(_1).BlockId; _tmpId3 = _chain.OpenBlock(_2).BlockId; // Second child for second block _tmpId4 = _chain.OpenBlock(_2).BlockId; }
public void BlockMinerTest() { // create 30 transactions, that should result in 3 blocks in the chain. SimpleBlockchain <Transaction> chain = new SimpleBlockchain <Transaction>(); // Respresents a proof of work miner // Creates Miner <Transaction> miner = new Miner <Transaction>(chain); // This represents transactions being created by a network for (int i = 0; i < 30; i++) { miner.mine(new Transaction("" + i)); } Console.WriteLine("Number of Blocks Mined = " + chain.getChain().Count); Assert.IsTrue(chain.getChain().Count == 3); }
/// <summary> /// Writes the blockchain to a stream in a way that the /// <see cref="ReadFrom"/> method can extract it sensibly. /// </summary> public static void WriteTo(BinaryWriter writer, SimpleBlockchain chain) { writer.Write(chain.CommittedBlockCount); foreach (var block in chain.GetCommitted()) { block.BlockId.WriteTo(writer); writer.Write(block.Alias.Value); writer.Write(block.Parent.Value); } writer.Write(chain.UncommittedBlockCount); foreach (var block in chain.GetUncommitted()) { block.BlockId.WriteTo(writer); writer.Write(block.Alias.Value); writer.Write(block.Parent.Value); } }
public void Setup() { var sharedInboxDispatcherController = BoundedInbox.Create(); var sharedOutboxDispatcherController = BoundedInbox.Create(); var clientQueue = new ConcurrentQueue <ClientConnection>(); _dispatcher = new Dispatcher(clientQueue, 3, sharedOutboxDispatcherController, new BoundedInbox[4], sharedInboxDispatcherController); _chain = new SimpleBlockchain(); _controllerThread = new ControllerThread(_chain, default(OptimizedLineage), sharedInboxDispatcherController, sharedOutboxDispatcherController); _clientConn = new ClientConnection(_socket, ClientId.Next(), ClientServerMessage.MaxSizeInBytes); clientQueue.Enqueue(_clientConn); _dispatcher.DequeueNewClients(); // TODO there should really be three threads, one for each component. However, I do not know how to test this. }
public void AddValidBlocksWithFork() { _chain = new SimpleBlockchain(); var id = _chain.OpenFirstBlock().Alias; Assert.Equal(0, _chain.BlockchainHeight); _chain.CommitBlock(id, BlockId.Genesis); Assert.Equal(0, _chain.BlockchainHeight); id = _chain.OpenBlock(_1).Alias; Assert.Equal(1, _chain.BlockchainHeight); _chain.CommitBlock(id, _hash1); Assert.Equal(1, _chain.BlockchainHeight); id = _chain.OpenBlock(_2).Alias; Assert.Equal(2, _chain.BlockchainHeight); _chain.CommitBlock(id, _hash2); Assert.Equal(2, _chain.BlockchainHeight); // Second child for second block id = _chain.OpenBlock(_2).Alias; Assert.Equal(2, _chain.BlockchainHeight); _chain.CommitBlock(id, _hash3); Assert.Equal(2, _chain.BlockchainHeight); }
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])); }
public void AddBlockToUncommittedParent() { _chain = new SimpleBlockchain(); _chain.OpenFirstBlock(); _chain.OpenBlock(_1); }