From e5872a5664e04413dd67cab758cec0ff79b430e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20L=C3=B3pez=20Le=C3=B3n?= Date: Thu, 1 Dec 2022 18:43:41 -0300 Subject: [PATCH] [Shandong] EIP-3860 support (#4726) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add Shandong gas calculator to support EIP-3860 Signed-off-by: Diego López León Signed-off-by: Diego López León * add unit tests for Shandong gas calculator Signed-off-by: lukelee-sl Signed-off-by: Diego López León Signed-off-by: Diego López León Signed-off-by: lukelee-sl Co-authored-by: lukelee-sl --- .../mainnet/MainnetProtocolSpecs.java | 6 +- .../vm/operations/CreateOperationTest.java | 60 +++++++++++++++++++ .../gascalculator/ShandongGasCalculator.java | 47 +++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 evm/src/main/java/org/hyperledger/besu/evm/gascalculator/ShandongGasCalculator.java diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecs.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecs.java index 8e19696c5c..495406e35d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecs.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecs.java @@ -50,6 +50,7 @@ import org.hyperledger.besu.evm.gascalculator.HomesteadGasCalculator; import org.hyperledger.besu.evm.gascalculator.IstanbulGasCalculator; import org.hyperledger.besu.evm.gascalculator.LondonGasCalculator; import org.hyperledger.besu.evm.gascalculator.PetersburgGasCalculator; +import org.hyperledger.besu.evm.gascalculator.ShandongGasCalculator; import org.hyperledger.besu.evm.gascalculator.SpuriousDragonGasCalculator; import org.hyperledger.besu.evm.gascalculator.TangerineWhistleGasCalculator; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -79,6 +80,7 @@ public abstract class MainnetProtocolSpecs { public static final int FRONTIER_CONTRACT_SIZE_LIMIT = Integer.MAX_VALUE; public static final int SPURIOUS_DRAGON_CONTRACT_SIZE_LIMIT = 24576; + public static final int SHANDONG_CONTRACT_SIZE_LIMIT = 2 * SPURIOUS_DRAGON_CONTRACT_SIZE_LIMIT; private static final Address RIPEMD160_PRECOMPILE = Address.fromHexString("0x0000000000000000000000000000000000000003"); @@ -649,8 +651,7 @@ public abstract class MainnetProtocolSpecs { genesisConfigOptions.isZeroBaseFee() ? FeeMarket.zeroBaseFee(londonForkBlockNumber) : FeeMarket.london(londonForkBlockNumber, genesisConfigOptions.getBaseFeePerGas()); - final int contractSizeLimit = - configContractSizeLimit.orElse(SPURIOUS_DRAGON_CONTRACT_SIZE_LIMIT); + final int contractSizeLimit = configContractSizeLimit.orElse(SHANDONG_CONTRACT_SIZE_LIMIT); return parisDefinition( chainId, @@ -660,6 +661,7 @@ public abstract class MainnetProtocolSpecs { genesisConfigOptions, quorumCompatibilityMode, evmConfiguration) + .gasCalculator(ShandongGasCalculator::new) .evmBuilder( (gasCalculator, jdCacheConfig) -> MainnetEVMs.shandong( diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/operations/CreateOperationTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/operations/CreateOperationTest.java index 217dbfbc2c..2eb2f4a2b2 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/operations/CreateOperationTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/operations/CreateOperationTest.java @@ -16,6 +16,8 @@ package org.hyperledger.besu.ethereum.vm.operations; import static org.assertj.core.api.Assertions.assertThat; +import static org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSpecs.SHANDONG_CONTRACT_SIZE_LIMIT; +import static org.hyperledger.besu.evm.MainnetEVMs.DEV_NET_CHAIN_ID; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -71,6 +73,7 @@ public class CreateOperationTest { + "F3" // RETURN ); public static final String SENDER = "0xdeadc0de00000000000000000000000000000000"; + private static final int SHANDONG_CREATE_GAS = 41240; @Test public void createFromMemoryMutationSafe() { @@ -175,6 +178,63 @@ public class CreateOperationTest { assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO); } + @Test + public void shandongMaxInitCodeSizeCreate() { + final UInt256 memoryOffset = UInt256.fromHexString("0xFF"); + final UInt256 memoryLength = UInt256.valueOf(SHANDONG_CONTRACT_SIZE_LIMIT); + final ArrayDeque messageFrameStack = new ArrayDeque<>(); + final MessageFrame messageFrame = + testMemoryFrame(memoryOffset, memoryLength, UInt256.ZERO, 1, messageFrameStack); + + when(account.getMutable()).thenReturn(mutableAccount); + when(account.getNonce()).thenReturn(55L); + when(mutableAccount.getBalance()).thenReturn(Wei.ZERO); + when(worldUpdater.getAccount(any())).thenReturn(account); + when(worldUpdater.get(any())).thenReturn(account); + when(worldUpdater.getSenderAccount(any())).thenReturn(account); + when(worldUpdater.getOrCreate(any())).thenReturn(newAccount); + when(newAccount.getMutable()).thenReturn(newMutableAccount); + when(newMutableAccount.getCode()).thenReturn(Bytes.EMPTY); + when(worldUpdater.updater()).thenReturn(worldUpdater); + + final EVM evm = MainnetEVMs.shandong(DEV_NET_CHAIN_ID, EvmConfiguration.DEFAULT); + var result = operation.execute(messageFrame, evm); + final MessageFrame createFrame = messageFrameStack.peek(); + final ContractCreationProcessor ccp = + new ContractCreationProcessor(evm.getGasCalculator(), evm, false, List.of(), 0, List.of()); + ccp.process(createFrame, OperationTracer.NO_TRACING); + + final Log log = createFrame.getLogs().get(0); + final String calculatedTopic = log.getTopics().get(0).toUnprefixedHexString(); + assertThat(calculatedTopic).isEqualTo(TOPIC); + assertThat(result.getGasCost()).isEqualTo(SHANDONG_CREATE_GAS); + } + + @Test + public void shandongMaxInitCodeSizePlus1Create() { + final UInt256 memoryOffset = UInt256.fromHexString("0xFF"); + final UInt256 memoryLength = UInt256.valueOf(SHANDONG_CONTRACT_SIZE_LIMIT + 1); + final ArrayDeque messageFrameStack = new ArrayDeque<>(); + final MessageFrame messageFrame = + testMemoryFrame(memoryOffset, memoryLength, UInt256.ZERO, 1, messageFrameStack); + + when(account.getMutable()).thenReturn(mutableAccount); + when(account.getNonce()).thenReturn(55L); + when(mutableAccount.getBalance()).thenReturn(Wei.ZERO); + when(worldUpdater.getAccount(any())).thenReturn(account); + when(worldUpdater.get(any())).thenReturn(account); + when(worldUpdater.getSenderAccount(any())).thenReturn(account); + when(worldUpdater.getOrCreate(any())).thenReturn(newAccount); + when(newAccount.getMutable()).thenReturn(newMutableAccount); + when(newMutableAccount.getCode()).thenReturn(Bytes.EMPTY); + when(worldUpdater.updater()).thenReturn(worldUpdater); + + final EVM evm = MainnetEVMs.shandong(DEV_NET_CHAIN_ID, EvmConfiguration.DEFAULT); + var result = operation.execute(messageFrame, evm); + assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO); + assertThat(result.getGasCost()).isEqualTo(SHANDONG_CREATE_GAS); + } + @NotNull private MessageFrame testMemoryFrame( final UInt256 memoryOffset, diff --git a/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/ShandongGasCalculator.java b/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/ShandongGasCalculator.java new file mode 100644 index 0000000000..bc04807b66 --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/gascalculator/ShandongGasCalculator.java @@ -0,0 +1,47 @@ +package org.hyperledger.besu.evm.gascalculator; +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +import static org.hyperledger.besu.evm.internal.Words.clampedAdd; +import static org.hyperledger.besu.evm.internal.Words.clampedToLong; + +import org.hyperledger.besu.evm.frame.MessageFrame; + +import org.apache.tuweni.bytes.Bytes; + +public class ShandongGasCalculator extends LondonGasCalculator { + + private static final long INIT_CODE_COST = 2L; + + @Override + public long transactionIntrinsicGasCost(final Bytes payload, final boolean isContractCreation) { + long intrinsicGasCost = super.transactionIntrinsicGasCost(payload, isContractCreation); + if (isContractCreation) { + return clampedAdd(intrinsicGasCost, calculateInitGasCost(payload.size())); + } else { + return intrinsicGasCost; + } + } + + @Override + public long createOperationGasCost(final MessageFrame frame) { + final long initCodeLength = clampedToLong(frame.getStackItem(2)); + return clampedAdd(super.createOperationGasCost(frame), calculateInitGasCost(initCodeLength)); + } + + private static long calculateInitGasCost(final long initCodeLength) { + final int dataLength = (int) Math.ceil(initCodeLength / 32.0); + return dataLength * INIT_CODE_COST; + } +}