Skip to content

Commit

Permalink
Merge pull request #15 from mehdi-aouadi/8123-indexed-attestation-con…
Browse files Browse the repository at this point in the history
…tainers

refcator indexed attestaion containers for Electra
  • Loading branch information
mehdi-aouadi committed Mar 26, 2024
2 parents 9d1aefb + 9b93e2e commit 3a70a0f
Show file tree
Hide file tree
Showing 35 changed files with 550 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing.AttesterSlashingSchema;

@SuppressWarnings("JavaCase")
public class AttesterSlashing {
public class AttesterSlashing implements AttesterSlashingContainer {
public final IndexedAttestation attestation_1;
public final IndexedAttestation attestation_2;

Expand All @@ -44,6 +45,7 @@ public AttesterSlashing(
return asInternalAttesterSlashing(spec.atSlot(attestation_1.data.slot));
}

@Override
public tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing
asInternalAttesterSlashing(final SpecVersion spec) {
final AttesterSlashingSchema attesterSlashingSchema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.electra.AttesterSlashingElectra;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
import tech.pegasys.teku.spec.datastructures.operations.versions.electra.IndexedAttestationElectra;

@SuppressWarnings("JavaCase")
public class BeaconBlockBody {
Expand All @@ -40,7 +44,7 @@ public class BeaconBlockBody {
public final Bytes32 graffiti;

public final List<ProposerSlashing> proposer_slashings;
public final List<AttesterSlashing> attester_slashings;
public final List<AttesterSlashingContainer> attester_slashings;
public final List<Attestation> attestations;
public final List<Deposit> deposits;
public final List<SignedVoluntaryExit> voluntary_exits;
Expand All @@ -51,7 +55,7 @@ public BeaconBlockBody(
@JsonProperty("eth1_data") final Eth1Data eth1_data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposer_slashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attester_slashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attester_slashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntary_exits) {
Expand All @@ -72,8 +76,16 @@ public BeaconBlockBody(
this.graffiti = body.getGraffiti();
this.proposer_slashings =
body.getProposerSlashings().stream().map(ProposerSlashing::new).toList();
this.attester_slashings =
body.getAttesterSlashings().stream().map(AttesterSlashing::new).toList();
this.attester_slashings = new ArrayList<>();
body.getAttesterSlashings().stream()
.forEach(
attesterSlashing -> {
if (attesterSlashing.getAttestation1() instanceof IndexedAttestationElectra) {
this.attester_slashings.add(new AttesterSlashingElectra(attesterSlashing));
} else {
this.attester_slashings.add(new AttesterSlashing(attesterSlashing));
}
});
this.attestations = body.getAttestations().stream().map(Attestation::new).toList();
this.deposits = body.getDeposits().stream().map(Deposit::new).toList();
this.voluntary_exits = body.getVoluntaryExits().stream().map(SignedVoluntaryExit::new).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.Objects;
import tech.pegasys.teku.api.schema.interfaces.IndexedAttestationContainer;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema;
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationContainer;

@SuppressWarnings("JavaCase")
public class IndexedAttestation {
public class IndexedAttestation implements IndexedAttestationContainer {
@ArraySchema(schema = @Schema(type = "string", format = "uint64"))
public final List<UInt64> attesting_indices;

Expand All @@ -37,7 +37,9 @@ public class IndexedAttestation {
@Schema(type = "string", format = "byte", description = DESCRIPTION_BYTES96)
public final BLSSignature signature;

public IndexedAttestation(IndexedAttestationContainer indexedAttestation) {
public IndexedAttestation(
tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationContainer
indexedAttestation) {
this.attesting_indices = indexedAttestation.getAttestingIndices().streamUnboxed().toList();
this.data = new AttestationData(indexedAttestation.getData());
this.signature = new BLSSignature(indexedAttestation.getSignature());
Expand All @@ -53,12 +55,12 @@ public IndexedAttestation(
this.signature = signature;
}

public tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation
public tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationContainer
asInternalIndexedAttestation(final Spec spec) {
return asInternalIndexedAttestation(spec.atSlot(data.slot));
}

public tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation
public tech.pegasys.teku.spec.datastructures.operations.IndexedAttestationContainer
asInternalIndexedAttestation(final SpecVersion spec) {
final IndexedAttestationSchema indexedAttestationSchema =
spec.getSchemaDefinitions().getIndexedAttestationSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.BeaconBlockBody;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
Expand All @@ -45,7 +45,7 @@ public BeaconBlockBodyAltair(
@JsonProperty("eth1_data") final Eth1Data eth1_data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposer_slashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attester_slashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attester_slashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntary_exits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
Expand All @@ -45,7 +45,7 @@ public BeaconBlockBodyBellatrix(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
Expand All @@ -45,7 +45,7 @@ public BlindedBeaconBlockBodyBellatrix(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.spec.SpecVersion;
Expand All @@ -48,7 +48,7 @@ public BeaconBlockBodyCapella(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.spec.SpecVersion;
Expand All @@ -49,7 +49,7 @@ public BlindedBeaconBlockBodyCapella(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
Expand All @@ -30,6 +29,7 @@
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.capella.SignedBlsToExecutionChange;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.spec.SpecVersion;
Expand All @@ -54,7 +54,7 @@ public BeaconBlockBodyDeneb(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
Expand All @@ -30,6 +29,7 @@
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.capella.SignedBlsToExecutionChange;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.spec.SpecVersion;
Expand All @@ -55,7 +55,7 @@ public BlindedBeaconBlockBodyDeneb(
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashingContainer> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.api.schema.electra;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import tech.pegasys.teku.api.schema.interfaces.AttesterSlashingContainer;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing.AttesterSlashingSchema;

@SuppressWarnings("JavaCase")
public class AttesterSlashingElectra implements AttesterSlashingContainer {
public final IndexedAttestationElectra attestation_1;
public final IndexedAttestationElectra attestation_2;

public AttesterSlashingElectra(AttesterSlashing attesterSlashing) {
this.attestation_1 = new IndexedAttestationElectra(attesterSlashing.getAttestation1());
this.attestation_2 = new IndexedAttestationElectra(attesterSlashing.getAttestation2());
}

@JsonCreator
public AttesterSlashingElectra(
@JsonProperty("attestation_1") final IndexedAttestationElectra attestation_1,
@JsonProperty("attestation_2") final IndexedAttestationElectra attestation_2) {
this.attestation_1 = attestation_1;
this.attestation_2 = attestation_2;
}

public AttesterSlashing asInternalAttesterSlashing(final Spec spec) {
return asInternalAttesterSlashing(spec.atSlot(attestation_1.data.slot));
}

@Override
public AttesterSlashing asInternalAttesterSlashing(final SpecVersion spec) {
final AttesterSlashingSchema attesterSlashingSchema =
spec.getSchemaDefinitions().getAttesterSlashingSchema();
return attesterSlashingSchema.create(
attestation_1.asInternalIndexedAttestation(spec),
attestation_2.asInternalIndexedAttestation(spec));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AttesterSlashingElectra)) {
return false;
}
AttesterSlashingElectra that = (AttesterSlashingElectra) o;
return Objects.equals(attestation_1, that.attestation_1)
&& Objects.equals(attestation_2, that.attestation_2);
}

@Override
public int hashCode() {
return Objects.hash(attestation_1, attestation_2);
}
}
Loading

0 comments on commit 3a70a0f

Please sign in to comment.