Skip to content

Commit

Permalink
OnchainAttestation
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Sep 25, 2024
1 parent bb3856f commit 6832183
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectra;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.phase0.AttestationPhase0;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionCache;
import tech.pegasys.teku.spec.util.DataStructureUtil;
Expand Down Expand Up @@ -111,7 +111,7 @@ void shouldReadRequestBody() throws IOException {
((SignedAggregateAndProof) ((List<?>) requestBody).get(0))
.getMessage()
.getAggregate())
.isInstanceOf(AttestationElectra.class);
.isInstanceOf(OnchainAttestation.class);
} else {
assertThat(
((SignedAggregateAndProof) ((List<?>) requestBody).get(0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.constants.Domain;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectra;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestation;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestationSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public class ValidatableAttestation {
Expand All @@ -54,29 +56,41 @@ public class ValidatableAttestation {

private volatile Optional<SszBitlist> singleAttestationAggregationBits = Optional.empty();

public ValidatableAttestation convertFromSingleAttestationIfRequired() {
if (!attestation.isSingleAttestation()) {
public ValidatableAttestation convertToOnchainAttestationIfRequired() {

if (attestation.isOnchainAttestation()) {
return this;
}

final AttestationElectraSchema attestationElectraSchema =
spec.atSlot(attestation.getData().getSlot())
.getSchemaDefinitions()
.getAttestationSchema()
.toVersionElectra()
.orElseThrow();

final AttestationElectra convertedAttestation =
attestationElectraSchema.create(
singleAttestationAggregationBits.orElseThrow(),
attestation.getData(),
attestation.getAggregateSignature(),
attestationElectraSchema
.getCommitteeBitsSchema()
.orElseThrow()
.ofBits(attestation.getFirstCommitteeIndex().intValue()));

return from(spec, convertedAttestation);
if (attestation.isSingleAttestation()) {
final OnchainAttestationSchema attestationElectraSchema =
spec.atSlot(attestation.getData().getSlot())
.getSchemaDefinitions()
.toVersionElectra()
.orElseThrow()
.getOnchainAttestationAttestationSchema();

final OnchainAttestation convertedAttestation =
attestationElectraSchema.create(
singleAttestationAggregationBits.orElseThrow(),
attestation.getData(),
attestation.getAggregateSignature(),
attestationElectraSchema
.getCommitteeBitsSchema()
.orElseThrow()
.ofBits(attestation.getFirstCommitteeIndex().intValue()));

return from(spec, convertedAttestation);
}

// if we are going to change Attestation in Electra (ie adding committee_index)
// we could directly check the version here instead of looking up in the spec version
final SpecVersion specVersion = spec.atSlot(attestation.getData().getSlot());
if (specVersion.getMilestone().isGreaterThanOrEqualTo(SpecMilestone.ELECTRA)) {
// TODO convert
}

return this;
}

public static ValidatableAttestation from(final Spec spec, final Attestation attestation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChange;
import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChangeSchema;
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestationSchema;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;
Expand Down Expand Up @@ -118,7 +118,7 @@ public static BeaconBlockBodySchemaElectraImpl create(
namedSchema(
BlockBodyFields.ATTESTATIONS,
SszListSchema.create(
new AttestationElectraSchema(
new OnchainAttestationSchema(
maxValidatorsPerAttestation, specConfig.getMaxCommitteesPerSlot())
.castTypeToAttestationSchema(),
specConfig.getMaxAttestationsElectra())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChange;
import tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChangeSchema;
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestationSchema;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;
Expand Down Expand Up @@ -117,7 +117,7 @@ public static BlindedBeaconBlockBodySchemaElectraImpl create(
namedSchema(
BlockBodyFields.ATTESTATIONS,
SszListSchema.create(
new AttestationElectraSchema(
new OnchainAttestationSchema(
maxValidatorsPerAttestation, specConfig.getMaxCommitteesPerSlot())
.castTypeToAttestationSchema(),
specConfig.getMaxAttestationsElectra())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ default boolean isSingleAttestation() {
return false;
}

default boolean isOnchainAttestation() {
return false;
}

default Optional<UInt64> getValidatorIndex() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import tech.pegasys.teku.infrastructure.ssz.schema.SszContainerSchema;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitlistSchema;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBitvectorSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.OnchainAttestationSchema;

public interface AttestationSchema<T extends Attestation> extends SszContainerSchema<T> {

Expand Down Expand Up @@ -55,7 +55,7 @@ default AttestationSchema<Attestation> castTypeToAttestationSchema() {
return (AttestationSchema<Attestation>) this;
}

default Optional<AttestationElectraSchema> toVersionElectra() {
default Optional<OnchainAttestationSchema> toVersionElectra() {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;

public class AttestationElectra
extends Container4<AttestationElectra, SszBitlist, AttestationData, SszSignature, SszBitvector>
public class OnchainAttestation
extends Container4<OnchainAttestation, SszBitlist, AttestationData, SszSignature, SszBitvector>
implements Attestation {

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

public AttestationElectra(
final AttestationElectraSchema schema,
public OnchainAttestation(
final OnchainAttestationSchema schema,
final SszBitlist aggregationBits,
final AttestationData data,
final BLSSignature signature,
Expand All @@ -43,8 +43,8 @@ public AttestationElectra(
}

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

@Override
Expand Down Expand Up @@ -82,4 +82,9 @@ public UInt64 getFirstCommitteeIndex() {
public boolean requiresCommitteeBits() {
return true;
}

@Override
public boolean isOnchainAttestation() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;

public class AttestationElectraSchema
public class OnchainAttestationSchema
extends ContainerSchema4<
AttestationElectra, SszBitlist, AttestationData, SszSignature, SszBitvector>
implements AttestationSchema<AttestationElectra> {
OnchainAttestation, SszBitlist, AttestationData, SszSignature, SszBitvector>
implements AttestationSchema<OnchainAttestation> {

public AttestationElectraSchema(
public OnchainAttestationSchema(
final long maxValidatorsPerAttestation, final long maxCommitteePerSlot) {
super(
"AttestationElectra",
"OnchainAttestation",
namedSchema("aggregation_bits", SszBitlistSchema.create(maxValidatorsPerAttestation)),
namedSchema("data", AttestationData.SSZ_SCHEMA),
namedSchema("signature", SszSignatureSchema.INSTANCE),
Expand All @@ -56,8 +56,8 @@ public Optional<SszBitvectorSchema<?>> getCommitteeBitsSchema() {
}

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

@Override
Expand All @@ -67,20 +67,20 @@ public Attestation create(
final BLSSignature signature,
final Supplier<SszBitvector> committeeBits) {
final SszBitvector suppliedCommitteeBits = committeeBits.get();
checkNotNull(suppliedCommitteeBits, "committeeBits must be provided in Electra");
return new AttestationElectra(this, aggregationBits, data, signature, suppliedCommitteeBits);
checkNotNull(suppliedCommitteeBits, "committeeBits must be provided");
return new OnchainAttestation(this, aggregationBits, data, signature, suppliedCommitteeBits);
}

public AttestationElectra create(
public OnchainAttestation create(
final SszBitlist aggregationBits,
final AttestationData data,
final BLSSignature signature,
final SszBitvector committeeBits) {
return new AttestationElectra(this, aggregationBits, data, signature, committeeBits);
return new OnchainAttestation(this, aggregationBits, data, signature, committeeBits);
}

@Override
public Optional<AttestationElectraSchema> toVersionElectra() {
public Optional<OnchainAttestationSchema> toVersionElectra() {
return Optional.of(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.WithdrawalRequest;
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.WithdrawalRequestSchema;
import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof.AggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashingSchema;
Expand All @@ -59,9 +58,9 @@
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof.SignedAggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.SingleAttestation;
import tech.pegasys.teku.spec.datastructures.operations.SingleAttestationSchema;
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.operations.versions.electra.OnchainAttestationSchema;
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 All @@ -73,7 +72,7 @@
public class SchemaDefinitionsElectra extends SchemaDefinitionsDeneb {
private final IndexedAttestationSchema<IndexedAttestation> indexedAttestationSchema;
private final AttesterSlashingSchema<AttesterSlashing> attesterSlashingSchema;
private final AttestationSchema<Attestation> attestationSchema;
private final OnchainAttestationSchema onchainAttestationAttestationSchema;
private final SignedAggregateAndProofSchema signedAggregateAndProofSchema;
private final AggregateAndProofSchema aggregateAndProofSchema;

Expand Down Expand Up @@ -124,11 +123,10 @@ public SchemaDefinitionsElectra(final SpecConfigElectra specConfig) {

this.singleAttestationSchema = new SingleAttestationSchema();

this.attestationSchema =
new AttestationElectraSchema(
maxValidatorsPerAttestation, specConfig.getMaxCommitteesPerSlot())
.castTypeToAttestationSchema();
this.aggregateAndProofSchema = new AggregateAndProofSchema(attestationSchema);
this.onchainAttestationAttestationSchema =
new OnchainAttestationSchema(
maxValidatorsPerAttestation, specConfig.getMaxCommitteesPerSlot());
this.aggregateAndProofSchema = new AggregateAndProofSchema(onchainAttestationAttestationSchema);
this.signedAggregateAndProofSchema = new SignedAggregateAndProofSchema(aggregateAndProofSchema);

this.executionPayloadSchemaElectra = new ExecutionPayloadSchemaElectra(specConfig);
Expand Down Expand Up @@ -208,11 +206,6 @@ public AggregateAndProofSchema getAggregateAndProofSchema() {
return aggregateAndProofSchema;
}

@Override
public AttestationSchema<Attestation> getAttestationSchema() {
return attestationSchema;
}

@Override
public IndexedAttestationSchema<IndexedAttestation> getIndexedAttestationSchema() {
return indexedAttestationSchema;
Expand Down Expand Up @@ -361,6 +354,10 @@ public AttestationSchema<SingleAttestation> getSingleAttestationSchema() {
return singleAttestationSchema;
}

public OnchainAttestationSchema getOnchainAttestationAttestationSchema() {
return onchainAttestationAttestationSchema;
}

public PendingPartialWithdrawal.PendingPartialWithdrawalSchema
getPendingPartialWithdrawalSchema() {
return pendingPartialWithdrawalSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ public AttestationData getAttestationData() {
* @return True if the attestation was added, false otherwise
*/
public boolean add(final ValidatableAttestation attestation) {
final ValidatableAttestation converted = attestation.convertFromSingleAttestationIfRequired();
final ValidatableAttestation converted = attestation.convertToOnchainAttestationIfRequired();
if (includedValidators.isSuperSetOf(converted.getAttestation())) {
// All attestation bits have already been included on chain
return false;
}
if (committeeShufflingSeed.isEmpty()) {
committeeShufflingSeed = converted.getCommitteeShufflingSeed();
// converted do not maintain shuffling, we need to take it from the original
committeeShufflingSeed = attestation.getCommitteeShufflingSeed();
}
return attestationsByValidatorCount
.computeIfAbsent(
Expand Down

0 comments on commit 6832183

Please sign in to comment.