Skip to content

Commit

Permalink
add input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 committed Oct 30, 2024
1 parent 06f6bed commit ef23ace
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 14 deletions.
13 changes: 7 additions & 6 deletions tools/Evm/Evm/t8n/JsonTypes/EnvJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class EnvJson
public ulong CurrentTimestamp { get; set; }
public Withdrawal[] Withdrawals { get; set; } = [];

private UInt256? CurrentRandom { get; set; }
public string? CurrentRandom { get; set; }
public ulong ParentTimestamp { get; set; }
public UInt256? ParentDifficulty { get; set; }
public UInt256? CurrentBaseFee { get; set; }
Expand All @@ -32,12 +32,13 @@ public class EnvJson
public Dictionary<string, Hash256> BlockHashes { get; set; } = [];
public Ommer[] Ommers { get; set; } = [];

public Hash256? GetCurrentRandom()
public Hash256? GetCurrentRandomHash256()
{
if (CurrentRandom == null) return null;
// if (CurrentRandom == null)
return null;

Span<byte> bytes = stackalloc byte[32];
CurrentRandom?.ToBigEndian(bytes);
return new Hash256(bytes);
// Span<byte> bytes = stackalloc byte[32];
// CurrentRandom?.ToBigEndian(bytes);
// return new Hash256(bytes);
}
}
2 changes: 0 additions & 2 deletions tools/Evm/Evm/t8n/T8NExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ public static class T8NExecutor
public static void Execute(T8NCommandArguments arguments)
{
var t8nTest = T8NInputProcessor.Process(arguments);

Console.Write(t8nTest);
}
}
14 changes: 11 additions & 3 deletions tools/Evm/Evm/t8n/T8NInputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static T8NTest Process(T8NCommandArguments arguments)
throw new T8NException("Env is not provided", T8NErrorCodes.ErrorIO);
}

(ISpecProvider specProvider, IReleaseSpec spec) = GetSpec(arguments);
(ISpecProvider specProvider, IReleaseSpec spec) = GetSpec(arguments, inputData.Env);

T8NValidator.ApplyChecks(inputData.Env, specProvider, spec);

var gethTraceOptions = new GethTraceOptions
{
Expand All @@ -44,7 +46,7 @@ public static T8NTest Process(T8NCommandArguments arguments)
CurrentTimestamp = inputData.Env.CurrentTimestamp,
CurrentNumber = inputData.Env.CurrentNumber,
Withdrawals = inputData.Env.Withdrawals,
CurrentRandom = inputData.Env.GetCurrentRandom(),
CurrentRandom = inputData.Env.GetCurrentRandomHash256(),
ParentTimestamp = inputData.Env.ParentTimestamp,
ParentDifficulty = inputData.Env.ParentDifficulty,
CurrentBaseFee = inputData.Env.CurrentBaseFee,
Expand All @@ -61,12 +63,13 @@ public static T8NTest Process(T8NCommandArguments arguments)
BlockHashes = inputData.Env.BlockHashes,
StateChainId = arguments.StateChainId,
GethTraceOptions = gethTraceOptions,
IsTraceEnabled = arguments.Trace,
};

return test;
}

private static (ISpecProvider, IReleaseSpec) GetSpec(T8NCommandArguments arguments)
private static (ISpecProvider, IReleaseSpec) GetSpec(T8NCommandArguments arguments, EnvJson env)
{
IReleaseSpec spec;
try
Expand All @@ -88,6 +91,11 @@ private static (ISpecProvider, IReleaseSpec) GetSpec(T8NCommandArguments argumen
: new CustomSpecProvider(((ForkActivation)0, Frontier.Instance),
((ForkActivation)1, overridableReleaseSpec));

if (spec is Paris)
{
specProvider.UpdateMergeTransitionInfo(env.CurrentNumber, 0);
}

return (specProvider, overridableReleaseSpec);
}
}
2 changes: 1 addition & 1 deletion tools/Evm/Evm/t8n/T8NInputReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static InputData ReadInputData(T8NCommandArguments arguments)

