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

EIP-7667: Update gas costs #6932

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec
/// </summary>
bool IsEip6780Enabled { get; }

/// <summary>
/// Raise gas costs of hash functions
/// </summary>
bool IsEip7667Enabled { get; }

/// <summary>
/// Should transactions be validated against chainId.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Nethermind/Nethermind.Evm/GasCostOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public static class GasCostOf
public const long Log = 375;
public const long LogTopic = 375;
public const long LogData = 8;
public const long LogDataEip7667 = 10;
public const long Sha3 = 30;
public const long Sha3Eip7667 = 300;
public const long Sha3Word = 6;
public const long Sha3WordEip7667 = 60;
public const long BlockHash = 20;
public const long SelfDestruct = 0;
public const long SelfDestructEip150 = 5000;
Expand All @@ -62,5 +65,14 @@ public static class GasCostOf
public const long AccessStorageListEntry = 1900; // eip-2930
public const long TLoad = WarmStateRead; // eip-1153
public const long TStore = WarmStateRead; // eip-1153

public const long Blake2GFRoundEip7667 = 10;
public const long Blake2GFRound = 1;

public const long Sha256PrecompileBaseCostEip7667 = 300L;
public const long Sha256PrecompileBaseCost = 60L;

public const long Sha256PrecompileWordCostEip7667 = 60L;
public const long Sha256PrecompileWordCost = 12L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseS

uint rounds = inputData[..4].Span.ReadEthUInt32();

return rounds;
return rounds * releaseSpec.GetBlake2GFRoundDataCost();
}

public (ReadOnlyMemory<byte>, bool) Run(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ private static void InitIfNeeded()

public long BaseGasCost(IReleaseSpec releaseSpec)
{
return 60L;
return releaseSpec.GetSha256PrecompileBaseCost();
}

public long DataGasCost(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
{
return 12L * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length);
return releaseSpec.GetSha256PrecompileWordCost() * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length);
}

public (ReadOnlyMemory<byte>, bool) Run(in ReadOnlyMemory<byte> inputData, IReleaseSpec releaseSpec)
Expand Down
30 changes: 30 additions & 0 deletions src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,35 @@ public static class ReleaseSpecExtensions
spec.UseExpDDosProtection
? GasCostOf.ExpByteEip160
: GasCostOf.ExpByte;

public static long GetSha3Cost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.Sha3Eip7667
: GasCostOf.Sha3;

public static long GetSha3WordCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.Sha3WordEip7667
: GasCostOf.Sha3Word;

public static long GetLogDataCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.LogDataEip7667
: GasCostOf.LogData;

public static long GetSha256PrecompileBaseCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.Sha256PrecompileBaseCostEip7667
: GasCostOf.Sha256PrecompileBaseCost;

public static long GetSha256PrecompileWordCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.Sha256PrecompileWordCostEip7667
: GasCostOf.Sha256PrecompileWordCost;

