diff --git a/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleMultiBlockTest.java b/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleMultiBlockTest.java index 5e218b0eab..d65d426b09 100644 --- a/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleMultiBlockTest.java +++ b/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleMultiBlockTest.java @@ -225,7 +225,7 @@ void testWithFrameworkEntrypoint() { .address(Address.fromHexString("0x22222")) .balance(Wei.of(1000)) .nonce(6) - .code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class)) .build(); ToyAccount snippetAccount = @@ -233,7 +233,7 @@ void testWithFrameworkEntrypoint() { .address(Address.fromHexString("0x11111")) .balance(Wei.of(1000)) .nonce(7) - .code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class)) .build(); Function snippetFunction = diff --git a/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleSolidityTest.java b/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleSolidityTest.java index 53a1235ad3..5f5eedeba0 100644 --- a/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleSolidityTest.java +++ b/arithmetization/src/test/java/net/consensys/linea/zktracer/ExampleSolidityTest.java @@ -63,7 +63,7 @@ void testWithFrameworkEntrypoint() { .address(Address.fromHexString("0x22222")) .balance(Wei.ONE) .nonce(5) - .code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class)) .build(); ToyAccount snippetAccount = @@ -71,7 +71,7 @@ void testWithFrameworkEntrypoint() { .address(Address.fromHexString("0x11111")) .balance(Wei.ONE) .nonce(6) - .code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class)) .build(); Function snippetFunction = @@ -151,7 +151,7 @@ void testSnippetIndependently() { .address(Address.fromHexString("0x11111")) .balance(Wei.ONE) .nonce(6) - .code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class)) .build(); Function function = @@ -201,7 +201,7 @@ void testContractNotRelatedToTestingFramework() { .address(Address.fromHexString("0x11111")) .balance(Wei.ONE) .nonce(6) - .code(SmartContractUtils.getSolidityContractByteCode(TestStorage.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestStorage.class)) .build(); Function function = @@ -240,7 +240,7 @@ void testYul() { .address(Address.fromHexString("0x22222")) .balance(Wei.ONE) .nonce(5) - .code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class)) + .code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class)) .build(); ToyAccount yulAccount = @@ -248,7 +248,7 @@ void testYul() { .address(Address.fromHexString("0x11111")) .balance(Wei.ONE) .nonce(6) - .code(SmartContractUtils.getYulContractByteCode("DynamicBytecode.yul")) + .code(SmartContractUtils.getYulContractRuntimeByteCode("DynamicBytecode.yul")) .build(); Function yulFunction = new Function("Write", Collections.emptyList(), Collections.emptyList()); diff --git a/testing/src/main/java/net/consensys/linea/testing/SmartContractUtils.java b/testing/src/main/java/net/consensys/linea/testing/SmartContractUtils.java index 52dfec6a3c..20367da4e8 100644 --- a/testing/src/main/java/net/consensys/linea/testing/SmartContractUtils.java +++ b/testing/src/main/java/net/consensys/linea/testing/SmartContractUtils.java @@ -36,9 +36,9 @@ public class SmartContractUtils { * runtime bytecode for solidity contract. * * @param contractClass: The web3j wrapper class that is autogenerated from the solidity contract - * @return The depolyed or runtime bytecode for the solidity contract + * @return The deployed or runtime bytecode for the solidity contract */ - public static Bytes getSolidityContractByteCode(Class contractClass) { + public static Bytes getSolidityContractRuntimeByteCode(Class contractClass) { String contractResourcePath = "solidity/%s.json".formatted(contractClass.getSimpleName()); URL contractResourceURL = classLoader.getResource(contractResourcePath); try { @@ -56,15 +56,37 @@ public static Bytes getSolidityContractByteCode(Class contra throw new RuntimeException("Could not find contract bytecode"); } + /** + * @param contractClass: The web3j wrapper class that is autogenerated from the solidity contract + * @return The compiled bytecode for the solidity contract + */ + public static Bytes getSolidityContractCompiledByteCode(Class contractClass) { + String contractResourcePath = "solidity/%s.json".formatted(contractClass.getSimpleName()); + URL contractResourceURL = classLoader.getResource(contractResourcePath); + try { + JsonNode jsonRoot = objectMapper.readTree(contractResourceURL); + Iterator> contracts = jsonRoot.get("contracts").fields(); + while (contracts.hasNext()) { + Map.Entry contract = contracts.next(); + if (contract.getKey().contains(contractClass.getSimpleName())) { + return Bytes.fromHexStringLenient(contract.getValue().get("bin").asText()); + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + throw new RuntimeException("Could not find contract bytecode"); + } + /** * The yul contracts under testing/src/main/yul are compiled using the solidity compiler via the * yul plugin This method reads the output of the solidity compiler output file and returns the * deployed or runtime bytecode for yul contract. * * @param yulFileName: The yul contract filename along with .yul suffix - * @return The depolyed or runtime bytecode for the yul contract + * @return The deployed or runtime bytecode for the yul contract */ - public static Bytes getYulContractByteCode(final String yulFileName) { + public static Bytes getYulContractRuntimeByteCode(final String yulFileName) { if (!yulFileName.contains(".yul")) { throw new IllegalArgumentException("Yul file name should contain the suffix: .yul"); } @@ -84,7 +106,35 @@ public static Bytes getYulContractByteCode(final String yulFileName) { .asText(); return Bytes.fromHexStringLenient(byteCode); } catch (Exception e) { - throw new RuntimeException(e); + throw new RuntimeException("Could not find contract bytecode", e); + } + } + + /** + * @param yulFileName: The yul contract filename along with .yul suffix + * @return The compiled bytecode for the yul contract + */ + public static Bytes getYulContractCompiledByteCode(final String yulFileName) { + if (!yulFileName.contains(".yul")) { + throw new IllegalArgumentException("Yul file name should contain the suffix: .yul"); + } + String yulFileNameWithoutSuffix = yulFileName.replace(".yul", ""); + String contractResourcePath = "yul/%s.json".formatted(yulFileNameWithoutSuffix); + URL contractResourceURL = classLoader.getResource(contractResourcePath); + try { + JsonNode jsonRoot = objectMapper.readTree(contractResourceURL); + String byteCode = + jsonRoot + .get("contracts") + .get(yulFileName) + .get(yulFileNameWithoutSuffix) + .get("evm") + .get("bytecode") + .get("object") + .asText(); + return Bytes.fromHexStringLenient(byteCode); + } catch (Exception e) { + throw new RuntimeException("Could not find contract bytecode", e); } } } diff --git a/testing/src/main/resources/templates/compiler_yul.json.template b/testing/src/main/resources/templates/compiler_yul.json.template index 11c1bab0c1..a41daf4d5c 100644 --- a/testing/src/main/resources/templates/compiler_yul.json.template +++ b/testing/src/main/resources/templates/compiler_yul.json.template @@ -7,7 +7,7 @@ }, "settings": { "evmVersion": "london", - "optimizer": { "enabled": true, "details": { "yul": true } }, + "optimizer": { "enabled": false, "details": { "yul": true } }, "outputSelection": { "*": { "": ["ast"],