Пример #1
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.");
                }
                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}.");
                }
                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}.");
                }
                return(false);
            }

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

            if (header.AuRaStep - parent.AuRaStep != 1)
            {
                // report_skipped
            }

            // 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}.");
                }
                // report malicious
            }

            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);
        }
Пример #2
0
 protected override UInt256 CalculateDifficulty(BlockHeader parent, UInt256 timestamp)
 => AuraDifficultyCalculator.CalculateDifficulty(parent.AuRaStep.Value, _auRaStepCalculator.CurrentStep);
Пример #3
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);
            }
        }
Пример #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 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);
        }