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

Plumb maxCode overriddes into EVM #7557

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ static ProtocolSpecBuilder shanghaiDefinition(
TransactionType.FRONTIER,
TransactionType.ACCESS_LIST,
TransactionType.EIP1559),
evm.getEvmVersion().getMaxInitcodeSize()))
evm.getMaxInitcodeSize()))
.withdrawalsProcessor(new WithdrawalsProcessor())
.withdrawalsValidator(new WithdrawalsValidator.AllowedWithdrawals())
.name("Shanghai");
Expand Down Expand Up @@ -730,7 +730,7 @@ static ProtocolSpecBuilder cancunDefinition(
TransactionType.ACCESS_LIST,
TransactionType.EIP1559,
TransactionType.BLOB),
evm.getEvmVersion().getMaxInitcodeSize()))
evm.getMaxInitcodeSize()))
.precompileContractRegistryBuilder(MainnetPrecompiledContractRegistries::cancun)
.blockHeaderValidatorBuilder(MainnetBlockHeaderValidator::cancunBlockHeaderValidator)
.blockHashProcessor(new CancunBlockHashProcessor())
Expand Down Expand Up @@ -814,7 +814,7 @@ static ProtocolSpecBuilder pragueDefinition(
TransactionType.EIP1559,
TransactionType.BLOB,
TransactionType.SET_CODE),
evm.getEvmVersion().getMaxInitcodeSize()))
evm.getMaxInitcodeSize()))

