Ejemplo n.º 1
0
        public (bool, object) validate_params(BlockHeader parentBlock, BlockHeader block, Action <AuRaParameters> modifyParameters, Repeat repeat, bool parentIsHead, bool isValidSealer)
        {
            _blockTree.Head.Returns(parentIsHead ? new Block(parentBlock) : new Block(Build.A.BlockHeader.WithNumber(parentBlock.Number - 1).TestObject));
            _validSealerStrategy.IsValidSealer(Arg.Any <IList <Address> >(), block.Beneficiary, block.AuRaStep.Value).Returns(isValidSealer);

            object cause = null;

            _reportingValidator.ReportBenign(Arg.Any <Address>(), Arg.Any <long>(), Arg.Do <IReportingValidator.BenignCause>(c => cause ??= c));
            _reportingValidator.ReportMalicious(Arg.Any <Address>(), Arg.Any <long>(), Arg.Any <byte[]>(), Arg.Do <IReportingValidator.MaliciousCause>(c => cause ??= c));
            BlockHeader header = null, parent = null;

            _reportingValidator.TryReportSkipped(Arg.Do <BlockHeader>(h => header = h), Arg.Do <BlockHeader>(h => parent = h));

            modifyParameters?.Invoke(_auRaParameters);
            var validateParams = _sealValidator.ValidateParams(parentBlock, block);

            if (header?.AuRaStep > parent?.AuRaStep + 1)
            {
                _reportingValidator.ReportBenign(header.Beneficiary, header.Number, IReportingValidator.BenignCause.SkippedStep);
            }

            if (repeat != Repeat.No)
            {
                if (repeat == Repeat.YesChangeHash)
                {
                    block.Hash = Keccak.Compute("AAA");
                }

                validateParams = _sealValidator.ValidateParams(parentBlock, block);
            }

            return(validateParams, cause);
        }
Ejemplo n.º 2
0
        public (bool, object) validate_params(BlockHeader parentBlock, BlockHeader block, Action <AuRaParameters> modifyParameters, Repeat repeat)
        {
            object cause = null;

            _reportingValidator.ReportBenign(Arg.Any <Address>(), Arg.Any <long>(), Arg.Do <IReportingValidator.BenignCause>(c => cause ??= c));
            _reportingValidator.ReportMalicious(Arg.Any <Address>(), Arg.Any <long>(), Arg.Any <byte[]>(), Arg.Do <IReportingValidator.MaliciousCause>(c => cause ??= c));
            BlockHeader header = null, parent = null;

            _reportingValidator.TryReportSkipped(Arg.Do <BlockHeader>(h => header = h), Arg.Do <BlockHeader>(h => parent = h));

            modifyParameters?.Invoke(_auRaParameters);
            var validateParams = _sealValidator.ValidateParams(parentBlock, block);

            if (header?.AuRaStep > parent?.AuRaStep + 1)
            {
                _reportingValidator.ReportBenign(header.Beneficiary, header.Number, IReportingValidator.BenignCause.SkippedStep);
            }

            if (repeat != Repeat.No)
            {
                if (repeat == Repeat.YesChangeHash)
                {
                    block.Hash = Keccak.Compute("AAA");
                }

                validateParams = _sealValidator.ValidateParams(parentBlock, block);
            }

            return(validateParams, cause);
        }
Ejemplo n.º 3
0
 protected override Task <Block> SealBlock(Block block, BlockHeader parent, CancellationToken token)
 {
     // if (block.Number < EmptyStepsTransition)
     _reportingValidator.TryReportSkipped(block.Header, parent);
     return(base.SealBlock(block, parent, token));
 }
