Skip to content

Commit

Permalink
Implement test by file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Trintinalia committed Apr 24, 2023
1 parent 6014c14 commit cdb9672
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 53 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ allprojects {

implementation "org.hyperledger.besu:plugin-api:$besuVersion"

compileOnly 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.core:jackson-databind'

errorprone("com.google.errorprone:error_prone_core")

Expand All @@ -113,7 +113,7 @@ allprojects {
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'

compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'

implementation 'org.apache.tuweni:tuweni-bytes:2.3.1'
implementation 'org.apache.tuweni:tuweni-units:2.3.1'
Expand Down
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();
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,25 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.Operation;
package net.consensys.linea.zktracer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

import net.consensys.linea.CorsetValidator;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.ZkTraceBuilder;
import net.consensys.linea.zktracer.ZkTracer;
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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;

@TestInstance(Lifecycle.PER_CLASS)
public abstract class AbstractModuleTracerTest {
public abstract class AbstractModuleTracerCorsetTest extends AbstractBaseModuleTracerTest {
static final Random rand = new Random();
private static final int TEST_REPETITIONS = 8;

private ZkTracer zkTracer;
private ZkTraceBuilder zkTraceBuilder;
@Mock MessageFrame mockFrame;
@Mock Operation mockOperation;
static ModuleTracer moduleTracer;

@ParameterizedTest()
@MethodSource("provideRandomArguments")
void randomArgumentsTest(OpCode opCode, final Bytes32 arg1, Bytes32 arg2) {
Expand All @@ -61,22 +43,6 @@ void nonRandomArgumentsTest(OpCode opCode, final Bytes32 arg1, Bytes32 arg2) {
runTest(opCode, arg1, arg2);
}

protected void runTest(OpCode opCode, final Bytes32 arg1, Bytes32 arg2) {
when(mockOperation.getOpcode()).thenReturn((int) opCode.value);
when(mockFrame.getStackItem(0)).thenReturn(arg1);
when(mockFrame.getStackItem(1)).thenReturn(arg2);
zkTracer.tracePreExecution(mockFrame);
assertThat(CorsetValidator.isValid(zkTraceBuilder.build().toJson())).isTrue();
}

@BeforeEach
void setUp() {
zkTraceBuilder = new ZkTraceBuilder();
moduleTracer = getModuleTracer();
zkTracer = new ZkTracer(zkTraceBuilder, List.of(moduleTracer));
when(mockFrame.getCurrentOperation()).thenReturn(mockOperation);
}

protected abstract Stream<Arguments> provideNonRandomArguments();

public Stream<Arguments> provideRandomArguments() {
Expand All @@ -89,9 +55,7 @@ public Stream<Arguments> provideRandomArguments() {
return arguments.stream();
}

protected abstract ModuleTracer getModuleTracer();

protected OpCode getRandomSupportedOpcode() {
public OpCode getRandomSupportedOpcode() {
var supportedOpCodes = getModuleTracer().supportedOpCodes();
int index = rand.nextInt(supportedOpCodes.size());
return supportedOpCodes.get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea;
package net.consensys.linea.zktracer.corset;

import static java.nio.file.StandardOpenOption.WRITE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.add;
package net.consensys.linea.zktracer.corset.module.alu.add;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.AbstractModuleTracerTest;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.add.AddTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand All @@ -32,7 +33,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AddTracerTest extends AbstractModuleTracerTest {
class AddTracerTest extends AbstractModuleTracerCorsetTest {
private static final Random rand = new Random();

private static final int TEST_ADD_REPETITIONS = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.add;
package net.consensys.linea.zktracer.corset.module.alu.add;

import static org.assertj.core.api.Assertions.assertThat;

import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.bytestheta.BaseBytes;
import net.consensys.linea.zktracer.module.alu.add.Adder;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.mod;
package net.consensys.linea.zktracer.corset.module.alu.mod;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.AbstractModuleTracerTest;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.mod.ModTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand All @@ -33,7 +34,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ModTracerTest extends AbstractModuleTracerTest {
class ModTracerTest extends AbstractModuleTracerCorsetTest {
static final Random rand = new Random();

final int TEST_MOD_REPETITIONS = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.shf;
package net.consensys.linea.zktracer.corset.module.shf;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;
Expand All @@ -24,10 +24,11 @@
import java.util.Random;
import java.util.stream.Stream;

import net.consensys.linea.CorsetValidator;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.ZkTraceBuilder;
import net.consensys.linea.zktracer.ZkTracer;
import net.consensys.linea.zktracer.corset.CorsetValidator;
import net.consensys.linea.zktracer.module.shf.ShfTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Loading

0 comments on commit cdb9672

Please sign in to comment.