Skip to content

Commit

Permalink
add init code for block validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rjnrohit committed Aug 16, 2024
1 parent b865db8 commit 7707396
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.BlockValidation/BlockValidation.cs
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 src/Nethermind/Nethermind.BlockValidation/Data/BidTrace.cs
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; }
}
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; }
}
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";
}
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; }
}
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 src/Nethermind/Nethermind.BlockValidation/FlashbotsRpcModule.cs
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);

}
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 src/Nethermind/Nethermind.BlockValidation/IFlashbotsRpcModule.cs
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);
}
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>
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.JsonRpc/Modules/ModuleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class ModuleType
public const string Erc20 = nameof(Erc20);
public const string Eth = nameof(Eth);
public const string Evm = nameof(Evm);
public const string Flashbots = nameof(Flashbots);
public const string Net = nameof(Net);
public const string Nft = nameof(Nft);
public const string Parity = nameof(Parity);
Expand All @@ -39,6 +40,7 @@ public static class ModuleType
Erc20,
Eth,
Evm,
Flashbots,
Net,
Nft,
Parity,
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.sln
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Signer", "Signer", "{89311B
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nethermind.ExternalSigner.Plugin", "Nethermind.ExternalSigner.Plugin\Nethermind.ExternalSigner.Plugin.csproj", "{6528010D-7DCE-4935-9785-5270FF515F3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nethermind.BlockValidation", "Nethermind.BlockValidation\Nethermind.BlockValidation.csproj", "{580DB104-AE89-444F-BD99-7FE0C84C615C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -600,6 +602,10 @@ Global
{6528010D-7DCE-4935-9785-5270FF515F3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6528010D-7DCE-4935-9785-5270FF515F3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6528010D-7DCE-4935-9785-5270FF515F3E}.Release|Any CPU.Build.0 = Release|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{580DB104-AE89-444F-BD99-7FE0C84C615C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 7707396

Please sign in to comment.