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); }
public bool CanSeal(long blockNumber, Keccak parentHash) { bool StepNotYetProduced(long step) => !_blockTree.Head.Header.AuRaStep.HasValue ? throw new InvalidOperationException("Head block doesn't have AuRaStep specified.'") : _blockTree.Head.Header.AuRaStep.Value < step; bool IsThisNodeTurn(long step) { var validators = _validatorStore.GetValidators(); return(_validSealerStrategy.IsValidSealer(validators, _signer.Address, step)); } long currentStep = _auRaStepCalculator.CurrentStep; bool stepNotYetProduced = StepNotYetProduced(currentStep); bool isThisNodeTurn = IsThisNodeTurn(currentStep); if (isThisNodeTurn) { if (_logger.IsWarn && !stepNotYetProduced) { _logger.Warn($"Cannot seal block {blockNumber}: AuRa step {currentStep} already produced."); } else if (_logger.IsDebug && stepNotYetProduced) { _logger.Debug($"Can seal block {blockNumber}: {_signer.Address} is correct proposer of AuRa step {currentStep}."); } } else if (_logger.IsDebug) { _logger.Debug($"Skip seal block {blockNumber}: {_signer.Address} is not proposer of AuRa step {currentStep}."); } return(_signer.CanSign && stepNotYetProduced && isThisNodeTurn); }
public long?GetFinalizedLevel_test(int chainLength, int levelToCheck, Address[] beneficiaries, int minForFinalization) { SetupValidators(beneficiaries, minForFinalization); _validSealerStrategy.IsValidSealer(Arg.Any <IList <Address> >(), Arg.Any <Address>(), Arg.Any <long>()).Returns(c => beneficiaries.GetItemRoundRobin(c.Arg <long>()) == c.Arg <Address>()); var blockTreeBuilder = Build.A.BlockTree().OfChainLength(chainLength, 0, 0, beneficiaries); var blockTree = blockTreeBuilder.TestObject; var finalizationManager = new AuRaBlockFinalizationManager(blockTree, blockTreeBuilder.ChainLevelInfoRepository, _blockProcessor, _validatorStore, _validSealerStrategy, _logManager); var result = finalizationManager.GetFinalizedLevel(levelToCheck); return(result); }
public virtual void OnBlockProcessingStart(Block block, ProcessingOptions options = ProcessingOptions.None) { if (!options.IsProducingBlock() && !block.IsGenesis) { var auRaStep = block.Header.AuRaStep.Value; if (!_validSealerStrategy.IsValidSealer(Validators, block.Beneficiary, auRaStep)) { if (_logger.IsError) { _logger.Error($"Block from incorrect proposer at block {block.ToString(Block.Format.FullHashAndNumber)}, step {auRaStep} from author {block.Beneficiary}."); } throw new InvalidBlockException(block.Hash); } } }
public bool can_seal(long auRaStep, bool validSealer) { _auRaStepCalculator.CurrentStep.Returns(auRaStep); _validSealerStrategy.IsValidSealer(Arg.Any <IList <Address> >(), _address, auRaStep).Returns(validSealer); return(_auRaSealer.CanSeal(10, _blockTree.Head.Hash)); }
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); } }