-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/Nethermind/Nethermind.BlockValidation/BlockValidation.cs
This file contains 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,26 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using Nethermind.Api; | ||
using Nethermind.Api.Extensions; | ||
|
||
namespace Nethermind.BlockValidation; | ||
|
||
public class BlockValidation: INethermindPlugin | ||
{ | ||
public virtual string Name => "BlockValidation"; | ||
public virtual string Description => "BlockValidation"; | ||
public string Author => "Nethermind"; | ||
public Task InitRpcModules() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task Init(INethermindApi api) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public ValueTask DisposeAsync() => ValueTask.CompletedTask; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Nethermind/Nethermind.BlockValidation/Data/BidTrace.cs
This file contains 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,21 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using Nethermind.Core; | ||
using Nethermind.Core.Crypto; | ||
using Nethermind.Int256; | ||
|
||
namespace Nethermind.BlockValidation.Data; | ||
|
||
public readonly struct BidTrace | ||
{ | ||
public ulong Slot { get; } | ||
public Hash256 ParentHash { get; } | ||
public Hash256 BlockHash { get; } | ||
public PublicKey BuilderPublicKey { get; } | ||
public PublicKey ProposerPublicKey { get; } | ||
public Address ProposerFeeRecipient { get; } | ||
public long GasLimit { get; } | ||
public long GasUsed { get; } | ||
public UInt256 Value { get; } | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Nethermind/Nethermind.BlockValidation/Data/BlockValidationResult.cs
This file contains 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,47 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Text.Json.Serialization; | ||
using Nethermind.JsonRpc; | ||
|
||
namespace Nethermind.BlockValidation.Data; | ||
|
||
/// <summary> | ||
/// Represents the result of a block validation. | ||
/// </summary> | ||
public class BlockValidationResult | ||
{ | ||
|
||
public static ResultWrapper<BlockValidationResult> Invalid(string error) | ||
{ | ||
return ResultWrapper<BlockValidationResult>.Success(new BlockValidationResult | ||
{ | ||
Status = BlockValidationStatus.Invalid, | ||
ValidationError = error | ||
}); | ||
} | ||
|
||
public static ResultWrapper<BlockValidationResult> Valid() | ||
{ | ||
return ResultWrapper<BlockValidationResult>.Success(new BlockValidationResult | ||
{ | ||
Status = BlockValidationStatus.Valid | ||
}); | ||
} | ||
|
||
public static ResultWrapper<BlockValidationResult> Error(string error) | ||
{ | ||
return ResultWrapper<BlockValidationResult>.Fail(error); | ||
} | ||
|
||
/// <summary> | ||
/// The status of the validation of the builder submissions | ||
/// </summary> | ||
public string Status { get; set; } = BlockValidationStatus.Invalid; | ||
|
||
/// <summary> | ||
/// Message providing additional details on the validation error if the payload is classified as <see cref="ValidationStatus.Invalid"/>. | ||
/// </summary> | ||
[JsonIgnore(Condition = JsonIgnoreCondition.Never)] | ||
public string? ValidationError { get; set; } | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Nethermind/Nethermind.BlockValidation/Data/BlockValidationStatus.cs
This file contains 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,17 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
namespace Nethermind.BlockValidation.Data; | ||
|
||
public static class BlockValidationStatus | ||
{ | ||
/// <summary> | ||
/// The submissions are invalid. | ||
/// </summary> | ||
public const string Invalid = "Invalid"; | ||
|
||
/// <summary> | ||
/// The submissions are valid. | ||
/// </summary> | ||
public const string Valid = "Valid"; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Nethermind/Nethermind.BlockValidation/Data/BuilderBlockValidationRequest.cs
This file contains 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,19 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using Nethermind.Core.Crypto; | ||
|
||
namespace Nethermind.BlockValidation.Data; | ||
|
||
public class BuilderBlockValidationRequest | ||
{ | ||
/// <summary> | ||
/// The block hash of the parent beacon block. | ||
/// <see cref=https://github.com/flashbots/builder/blob/df9c765067d57ab4b2d0ad39dbb156cbe4965778/eth/block-validation/api.go#L198"/> | ||
/// </summary> | ||
public Hash256 ParentBeaconBlockRoot { get; set; } = Keccak.Zero; | ||
|
||
public ulong RegisterGasLimit { get; set; } | ||
|
||
public SubmitBlockRequest BlockRequest { get; set; } | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Nethermind/Nethermind.BlockValidation/Data/SubmitBlockRequest.cs
This file contains 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,21 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using Nethermind.Core.Crypto; | ||
using Nethermind.Merge.Plugin.Data; | ||
|
||
namespace Nethermind.BlockValidation.Data; | ||
|
||
public readonly struct SubmitBlockRequest { | ||
private readonly ExecutionPayload _executionPayload; | ||
private readonly BlobsBundleV1 _blobsBundle; | ||
|
||
public SubmitBlockRequest(ExecutionPayload executionPayload, BlobsBundleV1 blobsBundle, BidTrace message) { | ||
_executionPayload = executionPayload; | ||
_blobsBundle = blobsBundle; | ||
Message = message; | ||
} | ||
public readonly ExecutionPayload ExecutionPayload => _executionPayload; | ||
public readonly BlobsBundleV1 BlobsBundle => _blobsBundle; | ||
public BidTrace Message { get;} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Nethermind/Nethermind.BlockValidation/FlashbotsRpcModule.cs
This file contains 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,23 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using Nethermind.BlockValidation.Data; | ||
using Nethermind.BlockValidation.Handlers; | ||
using Nethermind.JsonRpc; | ||
|
||
namespace Nethermind.BlockValidation; | ||
|
||
public class FlashbotsRpcModule: IFlashbotsRpcModule | ||
{ | ||
private readonly ValidateSubmissionHandler _validateSubmissionHandler; | ||
|
||
public FlashbotsRpcModule(ValidateSubmissionHandler validateSubmissionHandler) | ||
{ | ||
_validateSubmissionHandler = validateSubmissionHandler; | ||
} | ||
|
||
Task<ResultWrapper<BlockValidationResult>> IFlashbotsRpcModule.flashbots_validateBuilderSubmissionV3(BuilderBlockValidationRequest @params) => | ||
_validateSubmissionHandler.ValidateSubmission(@params); | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/Nethermind/Nethermind.BlockValidation/Handlers/ValidateBuilderSubmissionHandler.cs
This file contains 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,73 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using Nethermind.BlockValidation.Data; | ||
using Nethermind.Consensus.Validators; | ||
using Nethermind.Core; | ||
using Nethermind.JsonRpc; | ||
using Nethermind.Logging; | ||
using Nethermind.Merge.Plugin.Data; | ||
|
||
namespace Nethermind.BlockValidation.Handlers; | ||
|
||
public class ValidateSubmissionHandler | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public ValidateSubmissionHandler(ILogManager logManager) | ||
{ | ||
_logger = logManager.GetClassLogger(); | ||
} | ||
|
||
private bool ValidateBlock(Block block, BidTrace message, ulong registerGasLimit, out string? error) | ||
{ | ||
error = null; | ||
|
||
if (message.ParentHash != block.Header.ParentHash) | ||
{ | ||
error = $"Parent hash mismatch. Expected {message.ParentHash} but got {block.Header.ParentHash}"; | ||
return false; | ||
} | ||
|
||
if (message.BlockHash != block.Header.Hash) | ||
{ | ||
error = $"Block hash mismatch. Expected {message.BlockHash} but got {block.Header.Hash}"; | ||
return false; | ||
} | ||
|
||
if(message.GasLimit != block.GasLimit) | ||
{ | ||
error = $"Gas limit mismatch. Expected {message.GasLimit} but got {block.GasLimit}"; | ||
return false; | ||
} | ||
|
||
if(message.GasUsed != block.GasUsed) | ||
{ | ||
error = $"Gas used mismatch. Expected {message.GasUsed} but got {block.GasUsed}"; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public Task<ResultWrapper<BlockValidationResult>> ValidateSubmission(BuilderBlockValidationRequest request) | ||
{ | ||
ExecutionPayload payload = request.BlockRequest.ExecutionPayload; | ||
BlobsBundleV1 blobsBundle = request.BlockRequest.BlobsBundle; | ||
|
||
string payloadStr = $"BuilderBlock: {payload}"; | ||
|
||
_logger.Info($"blobs bundle blobs {blobsBundle.Blobs.Length} commits {blobsBundle.Commitments.Length} proofs {blobsBundle.Proofs.Length}"); | ||
|
||
if(!payload.TryGetBlock(out Block? block)) | ||
{ | ||
if(_logger.IsWarn) _logger.Warn($"Invalid block. Result of {payloadStr}."); | ||
return BlockValidationResult.Invalid($"Block {payload} coud not be parsed as a block"); | ||
} | ||
|
||
|
||
|
||
return BlockValidationResult.Valid(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Nethermind/Nethermind.BlockValidation/IFlashbotsRpcModule.cs
This file contains 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,19 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using Nethermind.BlockValidation.Data; | ||
using Nethermind.JsonRpc; | ||
using Nethermind.JsonRpc.Modules; | ||
|
||
namespace Nethermind.BlockValidation; | ||
|
||
[RpcModule(ModuleType.Flashbots)] | ||
public interface IFlashbotsRpcModule : IRpcModule | ||
{ | ||
[JsonRpcMethod( | ||
Description = " validate the builder submissions as received by a relay", | ||
IsSharable = true, | ||
IsImplemented = true)] | ||
Task<ResultWrapper<BlockValidationResult>> flashbots_validateBuilderSubmissionV3(BuilderBlockValidationRequest @params); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Nethermind/Nethermind.BlockValidation/Nethermind.BlockValidation.csproj
This file contains 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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Nethermind.BlockValidation</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nethermind.Merge.Plugin\Nethermind.Merge.Plugin.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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 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