private void DoWork(object state)
        {
            if (_isRunning)
            {
                return;
            }

            Task.Run(async() =>
            {
                try
                {
                    using (var scope = _scopeFactory.CreateScope())
                    {
                        var dbContext = scope.ServiceProvider.GetRequiredService <WalletDbContext>();

                        if (!(dbContext.PageData.FirstOrDefault()?.IsTransactionsSaved ?? false))
                        {
                            return;
                        }

                        _isRunning = true;

                        _lastBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value;

                        if (_lastCheckedBlockNumber == 0)
                        {
                            _lastCheckedBlockNumber = (int)(dbContext.BlockChainTransactions
                                                            .Max(w => w.BlockNumber));
                        }

                        if (_lastCheckedBlockNumber < _lastBlockNumber)
                        {
                            var transactions = _explorer.GetLatestTransactions(_lastCheckedBlockNumber,
                                                                               _lastCheckedBlockNumber);

                            dbContext.BlockChainTransactions.AddRange(transactions);
                            dbContext.SaveChanges();
                            _lastCheckedBlockNumber++;
                        }

                        _isRunning = false;
                    }
                }
                catch (Exception e)
                {
                    _isRunning = false;
                }
            });
        }
Esempio n. 2
0
        public async Task <IActionResult> SaveLatestTransactions()
        {
            var status = _dbContext.PageData.FirstOrDefault();

            if (status != null && status.IsTransactionsSaved)
            {
                return(Ok());
            }

            var lastKnownBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value;
            var tasks = new List <Task <List <BlockChainTransaction> > >();

            for (int i = lastKnownBlockNumber - 5000; i < lastKnownBlockNumber; i += 100)
            {
                var i1   = i;
                var task = Task.Run(() => _explorer.GetLatestTransactions(i1, i1 + 99));
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            var result = new List <BlockChainTransaction>();

            foreach (var task in tasks)
            {
                task.Result.ForEach(t => result.Add(t));
            }

            _dbContext.ChangeTracker.AutoDetectChangesEnabled = false;

            var tempList = new List <BlockChainTransaction>();

            foreach (var transact in result)
            {
                tempList.Add(transact);
                if (tempList.Count == 100)
                {
                    try
                    {
                        _dbContext.BlockChainTransactions.AddRange(tempList);
                        _dbContext.SaveChanges();
                        tempList.Clear();
                    }
                    catch (Exception e)
                    {
                        tempList.Clear();
                    }
                }
            }

            _dbContext.ChangeTracker.AutoDetectChangesEnabled = true;

            var lastSavedNumber = (int)(_dbContext.BlockChainTransactions
                                        .Max(w => w.BlockNumber));

            lastKnownBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value;
            var newTransacts = _explorer.GetLatestTransactions(lastSavedNumber, lastKnownBlockNumber);

            _dbContext.BlockChainTransactions.AddRange(newTransacts);
            _dbContext.SaveChanges();

            var data = _dbContext.PageData.FirstOrDefault();

            if (data != null)
            {
                data.IsTransactionsSaved = true;
                _dbContext.PageData.Update(data);
            }

            _dbContext.SaveChanges();

            return(Ok());
        }