private SyncPoolTransactions FindPoolInternal(SyncConnection connection, SyncingBlocks syncingBlocks) { watch.Restart(); BitcoinClient client = CryptoClientFactory.Create(connection.ServerDomain, connection.RpcAccessPort, connection.User, connection.Password, connection.Secure); IEnumerable <string> memPool = client.GetRawMemPool(); var currentMemoryPool = new HashSet <string>(memPool); var currentTable = new HashSet <string>(syncingBlocks.CurrentPoolSyncing); var newTransactions = currentMemoryPool.Except(currentTable).ToList(); var deleteTransaction = currentTable.Except(currentMemoryPool).ToList(); //var newTransactionsLimited = newTransactions.Count() < 1000 ? newTransactions : newTransactions.Take(1000).ToList(); syncingBlocks.CurrentPoolSyncing.AddRange(newTransactions); deleteTransaction.ForEach(t => syncingBlocks.CurrentPoolSyncing.Remove(t)); watch.Stop(); log.LogDebug($"SyncPool: Seconds = {watch.Elapsed.TotalSeconds} - New Transactions = {newTransactions.Count()}"); return(new SyncPoolTransactions { Transactions = newTransactions }); }
private SyncPoolTransactions FindPoolInternal(SyncConnection connection, SyncingBlocks syncingBlocks) { var stoper = Stopwatch.Start(); var client = CryptoClientFactory.Create(connection.ServerDomain, connection.RpcAccessPort, connection.User, connection.Password, connection.Secure); var memPool = client.GetRawMemPool(); var currentMemoryPool = new HashSet <string>(memPool); var currentTable = new HashSet <string>(syncingBlocks.CurrentPoolSyncing); var newTransactions = currentMemoryPool.Except(currentTable).ToList(); var deleteTransaction = currentTable.Except(currentMemoryPool).ToList(); //var newTransactionsLimited = newTransactions.Count() < 1000 ? newTransactions : newTransactions.Take(1000).ToList(); syncingBlocks.CurrentPoolSyncing.AddRange(newTransactions); deleteTransaction.ForEach(t => syncingBlocks.CurrentPoolSyncing.Remove(t)); stoper.Stop(); this.tracer.DetailedTrace("SyncPool", string.Format("Seconds = {0} - New Transactions = {1}", stoper.Elapsed.TotalSeconds, newTransactions.Count())); return(new SyncPoolTransactions { Transactions = newTransactions }); }
/// <summary> /// Initializes a new instance of the <see cref="Runner"/> class. /// </summary> public Runner(IEnumerable <TaskStarter> taskStarters, IEnumerable <TaskRunner> taskRunners) { this.taskStarters = taskStarters; this.taskRunners = taskRunners; SyncingBlocks = new SyncingBlocks { CurrentSyncing = new ConcurrentDictionary <string, BlockInfo>(), CurrentPoolSyncing = new List <string>() }; }
/// <inheritdoc /> public override async Task <bool> OnExecute() { if (!config.SyncMemoryPool) { Abort = true; return(true); } SyncingBlocks syncingBlocks = Runner.SyncingBlocks; if (syncingBlocks.Blocked) { return(false); } if (syncingBlocks.LastBlock == null || syncingBlocks.LastBlock.Height + 10 < syncingBlocks.LastClientBlockIndex) { // Don't sync mempool until api is at tip return(false); } if (Runner.Get <BlockSyncer>().Queue.Count() >= config.MaxItemsInQueue) { return(false); } watch.Restart(); SyncPoolTransactions pool = syncOperations.FindPoolTransactions(syncConnection, syncingBlocks); if (!pool.Transactions.Any()) { return(false); } watch.Stop(); log.LogDebug($"Seconds = {watch.Elapsed.TotalSeconds} - New Transactions = {pool.Transactions.Count}/{syncingBlocks.CurrentPoolSyncing.Count()}"); Runner.Get <BlockSyncer>().Enqueue(new SyncBlockOperation { PoolTransactions = pool }); return(await Task.FromResult(false)); }
/// <inheritdoc /> public override async Task <bool> OnExecute() { if (!config.SyncBlockchain) { Abort = true; return(true); } SyncingBlocks syncingBlocks = Runner.SyncingBlocks; if (syncingBlocks.Blocked) { return(false); } if (Runner.Get <BlockSyncer>().Queue.Count() >= config.MaxItemsInQueue) { return(false); } watch.Restart(); SyncBlockOperation block = syncOperations.FindBlock(syncConnection, syncingBlocks); if (block == null || block.BlockInfo == null) { return(false); } watch.Stop(); string blockStatus = block.IncompleteBlock ? "Incomplete" : string.Empty; log.LogDebug($"Seconds = {watch.Elapsed.TotalSeconds} - SyncedIndex = {block.BlockInfo.Height}/{block.LastCryptoBlockIndex} - {block.LastCryptoBlockIndex - block.BlockInfo.Height} {blockStatus}"); Runner.Get <BlockSyncer>().Enqueue(block); if (block.LastCryptoBlockIndex - block.BlockInfo.Height < 2) { MongoStorageOperations mongoStorageOperations = new MongoStorageOperations(storage, configuration, syncConnection); mongoStorageOperations.UpdateConfirmations(); } return(await Task.FromResult(true)); }
private SyncBlockOperation FindBlockInternal(SyncConnection connection, SyncingBlocks syncingBlocks) { var stoper = Stopwatch.Start(); var client = CryptoClientFactory.Create(connection.ServerDomain, connection.RpcAccessPort, connection.User, connection.Password, connection.Secure); var lastCryptoBlockIndex = client.GetBlockCount(); var blockToSync = this.GetNextBlockToSync(client, connection, lastCryptoBlockIndex, syncingBlocks); if (blockToSync != null && blockToSync.BlockInfo != null) { syncingBlocks.CurrentSyncing.TryAdd(blockToSync.BlockInfo.Hash, blockToSync.BlockInfo); } stoper.Stop(); return(blockToSync); }
private SyncBlockOperation FindBlockInternal(SyncConnection connection, SyncingBlocks syncingBlocks) { watch.Restart(); BitcoinClient client = CryptoClientFactory.Create(connection.ServerDomain, connection.RpcAccessPort, connection.User, connection.Password, connection.Secure); syncingBlocks.LastClientBlockIndex = GetBlockCount(client); SyncBlockOperation blockToSync = GetNextBlockToSync(client, connection, syncingBlocks.LastClientBlockIndex, syncingBlocks); if (blockToSync != null && blockToSync.BlockInfo != null) { syncingBlocks.CurrentSyncing.TryAdd(blockToSync.BlockInfo.Hash, blockToSync.BlockInfo); } watch.Stop(); return(blockToSync); }
public SyncPoolTransactions FindPoolTransactions(SyncConnection connection, SyncingBlocks container) { return(this.FindPoolInternal(connection, container)); }
public SyncBlockOperation FindBlock(SyncConnection connection, SyncingBlocks container) { return(this.FindBlockInternal(connection, container)); }
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 = this.storage.BlockGetBlockCount(6).Where(b => !b.SyncComplete).ToList(); ////this.storage.BlockGetIncompleteBlocks().ToList(); var incompleteToSync = incomplete.OrderBy(o => o.BlockIndex).FirstOrDefault(f => !syncingBlocks.CurrentSyncing.ContainsKey(f.BlockHash)); if (incompleteToSync != null) { var incompleteBlock = client.GetBlock(incompleteToSync.BlockHash); return(new SyncBlockOperation { BlockInfo = incompleteBlock, IncompleteBlock = true, LastCryptoBlockIndex = lastCryptoBlockIndex }); } string blockHashsToSync; var blokcs = this.storage.BlockGetBlockCount(1).ToList(); if (blokcs.Any()) { var lastBlockIndex = blokcs.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); } var 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)); } var nextHash = client.GetblockHash(syncingBlocks.LastBlock.Height + 1); var nextBlock = client.GetBlock(nextHash); syncingBlocks.LastBlock = nextBlock; return(new SyncBlockOperation { BlockInfo = nextBlock, LastCryptoBlockIndex = lastCryptoBlockIndex }); }