Beispiel #1
0
        public void NormalState()
        {
            ReceiveAsync <IndexBlockMessage>(async(message) =>
            {
                var indexerId             = message.IndexerId;
                var currentBlockNumber    = message.BlockNumber;
                int iterationVector       = 0;
                BlockContent blockContent = null;
                int transactionCount      = 0;

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

                await RetryPolicy.ExecuteAsync(async() =>
                {
                    blockContent = blockContent ?? await _rpcBlockReader.ReadBlockAsync(currentBlockNumber);

                    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 _indexingService.IndexBlockAsync(blockContext);
                }, 5, 100);

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

                var indexed         = currentBlockNumber;
                currentBlockNumber += iterationVector;

                Sender.Tell(Messages.Common.CreateIndexedBlockNumberMessage(indexerId, indexed, currentBlockNumber));
            });
        }
Beispiel #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);
        }