public void AddBlock(StoredBlock block)
 {
     JoinCurrentTransaction();
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         repo.InsertBlock(block);
     }
 }
 public StoredBlock AddBlockContent(byte[] hash, byte[] content)
 {
     JoinCurrentTransaction();
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         //todo: what if there is no such block or it already has content ?
         return repo.AddBlockContent(hash, content);
     }
 }
 public void AddUnspentOutput(UnspentOutput unspentOutput)
 {
     JoinCurrentTransaction();
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         repo.AddUnspentOutput(unspentOutput);
     }
 }
 public List<StoredBlock> GetOldestBlocksWithoutContent(int maxCount)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.GetOldestBlocksWithoutContent(maxCount);
     }
 }
 public void RemoveUnspentOutput(byte[] transactionHash, int outputNumber)
 {
     JoinCurrentTransaction();
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         repo.RemoveUnspentOutput(transactionHash, outputNumber);
     }
 }
 public List<StoredBlock> GetBlocksByHeight(int[] heights)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.ReadHeadersWithHeight(heights);
     }
 }
        public BlockLocator GetCurrentChainLocator()
        {
            List<StoredBlock> headers = new List<StoredBlock>();

            using (BlockchainRepository repo = new BlockchainRepository(conn))
            {
                StoredBlock lastBlock = repo.GetLastBlockHeader();
                if (lastBlock != null)
                {
                    headers = repo.ReadHeadersWithHeight(BlockLocator.GetRequiredBlockHeights(lastBlock.Height));
                }
            }

            BlockLocator locator = new BlockLocator();

            foreach (StoredBlock header in headers)
            {
                locator.AddHash(header.Height, header.Hash);
            }

            return locator;
        }
 public List<UnspentOutput> FindUnspentOutputs(byte[] transactionHash)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.FindUnspentOutputs(transactionHash);
     }
 }
 // todo: add test
 public byte[] GetBlockContent(byte[] hash)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         //todo: what if there is no such block or it already has content ?
         return repo.GetBlockContent(hash);
     }
 }
        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 FindFirst(BlockSelector selector)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.FindFirst(selector);
     }
 }
        public StoredBlock FindBlockByHeight(int height)
        {
            using (BlockchainRepository repo = new BlockchainRepository(conn))
            {
                //todo: rewrite this method
                List<StoredBlock> blocks = repo.ReadHeadersWithHeight(new int[] {height});

                if (!blocks.Any())
                {
                    return null;
                }

                return blocks[0];
            }
        }
 public StoredBlock FindBlockByHash(byte[] hash)
 {
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.FindBlockByHash(hash);
     }
 }
 public List<StoredBlock> Find(BlockSelector selector, int count)
 {
     if (count <= 0 || count > 256)
     {
         throw new ArgumentException($"The parameter '{nameof(count)}' is not within its bounds.", nameof(count));
     }
     using (BlockchainRepository repo = new BlockchainRepository(conn))
     {
         return repo.Find(selector, count);
     }
 }