Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip hash validations when block comes from NewPayload #6952

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public bool ValidateSuggestedBlock(Block block)
{
return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue();
}
public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error)
public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error, bool validateHashes = true)
{
error = null;
return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public bool ValidateOrphanedBlock(Block block, out string? error)
return _result;
}

public bool ValidateSuggestedBlock(Block block, out string? error)
public bool ValidateSuggestedBlock(Block block, out string? error, bool validateHashes = true)
{
error = null;
return _result;
Expand Down
27 changes: 17 additions & 10 deletions src/Nethermind/Nethermind.Consensus/Validators/BlockValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ public bool ValidateSuggestedBlock(Block block)
{
return ValidateSuggestedBlock(block, out _);
}

/// <summary>
/// Suggested block validation runs basic checks that can be executed before going through the expensive EVM processing.
/// </summary>
/// <param name="block">A block to validate</param>
/// <param name="errorMessage">Message detailing a validation failure.</param>
/// <param name="validateHashes"></param>
/// <returns>
/// <c>true</c> if the <paramref name="block"/> is valid; otherwise, <c>false</c>.
/// </returns>
public bool ValidateSuggestedBlock(Block block, out string? errorMessage)
public bool ValidateSuggestedBlock(Block block, out string? errorMessage, bool validateHashes = true)
{
IReleaseSpec spec = _specProvider.GetSpec(block.Header);

Expand All @@ -113,14 +115,14 @@ public bool ValidateSuggestedBlock(Block block, out string? errorMessage)
return false;
}

if (!ValidateUnclesHashMatches(block, out Hash256 unclesHash))
if (validateHashes && !ValidateUnclesHashMatches(block, out Hash256 unclesHash))
{
if (_logger.IsDebug) _logger.Debug($"{Invalid(block)} Uncles hash mismatch: expected {block.Header.UnclesHash}, got {unclesHash}");
errorMessage = BlockErrorMessages.InvalidUnclesHash;
return false;
}

if (!_unclesValidator.Validate(block.Header, block.Uncles))
if (block.Uncles.Length > 0 && !_unclesValidator.Validate(block.Header, block.Uncles))
{
if (_logger.IsDebug) _logger.Debug($"{Invalid(block)} Invalid uncles");
errorMessage = BlockErrorMessages.InvalidUncle;
Expand All @@ -134,15 +136,20 @@ public bool ValidateSuggestedBlock(Block block, out string? errorMessage)
return false;
}

if (!ValidateTxRootMatchesTxs(block, out Hash256 txRoot))
if (validateHashes)
{
if (_logger.IsDebug) _logger.Debug($"{Invalid(block)} Transaction root hash mismatch: expected {block.Header.TxRoot}, got {txRoot}");
errorMessage = BlockErrorMessages.InvalidTxRoot(block.Header.TxRoot, txRoot);
return false;
}
if (!ValidateTxRootMatchesTxs(block, out Hash256 txRoot))
{
if (_logger.IsDebug) _logger.Debug($"{Invalid(block)} Transaction root hash mismatch: expected {block.Header.TxRoot}, got {txRoot}");
errorMessage = BlockErrorMessages.InvalidTxRoot(block.Header.TxRoot!, txRoot);
return false;
}

if (!ValidateWithdrawals(block, spec, out errorMessage))
return false;
if (!ValidateWithdrawals(block, spec, out errorMessage))
{
return false;
}
smartprogrammer93 marked this conversation as resolved.
Show resolved Hide resolved
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Nethermind.Consensus.Validators;
public interface IBlockValidator : IHeaderValidator, IWithdrawalValidator
{
bool ValidateOrphanedBlock(Block block, [NotNullWhen(false)] out string? error);
bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error);
bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error, bool validateHashes = true);
bool ValidateProcessedBlock(Block processedBlock, TxReceipt[] receipts, Block suggestedBlock, [NotNullWhen(false)] out string? error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public bool ValidateOrphanedBlock(Block block, out string? error)
return false;
}

