public Subchain FindSubchain(byte[] hash, int length) { //todo: move this method to InternalBlockchain ? using (BlockchainRepository repo = new BlockchainRepository(conn)) { StoredBlock block = repo.FindBlockByHash(hash); if (block == null) { return null; } List<StoredBlock> blocks = new List<StoredBlock>(); blocks.Add(block); for (int i = 1; i < length; i++) { if (block.Header.IsFirst) { //genesis block is reached; break; } block = repo.FindBlockByHash(block.Header.PrevBlock); if (block == null) { throw new InvalidOperationException("The storage has a chain that starts with an invalid genesis block."); } blocks.Add(block); } blocks.Reverse(); return new Subchain(blocks); } }
public StoredBlock FindBlockByHash(byte[] hash) { using (BlockchainRepository repo = new BlockchainRepository(conn)) { return repo.FindBlockByHash(hash); } }