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
3 changes: 3 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 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
25 changes: 25 additions & 0 deletions src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,30 @@ 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
? 300L
: 60L;

public static long GetSha256PrecompileWordCost(this IReleaseSpec spec) =>
spec.IsEip7667Enabled
? 60L
: 12L;
}
}
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 @@ -2560,15 +2560,15 @@ 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;
if (!stack.PopUInt256(out UInt256 length)) return EvmExceptionType.StackUnderflow;
long topicsCount = instruction - Instruction.LOG0;
if (!UpdateMemoryCost(vmState, ref gasAvailable, in position, length)) return EvmExceptionType.OutOfGas;
if (!UpdateGas(
GasCostOf.Log + topicsCount * GasCostOf.LogTopic +
spec.GetLogDataCost() + topicsCount * GasCostOf.LogTopic +
(long)length * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas;
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved

ReadOnlyMemory<byte> data = vmState.Memory.Load(in position, length);
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
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected Cancun()
IsEip4844Enabled = true;
IsEip5656Enabled = true;
IsEip6780Enabled = true;
IsEip7667Enabled = true;
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
Eip4788ContractAddress = Eip4788Constants.BeaconRootsAddress;
}

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.IsEip3607Enabled;

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