Skip to content

Commit

Permalink
Update EOF validation error strings (hyperledger#7487)
Browse files Browse the repository at this point in the history
Update the EOF validation error strings so that they can validate
against expected exceptions in reference tests.

Signed-off-by: Danno Ferrin <danno@numisight.com>
  • Loading branch information
shemnon authored Aug 20, 2024
1 parent 96f04a7 commit 4c0d7b5
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes32;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -81,9 +79,6 @@ public class BlockchainTestSubCommand implements Runnable {
*/
public static final String COMMAND_NAME = "block-test";

static final Supplier<ReferenceTestProtocolSchedules> referenceTestProtocolSchedules =
Suppliers.memoize(ReferenceTestProtocolSchedules::create);

@Option(
names = {"--test-name"},
description = "Limit execution to one named test.")
Expand Down Expand Up @@ -177,7 +172,7 @@ private void traceTestSpecs(final String test, final BlockchainReferenceTestCase
.orElseThrow();

final ProtocolSchedule schedule =
referenceTestProtocolSchedules.get().getByName(spec.getNetwork());
ReferenceTestProtocolSchedules.getInstance().getByName(spec.getNetwork());

final MutableBlockchain blockchain = spec.getBlockchain();
final ProtocolContext context = spec.getProtocolContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public CodeValidateSubCommand() {
Suppliers.memoize(
() -> {
ProtocolSpec protocolSpec =
ReferenceTestProtocolSchedules.create().geSpecByName(fork);
ReferenceTestProtocolSchedules.getInstance().geSpecByName(fork);
return protocolSpec.getEvm();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void run() {
fork = parentCommand.getFork();
}
ProtocolSpec protocolSpec =
ReferenceTestProtocolSchedules.create()
ReferenceTestProtocolSchedules.getInstance()
.geSpecByName(fork == null ? EvmSpecVersion.PRAGUE.getName() : fork);
evm = protocolSpec.getEvm();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void run() {
if (parentCommand.hasFork()) {
fork = parentCommand.getFork();
}
ProtocolSpec protocolSpec = ReferenceTestProtocolSchedules.create().geSpecByName(fork);
ProtocolSpec protocolSpec = ReferenceTestProtocolSchedules.getInstance().geSpecByName(fork);
EVM evm = protocolSpec.getEvm();
EOFLayout layout = evm.parseEOF(container);
if (layout.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Stopwatch;
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -90,9 +88,6 @@ public class StateTestSubCommand implements Runnable {
*/
public static final String COMMAND_NAME = "state-test";

static final Supplier<ReferenceTestProtocolSchedules> referenceTestProtocolSchedules =
Suppliers.memoize(ReferenceTestProtocolSchedules::create);

@SuppressWarnings({"FieldCanBeFinal"})
@Option(
names = {"--fork"},
Expand Down Expand Up @@ -258,7 +253,7 @@ private void traceTestSpecs(final String test, final List<GeneralStateTestCaseEi

final String forkName = fork == null ? spec.getFork() : fork;
final ProtocolSchedule protocolSchedule =
referenceTestProtocolSchedules.get().getByName(forkName);
ReferenceTestProtocolSchedules.getInstance().getByName(forkName);
if (protocolSchedule == null) {
throw new UnsupportedForkException(forkName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"CancunEOF"
],
"stdin": "",
"stdout": "EOF Code Invalid : STOP is only a valid opcode in containers used for runtime operations.\n"
"stdout": "EOF Code Invalid : incompatible_container_kind opcode STOP is only valid for runtime.\n"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public class ReferenceTestProtocolSchedules {
private static final List<String> SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS =
Arrays.asList("Frontier", "Homestead", "EIP150");

private static ReferenceTestProtocolSchedules instance;

public static ReferenceTestProtocolSchedules getInstance() {
if (instance == null) {
instance = create();
}
return instance;
}

public static ReferenceTestProtocolSchedules create() {
return create(new StubGenesisConfigOptions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@

public class TransactionTest {

private static final ReferenceTestProtocolSchedules REFERENCE_TEST_PROTOCOL_SCHEDULES =
ReferenceTestProtocolSchedules.create();

private static TransactionValidator transactionValidator(final String name) {
return REFERENCE_TEST_PROTOCOL_SCHEDULES
return ReferenceTestProtocolSchedules.getInstance()
.getByName(name)
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader())
.getTransactionValidatorFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ public static Collection<Object[]> generateTestParametersForConfig(final String[

@SuppressWarnings("java:S5960") // This is not production code, this is testing code.
public static void executeTest(
final String name,
final String fork,
final Bytes code,
final String containerKind,
final EOFTestCaseSpec.TestResult expected) {
EVM evm = ReferenceTestProtocolSchedules.create().geSpecByName(fork).getEvm();
EVM evm = ReferenceTestProtocolSchedules.getInstance().geSpecByName(fork).getEvm();
assertThat(evm).isNotNull();

// hardwire in the magic byte transaction checks
Expand All @@ -144,26 +145,32 @@ public static void executeTest(
.withFailMessage("Code did not parse to valid containerKind of " + expectedMode)
.isNotEqualTo(expectedMode);
} else {
if (expected.result()) {
assertThat(parsedCode.isValid())
.withFailMessage(
() -> "Valid code failed with " + ((CodeInvalid) parsedCode).getInvalidReason())
.isTrue();
} else {
assertThat(parsedCode.isValid())
.withFailMessage("Invalid code expected " + expected.exception() + " but was valid")
.isFalse();
if (name.contains("eip7692")) {
// if the test is from EEST, validate the exception name.
assertThat(((CodeInvalid) parsedCode).getInvalidReason())
.withFailMessage(
() ->
EOFLayout.parseEOF(code).prettyPrint()
+ "\nExpected exception :"
+ expected.exception()
+ " actual exception :"
+ (parsedCode.isValid()
"Expected exception :%s actual exception: %s"
.formatted(
expected.exception(),
(parsedCode.isValid()
? null
: ((CodeInvalid) parsedCode).getInvalidReason()))
.isEqualTo(expected.result());

if (expected.result()) {
assertThat(code)
.withFailMessage("Container round trip failed")
.isEqualTo(layout.writeContainer(null));
: ((CodeInvalid) parsedCode).getInvalidReason())))
.containsIgnoringCase(expected.exception().replace("EOFException.", ""));
}
}
}
} else {
assertThat(layout.isValid())
assertThat(false)
.withFailMessage(
() ->
"Expected exception - "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import org.assertj.core.api.Assertions;

public class BlockchainReferenceTestTools {
private static final ReferenceTestProtocolSchedules REFERENCE_TEST_PROTOCOL_SCHEDULES =
ReferenceTestProtocolSchedules.create();

private static final List<String> NETWORKS_TO_RUN;

Expand Down Expand Up @@ -114,7 +112,7 @@ public static void executeTest(final BlockchainReferenceTestCaseSpec spec) {
.orElseThrow();

final ProtocolSchedule schedule =
REFERENCE_TEST_PROTOCOL_SCHEDULES.getByName(spec.getNetwork());
ReferenceTestProtocolSchedules.getInstance().getByName(spec.getNetwork());

final MutableBlockchain blockchain = spec.getBlockchain();
final ProtocolContext context = spec.getProtocolContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import org.hyperledger.besu.testutil.JsonTestParameters;

public class GeneralStateReferenceTestTools {
private static final ReferenceTestProtocolSchedules REFERENCE_TEST_PROTOCOL_SCHEDULES =
ReferenceTestProtocolSchedules.create();
private static final List<String> SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS =
Arrays.asList("Frontier", "Homestead", "EIP150");

Expand All @@ -54,7 +52,7 @@ private static MainnetTransactionProcessor transactionProcessor(final String nam
}

private static ProtocolSpec protocolSpec(final String name) {
return REFERENCE_TEST_PROTOCOL_SCHEDULES
return ReferenceTestProtocolSchedules.getInstance()
.getByName(name)
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.hyperledger.besu.ethereum.referencetests.EOFTestCaseSpec;

import java.util.Arrays;
import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assumptions.assumeTrue;

/** The general state test operation testing framework entry point. */
public class %%TESTS_NAME%% {

Expand All @@ -38,6 +35,6 @@ public class %%TESTS_NAME%% {
final EOFTestCaseSpec.TestResult results,
final boolean runTest) {
assumeTrue(runTest, "Test " + name + " was ignored");
executeTest(fork, code, containerKind, results);
executeTest(name, fork, code, containerKind, results);
}
}
Loading

0 comments on commit 4c0d7b5

Please sign in to comment.