-
Notifications
You must be signed in to change notification settings - Fork 624
Harden Orphaned Block validations #9389
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0dc45f
Make parent in Validations non-optional
LukaszRozmej 27c322e
Remove unused method
LukaszRozmej e9f1360
BlockValidator refactor + fixes
LukaszRozmej a511290
Add IHeaderValidator.ValidateOrphaned
LukaszRozmej acee31f
fix tests
LukaszRozmej 4e273c9
rename TSuggested->TOrphaned
LukaszRozmej 270d876
whitespace
LukaszRozmej 3406a6e
nit changes
Marchhill 7b538c1
review comments
LukaszRozmej a85c612
Optimism fix
LukaszRozmej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 10 additions & 50 deletions
60
src/Nethermind/Nethermind.Blockchain.Test/Validators/TestBlockValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,30 @@ | ||
| // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Nethermind.Consensus.Validators; | ||
| using Nethermind.Core; | ||
|
|
||
| namespace Nethermind.Blockchain.Test.Validators; | ||
|
|
||
| public class TestBlockValidator : IBlockValidator | ||
| public class TestBlockValidator(bool suggestedValidationResult = true) : IBlockValidator | ||
| { | ||
| public static TestBlockValidator AlwaysValid = new(); | ||
| public static TestBlockValidator NeverValid = new(false, false); | ||
| private readonly Queue<bool> _processedValidationResults = null!; | ||
| private readonly Queue<bool> _suggestedValidationResults = null!; | ||
| private readonly bool? _alwaysSameResultForProcessed; | ||
| private readonly bool? _alwaysSameResultForSuggested; | ||
| private readonly bool? _alwaysSameResultForSuggested = suggestedValidationResult; | ||
|
|
||
| public TestBlockValidator(bool suggestedValidationResult = true, bool processedValidationResult = true) | ||
| { | ||
| _alwaysSameResultForSuggested = suggestedValidationResult; | ||
| _alwaysSameResultForProcessed = processedValidationResult; | ||
| } | ||
|
|
||
| public TestBlockValidator(Queue<bool> suggestedValidationResults, Queue<bool> processedValidationResults) | ||
| { | ||
| _suggestedValidationResults = suggestedValidationResults ?? throw new ArgumentNullException(nameof(suggestedValidationResults)); | ||
| _processedValidationResults = processedValidationResults ?? throw new ArgumentNullException(nameof(processedValidationResults)); | ||
| } | ||
|
|
||
| public bool Validate(BlockHeader header, BlockHeader? parent, bool isUncle, [NotNullWhen(false)] out string? error) | ||
| public bool Validate(BlockHeader header, BlockHeader parent, bool isUncle, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| public bool ValidateOrphaned(BlockHeader header, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| public bool ValidateSuggestedBlock(Block block, BlockHeader parent, [NotNullWhen(false)] out string? error, bool validateHashes = true) => Validate(out error); | ||
| public bool ValidateProcessedBlock(Block processedBlock, TxReceipt[] receipts, Block suggestedBlock, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| public bool ValidateWithdrawals(Block block, out string? error) => Validate(out error); | ||
| public bool ValidateOrphanedBlock(Block block, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| public bool ValidateBodyAgainstHeader(BlockHeader header, BlockBody toBeValidated, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| private bool Validate(out string? error) | ||
| { | ||
| var result = _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue(); | ||
| error = result ? null : ""; | ||
| return result; | ||
| } | ||
|
|
||
| public bool ValidateSuggestedBlock(Block block, BlockHeader? parent, [NotNullWhen(false)] out string? error, bool validateHashes = true) | ||
| { | ||
| error = null; | ||
| return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue(); | ||
| } | ||
|
|
||
| public bool ValidateProcessedBlock(Block processedBlock, TxReceipt[] receipts, Block suggestedBlock, [NotNullWhen(false)] out string? error) | ||
| { | ||
| error = null; | ||
| return _alwaysSameResultForProcessed ?? _processedValidationResults.Dequeue(); | ||
| } | ||
|
|
||
| public bool ValidateWithdrawals(Block block, out string? error) | ||
| { | ||
| error = null; | ||
|
|
||
| return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue(); | ||
| } | ||
|
|
||
| public bool ValidateOrphanedBlock(Block block, [NotNullWhen(false)] out string? error) | ||
| { | ||
| error = null; | ||
| return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue(); | ||
| } | ||
|
|
||
| public bool ValidateBodyAgainstHeader(BlockHeader header, BlockBody toBeValidated, [NotNullWhen(false)] out string? errorMessage) | ||
| { | ||
| errorMessage = null; | ||
| return _alwaysSameResultForSuggested ?? _suggestedValidationResults.Dequeue(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
| using Nethermind.Core; | ||
| using Nethermind.Core.Specs; | ||
| using Nethermind.TxPool; | ||
|
|
||
| namespace Nethermind.Consensus.Validators; | ||
|
|
||
| public class Always : IBlockValidator, ISealValidator, IUnclesValidator, ITxValidator | ||
| { | ||
| private readonly bool _result; | ||
| private readonly ValidationResult _validationResult; | ||
|
|
||
| private Always(bool result) | ||
| { | ||
| _validationResult = result ? ValidationResult.Success : "Always invalid."; | ||
| _result = result; | ||
| } | ||
|
|
||
| // ReSharper disable once NotNullMemberIsNotInitialized | ||
| private static Always _valid; | ||
|
|
||
| public static Always Valid => LazyInitializer.EnsureInitialized(ref _valid, static () => new Always(true)); | ||
|
|
||
| // ReSharper disable once NotNullMemberIsNotInitialized | ||
| private static Always _invalid; | ||
|
|
||
| public static Always Invalid => LazyInitializer.EnsureInitialized(ref _invalid, static () => new Always(false)); | ||
|
|
||
| public bool Validate(BlockHeader header, BlockHeader parent, bool isUncle, out string? error) => Validate(out error); | ||
| public bool ValidateOrphaned(BlockHeader header, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
| public bool ValidateParams(BlockHeader parent, BlockHeader header, bool isUncle = false) => Validate(out _); | ||
| public bool ValidateSeal(BlockHeader header, bool force) => Validate(out _); | ||
| public bool Validate(BlockHeader header, BlockHeader[] uncles) => Validate(out _); | ||
| public ValidationResult IsWellFormed(Transaction transaction, IReleaseSpec releaseSpec) => _validationResult; | ||
| public bool ValidateWithdrawals(Block block, out string? error) => Validate(out error); | ||
| public bool ValidateOrphanedBlock(Block block, out string? error) => Validate(out error); | ||
| public bool ValidateSuggestedBlock(Block block, BlockHeader parent, out string? error, bool validateHashes = true) => Validate(out error); | ||
| public bool ValidateProcessedBlock(Block processedBlock, TxReceipt[] receipts, Block suggestedBlock, out string? error) => Validate(out error); | ||
| public bool ValidateBodyAgainstHeader(BlockHeader header, BlockBody toBeValidated, [NotNullWhen(false)] out string? error) => Validate(out error); | ||
|
|
||
| private bool Validate(out string? error) | ||
| { | ||
| error = _result ? null : "Always invalid."; | ||
| return _result; | ||
| } | ||
| } |
95 changes: 0 additions & 95 deletions
95
src/Nethermind/Nethermind.Consensus/Validators/AlwaysValid.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.