예제 #1
0
        public async Task IndexBlockAsync(BlockContext blockContext)
        {
            var blockContent      = blockContext.BlockContent;
            var addressHistory    = blockContent.AddressHistory;
            var blockModel        = blockContent.BlockModel;
            var blockNumber       = (ulong)blockModel.Number;
            var contractAddresses = blockContent.DeployedContracts;
            var internalMessages  = blockContent.InternalMessages;
            var transactions      = blockContent.Transactions;
            var transfers         = blockContent.Transfers;

            await _blockRepository.SaveAsync(blockModel);

            #region ProcessTransactions

            try
            {
                await _transactionRepository.SaveManyForBlockAsync(transactions, blockNumber);
            }
            catch (Exception e)
            {
                foreach (var transaction in transactions)
                {
                    var trHash = transaction.TransactionHash;

                    await _transactionRepository.DeleteByHash(trHash);

                    await _internalMessageRepository.DeleteAllForHash(trHash);

                    await _addressHistoryRepository.DeleteByHash(trHash);

                    await _erc20ContractRepository.DeleteByHash(trHash);

                    await _erc20TransferHistoryRepository.DeleteAllForHash(trHash);
                }

                await _transactionRepository.SaveManyForBlockAsync(transactions, blockNumber);
            }

            #endregion

            await _internalMessageRepository.SaveManyForBlockAsync(internalMessages, blockNumber);

            await _addressHistoryRepository.SaveManyForBlockAsync(addressHistory, blockNumber);

            await _erc20TransferHistoryRepository.SaveForBlockAsync(transfers, blockNumber);

            contractAddresses.ForEach(_erc20ContractIndexingQueue.Enqueue);

            // Mark block as indexed
            await _indexingRabbitNotifier.NotifyAsync(new Lykke.Service.EthereumSamurai.Models.Messages.RabbitIndexingMessage()
            {
                BlockNumber         = blockNumber,
                IndexingMessageType = Lykke.Service.EthereumSamurai.Models.Messages.IndexingMessageType.Block
            });

            await _blockIndexationHistoryRepository.MarkBlockAsIndexed(blockNumber, blockContext.JobVersion);

            await _blockSyncedInfoRepository.SaveAsync(new BlockSyncedInfoModel(blockContext.IndexerId, (ulong)blockModel.Number));
        }