Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eip4844 schema definitions #6441

Merged
merged 10 commits into from
Nov 15, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignedBeaconBlockAndBlobsSidecar, SignedBeaconBlock, BlobsSidecar> {

public static class SignedBeaconBlockAndBlobsSidecarSchema
extends ContainerSchema2<SignedBeaconBlockAndBlobsSidecar, SignedBeaconBlock, BlobsSidecar> {

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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SignedBeaconBlockAndBlobsSidecar, SignedBeaconBlock, BlobsSidecar> {

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlobsSidecar, SszBytes32, SszUInt64, SszList<Blob>, SszKZGProof> {

public static class BlobsSidecarSchema
extends ContainerSchema4<BlobsSidecar, SszBytes32, SszUInt64, SszList<Blob>, 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<Blob, ?> getBlobsSchema() {
return (SszListSchema<Blob, ?>) 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlobsSidecar, SszBytes32, SszUInt64, SszList<Blob>, 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<Blob, ?> getBlobsSchema() {
return (SszListSchema<Blob, ?>) 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -182,4 +183,8 @@ default Optional<BeaconStateBellatrix> toVersionBellatrix() {
default Optional<BeaconStateCapella> toVersionCapella() {
return Optional.empty();
}

default Optional<BeaconStateEip4844> toVersionEip4844() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -213,4 +214,8 @@ default Optional<MutableBeaconStateBellatrix> toMutableVersionBellatrix() {
default Optional<MutableBeaconStateCapella> toMutableVersionCapella() {
return Optional.empty();
}

default Optional<MutableBeaconStateEip4844> toMutableVersionEip4844() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeaconStateCapellaImpl>
Expand All @@ -45,7 +44,6 @@ protected BeaconStateCapellaImpl createImmutableBeaconState(

@Override
protected void addCustomFields(final MoreObjects.ToStringHelper stringBuilder) {
BeaconStateBellatrix.describeCustomBellatrixFields(stringBuilder, this);
BeaconStateCapella.describeCustomCapellaFields(stringBuilder, this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <E1 extends Exception, E2 extends Exception, E3 extends Exception>
BeaconStateEip4844 updatedEip4844(
final Mutator<MutableBeaconStateEip4844, E1, E2, E3> mutator) throws E1, E2, E3 {
MutableBeaconStateEip4844 writableCopy = createWritableCopy();
mutator.mutate(writableCopy);
return writableCopy.commitChanges();
}

@Override
default Optional<BeaconStateEip4844> toVersionEip4844() {
return Optional.of(this);
}
}
Original file line number Diff line number Diff line change
@@ -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<MutableBeaconStateEip4844>
implements BeaconStateEip4844, BeaconStateCache, ValidatorStatsAltair {

BeaconStateEip4844Impl(
final BeaconStateSchema<BeaconStateEip4844, MutableBeaconStateEip4844> schema) {
super(schema);
}

BeaconStateEip4844Impl(
final SszCompositeSchema<?> type,
final TreeNode backingNode,
final IntCache<SszData> cache,
final TransitionCaches transitionCaches) {
super(type, backingNode, cache, transitionCaches);
}

BeaconStateEip4844Impl(
final AbstractSszContainerSchema<? extends SszContainer> 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);
}
}
Loading