Example #1
0
        private async Task ProducerLoop()
        {
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                BlockHeader parentHeader = _blockTree.Head;
                if (parentHeader == null)
                {
                    if (_logger.IsWarn)
                    {
                        _logger.Warn($"Preparing new block - parent header is null");
                    }
                }
                else if (_sealer.CanSeal(parentHeader.Number + 1, parentHeader.Hash))
                {
                    ProduceNewBlock(parentHeader);
                }

                var timeToNextStep = _auRaStepCalculator.TimeToNextStep;
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Waiting {timeToNextStep} for next AuRa step.");
                }
                await TaskExt.DelayAtLeast(timeToNextStep);
            }
        }
        public async Task after_waiting_for_next_step_timeToNextStep_should_be_close_to_stepDuration_in_seconds(int stepDuration)
        {
            var calculator = new AuRaStepCalculator(stepDuration, new Timestamper());
            await TaskExt.DelayAtLeast(calculator.TimeToNextStep);

            calculator.TimeToNextStep.Should().BeCloseTo(TimeSpan.FromSeconds(stepDuration), TimeSpan.FromMilliseconds(100));
        }
        public async Task step_increases_after_timeToNextStep(int stepDuration)
        {
            var calculator = new AuRaStepCalculator(stepDuration, new Timestamper());
            var step       = calculator.CurrentStep;
            await TaskExt.DelayAtLeast(calculator.TimeToNextStep);

            calculator.CurrentStep.Should().Be(step + 1, calculator.TimeToNextStep.ToString());
        }
        public async Task should_not_cancel_block_production_trigger_on_next_step_finished()
        {
            List <BlockProductionEventArgs> args = new();

            await using (BuildBlocksOnAuRaSteps buildBlocksOnAuRaSteps = new(new TestAuRaStepCalculator(), LimboLogs.Instance))
            {
                buildBlocksOnAuRaSteps.TriggerBlockProduction += (o, e) =>
                {
                    args.Add(e);
                };

                while (args.Count < 2)
                {
                    await TaskExt.DelayAtLeast(TestAuRaStepCalculator.StepDurationTimeSpan);
                }
            }

            IEnumerable <bool> enumerable = args.Select(e => e.CancellationToken.IsCancellationRequested);

            enumerable.Should().AllBeEquivalentTo(false);
        }
        public async Task should_cancel_block_production_trigger_on_next_step_if_not_finished_yet()
        {
            List <BlockProductionEventArgs> args = new();

            await using (BuildBlocksOnAuRaSteps buildBlocksOnAuRaSteps = new(new TestAuRaStepCalculator(), LimboLogs.Instance))
            {
                buildBlocksOnAuRaSteps.TriggerBlockProduction += (o, e) =>
                {
                    args.Add(e);
                    e.BlockProductionTask = TaskExt.DelayAtLeast(TestAuRaStepCalculator.StepDurationTimeSpan * 10, e.CancellationToken)
                                            .ContinueWith(t => (Block?)null, e.CancellationToken);
                };

                while (args.Count < 4)
                {
                    await TaskExt.DelayAtLeast(TestAuRaStepCalculator.StepDurationTimeSpan);
                }
            }

            bool[] allButLastCancellations = args.Skip(1).SkipLast(1).Select(e => e.CancellationToken.IsCancellationRequested).ToArray();
            allButLastCancellations.Should().AllBeEquivalentTo(true);
            allButLastCancellations.Should().HaveCountGreaterOrEqualTo(2);
        }