diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java index 8232f326bbd..b255ded6cb5 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java @@ -20,7 +20,6 @@ import static org.hyperledger.besu.ethereum.mainnet.PrivateStateUtils.KEY_TRANSACTION_HASH; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader; @@ -378,9 +377,7 @@ public TransactionProcessingResult processTransaction( .contract(contractAddress) .inputData(Bytes.EMPTY) .versionedHashes(transaction.getVersionedHashes()) - .code( - contractCreationProcessor.getCodeFromEVM( - Hash.hash(initCodeBytes), initCodeBytes)) + .code(contractCreationProcessor.getCodeFromEVM(null, initCodeBytes)) .build(); } else { @SuppressWarnings("OptionalGetWithoutIsPresent") // isContractCall tests isPresent diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionProcessor.java index e9f4fa5b798..4628ef6b151 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionProcessor.java @@ -150,9 +150,7 @@ public TransactionProcessingResult processTransaction( .address(privateContractAddress) .contract(privateContractAddress) .inputData(Bytes.EMPTY) - .code( - contractCreationProcessor.getCodeFromEVM( - Hash.hash(initCodeBytes), initCodeBytes)) + .code(contractCreationProcessor.getCodeFromEVM(null, initCodeBytes)) .build(); } else { final Address to = transaction.getTo().get(); diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/CodeValidateSubCommand.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/CodeValidateSubCommand.java index 56bef3cad5c..606b3899657 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/CodeValidateSubCommand.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/CodeValidateSubCommand.java @@ -17,7 +17,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.hyperledger.besu.evmtool.CodeValidateSubCommand.COMMAND_NAME; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.code.CodeFactory; import org.hyperledger.besu.evm.code.CodeInvalid; import org.hyperledger.besu.evm.code.EOFLayout; @@ -117,7 +116,7 @@ public String considerCode(final String hexCode) { return "err: layout - " + layout.getInvalidReason() + "\n"; } - var code = CodeFactory.createCode(codeBytes, Hash.hash(codeBytes), 1, true); + var code = CodeFactory.createCode(codeBytes, 1, true); if (!code.isValid()) { return "err: " + ((CodeInvalid) code).getInvalidReason() + "\n"; } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/EVM.java b/evm/src/main/java/org/hyperledger/besu/evm/EVM.java index 2766770da3c..660374f7614 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/EVM.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/EVM.java @@ -349,11 +349,12 @@ public Operation[] getOperationsUnsafe() { * @return the code */ public Code getCode(final Hash codeHash, final Bytes codeBytes) { - Code result = codeCache.getIfPresent(codeHash); + Code result = codeHash == null ? null : codeCache.getIfPresent(codeHash); if (result == null) { - result = - CodeFactory.createCode(codeBytes, codeHash, evmSpecVersion.getMaxEofVersion(), false); - codeCache.put(codeHash, result); + result = CodeFactory.createCode(codeBytes, evmSpecVersion.getMaxEofVersion(), false); + if (codeHash != null) { + codeCache.put(codeHash, result); + } } return result; } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeFactory.java b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeFactory.java index f1ae0285c63..23b074e4334 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeFactory.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeFactory.java @@ -16,7 +16,6 @@ package org.hyperledger.besu.evm.code; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.apache.tuweni.bytes.Bytes; @@ -35,62 +34,57 @@ private CodeFactory() { * Create Code. * * @param bytes the bytes - * @param codeHash the code hash * @param maxEofVersion the max eof version * @param inCreateOperation the in create operation * @return the code */ public static Code createCode( - final Bytes bytes, - final Hash codeHash, - final int maxEofVersion, - final boolean inCreateOperation) { + final Bytes bytes, final int maxEofVersion, final boolean inCreateOperation) { if (maxEofVersion == 0) { - return new CodeV0(bytes, codeHash); + return new CodeV0(bytes); } else if (maxEofVersion == 1) { int codeSize = bytes.size(); if (codeSize > 0 && bytes.get(0) == EOF_LEAD_BYTE) { if (codeSize == 1 && !inCreateOperation) { - return new CodeV0(bytes, codeHash); + return new CodeV0(bytes); } if (codeSize < 3) { - return new CodeInvalid(codeHash, bytes, "EOF Container too short"); + return new CodeInvalid(bytes, "EOF Container too short"); } if (bytes.get(1) != 0) { if (inCreateOperation) { // because some 0xef code made it to mainnet, this is only an error at contract create - return new CodeInvalid(codeHash, bytes, "Incorrect second byte"); + return new CodeInvalid(bytes, "Incorrect second byte"); } else { - return new CodeV0(bytes, codeHash); + return new CodeV0(bytes); } } int version = bytes.get(2); if (version != 1) { - return new CodeInvalid(codeHash, bytes, "Unsupported EOF Version: " + version); + return new CodeInvalid(bytes, "Unsupported EOF Version: " + version); } final EOFLayout layout = EOFLayout.parseEOF(bytes); if (!layout.isValid()) { - return new CodeInvalid( - codeHash, bytes, "Invalid EOF Layout: " + layout.getInvalidReason()); + return new CodeInvalid(bytes, "Invalid EOF Layout: " + layout.getInvalidReason()); } final String codeValidationError = CodeV1Validation.validateCode(layout); if (codeValidationError != null) { - return new CodeInvalid(codeHash, bytes, "EOF Code Invalid : " + codeValidationError); + return new CodeInvalid(bytes, "EOF Code Invalid : " + codeValidationError); } final String stackValidationError = CodeV1Validation.validateStack(layout); if (stackValidationError != null) { - return new CodeInvalid(codeHash, bytes, "EOF Code Invalid : " + stackValidationError); + return new CodeInvalid(bytes, "EOF Code Invalid : " + stackValidationError); } - return new CodeV1(codeHash, layout); + return new CodeV1(layout); } else { - return new CodeV0(bytes, codeHash); + return new CodeV0(bytes); } } else { - return new CodeInvalid(codeHash, bytes, "Unsupported max code version " + maxEofVersion); + return new CodeInvalid(bytes, "Unsupported max code version " + maxEofVersion); } } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeInvalid.java b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeInvalid.java index 2c13c9b5de8..c28adb1b4a4 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeInvalid.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeInvalid.java @@ -19,6 +19,9 @@ import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; +import java.util.function.Supplier; + +import com.google.common.base.Suppliers; import org.apache.tuweni.bytes.Bytes; /** @@ -27,7 +30,7 @@ */ public class CodeInvalid implements Code { - private final Hash codeHash; + private final Supplier codeHash; private final Bytes codeBytes; private final String invalidReason; @@ -35,13 +38,12 @@ public class CodeInvalid implements Code { /** * Instantiates a new Code invalid. * - * @param codeHash the code hash * @param codeBytes the code bytes * @param invalidReason the invalid reason */ - public CodeInvalid(final Hash codeHash, final Bytes codeBytes, final String invalidReason) { - this.codeHash = codeHash; + public CodeInvalid(final Bytes codeBytes, final String invalidReason) { this.codeBytes = codeBytes; + this.codeHash = Suppliers.memoize(() -> Hash.hash(codeBytes)); this.invalidReason = invalidReason; } @@ -66,7 +68,7 @@ public Bytes getBytes() { @Override public Hash getCodeHash() { - return codeHash; + return codeHash.get(); } @Override diff --git a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV0.java b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV0.java index 4ba1a463bd6..c2263b42ac6 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV0.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV0.java @@ -19,22 +19,24 @@ import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.hyperledger.besu.evm.operation.JumpDestOperation; -import org.hyperledger.besu.evm.operation.PushOperation; + +import java.util.function.Supplier; import com.google.common.base.MoreObjects; +import com.google.common.base.Suppliers; import org.apache.tuweni.bytes.Bytes; /** The CodeV0. */ public class CodeV0 implements Code { /** The constant EMPTY_CODE. */ - public static final CodeV0 EMPTY_CODE = new CodeV0(Bytes.EMPTY, Hash.EMPTY); + public static final CodeV0 EMPTY_CODE = new CodeV0(Bytes.EMPTY); /** The bytes representing the code. */ private final Bytes bytes; /** The hash of the code, needed for accessing metadata about the bytecode */ - private final Hash codeHash; + private final Supplier codeHash; /** Used to cache valid jump destinations. */ private long[] validJumpDestinations; @@ -46,11 +48,10 @@ public class CodeV0 implements Code { * Public constructor. * * @param bytes The byte representation of the code. - * @param codeHash the Hash of the bytes in the code. */ - CodeV0(final Bytes bytes, final Hash codeHash) { + CodeV0(final Bytes bytes) { this.bytes = bytes; - this.codeHash = codeHash; + this.codeHash = Suppliers.memoize(() -> Hash.hash(bytes)); this.codeSectionZero = new CodeSection(bytes.size(), 0, -1, -1, 0); } @@ -97,7 +98,7 @@ public String toString() { @Override public Hash getCodeHash() { - return codeHash; + return codeHash.get(); } @Override @@ -155,15 +156,141 @@ long[] calculateJumpDests() { int j = i & 0x3F; for (; j < max; i++, j++) { final byte operationNum = rawCode[i]; - if (operationNum == JumpDestOperation.OPCODE) { - thisEntry |= 1L << j; - } else if (operationNum > PushOperation.PUSH_BASE) { - // not needed - && operationNum <= PushOperation.PUSH_MAX - // Java quirk, all bytes are signed, and PUSH32 is 127, which is Byte.MAX_VALUE - // so we don't need to check the upper bound as it will never be violated - final int multiByteDataLen = operationNum - PushOperation.PUSH_BASE; - j += multiByteDataLen; - i += multiByteDataLen; + if (operationNum >= JumpDestOperation.OPCODE) { + switch (operationNum) { + case JumpDestOperation.OPCODE: + thisEntry |= 1L << j; + break; + case 0x60: + i += 1; + j += 1; + break; + case 0x61: + i += 2; + j += 2; + break; + case 0x62: + i += 3; + j += 3; + break; + case 0x63: + i += 4; + j += 4; + break; + case 0x64: + i += 5; + j += 5; + break; + case 0x65: + i += 6; + j += 6; + break; + case 0x66: + i += 7; + j += 7; + break; + case 0x67: + i += 8; + j += 8; + break; + case 0x68: + i += 9; + j += 9; + break; + case 0x69: + i += 10; + j += 10; + break; + case 0x6a: + i += 11; + j += 11; + break; + case 0x6b: + i += 12; + j += 12; + break; + case 0x6c: + i += 13; + j += 13; + break; + case 0x6d: + i += 14; + j += 14; + break; + case 0x6e: + i += 15; + j += 15; + break; + case 0x6f: + i += 16; + j += 16; + break; + case 0x70: + i += 17; + j += 17; + break; + case 0x71: + i += 18; + j += 18; + break; + case 0x72: + i += 19; + j += 19; + break; + case 0x73: + i += 20; + j += 20; + break; + case 0x74: + i += 21; + j += 21; + break; + case 0x75: + i += 22; + j += 22; + break; + case 0x76: + i += 23; + j += 23; + break; + case 0x77: + i += 24; + j += 24; + break; + case 0x78: + i += 25; + j += 25; + break; + case 0x79: + i += 26; + j += 26; + break; + case 0x7a: + i += 27; + j += 27; + break; + case 0x7b: + i += 28; + j += 28; + break; + case 0x7c: + i += 29; + j += 29; + break; + case 0x7d: + i += 30; + j += 30; + break; + case 0x7e: + i += 31; + j += 31; + break; + case 0x7f: + i += 32; + j += 32; + break; + default: + } } } bitmap[entryPos] = thisEntry; diff --git a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV1.java b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV1.java index da5b5682ed5..f45cab025e3 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV1.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/code/CodeV1.java @@ -22,24 +22,25 @@ import org.hyperledger.besu.evm.Code; import java.util.Objects; +import java.util.function.Supplier; +import com.google.common.base.Suppliers; import org.apache.tuweni.bytes.Bytes; /** The CodeV1. */ public class CodeV1 implements Code { - private final Hash codeHash; + private final Supplier codeHash; EOFLayout eofLayout; /** * Instantiates a new CodeV1. * - * @param codeHash the code hash * @param layout the layout */ - CodeV1(final Hash codeHash, final EOFLayout layout) { - this.codeHash = codeHash; + CodeV1(final EOFLayout layout) { this.eofLayout = layout; + this.codeHash = Suppliers.memoize(() -> Hash.hash(eofLayout.getContainer())); } @Override @@ -66,7 +67,7 @@ public Bytes getBytes() { @Override public Hash getCodeHash() { - return codeHash; + return codeHash.get(); } @Override diff --git a/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/CachedInvalidCodeRule.java b/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/CachedInvalidCodeRule.java index 983d960b6ba..34c14ec8a7a 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/CachedInvalidCodeRule.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/CachedInvalidCodeRule.java @@ -16,7 +16,6 @@ package org.hyperledger.besu.evm.contractvalidation; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.hyperledger.besu.evm.EvmSpecVersion; import org.hyperledger.besu.evm.code.CodeFactory; @@ -44,8 +43,7 @@ public CachedInvalidCodeRule(final int maxEofVersion) { @Override public Optional validate( final Bytes contractCode, final MessageFrame frame) { - final Code code = - CodeFactory.createCode(contractCode, Hash.hash(contractCode), maxEofVersion, false); + final Code code = CodeFactory.createCode(contractCode, maxEofVersion, false); if (!code.isValid()) { return Optional.of(ExceptionalHaltReason.INVALID_CODE); } else { diff --git a/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/EOFValidationCodeRule.java b/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/EOFValidationCodeRule.java index e722fe14821..3481035ee0f 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/EOFValidationCodeRule.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/contractvalidation/EOFValidationCodeRule.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.evm.contractvalidation; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.hyperledger.besu.evm.code.CodeFactory; import org.hyperledger.besu.evm.code.CodeInvalid; @@ -54,9 +53,7 @@ private EOFValidationCodeRule(final int maxEofVersion, final boolean inCreateTra @Override public Optional validate( final Bytes contractCode, final MessageFrame frame) { - Code code = - CodeFactory.createCode( - contractCode, Hash.hash(contractCode), maxEofVersion, inCreateTransaction); + Code code = CodeFactory.createCode(contractCode, maxEofVersion, inCreateTransaction); if (!code.isValid()) { LOG.trace("EOF Validation Error: {}", ((CodeInvalid) code).getInvalidReason()); return Optional.of(ExceptionalHaltReason.INVALID_CODE); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java index 032c482b438..ca28eecdb62 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java @@ -17,7 +17,6 @@ import static org.hyperledger.besu.evm.internal.Words.clampedToLong; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.evm.Code; import org.hyperledger.besu.evm.EVM; @@ -96,7 +95,9 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { return new OperationResult(cost, ExceptionalHaltReason.CODE_TOO_LARGE); } final Bytes inputData = frame.readMemory(inputOffset, inputSize); - Code code = evm.getCode(Hash.hash(inputData), inputData); + // Never cache CREATEx initcode. The amount of reuse is very low, and caching mostly + // addresses disk loading delay, and we already have the code. + Code code = evm.getCode(null, inputData); if (code.isValid() && frame.getCode().getEofVersion() <= code.getEofVersion()) { frame.decrementRemainingGas(cost); @@ -174,11 +175,7 @@ private void complete(final MessageFrame frame, final MessageFrame childFrame, f frame.setState(MessageFrame.State.CODE_EXECUTING); Code outputCode = - CodeFactory.createCode( - childFrame.getOutputData(), - Hash.hash(childFrame.getOutputData()), - evm.getMaxEOFVersion(), - true); + CodeFactory.createCode(childFrame.getOutputData(), evm.getMaxEOFVersion(), true); frame.popStackItems(getStackItemsConsumed()); if (outputCode.isValid()) { diff --git a/evm/src/test/java/org/hyperledger/besu/evm/code/CodeFactoryTest.java b/evm/src/test/java/org/hyperledger/besu/evm/code/CodeFactoryTest.java index 826bada6470..79ddeb12429 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/code/CodeFactoryTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/code/CodeFactoryTest.java @@ -17,7 +17,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.apache.tuweni.bytes.Bytes; @@ -181,7 +180,7 @@ void invalidCodeUnknownSectionId3() { } private static void invalidCode(final String str) { - Code code = CodeFactory.createCode(Bytes.fromHexString(str), Hash.EMPTY, 1, true); + Code code = CodeFactory.createCode(Bytes.fromHexString(str), 1, true); assertThat(code.isValid()).isFalse(); } } diff --git a/evm/src/test/java/org/hyperledger/besu/evm/code/CodeV0Test.java b/evm/src/test/java/org/hyperledger/besu/evm/code/CodeV0Test.java index 80a9763d83d..80256f8a54c 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/code/CodeV0Test.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/code/CodeV0Test.java @@ -65,8 +65,7 @@ public void startUp() { public void shouldReuseJumpDestMap() { final JumpOperation operation = new JumpOperation(gasCalculator); final Bytes jumpBytes = Bytes.fromHexString("0x6003565b00"); - final CodeV0 getsCached = - (CodeV0) spy(CodeFactory.createCode(jumpBytes, Hash.hash(jumpBytes), 0, false)); + final CodeV0 getsCached = (CodeV0) spy(CodeFactory.createCode(jumpBytes, 0, false)); MessageFrame frame = createJumpFrame(getsCached); OperationResult result = operation.execute(frame, evm); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/internal/CodeCacheTest.java b/evm/src/test/java/org/hyperledger/besu/evm/internal/CodeCacheTest.java index 5afed6c230e..c00c7111878 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/internal/CodeCacheTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/internal/CodeCacheTest.java @@ -17,7 +17,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.Code; import org.hyperledger.besu.evm.code.CodeFactory; import org.hyperledger.besu.evm.operation.JumpDestOperation; @@ -34,8 +33,7 @@ public void testScale() { final Bytes contractBytes = Bytes.fromHexString("0xDEAD" + op + "BEEF" + op + "B0B0" + op + "C0DE" + op + "FACE"); final CodeScale scale = new CodeScale(); - final Code contractCode = - CodeFactory.createCode(contractBytes, Hash.hash(contractBytes), 0, false); + final Code contractCode = CodeFactory.createCode(contractBytes, 0, false); final int weight = scale.weigh(contractCode.getCodeHash(), contractCode); assertThat(weight) .isEqualTo(contractCode.getCodeHash().size() + (contractBytes.size() * 9 + 7) / 8); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operations/Create2OperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operations/Create2OperationTest.java index 5a5094e34e2..8cf9edc75af 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operations/Create2OperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operations/Create2OperationTest.java @@ -155,7 +155,7 @@ public void setUp(final String sender, final String salt, final String code) { .sender(Address.fromHexString(sender)) .value(Wei.ZERO) .apparentValue(Wei.ZERO) - .code(CodeFactory.createCode(codeBytes, Hash.hash(codeBytes), 0, true)) + .code(CodeFactory.createCode(codeBytes, 0, true)) .depth(1) .completer(__ -> {}) .address(Address.fromHexString(sender)) @@ -179,10 +179,7 @@ public void setUp(final String sender, final String salt, final String code) { when(worldUpdater.getAccount(any())).thenReturn(account); when(worldUpdater.updater()).thenReturn(worldUpdater); when(evm.getCode(any(), any())) - .thenAnswer( - invocation -> - CodeFactory.createCode( - invocation.getArgument(1), invocation.getArgument(0), 0, true)); + .thenAnswer(invocation -> CodeFactory.createCode(invocation.getArgument(1), 0, true)); } @ParameterizedTest @@ -283,7 +280,7 @@ private MessageFrame testMemoryFrame( .sender(Address.fromHexString(SENDER)) .value(Wei.ZERO) .apparentValue(Wei.ZERO) - .code(CodeFactory.createCode(SIMPLE_CREATE, Hash.hash(SIMPLE_CREATE), 0, true)) + .code(CodeFactory.createCode(SIMPLE_CREATE, 0, true)) .depth(depth) .completer(__ -> {}) .address(Address.fromHexString(SENDER)) @@ -312,7 +309,7 @@ void eofV1CannotCreateLegacy() { final UInt256 memoryLength = UInt256.valueOf(SIMPLE_CREATE.size()); final MessageFrame messageFrame = new TestMessageFrameBuilder() - .code(CodeFactory.createCode(SIMPLE_EOF, Hash.hash(SIMPLE_EOF), 1, true)) + .code(CodeFactory.createCode(SIMPLE_EOF, 1, true)) .pushStackItem(Bytes.EMPTY) .pushStackItem(memoryLength) .pushStackItem(memoryOffset) @@ -337,7 +334,7 @@ void legacyCanCreateEOFv1() { final UInt256 memoryLength = UInt256.valueOf(SIMPLE_EOF.size()); final MessageFrame messageFrame = new TestMessageFrameBuilder() - .code(CodeFactory.createCode(SIMPLE_CREATE, Hash.hash(SIMPLE_CREATE), 1, true)) + .code(CodeFactory.createCode(SIMPLE_CREATE, 1, true)) .pushStackItem(Bytes.EMPTY) .pushStackItem(memoryLength) .pushStackItem(memoryOffset) diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operations/CreateOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operations/CreateOperationTest.java index 01f58a19051..09aed560504 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operations/CreateOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operations/CreateOperationTest.java @@ -247,7 +247,7 @@ void eofV1CannotCreateLegacy() { final UInt256 memoryLength = UInt256.valueOf(SIMPLE_CREATE.size()); final MessageFrame messageFrame = new TestMessageFrameBuilder() - .code(CodeFactory.createCode(SIMPLE_EOF, Hash.hash(SIMPLE_EOF), 1, true)) + .code(CodeFactory.createCode(SIMPLE_EOF, 1, true)) .pushStackItem(memoryLength) .pushStackItem(memoryOffset) .pushStackItem(Bytes.EMPTY) @@ -271,7 +271,7 @@ void legacyCanCreateEOFv1() { final UInt256 memoryLength = UInt256.valueOf(SIMPLE_EOF.size()); final MessageFrame messageFrame = new TestMessageFrameBuilder() - .code(CodeFactory.createCode(SIMPLE_CREATE, Hash.hash(SIMPLE_CREATE), 1, true)) + .code(CodeFactory.createCode(SIMPLE_CREATE, 1, true)) .pushStackItem(memoryLength) .pushStackItem(memoryOffset) .pushStackItem(Bytes.EMPTY) @@ -308,7 +308,7 @@ private MessageFrame testMemoryFrame( .sender(Address.fromHexString(SENDER)) .value(Wei.ZERO) .apparentValue(Wei.ZERO) - .code(CodeFactory.createCode(SIMPLE_CREATE, Hash.hash(SIMPLE_CREATE), 0, true)) + .code(CodeFactory.createCode(SIMPLE_CREATE, 0, true)) .depth(depth) .completer(__ -> {}) .address(Address.fromHexString(SENDER)) diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operations/JumpOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operations/JumpOperationTest.java index 9240b510d6d..3697e0cb2ec 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operations/JumpOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operations/JumpOperationTest.java @@ -17,7 +17,6 @@ import static org.assertj.core.api.Assertions.assertThat; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.EVM; import org.hyperledger.besu.evm.EvmSpecVersion; import org.hyperledger.besu.evm.code.CodeFactory; @@ -75,7 +74,7 @@ public void shouldJumpWhenLocationIsJumpDest() { final MessageFrame frame = createMessageFrameBuilder(10_000L) .pushStackItem(UInt256.fromHexString("0x03")) - .code(CodeFactory.createCode(jumpBytes, Hash.hash(jumpBytes), 0, false)) + .code(CodeFactory.createCode(jumpBytes, 0, false)) .build(); frame.setPC(CURRENT_PC); @@ -90,7 +89,7 @@ public void shouldJumpWhenLocationIsJumpDestAndAtEndOfCode() { final MessageFrame frame = createMessageFrameBuilder(10_000L) .pushStackItem(UInt256.fromHexString("0x03")) - .code(CodeFactory.createCode(jumpBytes, Hash.hash(jumpBytes), 0, false)) + .code(CodeFactory.createCode(jumpBytes, 0, false)) .build(); frame.setPC(CURRENT_PC); @@ -105,7 +104,7 @@ public void shouldHaltWithInvalidJumDestinationWhenLocationIsOutsideOfCodeRange( final MessageFrame frameDestinationGreaterThanCodeSize = createMessageFrameBuilder(100L) .pushStackItem(UInt256.fromHexString("0xFFFFFFFF")) - .code(CodeFactory.createCode(jumpBytes, Hash.hash(jumpBytes), 0, false)) + .code(CodeFactory.createCode(jumpBytes, 0, false)) .build(); frameDestinationGreaterThanCodeSize.setPC(CURRENT_PC); @@ -115,7 +114,7 @@ public void shouldHaltWithInvalidJumDestinationWhenLocationIsOutsideOfCodeRange( final MessageFrame frameDestinationEqualsToCodeSize = createMessageFrameBuilder(100L) .pushStackItem(UInt256.fromHexString("0x04")) - .code(CodeFactory.createCode(badJump, Hash.hash(badJump), 0, false)) + .code(CodeFactory.createCode(badJump, 0, false)) .build(); frameDestinationEqualsToCodeSize.setPC(CURRENT_PC); @@ -133,7 +132,7 @@ public void longContractsValidate() { final MessageFrame longContract = createMessageFrameBuilder(100L) .pushStackItem(UInt256.fromHexString("0x12c")) - .code(CodeFactory.createCode(longCode, Hash.hash(longCode), 0, false)) + .code(CodeFactory.createCode(longCode, 0, false)) .build(); longContract.setPC(255); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/processor/ContractCreationProcessorTest.java b/evm/src/test/java/org/hyperledger/besu/evm/processor/ContractCreationProcessorTest.java index c163c5ec260..b941b650c58 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/processor/ContractCreationProcessorTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/processor/ContractCreationProcessorTest.java @@ -19,7 +19,6 @@ import static org.hyperledger.besu.evm.frame.MessageFrame.State.EXCEPTIONAL_HALT; import static org.mockito.Mockito.when; -import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.evm.EVM; import org.hyperledger.besu.evm.EvmSpecVersion; import org.hyperledger.besu.evm.code.CodeFactory; @@ -185,9 +184,7 @@ public void eofValidationShouldPreventLegacyCodeDeployment() { Bytes.fromHexString( "0xEF000101000C020003000b000200080300000000000002020100020100000260016002b00001b00002b101b160005360106000f3"); final MessageFrame messageFrame = - new TestMessageFrameBuilder() - .code(CodeFactory.createCode(initCode, Hash.hash(initCode), 1, true)) - .build(); + new TestMessageFrameBuilder().code(CodeFactory.createCode(initCode, 1, true)).build(); messageFrame.setOutputData(contractCode); messageFrame.setGasRemaining(100L); @@ -255,11 +252,7 @@ public void shouldThrowAnExceptionWhenDeployingInvalidContract() { final MessageFrame messageFrame = new TestMessageFrameBuilder() .code( - CodeFactory.createCode( - contractCreateCode, - Hash.hash(contractCreateCode), - evmSpecVersion.getMaxEofVersion(), - true)) + CodeFactory.createCode(contractCreateCode, evmSpecVersion.getMaxEofVersion(), true)) .build(); messageFrame.setOutputData(Bytes.fromHexString("0xef00010100010060"));