Skip to content

Commit

Permalink
Migrate Attestation and AttnetsENR schemas to registry (#8692)
Browse files Browse the repository at this point in the history
* migrate Attestation and AttnetsENR schemas to registry

* allow `addMilestoneMapping` with same `milestone` and `untilMilestone`
  • Loading branch information
tbenr authored Oct 9, 2024
1 parent 93c8769 commit 82725a0
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRootRequestMessage;
import tech.pegasys.teku.spec.datastructures.state.HistoricalBatch.HistoricalBatchSchema;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes;

public abstract class AbstractSchemaDefinitions implements SchemaDefinitions {
protected SchemaRegistry schemaRegistry;
Expand All @@ -39,8 +40,7 @@ public AbstractSchemaDefinitions(final SchemaRegistry schemaRegistry) {
this.beaconBlocksByRootRequestMessageSchema =
new BeaconBlocksByRootRequestMessage.BeaconBlocksByRootRequestMessageSchema(
schemaRegistry.getSpecConfig());
this.attnetsENRFieldSchema =
SszBitvectorSchema.create(schemaRegistry.getSpecConfig().getAttestationSubnetCount());
this.attnetsENRFieldSchema = schemaRegistry.get(SchemaTypes.ATTNETS_ENR_FIELD_SCHEMA);
}

abstract long getMaxValidatorPerAttestation(SpecConfig specConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationSchema;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof.SignedAggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttesterSlashingElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.IndexedAttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema;
Expand All @@ -64,6 +63,7 @@
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingDeposit;
import tech.pegasys.teku.spec.datastructures.state.versions.electra.PendingPartialWithdrawal;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes;

public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb {

Expand Down Expand Up @@ -114,10 +114,7 @@ public SchemaDefinitionsElectra(final SchemaRegistry schemaRegistry) {
new AttesterSlashingElectraSchema(indexedAttestationSchema)
.castTypeToAttesterSlashingSchema();

this.attestationSchema =
new AttestationElectraSchema(
maxValidatorsPerAttestation, specConfig.getMaxCommitteesPerSlot())
.castTypeToAttestationSchema();
this.attestationSchema = schemaRegistry.get(SchemaTypes.ATTESTATION_SCHEMA);
this.aggregateAndProofSchema = new AggregateAndProofSchema(attestationSchema);
this.signedAggregateAndProofSchema = new SignedAggregateAndProofSchema(aggregateAndProofSchema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationSchema;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof.SignedAggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.AttestationPhase0Schema;
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.AttesterSlashingPhase0Schema;
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.IndexedAttestationPhase0Schema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.phase0.BeaconStateSchemaPhase0;
import tech.pegasys.teku.spec.schemas.registry.SchemaRegistry;
import tech.pegasys.teku.spec.schemas.registry.SchemaTypes;

public class SchemaDefinitionsPhase0 extends AbstractSchemaDefinitions {
private final IndexedAttestationSchema<IndexedAttestation> indexedAttestationSchema;
Expand All @@ -63,10 +63,8 @@ public SchemaDefinitionsPhase0(final SchemaRegistry schemaRegistry) {
new AttesterSlashingPhase0Schema(indexedAttestationSchema)
.castTypeToAttesterSlashingSchema();

this.aggregateAndProofSchema =
new AggregateAndProofSchema(
new AttestationPhase0Schema(getMaxValidatorPerAttestation(specConfig)));
this.attestationSchema = this.aggregateAndProofSchema.getAttestationSchema();
this.attestationSchema = schemaRegistry.get(SchemaTypes.ATTESTATION_SCHEMA);
this.aggregateAndProofSchema = new AggregateAndProofSchema(attestationSchema);
this.signedAggregateAndProofSchema = new SignedAggregateAndProofSchema(aggregateAndProofSchema);
this.beaconStateSchema = BeaconStateSchemaPhase0.create(specConfig);
this.beaconBlockBodySchema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected AbstractSchemaProvider(final SchemaId<T> schemaId) {
protected void addMilestoneMapping(
final SpecMilestone milestone, final SpecMilestone untilMilestone) {
checkArgument(
untilMilestone.isGreaterThan(milestone),
"%s must be earlier than %s",
untilMilestone.isGreaterThanOrEqualTo(milestone),
"%s must be earlier then or equal to %s",
milestone,
untilMilestone);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.schemas.registry;

import static tech.pegasys.teku.spec.SpecMilestone.DENEB;
import static tech.pegasys.teku.spec.SpecMilestone.ELECTRA;
import static tech.pegasys.teku.spec.SpecMilestone.PHASE0;
import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.ATTESTATION_SCHEMA;

import java.util.Set;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.config.SpecConfig;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.AttestationPhase0Schema;

public class AttestationSchemaProvider
extends AbstractSchemaProvider<AttestationSchema<Attestation>> {

public AttestationSchemaProvider() {
super(ATTESTATION_SCHEMA);
addMilestoneMapping(PHASE0, DENEB);
addMilestoneMapping(ELECTRA, SpecMilestone.getHighestMilestone());
}

@Override
protected AttestationSchema<Attestation> createSchema(
final SchemaRegistry registry,
final SpecMilestone effectiveMilestone,
final SpecConfig specConfig) {
return switch (effectiveMilestone) {
case PHASE0 ->
new AttestationPhase0Schema(specConfig.getMaxValidatorsPerCommittee())
.castTypeToAttestationSchema();
case ELECTRA ->
new AttestationElectraSchema(
(long) specConfig.getMaxValidatorsPerCommittee()
* specConfig.getMaxCommitteesPerSlot(),
specConfig.getMaxCommitteesPerSlot())
.castTypeToAttestationSchema();
default ->
throw new IllegalArgumentException(
"It is not supposed to create a specific version for " + effectiveMilestone);
};
}

@Override
public Set<SpecMilestone> getSupportedMilestones() {
return ALL_MILESTONES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.schemas.registry;

import static tech.pegasys.teku.spec.SpecMilestone.PHASE0;
import static tech.pegasys.teku.spec.schemas.registry.SchemaTypes.ATTNETS_ENR_FIELD_SCHEMA;

import java.util.Set;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.config.SpecConfig;

public class AttnetsENRFieldSchemaProvider
extends AbstractSchemaProvider<SszBitvectorSchema<SszBitvector>> {
public AttnetsENRFieldSchemaProvider() {
super(ATTNETS_ENR_FIELD_SCHEMA);
addMilestoneMapping(PHASE0, SpecMilestone.getHighestMilestone());
}

@Override
protected SszBitvectorSchema<SszBitvector> createSchema(
final SchemaRegistry registry,
final SpecMilestone effectiveMilestone,
final SpecConfig specConfig) {
return SszBitvectorSchema.create(specConfig.getAttestationSubnetCount());
}

@Override
public Set<SpecMilestone> getSupportedMilestones() {
return ALL_MILESTONES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class SchemaRegistryBuilder {
private final SchemaCache cache;

public static SchemaRegistryBuilder create() {
return new SchemaRegistryBuilder();
return new SchemaRegistryBuilder()
.addProvider(new AttnetsENRFieldSchemaProvider())
.addProvider(new AttestationSchemaProvider());
}

public SchemaRegistryBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;

public class SchemaTypes {
// PHASE0
public static final SchemaId<SszBitvectorSchema<SszBitvector>> ATTNETS_ENR_FIELD_SCHEMA =
create("ATTNETS_ENR_FIELD_SCHEMA");

public static final SchemaId<AttestationSchema<Attestation>> ATTESTATION_SCHEMA =
create("ATTESTATION_SCHEMA");

// Altair

// Bellatrix
Expand Down

0 comments on commit 82725a0

Please sign in to comment.