Skip to content

Commit

Permalink
SingleAttestation
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Sep 25, 2024
1 parent fd599c4 commit bb3856f
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import org.apache.tuweni.bytes.Bytes32;
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.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.state.beaconstate.BeaconState;

public class ValidatableAttestation {
Expand All @@ -49,6 +52,33 @@ public class ValidatableAttestation {
private volatile Optional<Bytes32> committeeShufflingSeed = Optional.empty();
private volatile Optional<Int2IntMap> committeesSize = Optional.empty();

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

public ValidatableAttestation convertFromSingleAttestationIfRequired() {
if (!attestation.isSingleAttestation()) {
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);
}

public static ValidatableAttestation from(final Spec spec, final Attestation attestation) {
return new ValidatableAttestation(
spec, attestation, Optional.empty(), OptionalInt.empty(), false);
Expand Down Expand Up @@ -204,6 +234,15 @@ private void saveCommitteesSize(final BeaconState state) {
this.committeesSize = Optional.of(committeesSize);
}

public Optional<SszBitlist> getSingleAttestationAggregationBits() {
return singleAttestationAggregationBits;
}

public void setSingleAttestationAggregationBits(
final SszBitlist singleAttestationAggregationBits) {
this.singleAttestationAggregationBits = Optional.of(singleAttestationAggregationBits);
}

public boolean isGossiped() {
return gossiped.get();
}
Expand Down Expand Up @@ -273,6 +312,7 @@ public String toString() {
.add("committeeShufflingSeed", committeeShufflingSeed)
.add("committeesSize", committeesSize)
.add("receivedSubnetId", receivedSubnetId)
.add("singleAttestationAggregationBits", singleAttestationAggregationBits)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.datastructures.operations;

import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand All @@ -34,9 +35,13 @@ public interface Attestation extends SszContainer {
@Override
AttestationSchema<? extends Attestation> getSchema();

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

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

AttestationData getData();

Expand Down Expand Up @@ -65,4 +70,12 @@ default List<UInt64> getCommitteeIndicesRequired() {
}

boolean requiresCommitteeBits();

default boolean isSingleAttestation() {
return false;
}

default Optional<UInt64> getValidatorIndex() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ default SszBitlist createEmptyAggregationBits() {
return bitsSchema.ofBits(Math.toIntExact(bitsSchema.getMaxLength()));
}

default SszBitlist createAggregationBitsOf(final int... indices) {
final SszBitlistSchema<?> bitsSchema = getAggregationBitsSchema();
return bitsSchema.ofBits(Math.toIntExact(bitsSchema.getMaxLength()), indices);
}

default Optional<SszBitvector> createEmptyCommitteeBits() {
return getCommitteeBitsSchema().map(SszBitvectorSchema::ofBits);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.ssz.containers.Container4;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;

public class SingleAttestation
extends Container4<SingleAttestation, SszUInt64, SszUInt64, AttestationData, SszSignature>
implements Attestation {
public SingleAttestation(final SingleAttestationSchema type, final TreeNode backingNode) {
super(type, backingNode);
}

public SingleAttestation(
final SingleAttestationSchema schema,
final UInt64 committeeIndex,
final UInt64 validatorIndex,
final AttestationData data,
final BLSSignature signature) {
super(
schema,
SszUInt64.of(committeeIndex),
SszUInt64.of(validatorIndex),
data,
new SszSignature(signature));
}

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

@Override
public AttestationData getData() {
return getField2();
}

@Override
public SszBitlist getAggregationBits() {
throw new UnsupportedOperationException("Not supported in SingleAttestation");
}

@Override
public UInt64 getFirstCommitteeIndex() {
return getField0().get();
}

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

@Override
public boolean requiresCommitteeBits() {
return false;
}

@Override
public boolean isSingleAttestation() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.Optional;
import java.util.function.Supplier;
import tech.pegasys.teku.bls.BLSSignature;
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.primitive.SszUInt64;
import tech.pegasys.teku.infrastructure.ssz.schema.SszPrimitiveSchemas;
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.datastructures.type.SszSignature;
import tech.pegasys.teku.spec.datastructures.type.SszSignatureSchema;

public class SingleAttestationSchema
extends ContainerSchema4<SingleAttestation, SszUInt64, SszUInt64, AttestationData, SszSignature>
implements AttestationSchema<SingleAttestation> {
public SingleAttestationSchema() {
super(
"SingleAttestation",
namedSchema("committee_index", SszPrimitiveSchemas.UINT64_SCHEMA),
namedSchema("attester_index", SszPrimitiveSchemas.UINT64_SCHEMA),
namedSchema("data", AttestationData.SSZ_SCHEMA),
namedSchema("signature", SszSignatureSchema.INSTANCE));
}

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

@Override
public Attestation create(
final SszBitlist aggregationBits,
final AttestationData data,
final BLSSignature signature,
final Supplier<SszBitvector> committeeBits) {
throw new UnsupportedOperationException("Not supported in SingleAttestation");
}

@Override
public SszBitlistSchema<?> getAggregationBitsSchema() {
throw new UnsupportedOperationException("Not supported in SingleAttestation");
}

@Override
public Optional<SszBitvectorSchema<?>> getCommitteeBitsSchema() {
return Optional.empty();
}

@Override
public boolean requiresCommitteeBits() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@

package tech.pegasys.teku.spec.datastructures.operations.versions.electra;

import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
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.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
Expand All @@ -51,16 +47,6 @@ public AttestationElectraSchema getSchema() {
return (AttestationElectraSchema) super.getSchema();
}

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

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

@Override
public SszBitlist getAggregationBits() {
return getField0();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@

package tech.pegasys.teku.spec.datastructures.operations.versions.phase0;

import com.google.common.collect.Sets;
import java.util.Collection;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist;
import tech.pegasys.teku.infrastructure.ssz.containers.Container3;
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.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;
Expand All @@ -47,16 +43,6 @@ public AttestationPhase0Schema getSchema() {
return (AttestationPhase0Schema) super.getSchema();
}

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

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

@Override
public SszBitlist getAggregationBits() {
return getField0();
Expand Down
Loading

0 comments on commit bb3856f

Please sign in to comment.