public bool ValidateSeal(BlockHeader header) { UInt256 number = header.Number; // Retrieve the snapshot needed to validate this header and cache it Snapshot snapshot = GetOrCreateSnapshot(number - 1, header.ParentHash); // Resolve the authorization key and check against signers header.Author = header.Author ?? GetBlockSealer(header); Address signer = header.Author; if (!snapshot.Signers.ContainsKey(signer)) { if (_logger.IsWarn) { _logger.Warn($"Invalid block signer {signer} - not authorized to sign a block"); } return(false); } if (snapshot.HasSignedRecently(number, signer)) { if (_logger.IsWarn) { _logger.Warn($"Invalid block signer {signer} - the signer is among recents"); } return(false); } // Ensure that the difficulty corresponds to the turn-ness of the signer bool inTurn = snapshot.InTurn(header.Number, signer); if (inTurn && header.Difficulty != Clique.DifficultyInTurn) { if (_logger.IsWarn) { _logger.Warn($"Invalid block difficulty {header.Difficulty} - should be in-turn {Clique.DifficultyInTurn}"); } return(false); } if (!inTurn && header.Difficulty != Clique.DifficultyNoTurn) { if (_logger.IsWarn) { _logger.Warn($"Invalid block difficulty {header.Difficulty} - should be no-turn {Clique.DifficultyNoTurn}"); } return(false); } return(true); }
private UInt256 CalculateDifficulty(Snapshot snapshot, Address signer) { if (snapshot.InTurn(snapshot.Number + 1, signer)) { if (_logger.IsInfo) { _logger.Info("Producing in turn block"); } return(new UInt256(Clique.DifficultyInTurn)); } if (_logger.IsInfo) { _logger.Info("Producing out of turn block"); } return(new UInt256(Clique.DifficultyNoTurn)); }