Skip to content

Commit

Permalink
New Java 17 style switch for reference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 committed Sep 7, 2023
1 parent 98a033b commit 2836644
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ public enum BlsSetting {
IGNORED;

public static BlsSetting forCode(final int code) {
switch (code) {
case 0:
return DEFAULT;
case 1:
return REQUIRED;
case 2:
return IGNORED;
default:
throw new IllegalArgumentException("Unsupported bls setting: " + code);
}
return switch (code) {
case 0 -> DEFAULT;
case 1 -> REQUIRED;
case 2 -> IGNORED;
default -> throw new IllegalArgumentException("Unsupported bls setting: " + code);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,16 @@ protected void runReferenceTest(final TestDefinition testDefinition) throws Thro
}

private TestExecutor getExecutorFor(final TestDefinition testDefinition) {
TestExecutor testExecutor = null;

// Look for fork-specific tests first
switch (testDefinition.getFork()) {
case TestFork.PHASE0:
testExecutor = PHASE_0_TEST_TYPES.get(testDefinition.getTestType());
break;
case TestFork.ALTAIR:
testExecutor = ALTAIR_TEST_TYPES.get(testDefinition.getTestType());
break;
case TestFork.BELLATRIX:
testExecutor = BELLATRIX_TEST_TYPES.get(testDefinition.getTestType());
break;
case TestFork.CAPELLA:
testExecutor = CAPELLA_TEST_TYPES.get(testDefinition.getTestType());
break;
case TestFork.DENEB:
testExecutor = DENEB_TEST_TYPES.get(testDefinition.getTestType());
break;
}
TestExecutor testExecutor =
switch (testDefinition.getFork()) {
case TestFork.PHASE0 -> PHASE_0_TEST_TYPES.get(testDefinition.getTestType());
case TestFork.ALTAIR -> ALTAIR_TEST_TYPES.get(testDefinition.getTestType());
case TestFork.BELLATRIX -> BELLATRIX_TEST_TYPES.get(testDefinition.getTestType());
case TestFork.CAPELLA -> CAPELLA_TEST_TYPES.get(testDefinition.getTestType());
case TestFork.DENEB -> DENEB_TEST_TYPES.get(testDefinition.getTestType());
default -> null;
};

// Look for a common test type if no specific override present
if (testExecutor == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,18 @@ public void runTest(final TestDefinition testDefinition) throws Throwable {

private void processUpgrade(final TestDefinition testDefinition, final SpecMilestone milestone) {
final SpecVersion spec = testDefinition.getSpec().getGenesisSpec();
final BeaconStateSchema<?, ?> fromMilestoneSchema;
switch (milestone) {
case ALTAIR:
fromMilestoneSchema = BeaconStateSchemaPhase0.create(spec.getConfig());
break;
case BELLATRIX:
fromMilestoneSchema = BeaconStateSchemaAltair.create(spec.getConfig());
break;
case CAPELLA:
fromMilestoneSchema = BeaconStateSchemaBellatrix.create(spec.getConfig());
break;
case DENEB:
fromMilestoneSchema = BeaconStateSchemaCapella.create(spec.getConfig());
break;
default:
throw new IllegalStateException(
"Unhandled fork upgrade for test "
+ testDefinition.getDisplayName()
+ ": "
+ milestone);
}
final BeaconStateSchema<?, ?> fromMilestoneSchema =
switch (milestone) {
case ALTAIR -> BeaconStateSchemaPhase0.create(spec.getConfig());
case BELLATRIX -> BeaconStateSchemaAltair.create(spec.getConfig());
case CAPELLA -> BeaconStateSchemaBellatrix.create(spec.getConfig());
case DENEB -> BeaconStateSchemaCapella.create(spec.getConfig());
default -> throw new IllegalStateException(
"Unhandled fork upgrade for test "
+ testDefinition.getDisplayName()
+ ": "
+ milestone);
};
final BeaconState preState =
TestDataUtils.loadSsz(testDefinition, "pre.ssz_snappy", fromMilestoneSchema);
final BeaconState postState = TestDataUtils.loadStateFromSsz(testDefinition, "post.ssz_snappy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,24 @@ private void processUpgrade(final TestDefinition testDefinition, final MetaData
testDefinition.getConfigName(),
builder -> {
switch (milestone) {
case ALTAIR:
builder.altairBuilder(a -> a.altairForkEpoch(forkEpoch));
break;
case BELLATRIX:
builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(forkEpoch));
break;
case CAPELLA:
builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(UInt64.ZERO))
.capellaBuilder(c -> c.capellaForkEpoch(forkEpoch));
break;
case DENEB:
builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(UInt64.ZERO))
.capellaBuilder(c -> c.capellaForkEpoch(UInt64.ZERO))
.denebBuilder(d -> d.denebForkEpoch(forkEpoch).kzgNoop(true));
break;
default:
throw new IllegalStateException(
"Unhandled fork transition for test "
+ testDefinition.getDisplayName()
+ ": "
+ milestone);
case ALTAIR -> builder.altairBuilder(a -> a.altairForkEpoch(forkEpoch));
case BELLATRIX -> builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(forkEpoch));
case CAPELLA -> builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(UInt64.ZERO))
.capellaBuilder(c -> c.capellaForkEpoch(forkEpoch));
case DENEB -> builder
.altairBuilder(a -> a.altairForkEpoch(UInt64.ZERO))
.bellatrixBuilder(b -> b.bellatrixForkEpoch(UInt64.ZERO))
.capellaBuilder(c -> c.capellaForkEpoch(UInt64.ZERO))
.denebBuilder(d -> d.denebForkEpoch(forkEpoch).kzgNoop(true));
default -> throw new IllegalStateException(
"Unhandled fork transition for test "
+ testDefinition.getDisplayName()
+ ": "
+ milestone);
}
});
final Spec spec = SpecFactory.create(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,22 @@ public EpochProcessingExecutor(
public void executeOperation(final EpochOperation operation, final MutableBeaconState state)
throws EpochProcessingException {
switch (operation) {
case PROCESS_SLASHINGS:
processSlashings(state);
break;
case PROCESS_REGISTRY_UPDATES:
processRegistryUpdates(state);
break;
case PROCESS_REWARDS_AND_PENALTIES:
processRewardsAndPenalties(state);
break;
case PROCESS_JUSTIFICATION_AND_FINALIZATION:
processJustificationAndFinalization(state);
break;
case PROCESS_EFFECTIVE_BALANCE_UPDATES:
processEffectiveBalanceUpdates(state);
break;
case PROCESS_PARTICIPATION_FLAG_UPDATES:
epochProcessor.processParticipationUpdates(state);
break;
case PROCESS_ETH1_DATA_RESET:
epochProcessor.processEth1DataReset(state);
break;
case PROCESS_SLASHINGS_RESET:
epochProcessor.processSlashingsReset(state);
break;
case PROCESS_RANDAO_MIXES_RESET:
epochProcessor.processRandaoMixesReset(state);
break;
case PROCESS_HISTORICAL_ROOTS_UPDATE:
epochProcessor.processHistoricalRootsUpdate(state);
break;
case PROCESS_HISTORICAL_SUMMARIES_UPDATE:
epochProcessor.processHistoricalSummariesUpdate(state);
break;
case SYNC_COMMITTEE_UPDATES:
epochProcessor.processSyncCommitteeUpdates(state);
break;
case INACTIVITY_UPDATES:
processInactivityUpdates(state);
break;
default:
throw new UnsupportedOperationException(
"Attempted to execute unknown operation type: " + operation);
case PROCESS_SLASHINGS -> processSlashings(state);
case PROCESS_REGISTRY_UPDATES -> processRegistryUpdates(state);
case PROCESS_REWARDS_AND_PENALTIES -> processRewardsAndPenalties(state);
case PROCESS_JUSTIFICATION_AND_FINALIZATION -> processJustificationAndFinalization(state);
case PROCESS_EFFECTIVE_BALANCE_UPDATES -> processEffectiveBalanceUpdates(state);
case PROCESS_PARTICIPATION_FLAG_UPDATES -> epochProcessor.processParticipationUpdates(state);
case PROCESS_ETH1_DATA_RESET -> epochProcessor.processEth1DataReset(state);
case PROCESS_SLASHINGS_RESET -> epochProcessor.processSlashingsReset(state);
case PROCESS_RANDAO_MIXES_RESET -> epochProcessor.processRandaoMixesReset(state);
case PROCESS_HISTORICAL_ROOTS_UPDATE -> epochProcessor.processHistoricalRootsUpdate(state);
case PROCESS_HISTORICAL_SUMMARIES_UPDATE -> epochProcessor.processHistoricalSummariesUpdate(
state);
case SYNC_COMMITTEE_UPDATES -> epochProcessor.processSyncCommitteeUpdates(state);
case INACTIVITY_UPDATES -> processInactivityUpdates(state);
default -> throw new UnsupportedOperationException(
"Attempted to execute unknown operation type: " + operation);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,39 +229,39 @@ private void processOperation(
final OperationProcessor processor)
throws Exception {
switch (operation) {
case ATTESTER_SLASHING:
case ATTESTER_SLASHING -> {
final AttesterSlashing attesterSlashing = loadAttesterSlashing(testDefinition);
processor.processAttesterSlashing(state, attesterSlashing);
break;
case PROPOSER_SLASHING:
}
case PROPOSER_SLASHING -> {
final ProposerSlashing proposerSlashing = loadProposerSlashing(testDefinition);
processor.processProposerSlashing(state, proposerSlashing);
break;
case PROCESS_BLOCK_HEADER:
}
case PROCESS_BLOCK_HEADER -> {
final BeaconBlockSummary blockHeader =
loadSsz(
testDefinition,
dataFileName,
testDefinition.getSpec().getGenesisSchemaDefinitions().getBeaconBlockSchema());
processor.processBlockHeader(state, blockHeader);
break;
case DEPOSIT:
}
case DEPOSIT -> {
final Deposit deposit = loadSsz(testDefinition, dataFileName, Deposit.SSZ_SCHEMA);
processor.processDeposit(state, deposit);
break;
case VOLUNTARY_EXIT:
}
case VOLUNTARY_EXIT -> {
final SignedVoluntaryExit voluntaryExit = loadVoluntaryExit(testDefinition);
processor.processVoluntaryExit(state, voluntaryExit);
break;
case ATTESTATION:
}
case ATTESTATION -> {
final Attestation attestation =
loadSsz(
testDefinition,
dataFileName,
testDefinition.getSpec().getGenesisSchemaDefinitions().getAttestationSchema());
processor.processAttestation(state, attestation);
break;
case SYNC_AGGREGATE:
}
case SYNC_AGGREGATE -> {
final SyncAggregate syncAggregate =
loadSsz(
testDefinition,
Expand All @@ -273,8 +273,8 @@ private void processOperation(
.getBeaconBlockBodySchema())
.getSyncAggregateSchema());
processor.processSyncCommittee(state, syncAggregate);
break;
case EXECUTION_PAYLOAD:
}
case EXECUTION_PAYLOAD -> {
final ExecutionMeta executionMeta =
loadYaml(testDefinition, "execution.yaml", ExecutionMeta.class);

Expand All @@ -295,16 +295,11 @@ private void processOperation(
beaconBlockBody,
Optional.of(
(latestExecutionPayloadHeader, payloadToExecute) -> executionMeta.executionValid));
break;
case BLS_TO_EXECUTION_CHANGE:
processBlsToExecutionChange(testDefinition, state, processor);
break;
case WITHDRAWAL:
processWithdrawal(testDefinition, state, processor);
break;
default:
throw new UnsupportedOperationException(
"Operation " + operation + " not implemented in OperationTestExecutor");
}
case BLS_TO_EXECUTION_CHANGE -> processBlsToExecutionChange(testDefinition, state, processor);
case WITHDRAWAL -> processWithdrawal(testDefinition, state, processor);
default -> throw new UnsupportedOperationException(
"Operation " + operation + " not implemented in OperationTestExecutor");
}
}

Expand Down Expand Up @@ -358,43 +353,41 @@ public void checkBlockInclusionValidation(
final TestDefinition testDefinition, final BeaconState state, final boolean expectInclusion) {
final Spec spec = testDefinition.getSpec();
switch (operation) {
case ATTESTER_SLASHING:
case ATTESTER_SLASHING -> {
final AttesterSlashing attesterSlashing = loadAttesterSlashing(testDefinition);
final AttesterSlashingValidator validator = new AttesterSlashingValidator(null, spec);
checkValidationForBlockInclusion(validator, state, attesterSlashing, expectInclusion);
break;
case PROPOSER_SLASHING:
}
case PROPOSER_SLASHING -> {
final ProposerSlashing proposerSlashing = loadProposerSlashing(testDefinition);
final ProposerSlashingValidator proposerValidator =
new ProposerSlashingValidator(spec, null);
checkValidationForBlockInclusion(
proposerValidator, state, proposerSlashing, expectInclusion);
break;
case VOLUNTARY_EXIT:
}
case VOLUNTARY_EXIT -> {
final SignedVoluntaryExit voluntaryExit = loadVoluntaryExit(testDefinition);
final VoluntaryExitValidator voluntaryExitValidator =
new VoluntaryExitValidator(spec, null);
checkValidationForBlockInclusion(
voluntaryExitValidator, state, voluntaryExit, expectInclusion);
break;
case BLS_TO_EXECUTION_CHANGE:
}
case BLS_TO_EXECUTION_CHANGE -> {
final SignedBlsToExecutionChangeValidator blsToExecutionChangeValidator =
new SignedBlsToExecutionChangeValidator(
spec, new SystemTimeProvider(), null, new SimpleSignatureVerificationService());
final SignedBlsToExecutionChange blsToExecutionChange =
loadBlsToExecutionChange(testDefinition);
checkValidationForBlockInclusion(
blsToExecutionChangeValidator, state, blsToExecutionChange, expectInclusion);
break;

case PROCESS_BLOCK_HEADER:
case DEPOSIT:
case ATTESTATION:
case SYNC_AGGREGATE:
case EXECUTION_PAYLOAD:
case WITHDRAWAL:
}
// Not yet testing inclusion rules
break;
case PROCESS_BLOCK_HEADER,
DEPOSIT,
ATTESTATION,
SYNC_AGGREGATE,
EXECUTION_PAYLOAD,
WITHDRAWAL -> {}
}
}

Expand Down
Loading

0 comments on commit 2836644

Please sign in to comment.