Skip to content

Commit

Permalink
Increase scope of reference tests (hyperledger#6287)
Browse files Browse the repository at this point in the history
Add new forks to transaction tests, add bad rlp to rlp ref tests.

Signed-off-by: Danno Ferrin <[email protected]>
  • Loading branch information
shemnon authored and gfukushima committed Dec 15, 2023
1 parent 649747f commit 081300f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason;
import org.hyperledger.besu.evm.gascalculator.BerlinGasCalculator;
import org.hyperledger.besu.evm.gascalculator.ByzantiumGasCalculator;
import org.hyperledger.besu.evm.gascalculator.CancunGasCalculator;
import org.hyperledger.besu.evm.gascalculator.ConstantinopleGasCalculator;
import org.hyperledger.besu.evm.gascalculator.FrontierGasCalculator;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.gascalculator.HomesteadGasCalculator;
import org.hyperledger.besu.evm.gascalculator.IstanbulGasCalculator;
import org.hyperledger.besu.evm.gascalculator.LondonGasCalculator;
import org.hyperledger.besu.evm.gascalculator.PetersburgGasCalculator;
import org.hyperledger.besu.evm.gascalculator.ShanghaiGasCalculator;
import org.hyperledger.besu.evm.gascalculator.SpuriousDragonGasCalculator;
import org.hyperledger.besu.evm.gascalculator.TangerineWhistleGasCalculator;
import org.hyperledger.besu.testutil.JsonTestParameters;
Expand All @@ -54,15 +56,18 @@ private static TransactionValidator transactionValidator(final String name) {
return REFERENCE_TEST_PROTOCOL_SCHEDULES
.getByName(name)
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader())
.getTransactionValidatorFactory().get();
.getTransactionValidatorFactory()
.get();
}

private static final String TEST_CONFIG_FILE_DIR_PATH = "TransactionTests/";

public static Stream<Arguments> getTestParametersForConfig() {
return JsonTestParameters.create(TransactionTestCaseSpec.class)
.generator((name, fullPath, spec, collector) -> collector.add(name, fullPath, spec, true))
.generate(TEST_CONFIG_FILE_DIR_PATH).stream().map(params -> Arguments.of(params[0], params[1]));
.generate(TEST_CONFIG_FILE_DIR_PATH)
.stream()
.map(params -> Arguments.of(params[0], params[1]));
}

@ParameterizedTest(name = "Name: {0}")
Expand Down Expand Up @@ -125,8 +130,30 @@ public void london(final String name, final TransactionTestCaseSpec spec) {
milestone(spec, name, "London", new LondonGasCalculator(), Optional.of(Wei.of(0)));
}

@ParameterizedTest(name = "Name: {0}")
@MethodSource("getTestParametersForConfig")
public void merge(final String name, final TransactionTestCaseSpec spec) {
milestone(spec, name, "Merge", new LondonGasCalculator(), Optional.of(Wei.of(0)));
}

@ParameterizedTest(name = "Name: {0}")
@MethodSource("getTestParametersForConfig")
public void shanghai(final String name, final TransactionTestCaseSpec spec) {
milestone(spec, name, "Shanghai", new ShanghaiGasCalculator(), Optional.of(Wei.of(0)));
}

@ParameterizedTest(name = "Name: {0}")
@MethodSource("getTestParametersForConfig")
public void cancun(final String name, final TransactionTestCaseSpec spec) {
milestone(spec, name, "Cancun", new CancunGasCalculator(), Optional.of(Wei.of(0)));
}

public void milestone(
final TransactionTestCaseSpec spec, final String name, final String milestone, final GasCalculator gasCalculator, final Optional<Wei> baseFee) {
final TransactionTestCaseSpec spec,
final String name,
final String milestone,
final GasCalculator gasCalculator,
final Optional<Wei> baseFee) {

final TransactionTestCaseSpec.Expectation expected = spec.expectation(milestone);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Assumptions;

/** A Transaction test case specification. */
@JsonIgnoreProperties({"_info"})
Expand Down Expand Up @@ -109,9 +110,7 @@ public Bytes getRlp() {
public Expectation expectation(final String milestone) {
final Expectation expectation = expectations.get(milestone);

if (expectation == null) {
throw new IllegalStateException("Expectation for milestone " + milestone + " not found");
}
Assumptions.assumeFalse(expectation == null, () -> "No expectation for milestone " + milestone);

return expectation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,59 @@

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

import org.assertj.core.api.Assertions;
import org.hyperledger.besu.ethereum.rlp.util.RLPTestUtil;
import org.hyperledger.besu.testutil.JsonTestParameters;

import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/** The Ethereum reference RLP tests. */
public class RLPRefTest {

private static final String TEST_CONFIG_FILES = "RLPTests/rlptest.json";
private static final String[] TEST_CONFIG_FILES = {
"RLPTests/rlptest.json", "RLPTests/invalidRLPTest.json"
};

private static final Bytes INVALID = Bytes.fromHexString("0x494e56414c4944");

public static Stream<Arguments> getTestParametersForConfig() {
return JsonTestParameters.create(RLPRefTestCaseSpec.class).generate(TEST_CONFIG_FILES).stream().map(params -> Arguments.of(params[0], params[1], params[2]));
return JsonTestParameters.create(RLPRefTestCaseSpec.class).generate(TEST_CONFIG_FILES).stream()
.map(params -> Arguments.of(params[0], params[1], params[2]));
}

@ParameterizedTest(name = "Name: {0}")
@MethodSource("getTestParametersForConfig")
public void encode(final String name, final RLPRefTestCaseSpec spec, final boolean runTest) {
assumeTrue(runTest, "Test was blacklisted");
Assertions.assertThat(RLPTestUtil.encode(spec.getIn())).isEqualTo(spec.getOut());
if (!spec.getIn().equals(INVALID)) {
Assertions.assertThat(RLPTestUtil.encode(spec.getIn())).isEqualTo(spec.getOut());
}
}

@ParameterizedTest(name = "Name: {0}")
@MethodSource("getTestParametersForConfig")
public void decode(final String name, final RLPRefTestCaseSpec spec, final boolean runTest) {
assumeTrue(runTest, "Test was blacklisted");
Assertions.assertThat(RLPTestUtil.decode(spec.getOut())).isEqualTo(spec.getIn());
if (spec.getIn().equals(INVALID)) {
Assertions.assertThatThrownBy(() -> RLPTestUtil.decode(spec.getOut()))
.isInstanceOf(RLPException.class);
Assertions.assertThatThrownBy(() -> decode2(RLP.input(spec.getOut())))
.isInstanceOf(RLPException.class);
} else {
Assertions.assertThat(RLPTestUtil.decode(spec.getOut())).isEqualTo(spec.getIn());
}
}

private static Object decode2(final RLPInput in) {
if (in.nextIsList()) {
return in.readList(RLPRefTest::decode2);
} else {
return in.readBytes();
}
}
}

0 comments on commit 081300f

Please sign in to comment.