public QueryBlock GetBlock(string blockHash, bool getTransactions = true)
        {
            Storage.Types.SyncBlockInfo block = storage.BlockGetByHash(blockHash);

            if (block == null)
            {
                return(new QueryBlock());
            }

            var queryBlock = new QueryBlock
            {
                Symbol            = chainConfiguration.Symbol,
                BlockHash         = block.BlockHash,
                BlockIndex        = block.BlockIndex,
                BlockSize         = block.BlockSize,
                BlockTime         = block.BlockTime,
                NextBlockHash     = block.NextBlockHash,
                PreviousBlockHash = block.PreviousBlockHash,
                Synced            = block.SyncComplete,
                TransactionCount  = block.TransactionCount,
                Bits              = block.Bits,
                ChainWork         = block.ChainWork,
                Difficulty        = block.Difficulty,
                Confirmations     = block.Confirmations,
                Merkleroot        = block.Merkleroot,
                Nonce             = block.Nonce,
                PosBlockSignature = block.PosBlockSignature,
                PosBlockTrust     = block.PosBlockTrust,
                PosChainTrust     = block.PosChainTrust,
                PosFlags          = block.PosFlags,
                PosHashProof      = block.PosHashProof,
                PosModifierv2     = block.PosModifierv2,
                Version           = block.Version,
                Transactions      = Enumerable.Empty <string>()
            };

            if (getTransactions)
            {
                IEnumerable <Storage.Types.SyncTransactionInfo> transactions = storage.BlockTransactionGetByBlockIndex(block.BlockIndex);
                queryBlock.Transactions = transactions.Select(s => s.TransactionHash);
            }

            return(queryBlock);
        }
        public async Task CheckBlockReorganization(SyncConnection connection)
        {
            while (true)
            {
                Storage.Types.SyncBlockInfo block = storage.BlockGetBlockCount(1).FirstOrDefault();

                if (block == null)
                {
                    break;
                }

                BitcoinClient client      = CryptoClientFactory.Create(connection.ServerDomain, connection.RpcAccessPort, connection.User, connection.Password, connection.Secure);
                string        currentHash = await client.GetblockHashAsync(block.BlockIndex);

                if (currentHash == block.BlockHash)
                {
                    break;
                }

                log.LogInformation($"SyncOperations: Deleting block {block.BlockIndex}");

                storage.DeleteBlock(block.BlockHash);
            }
        }
        private SyncBlockOperation GetNextBlockToSync(BitcoinClient client, SyncConnection connection, long lastCryptoBlockIndex, SyncingBlocks syncingBlocks)
        {
            if (syncingBlocks.LastBlock == null)
            {
                // because inserting blocks is sequential we'll use the indexed 'height' filed to check if the last block is incomplete.
                var incomplete = storage.BlockGetBlockCount(6).Where(b => !b.SyncComplete).ToList(); ////this.storage.BlockGetIncompleteBlocks().ToList();

                Storage.Types.SyncBlockInfo incompleteToSync = incomplete.OrderBy(o => o.BlockIndex).FirstOrDefault(f => !syncingBlocks.CurrentSyncing.ContainsKey(f.BlockHash));

                if (incompleteToSync != null)
                {
                    BlockInfo incompleteBlock = client.GetBlock(incompleteToSync.BlockHash);

                    return(new SyncBlockOperation {
                        BlockInfo = incompleteBlock, IncompleteBlock = true, LastCryptoBlockIndex = lastCryptoBlockIndex
                    });
                }

                string blockHashsToSync;

                var blocks = storage.BlockGetBlockCount(1).ToList();

                if (blocks.Any())
                {
                    long lastBlockIndex = blocks.First().BlockIndex;

                    if (lastBlockIndex == lastCryptoBlockIndex)
                    {
                        // No new blocks.
                        return(default(SyncBlockOperation));
                    }

                    blockHashsToSync = client.GetblockHash(lastBlockIndex + 1);
                }
                else
                {
                    // No blocks in store start from zero configured block index.
                    blockHashsToSync = client.GetblockHash(connection.StartBlockIndex);
                }

                BlockInfo nextNewBlock = client.GetBlock(blockHashsToSync);

                syncingBlocks.LastBlock = nextNewBlock;

                return(new SyncBlockOperation {
                    BlockInfo = nextNewBlock, LastCryptoBlockIndex = lastCryptoBlockIndex
                });
            }

            if (syncingBlocks.LastBlock.Height == lastCryptoBlockIndex)
            {
                // No new blocks.
                return(default(SyncBlockOperation));
            }

            string nextHash = client.GetblockHash(syncingBlocks.LastBlock.Height + 1);

            BlockInfo nextBlock = client.GetBlock(nextHash);

            syncingBlocks.LastBlock = nextBlock;

            return(new SyncBlockOperation {
                BlockInfo = nextBlock, LastCryptoBlockIndex = lastCryptoBlockIndex
            });
        }