Ejemplo n.º 4
0
        public bool ValidateParams(BlockHeader parent, BlockHeader header)
        {
            const long rejectedStepDrift = 4;

            if (header.AuRaSignature == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} is missing signature.");
                }
                return(false);
            }

            // Ensure header is from the step after parent.
            if (header.AuRaStep == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} is missing step value.");
                }
                return(false);
            }
            else
            {
                long step = header.AuRaStep.Value;

                if (step == parent.AuRaStep)
                {
                    if (_logger.IsWarn)
                    {
                        _logger.Warn($"Multiple blocks proposed for step {step}. Block {header.Number}, hash {header.Hash} is duplicate.");
                    }
                    ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Array.Empty <byte>(), IReportingValidator.MaliciousCause.DuplicateStep);
                    return(false);
                }
                else if (step < parent.AuRaStep && header.Number >= _parameters.ValidateStepTransition)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Block {header.Number}, hash {header.Hash} step {step} is lesser than parents step {parent.AuRaStep}.");
                    }
                    ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Array.Empty <byte>(), IReportingValidator.MaliciousCause.DuplicateStep);
                    return(false);
                }

                // we can only validate if its correct proposer for step if parent was already processed as it can change validators
                // no worries we do this validation later before processing the block
                if (parent.Hash == _blockTree.Head?.Hash)
                {
                    if (!_validSealerStrategy.IsValidSealer(_validatorStore.GetValidators(), header.Beneficiary, step))
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Block from incorrect proposer at block {header.ToString(BlockHeader.Format.FullHashAndNumber)}, step {step} from author {header.Beneficiary}.");
                        }
                        return(false);
                    }
                }

                var currentStep = _stepCalculator.CurrentStep;

                if (step > currentStep + rejectedStepDrift)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Block {header.Number}, hash {header.Hash} step {step} is from the future. Current step is {currentStep}.");
                    }
                    ReportingValidator.ReportBenign(header.Beneficiary, header.Number, IReportingValidator.BenignCause.FutureBlock);
                    return(false);
                }

                if (step > currentStep)
                {
                    const int blockTooEarlyWarningMillisecondThreshold = 1000;

                    TimeSpan timeToStep = _stepCalculator.TimeToStep(step);
                    if (timeToStep.TotalMilliseconds > blockTooEarlyWarningMillisecondThreshold)
                    {
                        if (_logger.IsWarn)
                        {
                            _logger.Warn($"Block {header.Number}, hash {header.Hash} step {step} is {timeToStep:g} too early. Current step is {currentStep}.");
                        }
                    }
                }

                // if (!ValidateEmptySteps())
                // ReportBenign
                ReportingValidator.TryReportSkipped(header, parent);

                // Report malice if the validator produced other sibling blocks in the same step.
                if (_receivedSteps.ContainsSiblingOrInsert(header, _validatorStore.GetValidators().Length))
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Validator {header.Beneficiary} produced sibling blocks in the same step {step} in block {header.Number}.");
                    }
                    ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Array.Empty <byte>(), IReportingValidator.MaliciousCause.SiblingBlocksInSameStep);
                }

                if (header.Number >= _parameters.ValidateScoreTransition)
                {
                    if (header.Difficulty >= AuraDifficultyCalculator.MaxDifficulty)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Difficulty out of bounds for block {header.Number}, hash {header.Hash}, Max value {AuraDifficultyCalculator.MaxDifficulty}, but found {header.Difficulty}.");
                        }
                        return(false);
                    }

                    var expectedDifficulty = AuraDifficultyCalculator.CalculateDifficulty(parent.AuRaStep.Value, step, 0);
                    if (header.Difficulty != expectedDifficulty)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Invalid difficulty for block {header.Number}, hash {header.Hash}, expected value {expectedDifficulty}, but found {header.Difficulty}.");
                        }
                        return(false);
                    }
                }

                return(true);
            }
        }
Ejemplo n.º 5
0
        public bool ValidateParams(BlockHeader parent, BlockHeader header)
        {
            const long rejectedStepDrift = 4;

            if (header.AuRaSignature == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} is missing signature.");
                }
                return(false);
            }

            // Ensure header is from the step after parent.
            if (header.AuRaStep == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} is missing step value.");
                }
                return(false);
            }
            else if (header.AuRaStep == parent.AuRaStep)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Multiple blocks proposed for step {header.AuRaStep}. Block {header.Number}, hash {header.Hash} is duplicate.");
                }
                ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Bytes.Empty, IReportingValidator.MaliciousCause.DuplicateStep);
                return(false);
            }
            else if (header.AuRaStep < parent.AuRaStep && header.Number >= _parameters.ValidateStepTransition)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} step {header.AuRaStep} is lesser than parents step {parent.AuRaStep}.");
                }
                ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Bytes.Empty, IReportingValidator.MaliciousCause.DuplicateStep);
                return(false);
            }

            var currentStep = _stepCalculator.CurrentStep;

            if (header.AuRaStep > currentStep + rejectedStepDrift)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Block {header.Number}, hash {header.Hash} step {header.AuRaStep} is from the future. Current step is {currentStep}.");
                }
                ReportingValidator.ReportBenign(header.Beneficiary, header.Number, IReportingValidator.BenignCause.FutureBlock);
                return(false);
            }

            if (header.AuRaStep > currentStep)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Block {header.Number}, hash {header.Hash} step {header.AuRaStep} is {(header.AuRaStep == currentStep + 1 ? _stepCalculator.TimeToNextStep.ToString("g") :  string.Empty)} too early. Current step is {currentStep}.");
                }
            }

            // if (!ValidateEmptySteps())
            // ReportBenign
            ReportingValidator.TryReportSkipped(header, parent);

            // Report malice if the validator produced other sibling blocks in the same step.
            if (_receivedSteps.ContainsOrInsert(header, _validatorStore.GetValidators().Length))
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Validator {header.Beneficiary} produced sibling blocks in the same step {header.AuRaStep} in block {header.Number}.");
                }
                ReportingValidator.ReportMalicious(header.Beneficiary, header.Number, Bytes.Empty, IReportingValidator.MaliciousCause.SiblingBlocksInSameStep);
            }

            if (header.Number >= _parameters.ValidateScoreTransition)
            {
                if (header.Difficulty >= AuraDifficultyCalculator.MaxDifficulty)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Difficulty out of bounds for block {header.Number}, hash {header.Hash}, Max value {AuraDifficultyCalculator.MaxDifficulty}, but found {header.Difficulty}.");
                    }
                    return(false);
                }

                var expectedDifficulty = AuraDifficultyCalculator.CalculateDifficulty(parent.AuRaStep.Value, header.AuRaStep.Value, 0);
                if (header.Difficulty != expectedDifficulty)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Invalid difficulty for block {header.Number}, hash {header.Hash}, expected value {expectedDifficulty}, but found {header.Difficulty}.");
                    }
                    return(false);
                }
            }

            return(true);
        }