-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6014c14
commit cdb9672
Showing
12 changed files
with
307 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/net/consensys/linea/zktracer/AbstractBaseModuleTracerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* 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 | ||
*/ | ||
package net.consensys.linea.zktracer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.hyperledger.besu.evm.frame.MessageFrame; | ||
import org.hyperledger.besu.evm.operation.Operation; | ||
|
||
import java.util.List; | ||
|
||
import net.consensys.linea.zktracer.corset.CorsetValidator; | ||
import net.consensys.linea.zktracer.module.ModuleTracer; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
|
||
@TestInstance(Lifecycle.PER_CLASS) | ||
public abstract class AbstractBaseModuleTracerTest { | ||
private ZkTracer zkTracer; | ||
private ZkTraceBuilder zkTraceBuilder; | ||
MessageFrame mockFrame; | ||
Operation mockOperation; | ||
static ModuleTracer moduleTracer; | ||
|
||
protected void runTest(OpCode opCode, final Bytes32 arg1, Bytes32 arg2) { | ||
String trace = generateTrace(opCode, List.of(arg1, arg2)); | ||
assertThat(CorsetValidator.isValid(trace)).isTrue(); | ||
} | ||
|
||
protected String generateTrace(OpCode opCode, List<Bytes32> arguments) { | ||
when(mockOperation.getOpcode()).thenReturn((int) opCode.value); | ||
for (int i = 0; i < arguments.size(); i++) { | ||
when(mockFrame.getStackItem(i)).thenReturn(arguments.get(i)); | ||
} | ||
zkTracer.tracePreExecution(mockFrame); | ||
return zkTraceBuilder.build().toJson(); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
zkTraceBuilder = new ZkTraceBuilder(); | ||
moduleTracer = getModuleTracer(); | ||
zkTracer = new ZkTracer(zkTraceBuilder, List.of(moduleTracer)); | ||
mockFrame = mock(MessageFrame.class); | ||
mockOperation = mock(Operation.class); | ||
when(mockFrame.getCurrentOperation()).thenReturn(mockOperation); | ||
} | ||
|
||
protected abstract ModuleTracer getModuleTracer(); | ||
} |
110 changes: 110 additions & 0 deletions
110
src/test/java/net/consensys/linea/zktracer/AbstractModuleTracerBySpecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* 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 | ||
*/ | ||
package net.consensys.linea.zktracer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.util.Preconditions.checkState; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@TestInstance(Lifecycle.PER_CLASS) | ||
public abstract class AbstractModuleTracerBySpecTest extends AbstractBaseModuleTracerTest { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
@ParameterizedTest(name = "{index} {0}") | ||
@MethodSource("specs") | ||
public void traceWithSpecFile(final String ignored, URL specUrl) throws IOException { | ||
traceCall(specUrl); | ||
} | ||
|
||
private void traceCall(final URL specFile) throws IOException { | ||
InputStream inputStream = specFile.openStream(); | ||
final ObjectNode specNode = (ObjectNode) MAPPER.readTree(inputStream); | ||
final JsonNode request = specNode.get("input"); | ||
final JsonNode expectedTrace = specNode.get("output"); | ||
final JsonNode actualTrace = generateTrace(getModuleTracer().jsonKey(), request); | ||
|
||
assertThat(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(actualTrace)) | ||
.isEqualTo(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(expectedTrace)); | ||
} | ||
|
||
private JsonNode generateTrace(String moduleName, JsonNode jsonNodeParams) | ||
throws JsonProcessingException { | ||
OpCode opcode = OpCode.valueOf(jsonNodeParams.get("opcode").asText()); | ||
List<Bytes32> arguments = new ArrayList<>(); | ||
JsonNode arg = jsonNodeParams.get("params"); | ||
arg.forEach(bytes -> arguments.add(Bytes32.fromHexString(bytes.asText()))); | ||
String trace = generateTrace(opcode, arguments); | ||
return MAPPER.readTree(trace).get(moduleName); | ||
} | ||
|
||
public static Object[][] findSpecFiles(final String[] subDirectoryPaths) { | ||
final List<Object[]> specFiles = new ArrayList<>(); | ||
for (final String path : subDirectoryPaths) { | ||
final URL url = AbstractModuleTracerBySpecTest.class.getResource(path); | ||
checkState(url != null, "Cannot find test directory " + path); | ||
final Path dir; | ||
try { | ||
dir = Paths.get(url.toURI()); | ||
} catch (final URISyntaxException e) { | ||
throw new RuntimeException("Problem converting URL to URI " + url, e); | ||
} | ||
try (final Stream<Path> s = Files.walk(dir, 1)) { | ||
s.map(Path::toFile) | ||
.filter(f -> f.getPath().endsWith(".json")) | ||
.map(AbstractModuleTracerBySpecTest::fileToParams) | ||
.forEach(specFiles::add); | ||
} catch (final IOException e) { | ||
throw new RuntimeException("Problem reading directory " + dir, e); | ||
} | ||
} | ||
final Object[][] result = new Object[specFiles.size()][2]; | ||
for (int i = 0; i < specFiles.size(); i++) { | ||
result[i] = specFiles.get(i); | ||
} | ||
return result; | ||
} | ||
|
||
private static Object[] fileToParams(final File file) { | ||
try { | ||
final String fileName = file.toPath().getFileName().toString(); | ||
final URL fileURL = file.toURI().toURL(); | ||
return new Object[] {fileName, fileURL}; | ||
} catch (final MalformedURLException e) { | ||
throw new RuntimeException("Problem reading spec file " + file.getAbsolutePath(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.