From 6a8650306d52c917d3bee7f33f6c38b72e880c70 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Thu, 18 Apr 2024 17:34:48 +0500 Subject: [PATCH 01/13] Update SHA256Precompile gas cost --- src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs index 4aa42639104..ac13f12d1ed 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs @@ -34,12 +34,12 @@ private static void InitIfNeeded() public long BaseGasCost(IReleaseSpec releaseSpec) { - return 60L; + return 300L; } public long DataGasCost(in ReadOnlyMemory inputData, IReleaseSpec releaseSpec) { - return 12L * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length); + return 60L * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length); } public (ReadOnlyMemory, bool) Run(in ReadOnlyMemory inputData, IReleaseSpec releaseSpec) From fa3f7e46dd348ebb0aa990e3a7eb31239f7a9007 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Thu, 18 Apr 2024 17:54:55 +0500 Subject: [PATCH 02/13] update gas for GLOGBYTE, KECCAK_BASE_COST, KECCAK_WORD_COST --- src/Nethermind/Nethermind.Evm/GasCostOf.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/GasCostOf.cs b/src/Nethermind/Nethermind.Evm/GasCostOf.cs index 68dc8cae654..44be5d515b1 100644 --- a/src/Nethermind/Nethermind.Evm/GasCostOf.cs +++ b/src/Nethermind/Nethermind.Evm/GasCostOf.cs @@ -42,9 +42,9 @@ public static class GasCostOf public const long BlobHash = 3; public const long Log = 375; public const long LogTopic = 375; - public const long LogData = 8; - public const long Sha3 = 30; - public const long Sha3Word = 6; + public const long LogData = 10; + public const long Sha3 = 300; + public const long Sha3Word = 60; public const long BlockHash = 20; public const long SelfDestruct = 0; public const long SelfDestructEip150 = 5000; From d6127f4283d15f49c99db80637395eb49a729591 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Fri, 19 Apr 2024 00:40:31 +0500 Subject: [PATCH 03/13] added Eip7667 values in ReleaseSpecExtensions --- .../Nethermind.Core/Specs/IReleaseSpec.cs | 5 +++++ src/Nethermind/Nethermind.Evm/GasCostOf.cs | 9 ++++++--- .../Nethermind.Evm/ReleaseSpecExtensions.cs | 15 +++++++++++++++ src/Nethermind/Nethermind.Evm/VirtualMachine.cs | 8 ++++---- .../Nethermind.Specs/Forks/17_Cancun.cs | 1 + src/Nethermind/Nethermind.Specs/ReleaseSpec.cs | 1 + .../SystemTransactionReleaseSpec.cs | 2 ++ 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs b/src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs index f7e8519de72..f9a087f223d 100644 --- a/src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs +++ b/src/Nethermind/Nethermind.Core/Specs/IReleaseSpec.cs @@ -278,6 +278,11 @@ public interface IReleaseSpec : IEip1559Spec, IReceiptSpec /// bool IsEip6780Enabled { get; } + /// + /// Raise gas costs of hash functions + /// + bool IsEip7667Enabled { get; } + /// /// Should transactions be validated against chainId. /// diff --git a/src/Nethermind/Nethermind.Evm/GasCostOf.cs b/src/Nethermind/Nethermind.Evm/GasCostOf.cs index 44be5d515b1..8196c64c4b6 100644 --- a/src/Nethermind/Nethermind.Evm/GasCostOf.cs +++ b/src/Nethermind/Nethermind.Evm/GasCostOf.cs @@ -42,9 +42,12 @@ public static class GasCostOf public const long BlobHash = 3; public const long Log = 375; public const long LogTopic = 375; - public const long LogData = 10; - public const long Sha3 = 300; - public const long Sha3Word = 60; + 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; diff --git a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs index ec553bcca02..8270318d902 100644 --- a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs +++ b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs @@ -83,5 +83,20 @@ 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; } } diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index 64a9c11ea86..bd69a3ac4ef 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -1225,7 +1225,7 @@ private CallResult ExecuteCall(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; @@ -1824,7 +1824,7 @@ private CallResult ExecuteCall(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; } @@ -2560,7 +2560,7 @@ private EvmExceptionType InstructionSelfDestruct(EvmState vmState, ref } [SkipLocalsInit] - private static EvmExceptionType InstructionLog(EvmState vmState, ref EvmStack stack, ref long gasAvailable, Instruction instruction) + private static EvmExceptionType InstructionLog(EvmState vmState, ref EvmStack stack, ref long gasAvailable, Instruction instruction, IReleaseSpec spec) where TTracing : struct, IIsTracing { if (!stack.PopUInt256(out UInt256 position)) return EvmExceptionType.StackUnderflow; @@ -2568,7 +2568,7 @@ private static EvmExceptionType InstructionLog(EvmState vmState, ref E 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; ReadOnlyMemory data = vmState.Memory.Load(in position, length); diff --git a/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs b/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs index a95510c39b4..770db238226 100644 --- a/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs +++ b/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs @@ -19,6 +19,7 @@ protected Cancun() IsEip4844Enabled = true; IsEip5656Enabled = true; IsEip6780Enabled = true; + IsEip7667Enabled = true; Eip4788ContractAddress = Eip4788Constants.BeaconRootsAddress; } diff --git a/src/Nethermind/Nethermind.Specs/ReleaseSpec.cs b/src/Nethermind/Nethermind.Specs/ReleaseSpec.cs index 5de83c8356b..cbea42bbafe 100644 --- a/src/Nethermind/Nethermind.Specs/ReleaseSpec.cs +++ b/src/Nethermind/Nethermind.Specs/ReleaseSpec.cs @@ -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 diff --git a/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs b/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs index 8774d7a03ae..659c251ecf6 100644 --- a/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs +++ b/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs @@ -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); From 290fbf4ea2fa7883346d6d6c028cf30522649e4f Mon Sep 17 00:00:00 2001 From: yerke26 Date: Fri, 19 Apr 2024 00:48:55 +0500 Subject: [PATCH 04/13] fix Sha256Precompile --- .../Nethermind.Evm/Precompiles/Sha256Precompile.cs | 4 ++-- .../Nethermind.Evm/ReleaseSpecExtensions.cs | 12 +++++++++++- .../Nethermind.Specs.Test/OverridableReleaseSpec.cs | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs index ac13f12d1ed..ffc8662924e 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Sha256Precompile.cs @@ -34,12 +34,12 @@ private static void InitIfNeeded() public long BaseGasCost(IReleaseSpec releaseSpec) { - return 300L; + return releaseSpec.GetSha256PrecompileBaseCost(); } public long DataGasCost(in ReadOnlyMemory inputData, IReleaseSpec releaseSpec) { - return 60L * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length); + return releaseSpec.GetSha256PrecompileWordCost() * EvmPooledMemory.Div32Ceiling((ulong)inputData.Length); } public (ReadOnlyMemory, bool) Run(in ReadOnlyMemory inputData, IReleaseSpec releaseSpec) diff --git a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs index 8270318d902..9f1e108751d 100644 --- a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs +++ b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs @@ -93,10 +93,20 @@ public static class ReleaseSpecExtensions 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; } } diff --git a/src/Nethermind/Nethermind.Specs.Test/OverridableReleaseSpec.cs b/src/Nethermind/Nethermind.Specs.Test/OverridableReleaseSpec.cs index b36a57071d0..26c1587c4f8 100644 --- a/src/Nethermind/Nethermind.Specs.Test/OverridableReleaseSpec.cs +++ b/src/Nethermind/Nethermind.Specs.Test/OverridableReleaseSpec.cs @@ -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); From 4fb09dfac26344c33017b499791679656a7d4479 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Mon, 22 Apr 2024 15:02:38 +0500 Subject: [PATCH 05/13] Add Blake2GFRound gas cost --- src/Nethermind/Nethermind.Evm/GasCostOf.cs | 9 +++++++++ .../Nethermind.Evm/Precompiles/Blake2FPrecompile.cs | 2 +- .../Nethermind.Evm/ReleaseSpecExtensions.cs | 13 +++++++++---- src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs | 1 - 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/GasCostOf.cs b/src/Nethermind/Nethermind.Evm/GasCostOf.cs index 8196c64c4b6..a648cae4135 100644 --- a/src/Nethermind/Nethermind.Evm/GasCostOf.cs +++ b/src/Nethermind/Nethermind.Evm/GasCostOf.cs @@ -65,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; } } diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Blake2FPrecompile.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Blake2FPrecompile.cs index 14534399d3f..0329dc26b96 100644 --- a/src/Nethermind/Nethermind.Evm/Precompiles/Blake2FPrecompile.cs +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Blake2FPrecompile.cs @@ -36,7 +36,7 @@ public long DataGasCost(in ReadOnlyMemory inputData, IReleaseSpec releaseS uint rounds = inputData[..4].Span.ReadEthUInt32(); - return rounds; + return rounds * releaseSpec.GetBlake2GFRoundDataCost(); } public (ReadOnlyMemory, bool) Run(in ReadOnlyMemory inputData, IReleaseSpec releaseSpec) diff --git a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs index 9f1e108751d..bed5ef83978 100644 --- a/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs +++ b/src/Nethermind/Nethermind.Evm/ReleaseSpecExtensions.cs @@ -101,12 +101,17 @@ public static class ReleaseSpecExtensions public static long GetSha256PrecompileBaseCost(this IReleaseSpec spec) => spec.IsEip7667Enabled - ? 300L - : 60L; + ? GasCostOf.Sha256PrecompileBaseCostEip7667 + : GasCostOf.Sha256PrecompileBaseCost; public static long GetSha256PrecompileWordCost(this IReleaseSpec spec) => spec.IsEip7667Enabled - ? 60L - : 12L; + ? GasCostOf.Sha256PrecompileWordCostEip7667 + : GasCostOf.Sha256PrecompileWordCost; + + public static long GetBlake2GFRoundDataCost(this IReleaseSpec spec) => + spec.IsEip7667Enabled + ? GasCostOf.Blake2GFRoundEip7667 + : GasCostOf.Blake2GFRound; } } diff --git a/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs b/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs index 770db238226..a95510c39b4 100644 --- a/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs +++ b/src/Nethermind/Nethermind.Specs/Forks/17_Cancun.cs @@ -19,7 +19,6 @@ protected Cancun() IsEip4844Enabled = true; IsEip5656Enabled = true; IsEip6780Enabled = true; - IsEip7667Enabled = true; Eip4788ContractAddress = Eip4788Constants.BeaconRootsAddress; } From 38726de5bc94246934375be0977b3b9f6d239d72 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Mon, 22 Apr 2024 16:36:06 +0500 Subject: [PATCH 06/13] add json parsing --- .../Nethermind.Specs/ChainSpecStyle/ChainParameters.cs | 1 + .../ChainSpecStyle/ChainSpecBasedSpecProvider.cs | 1 + .../Nethermind.Specs/ChainSpecStyle/ChainSpecLoader.cs | 1 + .../Nethermind.Specs/ChainSpecStyle/Json/ChainSpecParamsJson.cs | 1 + src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs | 2 +- 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainParameters.cs b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainParameters.cs index 817250bc01e..9d6c5831e58 100644 --- a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainParameters.cs +++ b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainParameters.cs @@ -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 /// diff --git a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecBasedSpecProvider.cs b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecBasedSpecProvider.cs index 7aad4bac04f..c9ccd86888e 100644 --- a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecBasedSpecProvider.cs +++ b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecBasedSpecProvider.cs @@ -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; } diff --git a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecLoader.cs b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecLoader.cs index db4155117c8..d07bc2c5fa0 100644 --- a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecLoader.cs +++ b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecLoader.cs @@ -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, diff --git a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/Json/ChainSpecParamsJson.cs b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/Json/ChainSpecParamsJson.cs index 5f22e8e6a85..662752846ac 100644 --- a/src/Nethermind/Nethermind.Specs/ChainSpecStyle/Json/ChainSpecParamsJson.cs +++ b/src/Nethermind/Nethermind.Specs/ChainSpecStyle/Json/ChainSpecParamsJson.cs @@ -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; } diff --git a/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs b/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs index 659c251ecf6..2c44a2f7f8c 100644 --- a/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs +++ b/src/Nethermind/Nethermind.Specs/SystemTransactionReleaseSpec.cs @@ -110,7 +110,7 @@ public SystemTransactionReleaseSpec(IReleaseSpec spec) public bool IsEip3541Enabled => _spec.IsEip3541Enabled; public bool IsEip3607Enabled => _spec.IsEip3607Enabled; - public bool IsEip7667Enabled => _spec.IsEip3607Enabled; + public bool IsEip7667Enabled => _spec.IsEip7667Enabled; public bool IsEip158IgnoredAccount(Address address) { From ee215e1f66bbae7c2d02f6a08569ccb8388cb601 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Mon, 22 Apr 2024 20:13:35 +0500 Subject: [PATCH 07/13] fix --- src/Nethermind/Nethermind.Evm/VirtualMachine.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index bd69a3ac4ef..e250a0f67f7 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -2568,8 +2568,8 @@ private static EvmExceptionType InstructionLog(EvmState vmState, ref E long topicsCount = instruction - Instruction.LOG0; if (!UpdateMemoryCost(vmState, ref gasAvailable, in position, length)) return EvmExceptionType.OutOfGas; if (!UpdateGas( - spec.GetLogDataCost() + topicsCount * GasCostOf.LogTopic + - (long)length * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas; + GasCostOf.Log + topicsCount * GasCostOf.LogTopic + + (long)length * spec.GetLogDataCost(), ref gasAvailable)) return EvmExceptionType.OutOfGas; ReadOnlyMemory data = vmState.Memory.Load(in position, length); Hash256[] topics = new Hash256[topicsCount]; From 55a14fd258102a4ffcec0ecb94f01aca4dd5785a Mon Sep 17 00:00:00 2001 From: yerke26 Date: Mon, 22 Apr 2024 20:33:51 +0500 Subject: [PATCH 08/13] fix --- src/Nethermind/Nethermind.Evm/VirtualMachine.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index e250a0f67f7..ebf8e942cd6 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -2449,7 +2449,7 @@ private EvmExceptionType InstructionSelfDestruct(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); @@ -2569,7 +2569,7 @@ private static EvmExceptionType InstructionLog(EvmState vmState, ref E if (!UpdateMemoryCost(vmState, ref gasAvailable, in position, length)) return EvmExceptionType.OutOfGas; if (!UpdateGas( GasCostOf.Log + topicsCount * GasCostOf.LogTopic + - (long)length * spec.GetLogDataCost(), ref gasAvailable)) return EvmExceptionType.OutOfGas; + (long)length * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas; ReadOnlyMemory data = vmState.Memory.Load(in position, length); Hash256[] topics = new Hash256[topicsCount]; From 2eeee730d5f3eb924bc51417312a80db65ca8fce Mon Sep 17 00:00:00 2001 From: yerke26 Date: Tue, 23 Apr 2024 16:31:50 +0500 Subject: [PATCH 09/13] fix --- src/Nethermind/Nethermind.Evm/VirtualMachine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index ebf8e942cd6..19ea3e5a244 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -2569,7 +2569,7 @@ private static EvmExceptionType InstructionLog(EvmState vmState, ref E if (!UpdateMemoryCost(vmState, ref gasAvailable, in position, length)) return EvmExceptionType.OutOfGas; if (!UpdateGas( GasCostOf.Log + topicsCount * GasCostOf.LogTopic + - (long)length * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas; + (long)length * spec.GetLogDataCost(), ref gasAvailable)) return EvmExceptionType.OutOfGas; ReadOnlyMemory data = vmState.Memory.Load(in position, length); Hash256[] topics = new Hash256[topicsCount]; From 68b82f42428a06c60ebd5363e69e04053b319dfe Mon Sep 17 00:00:00 2001 From: yerke26 Date: Wed, 24 Apr 2024 21:20:08 +0500 Subject: [PATCH 10/13] Added Eip7667 tests --- .../Nethermind.Evm.Test/Eip7667Spec.cs | 18 ++ .../Nethermind.Evm.Test/Eip7667Tests.cs | 156 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs create mode 100644 src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs new file mode 100644 index 00000000000..57628994d23 --- /dev/null +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs @@ -0,0 +1,18 @@ +using System.Threading; +using Nethermind.Core.Specs; +using Nethermind.Specs.Forks; + +namespace Nethermind.Evm.Test; + +public class Eip7667Spec : Cancun +{ + private static IReleaseSpec _instance; + + protected Eip7667Spec() + { + Name = "Eip7667Spec"; + IsEip7667Enabled = true; + } + + public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Eip7667Spec()); +} \ No newline at end of file diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs new file mode 100644 index 00000000000..9074a499a34 --- /dev/null +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs @@ -0,0 +1,156 @@ +using Nethermind.Core.Specs; +using Nethermind.Evm.Precompiles; +using Nethermind.Int256; +using Nethermind.Specs.Forks; +using Nethermind.Specs.Test; +using NUnit.Framework; + +namespace Nethermind.Evm.Test; + +public class Eip7667Tests : VirtualMachineTestsBase +{ + [SetUp] + public void SetUp() + { + Setup(); + } + + protected override ISpecProvider SpecProvider => new CustomSpecProvider( + ((ForkActivation)0, Frontier.Instance), + ((ForkActivation)1, Cancun.Instance), + ((ForkActivation)2, Eip7667Spec.Instance)); + + [Test] + public void GetSha3Cost() + { + Assert.That(Cancun.Instance.GetSha3Cost(), Is.EqualTo(30)); + Assert.That(Eip7667Spec.Instance.GetSha3Cost(), Is.EqualTo(300)); + } + + [Test] + public void GetSha3WordCost() + { + Assert.That(Cancun.Instance.GetSha3WordCost(), Is.EqualTo(6)); + Assert.That(Eip7667Spec.Instance.GetSha3WordCost(), Is.EqualTo(60)); + } + + [Test] + public void GetLogDataCost() + { + Assert.That(Cancun.Instance.GetLogDataCost(), Is.EqualTo(8)); + Assert.That(Eip7667Spec.Instance.GetLogDataCost(), Is.EqualTo(10)); + } + + [Test] + public void GetSha256PrecompileBaseCost() + { + Assert.That(Cancun.Instance.GetSha256PrecompileBaseCost(), Is.EqualTo(60)); + Assert.That(Eip7667Spec.Instance.GetSha256PrecompileBaseCost(), Is.EqualTo(300)); + } + + [Test] + public void GetSha256PrecompileWordCost() + { + Assert.That(Cancun.Instance.GetSha256PrecompileWordCost(), Is.EqualTo(12)); + Assert.That(Eip7667Spec.Instance.GetSha256PrecompileWordCost(), Is.EqualTo(60)); + } + + [Test] + public void GetBlake2GFRoundDataCost() + { + Assert.That(Cancun.Instance.GetBlake2GFRoundDataCost(), Is.EqualTo(1)); + Assert.That(Eip7667Spec.Instance.GetBlake2GFRoundDataCost(), Is.EqualTo(10)); + } + + [Test] + public void Keccak256OpDifference() + { + byte[] code = Prepare.EvmCode + .PushData(32) + .PushData(0) + .Op(Instruction.KECCAK256) + .STOP() + .Done; + + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, code); + Setup(); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, code); + + long gasDifference = Eip7667Spec.Instance.GetSha3Cost() - Cancun.Instance.GetSha3Cost() + + Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost(); + + Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + } + + [Test] + public void CreateOpDifference() + { + byte[] salt = [4, 5, 6]; + byte[] deployedCode = [1, 2, 3]; + byte[] initCode = Prepare.EvmCode.ForInitOf(deployedCode).Done; + byte[] createCode = Prepare.EvmCode.Create2(initCode, salt, 0).STOP().Done; + + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, DefaultBlockGasLimit, createCode); + Setup(); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, DefaultBlockGasLimit, createCode); + + long gasDifference = (Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost()) + * EvmPooledMemory.Div32Ceiling((UInt256) initCode.Length); + + Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + } + + [Test] + public void LogOpDifference() + { + const int length = 32; + byte[] createCode = Prepare.EvmCode + .PushData(length) + .PushData(0) + .Op(Instruction.LOG0) + .STOP() + .Done; + + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, DefaultBlockGasLimit, createCode); + Setup(); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, DefaultBlockGasLimit, createCode); + + long gasDifference = (Eip7667Spec.Instance.GetLogDataCost() - Cancun.Instance.GetLogDataCost()) * length; + + Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + } + + [Test] + public void TestSha256Precompile() + { + var sha256Precompile = Sha256Precompile.Instance; + + Assert.That(sha256Precompile.BaseGasCost(Eip7667Spec.Instance), + Is.EqualTo(Eip7667Spec.Instance.GetSha256PrecompileBaseCost())); + + Assert.That(sha256Precompile.BaseGasCost(Cancun.Instance), + Is.EqualTo(Cancun.Instance.GetSha256PrecompileBaseCost())); + + var bytes = new byte[1]; + Assert.That(sha256Precompile.DataGasCost(bytes, Eip7667Spec.Instance), + Is.EqualTo(Eip7667Spec.Instance.GetSha256PrecompileWordCost())); + + Assert.That(sha256Precompile.DataGasCost(bytes, Cancun.Instance), + Is.EqualTo(Cancun.Instance.GetSha256PrecompileWordCost())); + } + + [Test] + public void TestBlake2FPrecompile() + { + var blake2FPrecompile = Blake2FPrecompile.Instance; + + var bytes = new byte[213]; + bytes[3] = 1; + + Assert.That(blake2FPrecompile.DataGasCost(bytes, Eip7667Spec.Instance), + Is.EqualTo(Eip7667Spec.Instance.GetBlake2GFRoundDataCost())); + + Assert.That(blake2FPrecompile.DataGasCost(bytes, Cancun.Instance), + Is.EqualTo(Cancun.Instance.GetBlake2GFRoundDataCost())); + } +} \ No newline at end of file From 2b954eab471e0ab46b4bcd01fdbec1ffb05e7fa2 Mon Sep 17 00:00:00 2001 From: yerke26 Date: Wed, 24 Apr 2024 21:22:44 +0500 Subject: [PATCH 11/13] whitespace fix --- .../Nethermind.Evm.Test/Eip7667Spec.cs | 4 ++-- .../Nethermind.Evm.Test/Eip7667Tests.cs | 22 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs index 57628994d23..0f54a79c333 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs @@ -1,4 +1,4 @@ -using System.Threading; +using System.Threading; using Nethermind.Core.Specs; using Nethermind.Specs.Forks; @@ -15,4 +15,4 @@ protected Eip7667Spec() } public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Eip7667Spec()); -} \ No newline at end of file +} diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs index 9074a499a34..806a1c3f5cd 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs @@ -1,4 +1,4 @@ -using Nethermind.Core.Specs; +using Nethermind.Core.Specs; using Nethermind.Evm.Precompiles; using Nethermind.Int256; using Nethermind.Specs.Forks; @@ -72,9 +72,9 @@ public void Keccak256OpDifference() .STOP() .Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, code); + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, code); Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, code); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, code); long gasDifference = Eip7667Spec.Instance.GetSha3Cost() - Cancun.Instance.GetSha3Cost() + Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost(); @@ -90,12 +90,12 @@ public void CreateOpDifference() byte[] initCode = Prepare.EvmCode.ForInitOf(deployedCode).Done; byte[] createCode = Prepare.EvmCode.Create2(initCode, salt, 0).STOP().Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, DefaultBlockGasLimit, createCode); Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, DefaultBlockGasLimit, createCode); long gasDifference = (Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost()) - * EvmPooledMemory.Div32Ceiling((UInt256) initCode.Length); + * EvmPooledMemory.Div32Ceiling((UInt256)initCode.Length); Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); } @@ -111,9 +111,9 @@ public void LogOpDifference() .STOP() .Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation) 1, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, DefaultBlockGasLimit, createCode); Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation) 2, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, DefaultBlockGasLimit, createCode); long gasDifference = (Eip7667Spec.Instance.GetLogDataCost() - Cancun.Instance.GetLogDataCost()) * length; @@ -134,7 +134,7 @@ public void TestSha256Precompile() var bytes = new byte[1]; Assert.That(sha256Precompile.DataGasCost(bytes, Eip7667Spec.Instance), Is.EqualTo(Eip7667Spec.Instance.GetSha256PrecompileWordCost())); - + Assert.That(sha256Precompile.DataGasCost(bytes, Cancun.Instance), Is.EqualTo(Cancun.Instance.GetSha256PrecompileWordCost())); } @@ -149,8 +149,8 @@ public void TestBlake2FPrecompile() Assert.That(blake2FPrecompile.DataGasCost(bytes, Eip7667Spec.Instance), Is.EqualTo(Eip7667Spec.Instance.GetBlake2GFRoundDataCost())); - + Assert.That(blake2FPrecompile.DataGasCost(bytes, Cancun.Instance), Is.EqualTo(Cancun.Instance.GetBlake2GFRoundDataCost())); } -} \ No newline at end of file +} From b4c165c5271679225f01fef50539aa3944631f5a Mon Sep 17 00:00:00 2001 From: yerke26 Date: Thu, 25 Apr 2024 16:32:15 +0500 Subject: [PATCH 12/13] fix tests --- .../Nethermind.Evm.Test/Eip7667Tests.cs | 197 ++++++++++++------ .../Forks/18_Prague.cs} | 6 +- .../Nethermind.Specs/MainnetSpecProvider.cs | 6 +- 3 files changed, 139 insertions(+), 70 deletions(-) rename src/Nethermind/{Nethermind.Evm.Test/Eip7667Spec.cs => Nethermind.Specs/Forks/18_Prague.cs} (71%) diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs index 806a1c3f5cd..d2332da5241 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs @@ -1,69 +1,77 @@ -using Nethermind.Core.Specs; +using FluentAssertions; using Nethermind.Evm.Precompiles; using Nethermind.Int256; -using Nethermind.Specs.Forks; -using Nethermind.Specs.Test; +using Nethermind.Specs; using NUnit.Framework; namespace Nethermind.Evm.Test; public class Eip7667Tests : VirtualMachineTestsBase { - [SetUp] - public void SetUp() + protected override long BlockNumber => MainnetSpecProvider.ParisBlockNumber; + protected override ulong Timestamp => MainnetSpecProvider.PragueBlockTimestamp; + + [Test] + public void Sha3_cost_before_eip_7667() { - Setup(); + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); + Assert.That(spec.GetSha3Cost(), Is.EqualTo(30)); } - - protected override ISpecProvider SpecProvider => new CustomSpecProvider( - ((ForkActivation)0, Frontier.Instance), - ((ForkActivation)1, Cancun.Instance), - ((ForkActivation)2, Eip7667Spec.Instance)); - + [Test] - public void GetSha3Cost() + public void Sha3_cost_after_eip_7667() { - Assert.That(Cancun.Instance.GetSha3Cost(), Is.EqualTo(30)); - Assert.That(Eip7667Spec.Instance.GetSha3Cost(), Is.EqualTo(300)); + Assert.That(Spec.GetSha3Cost(), Is.EqualTo(300)); } [Test] - public void GetSha3WordCost() + public void Sha3_word_cost_before_eip_7667() { - Assert.That(Cancun.Instance.GetSha3WordCost(), Is.EqualTo(6)); - Assert.That(Eip7667Spec.Instance.GetSha3WordCost(), Is.EqualTo(60)); + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); + Assert.That(spec.GetSha3WordCost(), Is.EqualTo(6)); } [Test] - public void GetLogDataCost() + public void Sha3_word_cost_after_eip_7667() { - Assert.That(Cancun.Instance.GetLogDataCost(), Is.EqualTo(8)); - Assert.That(Eip7667Spec.Instance.GetLogDataCost(), Is.EqualTo(10)); + Assert.That(Spec.GetSha3WordCost(), Is.EqualTo(60)); } [Test] - public void GetSha256PrecompileBaseCost() + public void Log_data_cost_before_eip_7667() { - Assert.That(Cancun.Instance.GetSha256PrecompileBaseCost(), Is.EqualTo(60)); - Assert.That(Eip7667Spec.Instance.GetSha256PrecompileBaseCost(), Is.EqualTo(300)); + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); + Assert.That(spec.GetLogDataCost(), Is.EqualTo(8)); } [Test] - public void GetSha256PrecompileWordCost() + public void Log_data_cost_after_eip_7667() { - Assert.That(Cancun.Instance.GetSha256PrecompileWordCost(), Is.EqualTo(12)); - Assert.That(Eip7667Spec.Instance.GetSha256PrecompileWordCost(), Is.EqualTo(60)); + Assert.That(Spec.GetLogDataCost(), Is.EqualTo(10)); } [Test] - public void GetBlake2GFRoundDataCost() + public void Keccak256_op_before_eip_7667() { - Assert.That(Cancun.Instance.GetBlake2GFRoundDataCost(), Is.EqualTo(1)); - Assert.That(Eip7667Spec.Instance.GetBlake2GFRoundDataCost(), Is.EqualTo(10)); + byte[] code = Prepare.EvmCode + .PushData(32) + .PushData(0) + .Op(Instruction.KECCAK256) + .STOP() + .Done; + + TestAllTracerWithOutput result = Execute((BlockNumber, Timestamp - 1), code); + + long gasSpent = GasCostOf.Transaction + GasCostOf.VeryLow * 2 // for push + + GasCostOf.VeryLow // memory gas cost + + GasCostOf.Sha3 + GasCostOf.Sha3Word; // Keccak256 + + result.StatusCode.Should().Be(1); + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } [Test] - public void Keccak256OpDifference() + public void Keccak256_op_after_eip_7667() { byte[] code = Prepare.EvmCode .PushData(32) @@ -72,36 +80,66 @@ public void Keccak256OpDifference() .STOP() .Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, code); - Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, code); + TestAllTracerWithOutput result = Execute(code); - long gasDifference = Eip7667Spec.Instance.GetSha3Cost() - Cancun.Instance.GetSha3Cost() - + Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost(); + long gasSpent = GasCostOf.Transaction + GasCostOf.VeryLow * 2 // for push + + GasCostOf.VeryLow // memory gas cost + + GasCostOf.Sha3Eip7667 + GasCostOf.Sha3WordEip7667; // Keccak256 - Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + result.StatusCode.Should().Be(1); + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } [Test] - public void CreateOpDifference() + public void Create_op_before_eip_7667() + { + byte[] salt = [4, 5, 6]; + byte[] deployedCode = [1, 2, 3]; + byte[] initCode = Prepare.EvmCode.ForInitOf(deployedCode).Done; + byte[] createCode = Prepare.EvmCode.Create2(initCode, salt, 0).STOP().Done; + + TestAllTracerWithOutput result = Execute((BlockNumber, Timestamp - 1), createCode); + + long gasSpent = GasCostOf.Sha3Word * EvmPooledMemory.Div32Ceiling((UInt256)initCode.Length) + 53658; + + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); + } + + [Test] + public void Create_op_after_eip_7667() { byte[] salt = [4, 5, 6]; byte[] deployedCode = [1, 2, 3]; byte[] initCode = Prepare.EvmCode.ForInitOf(deployedCode).Done; byte[] createCode = Prepare.EvmCode.Create2(initCode, salt, 0).STOP().Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, DefaultBlockGasLimit, createCode); - Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput result = Execute(createCode); - long gasDifference = (Eip7667Spec.Instance.GetSha3WordCost() - Cancun.Instance.GetSha3WordCost()) - * EvmPooledMemory.Div32Ceiling((UInt256)initCode.Length); + long gasSpent = GasCostOf.Sha3WordEip7667 * EvmPooledMemory.Div32Ceiling((UInt256)initCode.Length) + 53658; - Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } [Test] - public void LogOpDifference() + public void Log_op_before_eip7667() + { + const int length = 32; + byte[] createCode = Prepare.EvmCode + .PushData(length) + .PushData(0) + .Op(Instruction.LOG0) + .STOP() + .Done; + + TestAllTracerWithOutput result = Execute((BlockNumber, Timestamp - 1), createCode); + + long gasSpent = GasCostOf.LogData * length + 21384; + + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); + } + + [Test] + public void Log_op_after_eip7667() { const int length = 32; byte[] createCode = Prepare.EvmCode @@ -111,46 +149,75 @@ public void LogOpDifference() .STOP() .Done; - TestAllTracerWithOutput resultEipDisabled = Execute((ForkActivation)1, DefaultBlockGasLimit, createCode); - Setup(); - TestAllTracerWithOutput resultEipEnabled = Execute((ForkActivation)2, DefaultBlockGasLimit, createCode); + TestAllTracerWithOutput result = Execute(createCode); - long gasDifference = (Eip7667Spec.Instance.GetLogDataCost() - Cancun.Instance.GetLogDataCost()) * length; + long gasSpent = GasCostOf.LogDataEip7667 * length + 21384; - Assert.That(resultEipEnabled.GasSpent - resultEipDisabled.GasSpent, Is.EqualTo(gasDifference)); + Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } [Test] - public void TestSha256Precompile() + public void Sha256_precompile_base_cost_before_eip_7667() { var sha256Precompile = Sha256Precompile.Instance; + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); - Assert.That(sha256Precompile.BaseGasCost(Eip7667Spec.Instance), - Is.EqualTo(Eip7667Spec.Instance.GetSha256PrecompileBaseCost())); + Assert.That(sha256Precompile.BaseGasCost(spec), Is.EqualTo(GasCostOf.Sha256PrecompileBaseCost)); + Assert.That(GasCostOf.Sha256PrecompileBaseCost, Is.EqualTo(60)); + } + + [Test] + public void Sha256_precompile_base_cost_after_eip_7667() + { + var sha256Precompile = Sha256Precompile.Instance; - Assert.That(sha256Precompile.BaseGasCost(Cancun.Instance), - Is.EqualTo(Cancun.Instance.GetSha256PrecompileBaseCost())); + Assert.That(sha256Precompile.BaseGasCost(Spec), Is.EqualTo(GasCostOf.Sha256PrecompileBaseCostEip7667)); + Assert.That(GasCostOf.Sha256PrecompileBaseCostEip7667, Is.EqualTo(300)); + } + + [Test] + public void Sha256_precompile_data_cost_before_eip_7667() + { + var sha256Precompile = Sha256Precompile.Instance; + var bytes = new byte[1]; + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); + var dataCost = GasCostOf.Sha256PrecompileWordCost * EvmPooledMemory.Div32Ceiling((ulong)bytes.Length); + Assert.That(sha256Precompile.DataGasCost(bytes, spec), Is.EqualTo(dataCost)); + Assert.That(GasCostOf.Sha256PrecompileWordCost, Is.EqualTo(12)); + } + + [Test] + public void Sha256_precompile_data_cost_after_eip_7667() + { + var sha256Precompile = Sha256Precompile.Instance; var bytes = new byte[1]; - Assert.That(sha256Precompile.DataGasCost(bytes, Eip7667Spec.Instance), - Is.EqualTo(Eip7667Spec.Instance.GetSha256PrecompileWordCost())); + var dataCost = GasCostOf.Sha256PrecompileWordCostEip7667 * EvmPooledMemory.Div32Ceiling((ulong)bytes.Length); - Assert.That(sha256Precompile.DataGasCost(bytes, Cancun.Instance), - Is.EqualTo(Cancun.Instance.GetSha256PrecompileWordCost())); + Assert.That(sha256Precompile.DataGasCost(bytes, Spec), Is.EqualTo(dataCost)); + Assert.That(GasCostOf.Sha256PrecompileWordCostEip7667, Is.EqualTo(60)); } [Test] - public void TestBlake2FPrecompile() + public void Blake2F_precompile_data_cost_before_eip_7667() { var blake2FPrecompile = Blake2FPrecompile.Instance; - + var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); var bytes = new byte[213]; bytes[3] = 1; - Assert.That(blake2FPrecompile.DataGasCost(bytes, Eip7667Spec.Instance), - Is.EqualTo(Eip7667Spec.Instance.GetBlake2GFRoundDataCost())); + Assert.That(blake2FPrecompile.DataGasCost(bytes, spec), Is.EqualTo(GasCostOf.Blake2GFRound)); + Assert.That(GasCostOf.Blake2GFRound, Is.EqualTo(1)); + } + + [Test] + public void Blake2F_precompile_data_cost_after_eip_7667() + { + var blake2FPrecompile = Blake2FPrecompile.Instance; + var bytes = new byte[213]; + bytes[3] = 1; - Assert.That(blake2FPrecompile.DataGasCost(bytes, Cancun.Instance), - Is.EqualTo(Cancun.Instance.GetBlake2GFRoundDataCost())); + Assert.That(blake2FPrecompile.DataGasCost(bytes, Spec), Is.EqualTo(GasCostOf.Blake2GFRoundEip7667)); + Assert.That(GasCostOf.Blake2GFRoundEip7667, Is.EqualTo(10)); } } diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs b/src/Nethermind/Nethermind.Specs/Forks/18_Prague.cs similarity index 71% rename from src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs rename to src/Nethermind/Nethermind.Specs/Forks/18_Prague.cs index 0f54a79c333..14ae3d673d8 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Eip7667Spec.cs +++ b/src/Nethermind/Nethermind.Specs/Forks/18_Prague.cs @@ -4,15 +4,15 @@ namespace Nethermind.Evm.Test; -public class Eip7667Spec : Cancun +public class Prague : Cancun { private static IReleaseSpec _instance; - protected Eip7667Spec() + protected Prague() { Name = "Eip7667Spec"; IsEip7667Enabled = true; } - public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Eip7667Spec()); + public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Prague()); } diff --git a/src/Nethermind/Nethermind.Specs/MainnetSpecProvider.cs b/src/Nethermind/Nethermind.Specs/MainnetSpecProvider.cs index 4108dffe7b8..e0efb2f84ff 100644 --- a/src/Nethermind/Nethermind.Specs/MainnetSpecProvider.cs +++ b/src/Nethermind/Nethermind.Specs/MainnetSpecProvider.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using Nethermind.Core.Specs; +using Nethermind.Evm.Test; using Nethermind.Int256; using Nethermind.Specs.Forks; @@ -47,7 +48,8 @@ public class MainnetSpecProvider : ISpecProvider { BlockNumber: < ParisBlockNumber } => GrayGlacier.Instance, { Timestamp: null } or { Timestamp: < ShanghaiBlockTimestamp } => Paris.Instance, { Timestamp: < CancunBlockTimestamp } => Shanghai.Instance, - _ => Cancun.Instance + { Timestamp: < PragueBlockTimestamp } => Cancun.Instance, + _ => Prague.Instance }; public void UpdateMergeTransitionInfo(long? blockNumber, UInt256? terminalTotalDifficulty = null) @@ -86,7 +88,7 @@ public void UpdateMergeTransitionInfo(long? blockNumber, UInt256? terminalTotalD (ForkActivation)GrayGlacierBlockNumber, ShanghaiActivation, CancunActivation, - //PragueActivation, + // PragueActivation, //OsakaActivation }; From 88aff4ee12996cef3cdade5421b146fa970e8bfe Mon Sep 17 00:00:00 2001 From: yerke26 Date: Thu, 25 Apr 2024 16:34:05 +0500 Subject: [PATCH 13/13] fix whitespaces --- src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs index d2332da5241..dfb7b2d061f 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/Eip7667Tests.cs @@ -17,7 +17,7 @@ public void Sha3_cost_before_eip_7667() var spec = SpecProvider.GetSpec((BlockNumber, Timestamp - 1)); Assert.That(spec.GetSha3Cost(), Is.EqualTo(30)); } - + [Test] public void Sha3_cost_after_eip_7667() { @@ -104,7 +104,7 @@ public void Create_op_before_eip_7667() Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } - + [Test] public void Create_op_after_eip_7667() { @@ -137,7 +137,7 @@ public void Log_op_before_eip7667() Assert.That(result.GasSpent, Is.EqualTo(gasSpent)); } - + [Test] public void Log_op_after_eip7667() { @@ -165,7 +165,7 @@ public void Sha256_precompile_base_cost_before_eip_7667() Assert.That(sha256Precompile.BaseGasCost(spec), Is.EqualTo(GasCostOf.Sha256PrecompileBaseCost)); Assert.That(GasCostOf.Sha256PrecompileBaseCost, Is.EqualTo(60)); } - + [Test] public void Sha256_precompile_base_cost_after_eip_7667() { @@ -174,7 +174,7 @@ public void Sha256_precompile_base_cost_after_eip_7667() Assert.That(sha256Precompile.BaseGasCost(Spec), Is.EqualTo(GasCostOf.Sha256PrecompileBaseCostEip7667)); Assert.That(GasCostOf.Sha256PrecompileBaseCostEip7667, Is.EqualTo(300)); } - + [Test] public void Sha256_precompile_data_cost_before_eip_7667() { @@ -186,7 +186,7 @@ public void Sha256_precompile_data_cost_before_eip_7667() Assert.That(sha256Precompile.DataGasCost(bytes, spec), Is.EqualTo(dataCost)); Assert.That(GasCostOf.Sha256PrecompileWordCost, Is.EqualTo(12)); } - + [Test] public void Sha256_precompile_data_cost_after_eip_7667() { @@ -209,7 +209,7 @@ public void Blake2F_precompile_data_cost_before_eip_7667() Assert.That(blake2FPrecompile.DataGasCost(bytes, spec), Is.EqualTo(GasCostOf.Blake2GFRound)); Assert.That(GasCostOf.Blake2GFRound, Is.EqualTo(1)); } - + [Test] public void Blake2F_precompile_data_cost_after_eip_7667() {