-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Justin Florentine <justin+github@florentine.us>
- Loading branch information
Showing
6 changed files
with
134 additions
and
46 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
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
87 changes: 87 additions & 0 deletions
87
...src/test/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompileContractTest.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,87 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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 org.hyperledger.besu.evm.precompile; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.hyperledger.besu.evm.frame.MessageFrame; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class KZGPointEvalPrecompileContractTest { | ||
private static KZGPointEvalPrecompiledContract contract; | ||
private final MessageFrame toRun = mock(MessageFrame.class); | ||
|
||
@BeforeAll | ||
public static void init() { | ||
KZGPointEvalPrecompiledContract.init("foo"); | ||
contract = new KZGPointEvalPrecompiledContract(); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() { | ||
contract.tearDown(); | ||
} | ||
|
||
@ParameterizedTest(name = "{index}") | ||
@MethodSource("getPointEvaluationPrecompileTestVectors") | ||
public void testComputePrecompile(final PrecompileTestParameters parameters) { | ||
PrecompiledContract.PrecompileContractResult result = | ||
contract.computePrecompile(parameters.input, toRun); | ||
if (parameters.valid) { | ||
assertThat(result.getState()).isEqualTo(MessageFrame.State.COMPLETED_SUCCESS); | ||
assertThat(result.getOutput()).isEqualTo(parameters.returnValue); | ||
} else { | ||
assertThat(result.getState()).isNotEqualTo(MessageFrame.State.COMPLETED_SUCCESS); | ||
} | ||
} | ||
|
||
public static List<PrecompileTestParameters> getPointEvaluationPrecompileTestVectors() | ||
throws IOException { | ||
final JsonNode jsonNode; | ||
try (final InputStream testVectors = | ||
KZGPointEvalPrecompileContractTest.class.getResourceAsStream( | ||
"pointEvaluationPrecompile.json")) { | ||
jsonNode = new ObjectMapper().readTree(testVectors); | ||
} | ||
final ArrayNode testCases = (ArrayNode) jsonNode.get("TestCases"); | ||
final Bytes returnValue = Bytes.fromHexString(jsonNode.get("PrecompileReturnValue").asText()); | ||
return IntStream.range(0, testCases.size()) | ||
.mapToObj( | ||
i -> { | ||
final JsonNode testCase = testCases.get(i); | ||
final Bytes input = Bytes.fromHexString(testCase.get("Input").asText()); | ||
final boolean valid = testCase.get("Valid").asBoolean(); | ||
return new PrecompileTestParameters(input, valid, returnValue); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
record PrecompileTestParameters(Bytes input, boolean valid, Bytes returnValue) {} | ||
} |
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