Example #1
0
        public async Task GetNewBlockSyncRequests()
        {
            var blockNumber = await _blockService.GetBlockNumberFromNodeAsync();

            Console.WriteLine($"Latest block number {blockNumber} found.");

            using (var blockSyncRequestRepository = new MongoRepository <BlockSyncRequest>())
            {
                var orderByIndex = Builders <BlockSyncRequest> .Sort.Descending("block_number");

                var latestBlockSync = await blockSyncRequestRepository.GetAsync(null, orderByIndex);

                var newBlockSyncs = new List <BlockSyncRequest>();
                for (var i = latestBlockSync?.BlockNumber + 1 ?? 0; i <= blockNumber; i++)
                {
                    var blockSync = new BlockSyncRequest()
                    {
                        BlockNumber = i,
                        Processed   = false
                    };
                    newBlockSyncs.Add(blockSync);
                }

                Console.WriteLine($"Saving {newBlockSyncs.Count} new block sync requests.");

                if (newBlockSyncs.Count > 0)
                {
                    var saveBlockSyncRequestsOperations = newBlockSyncs.Split(10000);
                    var blockSyncRequestSaveTasks       = saveBlockSyncRequestsOperations.Select(SaveBlockSyncRequests).ToList();

                    await Task.WhenAll(blockSyncRequestSaveTasks);
                }
            }
        }
Example #2
0
        private async Task SyncBlockAsync(BlockSyncRequest blockSyncRequest)
        {
            var blockBuilder    = Builders <Block> .Filter;
            var blockFilter     = blockBuilder.Where(x => x.BlockNumber == blockSyncRequest.BlockNumber);
            var excistingBlocks = await _blockRepository.FindAsync(blockFilter, null);

            var blockExists = excistingBlocks.Count() > 0;

            Web3.Models.DTOs.Block block;
            try
            {
                block = await _blockService.GetBlockFromNodeAsync(blockSyncRequest.BlockNumber);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error getting block {blockSyncRequest.BlockNumber} from the node.");
                return;
            }

            if (block == null)
            {
                Console.WriteLine($"Block {blockSyncRequest.BlockNumber} could not be find in the node.");
                return;
            }

            var dbBlock = await _blockService.ConvertToDbBlockAsync(block, _tokenRepository, _priceRepository, _currentPrice);

            if (!blockExists)
            {
                await SaveBlock(dbBlock);
            }

            var transactions = new List <Transaction>();

            foreach (var transaction in dbBlock.Transactions)
            {
                var builder        = Builders <Transaction> .Filter;
                var filter         = builder.Where(x => x.TransactionHash == transaction.TransactionHash);
                var dbTransactions = await _transactionRepository.FindAsync(filter, null);

                var dbTransaction = dbTransactions.FirstOrDefault();
                if (dbTransaction == null)
                {
                    transactions.Add(transaction);
                }
                else
                {
                    dbTransaction.BlockHash   = dbBlock.Hash;
                    dbTransaction.BlockNumber = dbBlock.BlockNumber;
                    dbTransaction.Timestamp   = dbBlock.Timestamp;
                    transactions.Add(dbTransaction);
                }
            }

            await SaveTransactions(transactions);

            blockSyncRequest.Processed = true;
            await _blockSyncRequestRepository.SaveAsync(blockSyncRequest);
        }