Skip to content

Commit 6c8db2e

Browse files
authored
Update FuzzUtil to Electra (#8792)
1 parent 2a932e6 commit 6c8db2e

File tree

68 files changed

+515
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+515
-45
lines changed

fuzz/src/main/java/tech/pegasys/teku/fuzz/FuzzUtil.java

+113-9
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
import tech.pegasys.teku.fuzz.input.BlockFuzzInput;
2929
import tech.pegasys.teku.fuzz.input.BlockHeaderFuzzInput;
3030
import tech.pegasys.teku.fuzz.input.BlsToExecutionChangeFuzzInput;
31+
import tech.pegasys.teku.fuzz.input.ConsolidationRequestFuzzInput;
3132
import tech.pegasys.teku.fuzz.input.DepositFuzzInput;
33+
import tech.pegasys.teku.fuzz.input.DepositRequestFuzzInput;
3234
import tech.pegasys.teku.fuzz.input.ProposerSlashingFuzzInput;
3335
import tech.pegasys.teku.fuzz.input.SyncAggregateFuzzInput;
3436
import tech.pegasys.teku.fuzz.input.VoluntaryExitFuzzInput;
37+
import tech.pegasys.teku.fuzz.input.WithdrawalRequestFuzzInput;
3538
import tech.pegasys.teku.infrastructure.ssz.SszData;
3639
import tech.pegasys.teku.infrastructure.ssz.SszList;
3740
import tech.pegasys.teku.infrastructure.ssz.schema.SszSchema;
@@ -40,9 +43,13 @@
4043
import tech.pegasys.teku.spec.SpecMilestone;
4144
import tech.pegasys.teku.spec.SpecVersion;
4245
import tech.pegasys.teku.spec.TestSpecFactory;
46+
import tech.pegasys.teku.spec.config.SpecConfigElectra;
4347
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate;
44-
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodyCapella;
45-
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodySchemaCapella;
48+
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodyElectra;
49+
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodySchemaElectra;
50+
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ConsolidationRequest;
51+
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.DepositRequest;
52+
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.WithdrawalRequest;
4653
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
4754
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
4855
import tech.pegasys.teku.spec.datastructures.operations.Deposit;
@@ -52,14 +59,20 @@
5259
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
5360
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException;
5461
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
62+
import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateAccessorsElectra;
63+
import tech.pegasys.teku.spec.logic.versions.electra.helpers.BeaconStateMutatorsElectra;
64+
import tech.pegasys.teku.spec.logic.versions.electra.helpers.MiscHelpersElectra;
65+
import tech.pegasys.teku.spec.logic.versions.electra.helpers.PredicatesElectra;
66+
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;
5567

5668
public class FuzzUtil {
5769
// NOTE: alternatively could also have these all in separate classes, which implement a
5870
// "FuzzHarness" interface
5971

6072
private final Spec spec;
61-
private final BeaconBlockBodySchemaCapella<?> beaconBlockBodySchema;
73+
private final BeaconBlockBodySchemaElectra<?> beaconBlockBodySchema;
6274
private final SpecVersion specVersion;
75+
private final BeaconStateMutatorsElectra stateMutatorsElectra;
6376

6477
// Size of ValidatorIndex returned by shuffle
6578
private static final int OUTPUT_INDEX_BYTES = Long.BYTES;
@@ -70,14 +83,27 @@ public class FuzzUtil {
7083
public FuzzUtil(final boolean useMainnetConfig, final boolean disableBls) {
7184
spec =
7285
useMainnetConfig
73-
? TestSpecFactory.createMainnetCapella()
74-
: TestSpecFactory.createMinimalCapella();
75-
specVersion = spec.forMilestone(SpecMilestone.CAPELLA);
86+
? TestSpecFactory.createMainnetElectra()
87+
: TestSpecFactory.createMinimalElectra();
88+
specVersion = spec.forMilestone(SpecMilestone.ELECTRA);
7689
beaconBlockBodySchema =
77-
(BeaconBlockBodySchemaCapella<?>)
90+
(BeaconBlockBodySchemaElectra<?>)
7891
specVersion.getSchemaDefinitions().getBeaconBlockBodySchema();
7992
initialize(disableBls);
8093
this.signatureVerifier = disableBls ? BLSSignatureVerifier.NO_OP : BLSSignatureVerifier.SIMPLE;
94+
95+
final PredicatesElectra predicates = new PredicatesElectra(spec.getGenesisSpecConfig());
96+
final SchemaDefinitionsElectra schemaDefinitionsElectra =
97+
SchemaDefinitionsElectra.required(spec.getGenesisSchemaDefinitions());
98+
final SpecConfigElectra specConfig =
99+
spec.getGenesisSpecConfig().toVersionElectra().orElseThrow();
100+
final MiscHelpersElectra miscHelpersElectra =
101+
new MiscHelpersElectra(specConfig, predicates, schemaDefinitionsElectra);
102+
final BeaconStateAccessorsElectra stateAccessorsElectra =
103+
new BeaconStateAccessorsElectra(specConfig, predicates, miscHelpersElectra);
104+
stateMutatorsElectra =
105+
new BeaconStateMutatorsElectra(
106+
specConfig, miscHelpersElectra, stateAccessorsElectra, schemaDefinitionsElectra);
81107
}
82108

83109
public static void initialize(final boolean disableBls) {
@@ -146,7 +172,6 @@ public Optional<byte[]> fuzzBlock(final byte[] input) {
146172
Bytes output = postState.sszSerialize();
147173
return Optional.of(output.toArrayUnsafe());
148174
} catch (StateTransitionException e) {
149-
e.printStackTrace();
150175
// "expected error"
151176
return Optional.empty();
152177
}
@@ -293,7 +318,7 @@ public Optional<byte[]> fuzzExecutionPayload(final byte[] input) {
293318
BeaconBlockBodyFuzzInput structuredPayloadInput =
294319
deserialize(input, BeaconBlockBodyFuzzInput.createSchema(specVersion));
295320

296-
final BeaconBlockBodyCapella beaconBlockBody = structuredPayloadInput.getBeaconBlockBody();
321+
final BeaconBlockBodyElectra beaconBlockBody = structuredPayloadInput.getBeaconBlockBody();
297322
try {
298323
BeaconState postState =
299324
structuredPayloadInput
@@ -334,6 +359,85 @@ public Optional<byte[]> fuzzBlsToExecutionChange(final byte[] input) {
334359
}
335360
}
336361

362+
public Optional<byte[]> fuzzDepositRequest(final byte[] input) {
363+
DepositRequestFuzzInput structuredInput =
364+
deserialize(input, DepositRequestFuzzInput.createSchema(specVersion));
365+
SszList<DepositRequest> depositRequests =
366+
beaconBlockBodySchema
367+
.getExecutionRequestsSchema()
368+
.getDepositRequestsSchema()
369+
.of(structuredInput.getDepositRequest());
370+
371+
try {
372+
BeaconState postState =
373+
structuredInput
374+
.getState()
375+
.updated(
376+
state ->
377+
spec.getBlockProcessor(state.getSlot())
378+
.processDepositRequests(state, depositRequests.asList()));
379+
Bytes output = postState.sszSerialize();
380+
return Optional.of(output.toArrayUnsafe());
381+
} catch (BlockProcessingException e) {
382+
// "expected error"
383+
return Optional.empty();
384+
}
385+
}
386+
387+
public Optional<byte[]> fuzzWithdrawalRequest(final byte[] input) {
388+
WithdrawalRequestFuzzInput structuredInput =
389+
deserialize(input, WithdrawalRequestFuzzInput.createSchema(specVersion));
390+
SszList<WithdrawalRequest> withdrawalRequests =
391+
beaconBlockBodySchema
392+
.getExecutionRequestsSchema()
393+
.getWithdrawalRequestsSchema()
394+
.of(structuredInput.getWithdrawalRequest());
395+
396+
try {
397+
BeaconState postState =
398+
structuredInput
399+
.getState()
400+
.updated(
401+
state ->
402+
spec.getBlockProcessor(state.getSlot())
403+
.processWithdrawalRequests(
404+
state,
405+
withdrawalRequests.asList(),
406+
stateMutatorsElectra.createValidatorExitContextSupplier(
407+
structuredInput.getState())));
408+
Bytes output = postState.sszSerialize();
409+
return Optional.of(output.toArrayUnsafe());
410+
} catch (BlockProcessingException e) {
411+
// "expected error"
412+
return Optional.empty();
413+
}
414+
}
415+
416+
public Optional<byte[]> fuzzConsolidationRequest(final byte[] input) {
417+
ConsolidationRequestFuzzInput structuredInput =
418+
deserialize(input, ConsolidationRequestFuzzInput.createSchema(specVersion));
419+
SszList<ConsolidationRequest> consolidationRequests =
420+
beaconBlockBodySchema
421+
.getExecutionRequestsSchema()
422+
.getConsolidationRequestsSchema()
423+
.of(structuredInput.getConsolidationRequest());
424+
425+
try {
426+
BeaconState postState =
427+
structuredInput
428+
.getState()
429+
.updated(
430+
state ->
431+
spec.getBlockProcessor(state.getSlot())
432+
.processConsolidationRequests(state, consolidationRequests.asList()));
433+
Bytes output = postState.sszSerialize();
434+
return Optional.of(output.toArrayUnsafe());
435+
} catch (BlockProcessingException e) {
436+
// "expected error"
437+
return Optional.empty();
438+
}
439+
}
440+
337441
private <T extends SszData> T deserialize(final byte[] data, final SszSchema<T> type) {
338442
// allow exception to propagate on failure - indicates a preprocessing or deserializing error
339443
T structuredInput = type.sszDeserialize(Bytes.wrap(data));

fuzz/src/main/java/tech/pegasys/teku/fuzz/input/BeaconBlockBodyFuzzInput.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@
1919
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
2020
import tech.pegasys.teku.spec.Spec;
2121
import tech.pegasys.teku.spec.SpecVersion;
22-
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodyCapella;
23-
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodySchemaCapella;
22+
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodyElectra;
23+
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodySchemaElectra;
2424
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
2525

2626
public class BeaconBlockBodyFuzzInput
27-
extends Container2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyCapella> {
27+
extends Container2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyElectra> {
2828

29-
public static ContainerSchema2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyCapella>
29+
public static ContainerSchema2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyElectra>
3030
createSchema(final SpecVersion spec) {
31-
BeaconBlockBodySchemaCapella<?> beaconBlockBodySchema =
32-
spec.getSchemaDefinitions().getBeaconBlockBodySchema().toVersionCapella().orElseThrow();
31+
BeaconBlockBodySchemaElectra<?> beaconBlockBodySchema =
32+
spec.getSchemaDefinitions().getBeaconBlockBodySchema().toVersionElectra().orElseThrow();
3333
return ContainerSchema2.create(
3434
SszSchema.as(BeaconState.class, spec.getSchemaDefinitions().getBeaconStateSchema()),
35-
SszSchema.as(BeaconBlockBodyCapella.class, beaconBlockBodySchema),
35+
SszSchema.as(BeaconBlockBodyElectra.class, beaconBlockBodySchema),
3636
BeaconBlockBodyFuzzInput::new);
3737
}
3838

3939
public BeaconBlockBodyFuzzInput(
40-
final ContainerSchema2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyCapella> type,
40+
final ContainerSchema2<BeaconBlockBodyFuzzInput, BeaconState, BeaconBlockBodyElectra> type,
4141
final TreeNode backingNode) {
4242
super(type, backingNode);
4343
}
4444

4545
public BeaconBlockBodyFuzzInput(
46-
final Spec spec, final BeaconState state, final BeaconBlockBodyCapella beaconBlockBody) {
46+
final Spec spec, final BeaconState state, final BeaconBlockBodyElectra beaconBlockBody) {
4747
super(createSchema(spec.atSlot(state.getSlot())), state, beaconBlockBody);
4848
}
4949

50-
public BeaconBlockBodyCapella getBeaconBlockBody() {
50+
public BeaconBlockBodyElectra getBeaconBlockBody() {
5151
return getField1();
5252
}
5353

fuzz/src/main/java/tech/pegasys/teku/fuzz/input/BlsToExecutionChangeFuzzInput.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BlsToExecutionChangeFuzzInput
3131
return ContainerSchema2.create(
3232
SszSchema.as(BeaconState.class, spec.getSchemaDefinitions().getBeaconStateSchema()),
3333
spec.getSchemaDefinitions()
34-
.toVersionCapella()
34+
.toVersionElectra()
3535
.orElseThrow()
3636
.getSignedBlsToExecutionChangeSchema(),
3737
BlsToExecutionChangeFuzzInput::new);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Consensys Software Inc., 2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package tech.pegasys.teku.fuzz.input;
15+
16+
import tech.pegasys.teku.infrastructure.ssz.containers.Container2;
17+
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2;
18+
import tech.pegasys.teku.infrastructure.ssz.schema.SszSchema;
19+
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
20+
import tech.pegasys.teku.spec.Spec;
21+
import tech.pegasys.teku.spec.SpecVersion;
22+
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ConsolidationRequest;
23+
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
24+
25+
public class ConsolidationRequestFuzzInput
26+
extends Container2<ConsolidationRequestFuzzInput, BeaconState, ConsolidationRequest> {
27+
28+
public static ContainerSchema2<ConsolidationRequestFuzzInput, BeaconState, ConsolidationRequest>
29+
createSchema(final SpecVersion spec) {
30+
return ContainerSchema2.create(
31+
SszSchema.as(BeaconState.class, spec.getSchemaDefinitions().getBeaconStateSchema()),
32+
spec.getSchemaDefinitions()
33+
.toVersionElectra()
34+
.orElseThrow()
35+
.getExecutionRequestsSchema()
36+
.getConsolidationRequestsSchema()
37+
.getElementSchema(),
38+
ConsolidationRequestFuzzInput::new);
39+
}
40+
41+
public ConsolidationRequestFuzzInput(
42+
final ContainerSchema2<ConsolidationRequestFuzzInput, BeaconState, ConsolidationRequest> type,
43+
final TreeNode backingNode) {
44+
super(type, backingNode);
45+
}
46+
47+
public ConsolidationRequestFuzzInput(
48+
final Spec spec, final BeaconState state, final ConsolidationRequest depositRequest) {
49+
super(createSchema(spec.atSlot(state.getSlot())), state, depositRequest);
50+
}
51+
52+
public BeaconState getState() {
53+
return getField0();
54+
}
55+
56+
public ConsolidationRequest getConsolidationRequest() {
57+
return getField1();
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Consensys Software Inc., 2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package tech.pegasys.teku.fuzz.input;
15+
16+
import tech.pegasys.teku.infrastructure.ssz.containers.Container2;
17+
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2;
18+
import tech.pegasys.teku.infrastructure.ssz.schema.SszSchema;
19+
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
20+
import tech.pegasys.teku.spec.Spec;
21+
import tech.pegasys.teku.spec.SpecVersion;
22+
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.DepositRequest;
23+
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
24+
25+
public class DepositRequestFuzzInput
26+
extends Container2<DepositRequestFuzzInput, BeaconState, DepositRequest> {
27+
28+
public static ContainerSchema2<DepositRequestFuzzInput, BeaconState, DepositRequest> createSchema(
29+
final SpecVersion spec) {
30+
return ContainerSchema2.create(
31+
SszSchema.as(BeaconState.class, spec.getSchemaDefinitions().getBeaconStateSchema()),
32+
spec.getSchemaDefinitions()
33+
.toVersionElectra()
34+
.orElseThrow()
35+
.getExecutionRequestsSchema()
36+
.getDepositRequestsSchema()
37+
.getElementSchema(),
38+
DepositRequestFuzzInput::new);
39+
}
40+
41+
public DepositRequestFuzzInput(
42+
final ContainerSchema2<DepositRequestFuzzInput, BeaconState, DepositRequest> type,
43+
final TreeNode backingNode) {
44+
super(type, backingNode);
45+
}
46+
47+
public DepositRequestFuzzInput(
48+
final Spec spec, final BeaconState state, final DepositRequest depositRequest) {
49+
super(createSchema(spec.atSlot(state.getSlot())), state, depositRequest);
50+
}
51+
52+
public BeaconState getState() {
53+
return getField0();
54+
}
55+
56+
public DepositRequest getDepositRequest() {
57+
return getField1();
58+
}
59+
}

0 commit comments

Comments
 (0)