public bool ValidateSuggestedBlock(Block block, out string? error)
public bool ValidateSuggestedBlock(Block block, out string? error, bool validateHashes = true)
{
error = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void Setup()
public void TestValidateSuggestedBlock(bool baseReturnValue, bool isInvalidBlockReported)
{
Block block = Build.A.Block.TestObject;
_baseValidator.ValidateSuggestedBlock(block, out string? error).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateSuggestedBlock(block);
_baseValidator.ValidateSuggestedBlock(block, out _).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateSuggestedBlock(block, out _);

_tracker.Received().SetChildParent(block.GetOrCalculateHash(), block.ParentHash!);
if (isInvalidBlockReported)
Expand Down Expand Up @@ -103,7 +103,7 @@ public void TestBlockWithNotMatchingTxShouldNotGetTracked()
));

_baseValidator.ValidateSuggestedBlock(block, out _).Returns(false);
_invalidBlockInterceptor.ValidateSuggestedBlock(block);
_invalidBlockInterceptor.ValidateSuggestedBlock(block, out _);

_tracker.DidNotReceive().SetChildParent(block.GetOrCalculateHash(), block.ParentHash!);
_tracker.DidNotReceive().OnInvalidBlock(block.GetOrCalculateHash(), block.ParentHash);
Expand All @@ -121,7 +121,7 @@ public void TestBlockWithIncorrectWithdrawalsShouldNotGetTracked()
));

_baseValidator.ValidateSuggestedBlock(block, out _).Returns(false);
_invalidBlockInterceptor.ValidateSuggestedBlock(block);
_invalidBlockInterceptor.ValidateSuggestedBlock(block, out _);

_tracker.DidNotReceive().SetChildParent(block.GetOrCalculateHash(), block.ParentHash!);
_tracker.DidNotReceive().OnInvalidBlock(block.GetOrCalculateHash(), block.ParentHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public async Task<ResultWrapper<PayloadStatusV1>> HandleAsync(ExecutionPayload r

if (!ShouldProcessBlock(block, parentHeader, out ProcessingOptions processingOptions)) // we shouldn't process block
{
if (!_blockValidator.ValidateSuggestedBlock(block, out string? error))
if (!_blockValidator.ValidateSuggestedBlock(block, out string? error, validateHashes: false))
{
if (_logger.IsInfo) _logger.Info($"Rejecting invalid block received during the sync, block: {block}");
return NewPayloadV1Result.Invalid(error);
Expand Down Expand Up @@ -394,7 +394,7 @@ private bool ValidateWithBlockValidator(Block block, BlockHeader parent, out str
{
block.Header.TotalDifficulty ??= parent.TotalDifficulty + block.Difficulty;
block.Header.IsPostMerge = true; // I think we don't need to set it again here.
bool isValid = _blockValidator.ValidateSuggestedBlock(block, out error);
bool isValid = _blockValidator.ValidateSuggestedBlock(block, out error, validateHashes: false);
if (!isValid && _logger.IsWarn) _logger.Warn($"Block validator rejected the block {block.ToString(Block.Format.FullHashAndNumber)}.");
return isValid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ public bool Validate(BlockHeader header, bool isUncle, [NotNullWhen(false)] out
return result;
}

public bool ValidateSuggestedBlock(Block block)
public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error, bool validateHashes = true)
{
return ValidateSuggestedBlock(block, out _);
}
public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error)
{
bool result = _baseValidator.ValidateSuggestedBlock(block, out error);
bool result = _baseValidator.ValidateSuggestedBlock(block, out error, validateHashes);
if (!result)
{
if (_logger.IsTrace) _logger.Trace($"Intercepted a bad block {block}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public bool ValidateOrphanedBlock(Block block, [NotNullWhen(false)] out string?
return true;
}

public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error)
public bool ValidateSuggestedBlock(Block block, [NotNullWhen(false)] out string? error, bool validateHashes = true)
{
Thread.Sleep(1000);
error = null;
Expand Down