// EIP-2935 Blockhash processor
.blockHashProcessor(new PragueBlockHashProcessor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void executeTest(
// hardwire in the magic byte transaction checks
if (evm.getMaxEOFVersion() < 1) {
assertThat(expected.exception()).isEqualTo("EOF_InvalidCode");
} else if (code.size() > evm.getEvmVersion().getMaxInitcodeSize()) {
} else if (code.size() > evm.getMaxInitcodeSize()) {
// this check is in EOFCREATE and Transaction validator, but unit tests sniff it out.
assertThat(false)
.withFailMessage(
Expand Down
18 changes: 18 additions & 0 deletions evm/src/main/java/org/hyperledger/besu/evm/EVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ public int getMaxEOFVersion() {
return evmSpecVersion.maxEofVersion;
}

/**
* Gets the max code size, taking configuration and version into account
*
* @return The max code size override, if not set the max code size for the EVM version.
*/
public int getMaxCodeSize() {
return evmConfiguration.maxCodeSizeOverride().orElse(evmSpecVersion.maxCodeSize);
}

/**
* Gets the max initcode Size, taking configuration and version into account
*
* @return The max initcode size override, if not set the max initcode size for the EVM version.
*/
public int getMaxInitcodeSize() {
return evmConfiguration.maxInitcodeSizeOverride().orElse(evmSpecVersion.maxInitcodeSize);
}

/**
* Returns the non-fork related configuration parameters of the EVM.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.evm.EvmSpecVersion;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.EvmConfiguration;

import java.util.Optional;

Expand Down Expand Up @@ -75,16 +76,19 @@ public static ContractValidationRule of(final int maxCodeSize) {
* @return the contract validation rule
*/
public static ContractValidationRule from(final EVM evm) {
return from(evm.getEvmVersion());
return from(evm.getEvmVersion(), evm.getEvmConfiguration());
}

/**
* Fluent MaxCodeSizeRule from the EVM it is working with.
*
* @param evmspec The evm spec version to get the size rules from.
* @param evmConfiguration The evm configuration, including overrides
* @return the contract validation rule
*/
public static ContractValidationRule from(final EvmSpecVersion evmspec) {
return new MaxCodeSizeRule(evmspec.getMaxCodeSize());
public static ContractValidationRule from(
final EvmSpecVersion evmspec, final EvmConfiguration evmConfiguration) {
return new MaxCodeSizeRule(
evmConfiguration.maxCodeSizeOverride().orElse(evmspec.getMaxCodeSize()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public class EVMExecutor {
private OperationTracer tracer = OperationTracer.NO_TRACING;
private boolean requireDeposit = true;
private List<ContractValidationRule> contractValidationRules =
List.of(MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON), PrefixCodeRule.of());
List.of(
MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON, EvmConfiguration.DEFAULT),
PrefixCodeRule.of());
private long initialNonce = 1;
private Collection<Address> forceCommitAddresses = List.of(Address.fromHexString("0x03"));
private Set<Address> accessListWarmAddresses = new BytesTrieSet<>(Address.SIZE);
Expand Down Expand Up @@ -241,8 +243,7 @@ public static EVMExecutor spuriousDragon(final EvmConfiguration evmConfiguration
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.spuriousDragon(evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.frontier(executor.evm.getGasCalculator());
executor.contractValidationRules =
List.of(MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand All @@ -256,7 +257,7 @@ public static EVMExecutor byzantium(final EvmConfiguration evmConfiguration) {
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.byzantium(evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.byzantium(executor.evm.getGasCalculator());
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(EvmSpecVersion.BYZANTIUM));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand All @@ -270,7 +271,7 @@ public static EVMExecutor constantinople(final EvmConfiguration evmConfiguration
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.constantinople(evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.byzantium(executor.evm.getGasCalculator());
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(EvmSpecVersion.CONSTANTINOPLE));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand All @@ -284,7 +285,7 @@ public static EVMExecutor petersburg(final EvmConfiguration evmConfiguration) {
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.petersburg(evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.byzantium(executor.evm.getGasCalculator());
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(EvmSpecVersion.PETERSBURG));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand Down Expand Up @@ -319,7 +320,7 @@ public static EVMExecutor istanbul(
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.istanbul(chainId, evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.istanbul(executor.evm.getGasCalculator());
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(EvmSpecVersion.ISTANBUL));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand Down Expand Up @@ -354,7 +355,7 @@ public static EVMExecutor berlin(
final EVMExecutor executor = new EVMExecutor(MainnetEVMs.berlin(chainId, evmConfiguration));
executor.precompileContractRegistry =
MainnetPrecompiledContracts.istanbul(executor.evm.getGasCalculator());
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(EvmSpecVersion.BERLIN));
executor.contractValidationRules = List.of(MaxCodeSizeRule.from(executor.evm));
return executor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

Code code = codeSupplier.get();

if (code != null && code.getSize() > evm.getEvmVersion().getMaxInitcodeSize()) {
if (code != null && code.getSize() > evm.getMaxInitcodeSize()) {
frame.popStackItems(getStackItemsConsumed());
return new OperationResult(cost, ExceptionalHaltReason.CODE_TOO_LARGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
}

Bytes auxData = frame.readMemory(from, length);
if (code.getDataSize() + auxData.size() > evm.getEvmVersion().getMaxCodeSize()) {
if (code.getDataSize() + auxData.size() > evm.getMaxCodeSize()) {
return new OperationResult(cost, ExceptionalHaltReason.CODE_TOO_LARGE);
}
if (code.getDataSize() + auxData.size() < code.getDeclaredDataSize()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ void shouldThrowAnExceptionWhenCodeContractTooLarge() {
new ContractCreationProcessor(
evm,
true,
Collections.singletonList(MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON)),
Collections.singletonList(
MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON, EvmConfiguration.DEFAULT)),
1,
Collections.emptyList());
final Bytes contractCode =
Expand All @@ -227,7 +228,8 @@ void shouldNotThrowAnExceptionWhenCodeContractTooLarge() {
new ContractCreationProcessor(
evm,
true,
Collections.singletonList(MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON)),
Collections.singletonList(
MaxCodeSizeRule.from(EvmSpecVersion.SPURIOUS_DRAGON, EvmConfiguration.DEFAULT)),
1,
Collections.emptyList());
final Bytes contractCode =
Expand Down
Loading