Exemple #1
0
        /// <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)
        {
            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);
        }