From 6f685e11cae95b7128065cf9280190618bbdf05b Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Mon, 14 Nov 2022 21:51:56 +0400 Subject: [PATCH 1/9] Fix duplicate of ExecutionPayload in toString() --- .../versions/capella/MutableBeaconStateCapellaImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java index 8e4af0fa8b6..f5f9c0eaca7 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java @@ -45,7 +45,6 @@ protected BeaconStateCapellaImpl createImmutableBeaconState( @Override protected void addCustomFields(final MoreObjects.ToStringHelper stringBuilder) { - BeaconStateBellatrix.describeCustomBellatrixFields(stringBuilder, this); BeaconStateCapella.describeCustomCapellaFields(stringBuilder, this); } From 205e7c420d1603a7114b08dc02918b356bfe573c Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Mon, 14 Nov 2022 21:53:10 +0400 Subject: [PATCH 2/9] Fix BeaconBlockBody creation for Capella --- .../pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java index 1df3039d7e8..cea2bcb4d5c 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsCapella.java @@ -20,7 +20,7 @@ import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSchema; import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema; -import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.bellatrix.BeaconBlockBodySchemaBellatrix; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodySchemaCapella; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BeaconBlockBodySchemaCapellaImpl; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BlindedBeaconBlockBodySchemaCapella; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.capella.BlindedBeaconBlockBodySchemaCapellaImpl; @@ -45,7 +45,7 @@ public class SchemaDefinitionsCapella extends SchemaDefinitionsBellatrix { private final ExecutionPayloadSchemaCapella executionPayloadSchemaCapella; private final ExecutionPayloadHeaderSchemaCapella executionPayloadHeaderSchemaCapella; - private final BeaconBlockBodySchemaBellatrix beaconBlockBodySchema; + private final BeaconBlockBodySchemaCapella beaconBlockBodySchema; private final BlindedBeaconBlockBodySchemaCapella blindedBeaconBlockBodySchema; private final BeaconBlockSchema beaconBlockSchema; From 47d520fc57eeddc72b8a75187ee6d6ddbee733d7 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Mon, 14 Nov 2022 22:10:55 +0400 Subject: [PATCH 3/9] Add EIP-4844 schema definitions --- .../state/beaconstate/BeaconState.java | 5 + .../state/beaconstate/MutableBeaconState.java | 5 + .../MutableBeaconStateCapellaImpl.java | 1 - .../versions/eip4844/BeaconStateEip4844.java | 52 ++++++ .../eip4844/BeaconStateEip4844Impl.java | 64 +++++++ .../eip4844/BeaconStateSchemaEip4844.java | 119 +++++++++++++ .../eip4844/MutableBeaconStateEip4844.java | 37 ++++ .../MutableBeaconStateEip4844Impl.java | 60 +++++++ .../teku/spec/schemas/SchemaDefinitions.java | 5 + .../schemas/SchemaDefinitionsEip4844.java | 160 ++++++++++++++++++ 10 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844Impl.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844Impl.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/BeaconState.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/BeaconState.java index e59c277e83c..ad7233faf52 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/BeaconState.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/BeaconState.java @@ -38,6 +38,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateBellatrix; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateCapella; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844.BeaconStateEip4844; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.phase0.BeaconStatePhase0; public interface BeaconState extends SszContainer, ValidatorStats { @@ -182,4 +183,8 @@ default Optional toVersionBellatrix() { default Optional toVersionCapella() { return Optional.empty(); } + + default Optional toVersionEip4844() { + return Optional.empty(); + } } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/MutableBeaconState.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/MutableBeaconState.java index 0c3c213506e..46adda6e7ef 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/MutableBeaconState.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/MutableBeaconState.java @@ -39,6 +39,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.MutableBeaconStateBellatrix; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.MutableBeaconStateCapella; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844.MutableBeaconStateEip4844; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.phase0.MutableBeaconStatePhase0; public interface MutableBeaconState extends BeaconState, SszMutableRefContainer { @@ -213,4 +214,8 @@ default Optional toMutableVersionBellatrix() { default Optional toMutableVersionCapella() { return Optional.empty(); } + + default Optional toMutableVersionEip4844() { + return Optional.empty(); + } } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java index f5f9c0eaca7..bd8987ddd08 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/MutableBeaconStateCapellaImpl.java @@ -21,7 +21,6 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractMutableBeaconState; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.TransitionCaches; import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.ValidatorStatsAltair; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateBellatrix; public class MutableBeaconStateCapellaImpl extends AbstractMutableBeaconState diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844.java new file mode 100644 index 00000000000..2cb4d7b060a --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844.java @@ -0,0 +1,52 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844; + +import com.google.common.base.MoreObjects; +import java.util.Optional; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateCapella; + +public interface BeaconStateEip4844 extends BeaconStateCapella { + static BeaconStateEip4844 required(final BeaconState state) { + return state + .toVersionEip4844() + .orElseThrow( + () -> + new IllegalArgumentException( + "Expected a EIP-4844 state but got: " + state.getClass().getSimpleName())); + } + + static void describeCustomEip4844Fields( + MoreObjects.ToStringHelper stringBuilder, BeaconStateCapella state) { + BeaconStateCapella.describeCustomCapellaFields(stringBuilder, state); + // no new fields + } + + @Override + MutableBeaconStateEip4844 createWritableCopy(); + + default + BeaconStateEip4844 updatedEip4844( + final Mutator mutator) throws E1, E2, E3 { + MutableBeaconStateEip4844 writableCopy = createWritableCopy(); + mutator.mutate(writableCopy); + return writableCopy.commitChanges(); + } + + @Override + default Optional toVersionEip4844() { + return Optional.of(this); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844Impl.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844Impl.java new file mode 100644 index 00000000000..db33a37e01d --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateEip4844Impl.java @@ -0,0 +1,64 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844; + +import com.google.common.base.MoreObjects; +import tech.pegasys.teku.infrastructure.ssz.SszContainer; +import tech.pegasys.teku.infrastructure.ssz.SszData; +import tech.pegasys.teku.infrastructure.ssz.cache.IntCache; +import tech.pegasys.teku.infrastructure.ssz.schema.SszCompositeSchema; +import tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszContainerSchema; +import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconState; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.TransitionCaches; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.ValidatorStatsAltair; + +public class BeaconStateEip4844Impl extends AbstractBeaconState + implements BeaconStateEip4844, BeaconStateCache, ValidatorStatsAltair { + + BeaconStateEip4844Impl( + final BeaconStateSchema schema) { + super(schema); + } + + BeaconStateEip4844Impl( + final SszCompositeSchema type, + final TreeNode backingNode, + final IntCache cache, + final TransitionCaches transitionCaches) { + super(type, backingNode, cache, transitionCaches); + } + + BeaconStateEip4844Impl( + final AbstractSszContainerSchema type, final TreeNode backingNode) { + super(type, backingNode); + } + + @Override + public BeaconStateSchemaEip4844 getBeaconStateSchema() { + return (BeaconStateSchemaEip4844) getSchema(); + } + + @Override + public MutableBeaconStateEip4844 createWritableCopy() { + return new MutableBeaconStateEip4844Impl(this); + } + + @Override + protected void describeCustomFields(final MoreObjects.ToStringHelper stringBuilder) { + BeaconStateEip4844.describeCustomEip4844Fields(stringBuilder, this); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java new file mode 100644 index 00000000000..1f5bfc282c2 --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -0,0 +1,119 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844; + +import static com.google.common.base.Preconditions.checkArgument; +import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix.LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX; + +import com.google.common.annotations.VisibleForTesting; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import tech.pegasys.teku.infrastructure.ssz.primitive.SszByte; +import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszPrimitiveListSchema; +import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszUInt64ListSchema; +import tech.pegasys.teku.infrastructure.ssz.sos.SszField; +import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; +import tech.pegasys.teku.spec.config.SpecConfig; +import tech.pegasys.teku.spec.config.SpecConfigEip4844; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadHeaderSchemaEip4844; +import tech.pegasys.teku.spec.datastructures.state.SyncCommittee; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; + +public class BeaconStateSchemaEip4844 + extends AbstractBeaconStateSchema { + + @VisibleForTesting + BeaconStateSchemaEip4844(final SpecConfig specConfig) { + super("BeaconStateEip4844", getUniqueFields(specConfig), specConfig); + } + + private static List getUniqueFields(final SpecConfig specConfig) { + final SszField latestExecutionPayloadHeaderField = + new SszField( + LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, + BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, + () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); + return Stream.concat( + BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), + Stream.of(latestExecutionPayloadHeaderField)) + .collect(Collectors.toList()); + } + + @SuppressWarnings("unchecked") + public SszPrimitiveListSchema getPreviousEpochParticipationSchema() { + return (SszPrimitiveListSchema) + getChildSchema(getFieldIndex(BeaconStateFields.PREVIOUS_EPOCH_PARTICIPATION)); + } + + @SuppressWarnings("unchecked") + public SszPrimitiveListSchema getCurrentEpochParticipationSchema() { + return (SszPrimitiveListSchema) + getChildSchema(getFieldIndex(BeaconStateFields.CURRENT_EPOCH_PARTICIPATION)); + } + + public SszUInt64ListSchema getInactivityScoresSchema() { + return (SszUInt64ListSchema) + getChildSchema(getFieldIndex(BeaconStateFields.INACTIVITY_SCORES)); + } + + public SyncCommittee.SyncCommitteeSchema getCurrentSyncCommitteeSchema() { + return (SyncCommittee.SyncCommitteeSchema) + getChildSchema(getFieldIndex(BeaconStateFields.CURRENT_SYNC_COMMITTEE)); + } + + public SyncCommittee.SyncCommitteeSchema getNextSyncCommitteeSchema() { + return (SyncCommittee.SyncCommitteeSchema) + getChildSchema(getFieldIndex(BeaconStateFields.NEXT_SYNC_COMMITTEE)); + } + + public ExecutionPayloadHeaderSchemaEip4844 getLastExecutionPayloadHeaderSchema() { + return (ExecutionPayloadHeaderSchemaEip4844) + getChildSchema(getFieldIndex(BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER)); + } + + @Override + public MutableBeaconStateEip4844 createBuilder() { + return new MutableBeaconStateEip4844Impl(createEmptyBeaconStateImpl(), true); + } + + public static BeaconStateSchemaEip4844 create(final SpecConfig specConfig) { + return new BeaconStateSchemaEip4844(specConfig); + } + + public static BeaconStateSchemaEip4844 required(final BeaconStateSchema schema) { + checkArgument( + schema instanceof BeaconStateSchemaEip4844, + "Expected a BeaconStateSchemaEip4844 but was %s", + schema.getClass()); + return (BeaconStateSchemaEip4844) schema; + } + + @Override + public BeaconStateEip4844 createEmpty() { + return createEmptyBeaconStateImpl(); + } + + private BeaconStateEip4844Impl createEmptyBeaconStateImpl() { + return new BeaconStateEip4844Impl(this); + } + + @Override + public BeaconStateEip4844 createFromBackingNode(TreeNode node) { + return new BeaconStateEip4844Impl(this, node); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844.java new file mode 100644 index 00000000000..e19d7dd075c --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844.java @@ -0,0 +1,37 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844; + +import java.util.Optional; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.MutableBeaconStateCapella; + +public interface MutableBeaconStateEip4844 extends MutableBeaconStateCapella, BeaconStateEip4844 { + static MutableBeaconStateEip4844 required(final MutableBeaconState state) { + return state + .toMutableVersionEip4844() + .orElseThrow( + () -> + new IllegalArgumentException( + "Expected a EIP-4844 state but got: " + state.getClass().getSimpleName())); + } + + @Override + BeaconStateEip4844 commitChanges(); + + @Override + default Optional toMutableVersionEip4844() { + return Optional.of(this); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844Impl.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844Impl.java new file mode 100644 index 00000000000..97f528e280b --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/MutableBeaconStateEip4844Impl.java @@ -0,0 +1,60 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844; + +import com.google.common.base.MoreObjects; +import tech.pegasys.teku.infrastructure.ssz.SszData; +import tech.pegasys.teku.infrastructure.ssz.cache.IntCache; +import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractMutableBeaconState; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.TransitionCaches; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.ValidatorStatsAltair; + +public class MutableBeaconStateEip4844Impl + extends AbstractMutableBeaconState + implements MutableBeaconStateEip4844, BeaconStateCache, ValidatorStatsAltair { + + MutableBeaconStateEip4844Impl(final BeaconStateEip4844Impl backingImmutableView) { + super(backingImmutableView); + } + + MutableBeaconStateEip4844Impl( + final BeaconStateEip4844Impl backingImmutableView, final boolean builder) { + super(backingImmutableView, builder); + } + + @Override + protected BeaconStateEip4844Impl createImmutableBeaconState( + final TreeNode backingNode, + final IntCache viewCache, + final TransitionCaches transitionCache) { + return new BeaconStateEip4844Impl(getSchema(), backingNode, viewCache, transitionCache); + } + + @Override + protected void addCustomFields(final MoreObjects.ToStringHelper stringBuilder) { + BeaconStateEip4844.describeCustomEip4844Fields(stringBuilder, this); + } + + @Override + public BeaconStateEip4844 commitChanges() { + return (BeaconStateEip4844) super.commitChanges(); + } + + @Override + public MutableBeaconStateEip4844 createWritableCopy() { + return (MutableBeaconStateEip4844) super.createWritableCopy(); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java index f8ccb082e1a..a3141ed1ee7 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitions.java @@ -85,6 +85,11 @@ default Optional toVersionCapella() { return Optional.empty(); } + @NonSchema + default Optional toVersionEip4844() { + return Optional.empty(); + } + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface NonSchema {} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java new file mode 100644 index 00000000000..e60748bb3b8 --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java @@ -0,0 +1,160 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.schemas; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.util.Optional; +import tech.pegasys.teku.spec.config.SpecConfigEip4844; +import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSchema; +import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BeaconBlockBodySchemaEip4844; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BeaconBlockBodySchemaEip4844Impl; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BlindedBeaconBlockBodySchemaEip4844; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BlindedBeaconBlockBodySchemaEip4844Impl; +import tech.pegasys.teku.spec.datastructures.builder.BuilderBidSchema; +import tech.pegasys.teku.spec.datastructures.builder.SignedBuilderBidSchema; +import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeaderSchema; +import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadHeaderSchemaEip4844; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadSchemaEip4844; +import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChangeSchema; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844.BeaconStateEip4844; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844.BeaconStateSchemaEip4844; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.eip4844.MutableBeaconStateEip4844; + +public class SchemaDefinitionsEip4844 extends SchemaDefinitionsCapella { + + private final BeaconStateSchemaEip4844 beaconStateSchema; + + private final ExecutionPayloadSchemaEip4844 executionPayloadSchemaEip4844; + private final ExecutionPayloadHeaderSchemaEip4844 executionPayloadHeaderSchemaEip4844; + + private final BeaconBlockBodySchemaEip4844 beaconBlockBodySchema; + private final BlindedBeaconBlockBodySchemaEip4844 blindedBeaconBlockBodySchema; + + private final BeaconBlockSchema beaconBlockSchema; + private final BeaconBlockSchema blindedBeaconBlockSchema; + private final SignedBeaconBlockSchema signedBeaconBlockSchema; + private final SignedBeaconBlockSchema signedBlindedBeaconBlockSchema; + + private final BuilderBidSchema builderBidSchemaEip4844; + private final SignedBuilderBidSchema signedBuilderBidSchemaEip4844; + + public SchemaDefinitionsEip4844(final SpecConfigEip4844 specConfig) { + super(specConfig.toVersionEip4844().orElseThrow()); + this.executionPayloadSchemaEip4844 = new ExecutionPayloadSchemaEip4844(specConfig); + final SignedBlsToExecutionChangeSchema signedBlsToExecutionChangeSchema = + new SignedBlsToExecutionChangeSchema(); + + this.beaconStateSchema = BeaconStateSchemaEip4844.create(specConfig); + this.executionPayloadHeaderSchemaEip4844 = + beaconStateSchema.getLastExecutionPayloadHeaderSchema(); + this.beaconBlockBodySchema = + BeaconBlockBodySchemaEip4844Impl.create( + specConfig, + getAttesterSlashingSchema(), + signedBlsToExecutionChangeSchema, + "BeaconBlockBodyEip4844"); + this.blindedBeaconBlockBodySchema = + BlindedBeaconBlockBodySchemaEip4844Impl.create( + specConfig, + getAttesterSlashingSchema(), + signedBlsToExecutionChangeSchema, + "BlindedBlockBodyEip4844"); + this.beaconBlockSchema = new BeaconBlockSchema(beaconBlockBodySchema, "BeaconBlockEip4844"); + this.blindedBeaconBlockSchema = + new BeaconBlockSchema(blindedBeaconBlockBodySchema, "BlindedBlockEip4844"); + this.signedBeaconBlockSchema = + new SignedBeaconBlockSchema(beaconBlockSchema, "SignedBeaconBlockEip4844"); + this.signedBlindedBeaconBlockSchema = + new SignedBeaconBlockSchema(blindedBeaconBlockSchema, "SignedBlindedBlockEip4844"); + this.builderBidSchemaEip4844 = + new BuilderBidSchema("BuilderBidEip4844", executionPayloadHeaderSchemaEip4844); + this.signedBuilderBidSchemaEip4844 = + new SignedBuilderBidSchema("SignedBuilderBidEip4844", builderBidSchemaEip4844); + } + + public static SchemaDefinitionsEip4844 required(final SchemaDefinitions schemaDefinitions) { + checkArgument( + schemaDefinitions instanceof SchemaDefinitionsEip4844, + "Expected definitions of type %s by got %s", + SchemaDefinitionsEip4844.class, + schemaDefinitions.getClass()); + return (SchemaDefinitionsEip4844) schemaDefinitions; + } + + @Override + public BeaconStateSchema + getBeaconStateSchema() { + return beaconStateSchema; + } + + @Override + public BeaconBlockBodySchema getBeaconBlockBodySchema() { + return beaconBlockBodySchema; + } + + @Override + public BeaconBlockBodySchema getBlindedBeaconBlockBodySchema() { + return blindedBeaconBlockBodySchema; + } + + @Override + public BeaconBlockSchema getBeaconBlockSchema() { + return beaconBlockSchema; + } + + @Override + public BeaconBlockSchema getBlindedBeaconBlockSchema() { + return blindedBeaconBlockSchema; + } + + @Override + public SignedBeaconBlockSchema getSignedBeaconBlockSchema() { + return signedBeaconBlockSchema; + } + + @Override + public SignedBeaconBlockSchema getSignedBlindedBeaconBlockSchema() { + return signedBlindedBeaconBlockSchema; + } + + @Override + public ExecutionPayloadSchema getExecutionPayloadSchema() { + return executionPayloadSchemaEip4844; + } + + @Override + public ExecutionPayloadHeaderSchema getExecutionPayloadHeaderSchema() { + return executionPayloadHeaderSchemaEip4844; + } + + @Override + public BuilderBidSchema getBuilderBidSchema() { + return builderBidSchemaEip4844; + } + + @Override + public SignedBuilderBidSchema getSignedBuilderBidSchema() { + return signedBuilderBidSchemaEip4844; + } + + @Override + public Optional toVersionEip4844() { + return Optional.of(this); + } +} From 4e8f86316e808138db997f5c1cc2d219c8cdc90e Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Mon, 14 Nov 2022 22:38:07 +0400 Subject: [PATCH 4/9] Added Blob, BlobsSidecar, SignedBeaconBlockAndBlobsSidecar schema getters from EIP-4844 schema definitions --- .../SignedBeaconBlockAndBlobsSidecar.java | 28 +-------- ...ignedBeaconBlockAndBlobsSidecarSchema.java | 45 +++++++++++++++ .../versions/eip4844/BlobsSidecar.java | 39 +------------ .../versions/eip4844/BlobsSidecarSchema.java | 57 +++++++++++++++++++ .../schemas/SchemaDefinitionsEip4844.java | 24 ++++++++ 5 files changed, 128 insertions(+), 65 deletions(-) create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecarSchema.java create mode 100644 ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecarSchema.java diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecar.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecar.java index bff4ed7e147..9ce7ee5029d 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecar.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecar.java @@ -14,40 +14,14 @@ package tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844; import tech.pegasys.teku.infrastructure.ssz.containers.Container2; -import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2; import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; -import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema; import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.BlobsSidecar; public class SignedBeaconBlockAndBlobsSidecar extends Container2 { - public static class SignedBeaconBlockAndBlobsSidecarSchema - extends ContainerSchema2 { - - SignedBeaconBlockAndBlobsSidecarSchema( - final SignedBeaconBlockSchema signedBeaconBlockSchema, - final BlobsSidecar.BlobsSidecarSchema blobsSidecarSchema) { - super( - "SignedBeaconBlockAndBlobsSidecar", - namedSchema("beacon_block", signedBeaconBlockSchema), - namedSchema("blobs_sidecar", blobsSidecarSchema)); - } - - @Override - public SignedBeaconBlockAndBlobsSidecar createFromBackingNode(final TreeNode node) { - return new SignedBeaconBlockAndBlobsSidecar(this, node); - } - } - - public static SignedBeaconBlockAndBlobsSidecarSchema create( - final SignedBeaconBlockSchema signedBeaconBlockSchema, - final BlobsSidecar.BlobsSidecarSchema blobsSidecarSchema) { - return new SignedBeaconBlockAndBlobsSidecarSchema(signedBeaconBlockSchema, blobsSidecarSchema); - } - - private SignedBeaconBlockAndBlobsSidecar( + SignedBeaconBlockAndBlobsSidecar( final SignedBeaconBlockAndBlobsSidecarSchema type, final TreeNode backingNode) { super(type, backingNode); } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecarSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecarSchema.java new file mode 100644 index 00000000000..471a0f7d096 --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/blocks/blockbody/versions/eip4844/SignedBeaconBlockAndBlobsSidecarSchema.java @@ -0,0 +1,45 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844; + +import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema2; +import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; +import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; +import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.BlobsSidecar; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.BlobsSidecarSchema; + +public class SignedBeaconBlockAndBlobsSidecarSchema + extends ContainerSchema2 { + + SignedBeaconBlockAndBlobsSidecarSchema( + final SignedBeaconBlockSchema signedBeaconBlockSchema, + final BlobsSidecarSchema blobsSidecarSchema) { + super( + "SignedBeaconBlockAndBlobsSidecar", + namedSchema("beacon_block", signedBeaconBlockSchema), + namedSchema("blobs_sidecar", blobsSidecarSchema)); + } + + public static SignedBeaconBlockAndBlobsSidecarSchema create( + final SignedBeaconBlockSchema signedBeaconBlockSchema, + final BlobsSidecarSchema blobsSidecarSchema) { + return new SignedBeaconBlockAndBlobsSidecarSchema(signedBeaconBlockSchema, blobsSidecarSchema); + } + + @Override + public SignedBeaconBlockAndBlobsSidecar createFromBackingNode(final TreeNode node) { + return new SignedBeaconBlockAndBlobsSidecar(this, node); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecar.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecar.java index 441f67d00fa..5beed491b5d 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecar.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecar.java @@ -17,54 +17,17 @@ import org.apache.tuweni.bytes.Bytes32; import tech.pegasys.teku.infrastructure.ssz.SszList; import tech.pegasys.teku.infrastructure.ssz.containers.Container4; -import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema4; import tech.pegasys.teku.infrastructure.ssz.primitive.SszBytes32; import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64; -import tech.pegasys.teku.infrastructure.ssz.schema.SszFieldName; -import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema; -import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas; import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; import tech.pegasys.teku.infrastructure.unsigned.UInt64; import tech.pegasys.teku.kzg.KZGProof; -import tech.pegasys.teku.spec.config.SpecConfigEip4844; import tech.pegasys.teku.spec.datastructures.type.SszKZGProof; -import tech.pegasys.teku.spec.datastructures.type.SszKZGProofSchema; public class BlobsSidecar extends Container4, SszKZGProof> { - public static class BlobsSidecarSchema - extends ContainerSchema4, SszKZGProof> { - - static final SszFieldName FIELD_BLOBS = () -> "blobs"; - - BlobsSidecarSchema(final SpecConfigEip4844 specConfig, final BlobSchema blobSchema) { - super( - "BlobsSidecar", - namedSchema("beacon_block_root", SszPrimitiveSchemas.BYTES32_SCHEMA), - namedSchema("beacon_block_slot", SszPrimitiveSchemas.UINT64_SCHEMA), - namedSchema( - FIELD_BLOBS, SszListSchema.create(blobSchema, specConfig.getMaxBlobsPerBlock())), - namedSchema("kzg_aggregated_proof", SszKZGProofSchema.INSTANCE)); - } - - @SuppressWarnings("unchecked") - public SszListSchema getBlobsSchema() { - return (SszListSchema) getChildSchema(getFieldIndex(FIELD_BLOBS)); - } - - @Override - public BlobsSidecar createFromBackingNode(final TreeNode node) { - return new BlobsSidecar(this, node); - } - } - - public static BlobsSidecarSchema create( - final SpecConfigEip4844 specConfig, final BlobSchema blobSchema) { - return new BlobsSidecarSchema(specConfig, blobSchema); - } - - private BlobsSidecar(final BlobsSidecarSchema type, final TreeNode backingNode) { + BlobsSidecar(final BlobsSidecarSchema type, final TreeNode backingNode) { super(type, backingNode); } diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecarSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecarSchema.java new file mode 100644 index 00000000000..50693f1b127 --- /dev/null +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/execution/versions/eip4844/BlobsSidecarSchema.java @@ -0,0 +1,57 @@ +/* + * Copyright ConsenSys Software Inc., 2022 + * + * 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. + */ + +package tech.pegasys.teku.spec.datastructures.execution.versions.eip4844; + +import tech.pegasys.teku.infrastructure.ssz.SszList; +import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema4; +import tech.pegasys.teku.infrastructure.ssz.primitive.SszBytes32; +import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64; +import tech.pegasys.teku.infrastructure.ssz.schema.SszFieldName; +import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema; +import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas; +import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode; +import tech.pegasys.teku.spec.config.SpecConfigEip4844; +import tech.pegasys.teku.spec.datastructures.type.SszKZGProof; +import tech.pegasys.teku.spec.datastructures.type.SszKZGProofSchema; + +public class BlobsSidecarSchema + extends ContainerSchema4, SszKZGProof> { + + static final SszFieldName FIELD_BLOBS = () -> "blobs"; + + BlobsSidecarSchema(final SpecConfigEip4844 specConfig, final BlobSchema blobSchema) { + super( + "BlobsSidecar", + namedSchema("beacon_block_root", SszPrimitiveSchemas.BYTES32_SCHEMA), + namedSchema("beacon_block_slot", SszPrimitiveSchemas.UINT64_SCHEMA), + namedSchema( + FIELD_BLOBS, SszListSchema.create(blobSchema, specConfig.getMaxBlobsPerBlock())), + namedSchema("kzg_aggregated_proof", SszKZGProofSchema.INSTANCE)); + } + + @SuppressWarnings("unchecked") + public SszListSchema getBlobsSchema() { + return (SszListSchema) getChildSchema(getFieldIndex(FIELD_BLOBS)); + } + + public static BlobsSidecarSchema create( + final SpecConfigEip4844 specConfig, final BlobSchema blobSchema) { + return new BlobsSidecarSchema(specConfig, blobSchema); + } + + @Override + public BlobsSidecar createFromBackingNode(final TreeNode node) { + return new BlobsSidecar(this, node); + } +} diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java index e60748bb3b8..dbba04f0d8e 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/schemas/SchemaDefinitionsEip4844.java @@ -24,10 +24,13 @@ import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BeaconBlockBodySchemaEip4844Impl; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BlindedBeaconBlockBodySchemaEip4844; import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.BlindedBeaconBlockBodySchemaEip4844Impl; +import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip4844.SignedBeaconBlockAndBlobsSidecarSchema; import tech.pegasys.teku.spec.datastructures.builder.BuilderBidSchema; import tech.pegasys.teku.spec.datastructures.builder.SignedBuilderBidSchema; import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeaderSchema; import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.BlobSchema; +import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.BlobsSidecarSchema; import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadHeaderSchemaEip4844; import tech.pegasys.teku.spec.datastructures.execution.versions.eip4844.ExecutionPayloadSchemaEip4844; import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChangeSchema; @@ -54,6 +57,10 @@ public class SchemaDefinitionsEip4844 extends SchemaDefinitionsCapella { private final BuilderBidSchema builderBidSchemaEip4844; private final SignedBuilderBidSchema signedBuilderBidSchemaEip4844; + private final BlobSchema blobSchema; + private final BlobsSidecarSchema blobsSidecarSchema; + private final SignedBeaconBlockAndBlobsSidecarSchema signedBeaconBlockAndBlobsSidecarSchema; + public SchemaDefinitionsEip4844(final SpecConfigEip4844 specConfig) { super(specConfig.toVersionEip4844().orElseThrow()); this.executionPayloadSchemaEip4844 = new ExecutionPayloadSchemaEip4844(specConfig); @@ -86,6 +93,11 @@ public SchemaDefinitionsEip4844(final SpecConfigEip4844 specConfig) { new BuilderBidSchema("BuilderBidEip4844", executionPayloadHeaderSchemaEip4844); this.signedBuilderBidSchemaEip4844 = new SignedBuilderBidSchema("SignedBuilderBidEip4844", builderBidSchemaEip4844); + + this.blobSchema = new BlobSchema(specConfig); + this.blobsSidecarSchema = BlobsSidecarSchema.create(specConfig, blobSchema); + this.signedBeaconBlockAndBlobsSidecarSchema = + SignedBeaconBlockAndBlobsSidecarSchema.create(signedBeaconBlockSchema, blobsSidecarSchema); } public static SchemaDefinitionsEip4844 required(final SchemaDefinitions schemaDefinitions) { @@ -153,6 +165,18 @@ public SignedBuilderBidSchema getSignedBuilderBidSchema() { return signedBuilderBidSchemaEip4844; } + public BlobSchema getBlobSchema() { + return blobSchema; + } + + public BlobsSidecarSchema getBlobsSidecarSchema() { + return blobsSidecarSchema; + } + + public SignedBeaconBlockAndBlobsSidecarSchema getSignedBeaconBlockAndBlobsSidecarSchema() { + return signedBeaconBlockAndBlobsSidecarSchema; + } + @Override public Optional toVersionEip4844() { return Optional.of(this); From 58e1cd4ab54a2b54dd2d9440b4f03dd2870f52a0 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Tue, 15 Nov 2022 14:11:26 +0400 Subject: [PATCH 5/9] Fix uniqueFields for Capella and Eip4844 schemas --- .../versions/capella/BeaconStateSchemaCapella.java | 6 +++--- .../versions/eip4844/BeaconStateSchemaEip4844.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java index 8c2ffc18d7f..14ea3621384 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java @@ -33,7 +33,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix; public class BeaconStateSchemaCapella extends AbstractBeaconStateSchema { @@ -45,7 +45,7 @@ public class BeaconStateSchemaCapella super("BeaconStateCapella", getUniqueFields(specConfig), specConfig); } - private static List getUniqueFields(final SpecConfig specConfig) { + public static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = new SszField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, @@ -62,7 +62,7 @@ private static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, () -> SszPrimitiveSchemas.UINT64_SCHEMA); return Stream.concat( - BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), + BeaconStateSchemaBellatrix.getUniqueFields(specConfig).stream(), Stream.of( latestExecutionPayloadHeaderField, nextWithdrawalIndexField, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java index 1f5bfc282c2..995aef2b424 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -32,7 +32,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella; public class BeaconStateSchemaEip4844 extends AbstractBeaconStateSchema { @@ -49,7 +49,7 @@ private static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); return Stream.concat( - BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), + BeaconStateSchemaCapella.getUniqueFields(specConfig).stream(), Stream.of(latestExecutionPayloadHeaderField)) .collect(Collectors.toList()); } From 7b6aafccabfb04c7ec120d0859d4d3d13492b555 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Tue, 15 Nov 2022 14:26:09 +0400 Subject: [PATCH 6/9] Revert "Fix uniqueFields for Capella and Eip4844 schemas" This reverts commit 58e1cd4ab54a2b54dd2d9440b4f03dd2870f52a0. --- .../versions/capella/BeaconStateSchemaCapella.java | 6 +++--- .../versions/eip4844/BeaconStateSchemaEip4844.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java index 14ea3621384..8c2ffc18d7f 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java @@ -33,7 +33,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; public class BeaconStateSchemaCapella extends AbstractBeaconStateSchema { @@ -45,7 +45,7 @@ public class BeaconStateSchemaCapella super("BeaconStateCapella", getUniqueFields(specConfig), specConfig); } - public static List getUniqueFields(final SpecConfig specConfig) { + private static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = new SszField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, @@ -62,7 +62,7 @@ public static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, () -> SszPrimitiveSchemas.UINT64_SCHEMA); return Stream.concat( - BeaconStateSchemaBellatrix.getUniqueFields(specConfig).stream(), + BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), Stream.of( latestExecutionPayloadHeaderField, nextWithdrawalIndexField, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java index 995aef2b424..1f5bfc282c2 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -32,7 +32,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; public class BeaconStateSchemaEip4844 extends AbstractBeaconStateSchema { @@ -49,7 +49,7 @@ private static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); return Stream.concat( - BeaconStateSchemaCapella.getUniqueFields(specConfig).stream(), + BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), Stream.of(latestExecutionPayloadHeaderField)) .collect(Collectors.toList()); } From 30040cc7c4fa33f6206de57683395aee770b1873 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Tue, 15 Nov 2022 14:29:39 +0400 Subject: [PATCH 7/9] Fix uniqueFields for Eip4844 schema with new list, no replacement for existing field is allowed --- .../capella/BeaconStateSchemaCapella.java | 4 ++-- .../eip4844/BeaconStateSchemaEip4844.java | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java index 8c2ffc18d7f..2c452e06610 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java @@ -37,8 +37,8 @@ public class BeaconStateSchemaCapella extends AbstractBeaconStateSchema { - private static final int NEXT_WITHDRAWAL_INDEX = 25; - private static final int NEXT_WITHDRAWAL_VALIDATOR_INDEX = 26; + public static final int NEXT_WITHDRAWAL_INDEX = 25; + public static final int NEXT_WITHDRAWAL_VALIDATOR_INDEX = 26; @VisibleForTesting BeaconStateSchemaCapella(final SpecConfig specConfig) { diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java index 1f5bfc282c2..ea54ecca6f0 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -15,12 +15,15 @@ import static com.google.common.base.Preconditions.checkArgument; import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix.LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX; +import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_INDEX; +import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_VALIDATOR_INDEX; import com.google.common.annotations.VisibleForTesting; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import tech.pegasys.teku.infrastructure.ssz.primitive.SszByte; +import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszPrimitiveListSchema; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszUInt64ListSchema; import tech.pegasys.teku.infrastructure.ssz.sos.SszField; @@ -48,9 +51,23 @@ private static List getUniqueFields(final SpecConfig specConfig) { LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); + final SszField nextWithdrawalIndexField = + new SszField( + NEXT_WITHDRAWAL_INDEX, + BeaconStateFields.NEXT_WITHDRAWAL_INDEX, + () -> SszPrimitiveSchemas.UINT64_SCHEMA); + final SszField nextWithdrawalValidatorIndexField = + new SszField( + NEXT_WITHDRAWAL_VALIDATOR_INDEX, + BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, + () -> SszPrimitiveSchemas.UINT64_SCHEMA); + return Stream.concat( BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), - Stream.of(latestExecutionPayloadHeaderField)) + Stream.of( + latestExecutionPayloadHeaderField, + nextWithdrawalIndexField, + nextWithdrawalValidatorIndexField)) .collect(Collectors.toList()); } From 9052835b49690342efd5c0d8bb285bf08100fe81 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Tue, 15 Nov 2022 16:37:11 +0400 Subject: [PATCH 8/9] Temporary commit for SSZfield replacement idea --- .../common/AbstractBeaconStateSchema.java | 32 +++++++++++++++++-- .../capella/BeaconStateSchemaCapella.java | 8 ++--- .../eip4844/BeaconStateSchemaEip4844.java | 25 +++------------ .../common/AbstractBeaconStateSchemaTest.java | 5 ++- .../teku/infrastructure/ssz/sos/SszField.java | 20 +++++++++--- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java index 07b3a78e189..207ab75a563 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java @@ -15,8 +15,10 @@ import static com.google.common.base.Preconditions.checkArgument; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszContainerSchema; @@ -44,9 +46,33 @@ protected AbstractBeaconStateSchema( } private static List combineFields(List fieldsA, List fieldsB) { - return Stream.concat(fieldsA.stream(), fieldsB.stream()) - .sorted(Comparator.comparing(SszField::getIndex)) - .collect(Collectors.toList()); + List list = + Stream.concat(fieldsA.stream(), fieldsB.stream()) + .sorted( + Comparator.comparing(SszField::getIndex) + .thenComparing(SszField::isOverridesOtherField)) + .collect(Collectors.toList()); + List result = new ArrayList<>(); + Optional previous = Optional.empty(); + for (SszField sszField : list) { + if (previous.isPresent()) { + if (sszField.isOverridesOtherField()) { + if (!previous.get().getName().equals(sszField.getName()) + || previous.get().getIndex() != sszField.getIndex()) { + throw new RuntimeException( + String.format( + "Field override configured by field %s but replacement %s doesn't look like a correct override", + sszField, previous.get())); + } + } else { + result.add(previous.get()); + } + } + previous = Optional.of(sszField); + } + result.add(previous.orElseThrow()); + + return result; } private void validateFields(final List fields) { diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java index 2c452e06610..d124c6fe40c 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java @@ -33,7 +33,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix; public class BeaconStateSchemaCapella extends AbstractBeaconStateSchema { @@ -45,9 +45,9 @@ public class BeaconStateSchemaCapella super("BeaconStateCapella", getUniqueFields(specConfig), specConfig); } - private static List getUniqueFields(final SpecConfig specConfig) { + public static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = - new SszField( + SszField.createOverrideField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaCapella(SpecConfigCapella.required(specConfig))); @@ -62,7 +62,7 @@ private static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, () -> SszPrimitiveSchemas.UINT64_SCHEMA); return Stream.concat( - BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), + BeaconStateSchemaBellatrix.getUniqueFields(specConfig).stream(), Stream.of( latestExecutionPayloadHeaderField, nextWithdrawalIndexField, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java index ea54ecca6f0..fc9220f87e5 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -15,15 +15,12 @@ import static com.google.common.base.Preconditions.checkArgument; import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix.LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX; -import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_INDEX; -import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_VALIDATOR_INDEX; import com.google.common.annotations.VisibleForTesting; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import tech.pegasys.teku.infrastructure.ssz.primitive.SszByte; -import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszPrimitiveListSchema; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszUInt64ListSchema; import tech.pegasys.teku.infrastructure.ssz.sos.SszField; @@ -35,7 +32,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella; public class BeaconStateSchemaEip4844 extends AbstractBeaconStateSchema { @@ -47,27 +44,13 @@ public class BeaconStateSchemaEip4844 private static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = - new SszField( + SszField.createOverrideField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); - final SszField nextWithdrawalIndexField = - new SszField( - NEXT_WITHDRAWAL_INDEX, - BeaconStateFields.NEXT_WITHDRAWAL_INDEX, - () -> SszPrimitiveSchemas.UINT64_SCHEMA); - final SszField nextWithdrawalValidatorIndexField = - new SszField( - NEXT_WITHDRAWAL_VALIDATOR_INDEX, - BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, - () -> SszPrimitiveSchemas.UINT64_SCHEMA); - return Stream.concat( - BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), - Stream.of( - latestExecutionPayloadHeaderField, - nextWithdrawalIndexField, - nextWithdrawalValidatorIndexField)) + BeaconStateSchemaCapella.getUniqueFields(specConfig).stream(), + Stream.of(latestExecutionPayloadHeaderField)) .collect(Collectors.toList()); } diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java index c3583fc0fe2..3a55136c9b9 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java @@ -139,7 +139,10 @@ public void shouldValidateFieldCount() { public void shouldValidateInvariantFields() { final SszField randomField = new SszField( - 2, "random", () -> SszVectorSchema.create(SszPrimitiveSchemas.BYTES32_SCHEMA, 10)); + 2, + "random", + () -> SszVectorSchema.create(SszPrimitiveSchemas.BYTES32_SCHEMA, 10), + false); assertThatThrownBy( () -> createSchema( diff --git a/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java b/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java index c9bc042dfe1..f376f6f59d0 100644 --- a/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java +++ b/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java @@ -21,13 +21,14 @@ public class SszField { private final int index; private final String name; private final Supplier> viewType; + private final boolean overridesOtherField; public SszField(int index, SszSchema sszSchema) { this(index, () -> sszSchema); } public SszField(int index, Supplier> viewType) { - this(index, "field-" + index, viewType); + this(index, "field-" + index, viewType, false); } public SszField(int index, SszFieldName name, SszSchema sszSchema) { @@ -35,17 +36,24 @@ public SszField(int index, SszFieldName name, SszSchema sszSchema) { } public SszField(int index, String name, SszSchema sszSchema) { - this(index, name, () -> sszSchema); + this(index, name, () -> sszSchema, false); } public SszField(int index, SszFieldName name, Supplier> viewType) { - this(index, name.getSszFieldName(), viewType); + this(index, name.getSszFieldName(), viewType, false); } - public SszField(int index, String name, Supplier> viewType) { + public static SszField createOverrideField( + int index, SszFieldName name, Supplier> viewType) { + return new SszField(index, name.getSszFieldName(), viewType, true); + } + + public SszField( + int index, String name, Supplier> viewType, boolean overridesOtherField) { this.index = index; this.name = name; this.viewType = viewType; + this.overridesOtherField = overridesOtherField; } public int getIndex() { @@ -59,4 +67,8 @@ public String getName() { public Supplier> getSchema() { return viewType; } + + public boolean isOverridesOtherField() { + return overridesOtherField; + } } From 5146460b3fd4917f18cc0de54d5a81a4acf9c011 Mon Sep 17 00:00:00 2001 From: Dmitrii Shmatko Date: Tue, 15 Nov 2022 18:12:08 +0400 Subject: [PATCH 9/9] Revert "Temporary commit for SSZfield replacement idea" This reverts commit 9052835b49690342efd5c0d8bb285bf08100fe81. --- .../common/AbstractBeaconStateSchema.java | 32 ++----------------- .../capella/BeaconStateSchemaCapella.java | 8 ++--- .../eip4844/BeaconStateSchemaEip4844.java | 25 ++++++++++++--- .../common/AbstractBeaconStateSchemaTest.java | 5 +-- .../teku/infrastructure/ssz/sos/SszField.java | 20 +++--------- 5 files changed, 33 insertions(+), 57 deletions(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java index 207ab75a563..07b3a78e189 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchema.java @@ -15,10 +15,8 @@ import static com.google.common.base.Preconditions.checkArgument; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszContainerSchema; @@ -46,33 +44,9 @@ protected AbstractBeaconStateSchema( } private static List combineFields(List fieldsA, List fieldsB) { - List list = - Stream.concat(fieldsA.stream(), fieldsB.stream()) - .sorted( - Comparator.comparing(SszField::getIndex) - .thenComparing(SszField::isOverridesOtherField)) - .collect(Collectors.toList()); - List result = new ArrayList<>(); - Optional previous = Optional.empty(); - for (SszField sszField : list) { - if (previous.isPresent()) { - if (sszField.isOverridesOtherField()) { - if (!previous.get().getName().equals(sszField.getName()) - || previous.get().getIndex() != sszField.getIndex()) { - throw new RuntimeException( - String.format( - "Field override configured by field %s but replacement %s doesn't look like a correct override", - sszField, previous.get())); - } - } else { - result.add(previous.get()); - } - } - previous = Optional.of(sszField); - } - result.add(previous.orElseThrow()); - - return result; + return Stream.concat(fieldsA.stream(), fieldsB.stream()) + .sorted(Comparator.comparing(SszField::getIndex)) + .collect(Collectors.toList()); } private void validateFields(final List fields) { diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java index d124c6fe40c..2c452e06610 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/capella/BeaconStateSchemaCapella.java @@ -33,7 +33,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; public class BeaconStateSchemaCapella extends AbstractBeaconStateSchema { @@ -45,9 +45,9 @@ public class BeaconStateSchemaCapella super("BeaconStateCapella", getUniqueFields(specConfig), specConfig); } - public static List getUniqueFields(final SpecConfig specConfig) { + private static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = - SszField.createOverrideField( + new SszField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaCapella(SpecConfigCapella.required(specConfig))); @@ -62,7 +62,7 @@ public static List getUniqueFields(final SpecConfig specConfig) { BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, () -> SszPrimitiveSchemas.UINT64_SCHEMA); return Stream.concat( - BeaconStateSchemaBellatrix.getUniqueFields(specConfig).stream(), + BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), Stream.of( latestExecutionPayloadHeaderField, nextWithdrawalIndexField, diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java index fc9220f87e5..ea54ecca6f0 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/versions/eip4844/BeaconStateSchemaEip4844.java @@ -15,12 +15,15 @@ import static com.google.common.base.Preconditions.checkArgument; import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateSchemaBellatrix.LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX; +import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_INDEX; +import static tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella.NEXT_WITHDRAWAL_VALIDATOR_INDEX; import com.google.common.annotations.VisibleForTesting; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import tech.pegasys.teku.infrastructure.ssz.primitive.SszByte; +import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszPrimitiveListSchema; import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszUInt64ListSchema; import tech.pegasys.teku.infrastructure.ssz.sos.SszField; @@ -32,7 +35,7 @@ import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.AbstractBeaconStateSchema; import tech.pegasys.teku.spec.datastructures.state.beaconstate.common.BeaconStateFields; -import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.capella.BeaconStateSchemaCapella; +import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateSchemaAltair; public class BeaconStateSchemaEip4844 extends AbstractBeaconStateSchema { @@ -44,13 +47,27 @@ public class BeaconStateSchemaEip4844 private static List getUniqueFields(final SpecConfig specConfig) { final SszField latestExecutionPayloadHeaderField = - SszField.createOverrideField( + new SszField( LATEST_EXECUTION_PAYLOAD_HEADER_FIELD_INDEX, BeaconStateFields.LATEST_EXECUTION_PAYLOAD_HEADER, () -> new ExecutionPayloadHeaderSchemaEip4844(SpecConfigEip4844.required(specConfig))); + final SszField nextWithdrawalIndexField = + new SszField( + NEXT_WITHDRAWAL_INDEX, + BeaconStateFields.NEXT_WITHDRAWAL_INDEX, + () -> SszPrimitiveSchemas.UINT64_SCHEMA); + final SszField nextWithdrawalValidatorIndexField = + new SszField( + NEXT_WITHDRAWAL_VALIDATOR_INDEX, + BeaconStateFields.NEXT_WITHDRAWAL_VALIDATOR_INDEX, + () -> SszPrimitiveSchemas.UINT64_SCHEMA); + return Stream.concat( - BeaconStateSchemaCapella.getUniqueFields(specConfig).stream(), - Stream.of(latestExecutionPayloadHeaderField)) + BeaconStateSchemaAltair.getUniqueFields(specConfig).stream(), + Stream.of( + latestExecutionPayloadHeaderField, + nextWithdrawalIndexField, + nextWithdrawalValidatorIndexField)) .collect(Collectors.toList()); } diff --git a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java index 3a55136c9b9..c3583fc0fe2 100644 --- a/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java +++ b/ethereum/spec/src/test/java/tech/pegasys/teku/spec/datastructures/state/beaconstate/common/AbstractBeaconStateSchemaTest.java @@ -139,10 +139,7 @@ public void shouldValidateFieldCount() { public void shouldValidateInvariantFields() { final SszField randomField = new SszField( - 2, - "random", - () -> SszVectorSchema.create(SszPrimitiveSchemas.BYTES32_SCHEMA, 10), - false); + 2, "random", () -> SszVectorSchema.create(SszPrimitiveSchemas.BYTES32_SCHEMA, 10)); assertThatThrownBy( () -> createSchema( diff --git a/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java b/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java index f376f6f59d0..c9bc042dfe1 100644 --- a/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java +++ b/infrastructure/ssz/src/main/java/tech/pegasys/teku/infrastructure/ssz/sos/SszField.java @@ -21,14 +21,13 @@ public class SszField { private final int index; private final String name; private final Supplier> viewType; - private final boolean overridesOtherField; public SszField(int index, SszSchema sszSchema) { this(index, () -> sszSchema); } public SszField(int index, Supplier> viewType) { - this(index, "field-" + index, viewType, false); + this(index, "field-" + index, viewType); } public SszField(int index, SszFieldName name, SszSchema sszSchema) { @@ -36,24 +35,17 @@ public SszField(int index, SszFieldName name, SszSchema sszSchema) { } public SszField(int index, String name, SszSchema sszSchema) { - this(index, name, () -> sszSchema, false); + this(index, name, () -> sszSchema); } public SszField(int index, SszFieldName name, Supplier> viewType) { - this(index, name.getSszFieldName(), viewType, false); + this(index, name.getSszFieldName(), viewType); } - public static SszField createOverrideField( - int index, SszFieldName name, Supplier> viewType) { - return new SszField(index, name.getSszFieldName(), viewType, true); - } - - public SszField( - int index, String name, Supplier> viewType, boolean overridesOtherField) { + public SszField(int index, String name, Supplier> viewType) { this.index = index; this.name = name; this.viewType = viewType; - this.overridesOtherField = overridesOtherField; } public int getIndex() { @@ -67,8 +59,4 @@ public String getName() { public Supplier> getSchema() { return viewType; } - - public boolean isOverridesOtherField() { - return overridesOtherField; - } }