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

Unify Engine API failure responses #5154

Merged
merged 4 commits into from
Jan 18, 2023
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
43 changes: 37 additions & 6 deletions src/Nethermind/Nethermind.Consensus/Producers/PayloadAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;

namespace Nethermind.Consensus.Producers;

Expand All @@ -18,12 +20,8 @@ public class PayloadAttributes

public IList<Withdrawal>? Withdrawals { get; set; }

/// <summary>
/// GasLimit
/// </summary>
/// <remarks>
/// Only used for MEV-Boost
/// </remarks>
/// <summary>Gets or sets the gas limit.</summary>
/// <remarks>Used for MEV-Boost only.</remarks>
public long? GasLimit { get; set; }

public override string ToString() => ToString(string.Empty);
Expand All @@ -45,3 +43,36 @@ public string ToString(string indentation)
return sb.ToString();
}
}

public static class PayloadAttributesExtensions
{
public static int GetVersion(this PayloadAttributes executionPayload) =>
executionPayload.Withdrawals is null ? 1 : 2;

public static bool Validate(
this PayloadAttributes payloadAttributes,
IReleaseSpec spec,
int version,
[NotNullWhen(false)] out string? error)
{
int actualVersion = payloadAttributes.GetVersion();

error = actualVersion switch
{
1 when spec.WithdrawalsEnabled => "PayloadAttributesV2 expected",
> 1 when !spec.WithdrawalsEnabled => "PayloadAttributesV1 expected",
_ => actualVersion > version ? $"PayloadAttributesV{version} expected" : null
};

return error is null;
}

public static bool Validate(this PayloadAttributes payloadAttributes,
ISpecProvider specProvider,
int version,
[NotNullWhen(false)] out string? error) =>
payloadAttributes.Validate(
specProvider.GetSpec(ForkActivation.TimestampOnly(payloadAttributes.Timestamp)),
version,
out error);
}
67 changes: 23 additions & 44 deletions src/Nethermind/Nethermind.Core/Specs/ForkActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public ForkActivation(long blockNumber, ulong? timestamp = null)
BlockNumber = blockNumber;
Timestamp = timestamp;
}

/// <summary>
/// Fork activation for forks past The Merge/Paris
/// </summary>
/// <param name="timestamp">Timestamp of the fork or check</param>
/// <returns>Post merge fork activation</returns>
/// <remarks>
/// Post Merge are based only on timestamp and we can ignore block number.
/// </remarks>
public static ForkActivation TimestampOnly(ulong timestamp) => new(long.MaxValue, timestamp);

public void Deconstruct(out long blockNumber, out ulong? timestamp)
{
blockNumber = BlockNumber;
Expand All @@ -30,60 +41,28 @@ public static implicit operator ForkActivation((long blocknumber, ulong? timesta
public static implicit operator (long blocknumber, ulong? timestamp)(ForkActivation forkActivation)
=> (forkActivation.BlockNumber, forkActivation.Timestamp);

public bool Equals(ForkActivation other)
{
return BlockNumber == other.BlockNumber && Timestamp == other.Timestamp;
}
public bool Equals(ForkActivation other) => BlockNumber == other.BlockNumber && Timestamp == other.Timestamp;

public override bool Equals(object? obj)
{
return obj is ForkActivation other && Equals(other);
}
public override bool Equals(object? obj) => obj is ForkActivation other && Equals(other);

public override int GetHashCode()
{
return HashCode.Combine(BlockNumber, Timestamp);
}
public override int GetHashCode() => HashCode.Combine(BlockNumber, Timestamp);

public override string ToString()
{
return $"{BlockNumber} {Timestamp}";
}
public override string ToString() => $"{BlockNumber} {Timestamp}";

public int CompareTo(ForkActivation other)
{
return Timestamp is null || other.Timestamp is null
public int CompareTo(ForkActivation other) =>
Timestamp is null || other.Timestamp is null
? BlockNumber.CompareTo(other.BlockNumber)
: Timestamp.Value.CompareTo(other.Timestamp.Value);
}

public static bool operator ==(ForkActivation first, ForkActivation second)
{
return first.Equals(second);
}
public static bool operator ==(ForkActivation first, ForkActivation second) => first.Equals(second);

public static bool operator !=(ForkActivation first, ForkActivation second)
{
return !first.Equals(second);
}
public static bool operator !=(ForkActivation first, ForkActivation second) => !first.Equals(second);

public static bool operator <(ForkActivation first, ForkActivation second)
{
return first.CompareTo(second) < 0;
}
public static bool operator <(ForkActivation first, ForkActivation second) => first.CompareTo(second) < 0;

public static bool operator >(ForkActivation first, ForkActivation second)
{
return first.CompareTo(second) > 0;
}
public static bool operator >(ForkActivation first, ForkActivation second) => first.CompareTo(second) > 0;

public static bool operator <=(ForkActivation first, ForkActivation second)
{
return first.CompareTo(second) <= 0;
}
public static bool operator <=(ForkActivation first, ForkActivation second) => first.CompareTo(second) <= 0;

public static bool operator >=(ForkActivation first, ForkActivation second)
{
return first.CompareTo(second) >= 0;
}
public static bool operator >=(ForkActivation first, ForkActivation second) => first.CompareTo(second) >= 0;
}
Loading