public static long GetBlake2GFRoundDataCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? GasCostOf.Blake2GFRoundEip7667
: GasCostOf.Blake2GFRound;
}
}
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ private CallResult ExecuteCall<TTracingInstructions>(EvmState vmState, ReadOnlyM
{
if (!stack.PopUInt256(out a)) goto StackUnderflow;
if (!stack.PopUInt256(out b)) goto StackUnderflow;
gasAvailable -= GasCostOf.Sha3 + GasCostOf.Sha3Word * EvmPooledMemory.Div32Ceiling(in b);
gasAvailable -= spec.GetSha3Cost() + spec.GetSha3WordCost() * EvmPooledMemory.Div32Ceiling(in b);

if (!UpdateMemoryCost(vmState, ref gasAvailable, in a, b)) goto OutOfGas;

Expand Down Expand Up @@ -1824,7 +1824,7 @@ private CallResult ExecuteCall<TTracingInstructions>(EvmState vmState, ReadOnlyM
{
if (vmState.IsStatic) goto StaticCallViolation;

exceptionType = InstructionLog(vmState, ref stack, ref gasAvailable, instruction);
exceptionType = InstructionLog(vmState, ref stack, ref gasAvailable, instruction, spec);
if (exceptionType != EvmExceptionType.None) goto ReturnFailure;
break;
}
Expand Down Expand Up @@ -2449,7 +2449,7 @@ private EvmExceptionType InstructionSelfDestruct<TTracing>(EvmState vmState, ref
long gasCost = GasCostOf.Create +
(spec.IsEip3860Enabled ? GasCostOf.InitCodeWord * EvmPooledMemory.Div32Ceiling(initCodeLength) : 0) +
(instruction == Instruction.CREATE2
? GasCostOf.Sha3Word * EvmPooledMemory.Div32Ceiling(initCodeLength)
? spec.GetSha3WordCost() * EvmPooledMemory.Div32Ceiling(initCodeLength)
: 0);

if (!UpdateGas(gasCost, ref gasAvailable)) return (EvmExceptionType.OutOfGas, null);
Expand Down Expand Up @@ -2560,7 +2560,7 @@ private EvmExceptionType InstructionSelfDestruct<TTracing>(EvmState vmState, ref
}

[SkipLocalsInit]
private static EvmExceptionType InstructionLog<TTracing>(EvmState vmState, ref EvmStack<TTracing> stack, ref long gasAvailable, Instruction instruction)
private static EvmExceptionType InstructionLog<TTracing>(EvmState vmState, ref EvmStack<TTracing> stack, ref long gasAvailable, Instruction instruction, IReleaseSpec spec)
where TTracing : struct, IIsTracing
{
if (!stack.PopUInt256(out UInt256 position)) return EvmExceptionType.StackUnderflow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public OverridableReleaseSpec(IReleaseSpec spec)
public bool IsEip4844Enabled => _spec.IsEip4844Enabled;
public bool IsEip3607Enabled { get; set; }

public bool IsEip7667Enabled { get; set; }

public bool IsEip158IgnoredAccount(Address address)
{
return _spec.IsEip158IgnoredAccount(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public class ChainParameters
public ulong? Eip6780TransitionTimestamp { get; set; }
public ulong? Eip4788TransitionTimestamp { get; set; }
public Address Eip4788ContractAddress { get; set; }
public ulong? Eip7667TransitionTimestamp { get; set; }

#region EIP-4844 parameters
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static ReleaseSpec CreateReleaseSpec(ChainSpec chainSpec, long releaseSt
releaseSpec.IsEip6780Enabled = (chainSpec.Parameters.Eip6780TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;
releaseSpec.IsEip4788Enabled = (chainSpec.Parameters.Eip4788TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;
releaseSpec.Eip4788ContractAddress = chainSpec.Parameters.Eip4788ContractAddress;
releaseSpec.IsEip7667Enabled = (chainSpec.Parameters.Eip7667TransitionTimestamp ?? ulong.MaxValue) <= releaseStartTimestamp;

return releaseSpec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void LoadParameters(ChainSpecJson chainSpecJson, ChainSpec chainSpec)
Eip5656TransitionTimestamp = chainSpecJson.Params.Eip5656TransitionTimestamp,
Eip6780TransitionTimestamp = chainSpecJson.Params.Eip6780TransitionTimestamp,
Eip4788TransitionTimestamp = chainSpecJson.Params.Eip4788TransitionTimestamp,
Eip7667TransitionTimestamp = chainSpecJson.Params.Eip7667TransitionTimestamp,
Eip4788ContractAddress = chainSpecJson.Params.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress,
TransactionPermissionContract = chainSpecJson.Params.TransactionPermissionContract,
TransactionPermissionContractTransition = chainSpecJson.Params.TransactionPermissionContractTransition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ internal class ChainSpecParamsJson
public ulong? Eip5656TransitionTimestamp { get; set; }
public ulong? Eip6780TransitionTimestamp { get; set; }
public ulong? Eip4788TransitionTimestamp { get; set; }
public ulong? Eip7667TransitionTimestamp { get; set; }
public Address Eip4788ContractAddress { get; set; }
public UInt256? Eip4844BlobGasPriceUpdateFraction { get; set; }
public ulong? Eip4844MaxBlobGasPerBlock { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Specs/ReleaseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public ReleaseSpec Clone()
public bool IsEip5656Enabled { get; set; }
public bool IsEip6780Enabled { get; set; }
public bool IsEip4788Enabled { get; set; }
public bool IsEip7667Enabled { get; set; }

private Address _eip4788ContractAddress;
public Address Eip4788ContractAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public SystemTransactionReleaseSpec(IReleaseSpec spec)
public bool IsEip3541Enabled => _spec.IsEip3541Enabled;
public bool IsEip3607Enabled => _spec.IsEip3607Enabled;

public bool IsEip7667Enabled => _spec.IsEip7667Enabled;

public bool IsEip158IgnoredAccount(Address address)
{
return _spec.IsEip158IgnoredAccount(address);
Expand Down