if (arguments.InputEnv != Stdin)
{
inputData.Env = LoadDataFromFile<EnvJson>(arguments.InputAlloc, "env");
inputData.Env = LoadDataFromFile<EnvJson>(arguments.InputEnv, "env");
}

if (arguments.InputTxs != Stdin)
Expand Down
100 changes: 98 additions & 2 deletions tools/Evm/Evm/t8n/T8NValidator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,105 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Evm.t8n.Errors;
using Evm.t8n.JsonTypes;
using Nethermind.Consensus.Ethash;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Specs.Forks;

namespace Evm.t8n;

public class T8NValidator
public static class T8NValidator
{

public static void ApplyChecks(EnvJson env, ISpecProvider specProvider, IReleaseSpec spec)
{
ApplyLondonChecks(env, spec);
ApplyShanghaiChecks(env, spec);
ApplyCancunChecks(env, spec);
ApplyMergeChecks(env, specProvider);
}

private static void ApplyLondonChecks(EnvJson env, IReleaseSpec spec)
{
if (spec is not London) return;
if (env.CurrentBaseFee != null) return;

if (!env.ParentBaseFee.HasValue || env.CurrentNumber == 0)
{
throw new T8NException("EIP-1559 config but missing 'parentBaseFee' in env section",
T8NErrorCodes.ErrorConfig);
}

var parent = Build.A.BlockHeader.WithNumber(env.CurrentNumber - 1).WithBaseFee(env.ParentBaseFee.Value)
.WithGasUsed(env.ParentGasUsed).WithGasLimit(env.ParentGasLimit).TestObject;
env.CurrentBaseFee = BaseFeeCalculator.Calculate(parent, spec);
}

private static void ApplyShanghaiChecks(EnvJson env, IReleaseSpec spec)
{
if (spec is not Shanghai) return;
if (env.Withdrawals == null)
{
throw new T8NException("Shanghai config but missing 'withdrawals' in env section",
T8NErrorCodes.ErrorConfig);
}
}

private static void ApplyCancunChecks(EnvJson env, IReleaseSpec spec)
{
if (spec is not Cancun)
{
env.ParentBeaconBlockRoot = null;
return;
}

if (env.ParentBeaconBlockRoot == null)
{
throw new T8NException("post-cancun env requires parentBeaconBlockRoot to be set",
T8NErrorCodes.ErrorConfig);
}
}

private static void ApplyMergeChecks(EnvJson env, ISpecProvider specProvider)
{
if (specProvider.TerminalTotalDifficulty?.IsZero ?? false)
{
if (env.CurrentRandom == null)
throw new T8NException("post-merge requires currentRandom to be defined in env",
T8NErrorCodes.ErrorConfig);
if (env.CurrentDifficulty?.IsZero ?? false)
throw new T8NException("post-merge difficulty must be zero (or omitted) in env",
T8NErrorCodes.ErrorConfig);
return;
}

if (env.CurrentDifficulty != null) return;
if (!env.ParentDifficulty.HasValue)
{
throw new T8NException(
"currentDifficulty was not provided, and cannot be calculated due to missing parentDifficulty",
T8NErrorCodes.ErrorConfig);
}

if (env.CurrentNumber == 0)
{
throw new T8NException("currentDifficulty needs to be provided for block number 0",
T8NErrorCodes.ErrorConfig);
}

if (env.CurrentTimestamp <= env.ParentTimestamp)
{
throw new T8NException(
$"currentDifficulty cannot be calculated -- currentTime ({env.CurrentTimestamp}) needs to be after parent time ({env.ParentTimestamp})",
T8NErrorCodes.ErrorConfig);
}

EthashDifficultyCalculator difficultyCalculator = new(specProvider);

env.CurrentDifficulty = difficultyCalculator.Calculate(env.ParentDifficulty.Value, env.ParentTimestamp,
env.CurrentTimestamp, env.CurrentNumber, env.ParentUncleHash is not null);
}
}

0 comments on commit ef23ace

Please sign in to comment.