Example #1
0
        public void FirstRun()
        {
            ReceiveAsync <IndexBlockMessage>(async(message) =>
            {
                var indexerId          = message.IndexerId;
                var currentBlockNumber = message.BlockNumber;

                await _logger.WriteInfoAsync
                (
                    "BlockIndexingJob",
                    "RunAsync",
                    indexerId,
                    $"Indexing begins from block-{currentBlockNumber}",
                    DateTime.UtcNow
                );

                var blockContent = await _rpcBlockReader.ReadBlockAsync(currentBlockNumber);
                var blockContext = new BlockContext(Id, Version, indexerId, blockContent);

                await _indexingService.IndexBlockAsync(blockContext);

                currentBlockNumber++;

                _firstRun = false;

                //After first run
                Sender.Tell(Messages.Common.CreateIndexedBlockNumberMessage(indexerId, message.BlockNumber, currentBlockNumber));

                Become(NormalState);
            });
        }
Example #2
0
        private async Task <BigInteger> IndexBlocksAsync(string indexerId, BigInteger currentBlockNumber,
                                                         Func <BigInteger, bool> checkDelegate)
        {
            try
            {
                if (_firstRun && _indexingSettings.From == currentBlockNumber)
                {
                    await _logger.WriteInfoAsync
                    (
                        "BlockIndexingJob",
                        "IndexBlocksAsync",
                        indexerId,
                        $"Indexing begins from block-{currentBlockNumber}",
                        DateTime.UtcNow
                    );

                    var blockContent = await _rpcBlockReader.ReadBlockAsync(currentBlockNumber);

                    var blockContext = new BlockContext(Id, Version, indexerId, blockContent);

                    await _indexingService.IndexBlockAsync(blockContext);

                    currentBlockNumber++;

                    _firstRun = false;
                }

                var iterationVector = 0;

                while (checkDelegate(currentBlockNumber))
                {
                    BlockContent blockContent = null;

                    var transactionCount = 0;

                    await _logger.WriteInfoAsync
                    (
                        "BlockIndexingJob",
                        "IndexBlocksAsync",
                        indexerId,
                        $"Indexing block-{currentBlockNumber}, Vector:{iterationVector}",
                        DateTime.UtcNow
                    );

                    //Would throw on time out
                    await RetryPolicy.ExecuteAsync(async() =>
                    {
                        await _logger.WriteInfoAsync
                        (
                            "BlockIndexingJob",
                            "IndexBlocksAsync",
                            indexerId,
                            $"Block-{currentBlockNumber}, Vector:{iterationVector} Reading info",
                            DateTime.UtcNow
                        );

                        //Throws Timeout Exception
                        await TimeoutPolicy.ExecuteAsync(async() =>
                        {
                            blockContent = blockContent ?? await _rpcBlockReader.ReadBlockAsync(currentBlockNumber);
                        }, TimeSpan.FromMinutes(7));

                        await _logger.WriteInfoAsync
                        (
                            "BlockIndexingJob",
                            "IndexBlocksAsync",
                            indexerId,
                            $"Checking existence of the parent-{currentBlockNumber}",
                            DateTime.UtcNow
                        );

                        var blockContext = new BlockContext(Id, Version, indexerId, blockContent);
                        var blockExists  = await _blockService.DoesBlockExist(blockContent.BlockModel.ParentHash);

                        transactionCount = blockContent.Transactions.Count;
                        iterationVector  = blockExists ? 1 : -1; //That is how we deal with forks

                        await _logger.WriteInfoAsync
                        (
                            "BlockIndexingJob",
                            "IndexBlocksAsync",
                            indexerId,
                            $"Indexing block in DB -{currentBlockNumber}",
                            DateTime.UtcNow
                        );

                        // Throws Timeout Exception
                        await TimeoutPolicy.ExecuteAsync(async() =>
                        {
                            await _indexingService.IndexBlockAsync(blockContext);
                        }, TimeSpan.FromMinutes(5));
                    }, 5, 100);

                    await _logger.WriteInfoAsync
                    (
                        "BlockIndexingJob",
                        "IndexBlocksAsync",
                        indexerId,
                        $"Indexing completed for block-{currentBlockNumber}, Vector:{iterationVector}, transaction count - {transactionCount}",
                        DateTime.UtcNow
                    );

                    currentBlockNumber += iterationVector;
                }
            }
            catch (Exception e)
            {
                if (e is BlockIsNotYetMinedException)
                {
                    throw;
                }

                await _logger.WriteErrorAsync
                (
                    "BlockIndexingJob",
                    "RunAsync",
                    $"Indexing failed for block-{currentBlockNumber}",
                    e,
                    DateTime.UtcNow
                );

                throw;
            }

            return(currentBlockNumber);
        }