Skip to content

Commit

Permalink
Merge pull request #8 from mehdi-aouadi/8026-1-containers
Browse files Browse the repository at this point in the history
add electra attestation container
  • Loading branch information
mehdi-aouadi authored Mar 6, 2024
2 parents 7870a5e + 4afbef0 commit b4eaa72
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;

public class Attestation
extends Container3<Attestation, SszBitlist, AttestationData, SszSignature> {
public class Attestation extends Container3<Attestation, SszBitlist, AttestationData, SszSignature>
implements AttestationContainer {

public static class AttestationSchema
extends ContainerSchema3<Attestation, SszBitlist, AttestationData, SszSignature> {
extends ContainerSchema3<Attestation, SszBitlist, AttestationData, SszSignature>
implements AttestationContainerSchema<Attestation> {

public AttestationSchema(final SpecConfig specConfig) {
super(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.operations;

import java.util.Collection;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.SszContainer;
import tech.pegasys.teku.infrastructure.ssz.SszData;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;

public interface AttestationContainer extends SszData, SszContainer {
AttestationData getData();

BLSSignature getAggregateSignature();

UInt64 getEarliestSlotForForkChoiceProcessing(Spec spec);

Collection<Bytes32> getDependentBlockRoots();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.operations;

import tech.pegasys.teku.infrastructure.ssz.schema.SszContainerSchema;

public interface AttestationContainerSchema<T extends AttestationContainer>
extends SszContainerSchema<T> {

@SuppressWarnings("unchecked")
default AttestationContainerSchema<AttestationContainer> castTypeToAttestationContainer() {
return (AttestationContainerSchema<AttestationContainer>) this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.operations.versions.electra;

import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.Container4;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.operations.AttestationContainer;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;

public class AttestationElectra
extends Container4<
AttestationElectra, SszList<SszBitlist>, AttestationData, SszBitvector, SszSignature>
implements AttestationContainer {

public AttestationElectra(final AttestationElectraSchema type, final TreeNode backingNode) {
super(type, backingNode);
}

public AttestationElectra(
final AttestationElectraSchema schema,
final SszList<SszBitlist> aggregationBits,
final AttestationData data,
final SszBitvector committeeBits,
final BLSSignature signature) {
super(schema, aggregationBits, data, committeeBits, new SszSignature(signature));
}

@Override
public AttestationElectraSchema getSchema() {
return (AttestationElectraSchema) super.getSchema();
}

public UInt64 getEarliestSlotForForkChoiceProcessing(final Spec spec) {
return getData().getEarliestSlotForForkChoice(spec);
}

public Collection<Bytes32> getDependentBlockRoots() {
return Sets.newHashSet(getData().getTarget().getRoot(), getData().getBeaconBlockRoot());
}

public SszList<SszBitlist> getAggregationBits() {
return getField0();
}

public AttestationData getData() {
return getField1();
}

public SszBitvector getCommitteeBits() {
return getField2();
}

public BLSSignature getAggregateSignature() {
return getField3().getSignature();
}

public List<UInt64> getCommitteeIndices(AttestationElectra attestationElectra) {
return attestationElectra
.getCommitteeBits()
.getAllSetBits()
.intStream()
.mapToObj(UInt64::valueOf)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.operations.versions.electra;

import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.containers.ContainerSchema4;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitlistSchema;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.datastructures.operations.AttestationContainerSchema;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;

public class AttestationElectraSchema
extends ContainerSchema4<
AttestationElectra, SszList<SszBitlist>, AttestationData, SszBitvector, SszSignature>
implements AttestationContainerSchema<AttestationElectra> {

public AttestationElectraSchema(final SpecConfig specConfig) {
super(
"Attestation",
namedSchema(
"aggregation_bits",
SszListSchema.create(
SszBitlistSchema.create(specConfig.getMaxValidatorsPerCommittee()),
specConfig.getMaxCommitteesPerSlot())),
namedSchema("data", AttestationData.SSZ_SCHEMA),
namedSchema(
"committee_bits", SszBitvectorSchema.create(specConfig.getMaxCommitteesPerSlot())),
namedSchema("signature", SszSignatureSchema.INSTANCE));
}

public SszListSchema<SszList<SszBitvector>, ?> getAggregationBitsSchema() {
return (SszListSchema<SszList<SszBitvector>, ?>) getFieldSchema0();
}

@Override
public AttestationElectra createFromBackingNode(TreeNode node) {
return new AttestationElectra(this, node);
}

public AttestationElectra create(
final SszList<SszBitlist> aggregationBits,
final AttestationData data,
final SszBitvector committeeBits,
final BLSSignature signature) {
return new AttestationElectra(this, aggregationBits, data, committeeBits, signature);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadSchema;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionPayloadHeaderSchemaElectra;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.ExecutionPayloadSchemaElectra;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateElectra;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.electra.BeaconStateSchemaElectra;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb {
private final BlobsBundleSchema blobsBundleSchema;
private final ExecutionPayloadAndBlobsBundleSchema executionPayloadAndBlobsBundleSchema;

private final AttestationElectraSchema attestationElectraSchema;

public SchemaDefinitionsElectra(final SpecConfigElectra specConfig) {
super(specConfig);
this.executionPayloadSchemaElectra = new ExecutionPayloadSchemaElectra(specConfig);
Expand Down Expand Up @@ -115,6 +118,7 @@ public SchemaDefinitionsElectra(final SpecConfigElectra specConfig) {
"BlobsBundleElectra", getBlobSchema(), getBlobKzgCommitmentsSchema(), specConfig);
this.executionPayloadAndBlobsBundleSchema =
new ExecutionPayloadAndBlobsBundleSchema(executionPayloadSchemaElectra, blobsBundleSchema);
this.attestationElectraSchema = new AttestationElectraSchema(specConfig);
}

public static SchemaDefinitionsElectra required(final SchemaDefinitions schemaDefinitions) {
Expand Down Expand Up @@ -236,4 +240,8 @@ public ExecutionPayloadAndBlobsBundleSchema getExecutionPayloadAndBlobsBundleSch
public Optional<SchemaDefinitionsElectra> toVersionElectra() {
return Optional.of(this);
}

public AttestationElectraSchema getAttestationElectraSchema() {
return attestationElectraSchema;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.operations.versions.electra;

import static tech.pegasys.teku.spec.propertytest.util.PropertyTestHelper.assertDeserializeMutatedThrowsExpected;
import static tech.pegasys.teku.spec.propertytest.util.PropertyTestHelper.assertRoundTrip;

import com.fasterxml.jackson.core.JsonProcessingException;
import net.jqwik.api.ForAll;
import net.jqwik.api.Property;
import tech.pegasys.teku.spec.propertytest.suppliers.operations.versions.electra.AttestationElectraSupplier;

public class AttestationElectraPropertyTest {
@Property
void roundTrip(
@ForAll(supplier = AttestationElectraSupplier.class)
final AttestationElectra attestationElectra)
throws JsonProcessingException {
assertRoundTrip(attestationElectra);
}

@Property
void deserializeMutated(
@ForAll(supplier = AttestationElectraSupplier.class)
final AttestationElectra attestationElectra,
@ForAll final int seed) {
assertDeserializeMutatedThrowsExpected(attestationElectra, seed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.propertytest.suppliers.operations.versions.electra;

import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectra;
import tech.pegasys.teku.spec.propertytest.suppliers.DataStructureUtilSupplier;
import tech.pegasys.teku.spec.util.DataStructureUtil;

public class AttestationElectraSupplier extends DataStructureUtilSupplier<AttestationElectra> {
public AttestationElectraSupplier() {
super(DataStructureUtil::randomAttestationElectra, SpecMilestone.ELECTRA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import tech.pegasys.teku.spec.config.SpecConfigBellatrix;
import tech.pegasys.teku.spec.config.SpecConfigCapella;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.config.SpecConfigElectra;
import tech.pegasys.teku.spec.constants.Domain;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.Blob;
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobKzgCommitmentsSchema;
Expand Down Expand Up @@ -153,6 +154,7 @@
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;
import tech.pegasys.teku.spec.datastructures.operations.versions.bellatrix.BeaconPreparableProposer;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectra;
import tech.pegasys.teku.spec.datastructures.state.AnchorPoint;
import tech.pegasys.teku.spec.datastructures.state.Checkpoint;
import tech.pegasys.teku.spec.datastructures.state.Fork;
Expand Down Expand Up @@ -184,6 +186,7 @@
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsBellatrix;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsCapella;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsDeneb;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra;

public final class DataStructureUtil {

Expand Down Expand Up @@ -773,6 +776,32 @@ public Attestation randomAttestation() {
.create(randomBitlist(), randomAttestationData(), randomSignature());
}

public AttestationElectra randomAttestationElectra() {
return randomAttestationElectra(randomUInt64());
}

public AttestationElectra randomAttestationElectra(final UInt64 slot) {
final int maxCommitteePerSlot =
SpecConfigElectra.required(spec.forMilestone(SpecMilestone.ELECTRA).getConfig())
.getMaxCommitteesPerSlot();
final int maxValidatorsPerCommittee =
SpecConfigElectra.required(spec.forMilestone(SpecMilestone.ELECTRA).getConfig())
.getMaxValidatorsPerCommittee();
final SszList<SszBitlist> randomAggregationBits =
randomSszList(
SszListSchema.create(
SszBitlistSchema.create(maxValidatorsPerCommittee), maxCommitteePerSlot),
this::randomBitlist,
maxCommitteePerSlot);
return getElectraSchemaDefinitions(slot)
.getAttestationElectraSchema()
.create(
randomAggregationBits,
randomAttestationData(),
randomSszBitvector(maxCommitteePerSlot),
randomSignature());
}

public Attestation randomAttestation(final long slot) {
return randomAttestation(UInt64.valueOf(slot));
}
Expand Down Expand Up @@ -2566,6 +2595,10 @@ private SchemaDefinitionsDeneb getDenebSchemaDefinitions(final UInt64 slot) {
return SchemaDefinitionsDeneb.required(spec.atSlot(slot).getSchemaDefinitions());
}

private SchemaDefinitionsElectra getElectraSchemaDefinitions(final UInt64 slot) {
return SchemaDefinitionsElectra.required(spec.atSlot(slot).getSchemaDefinitions());
}

int getEpochsPerEth1VotingPeriod() {
return getConstant(SpecConfig::getEpochsPerEth1VotingPeriod);
}
Expand Down

0 comments on commit b4eaa72

Please sign in to comment.