Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal single attestation #8676

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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

import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
Expand All @@ -24,18 +26,20 @@
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.AttestationElectraSchema;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;

public class ValidatableAttestation {
private final Spec spec;
private final Attestation attestation;
private volatile Attestation attestation;
private final Optional<SignedAggregateAndProof> maybeAggregate;
private final Supplier<Bytes32> hashTreeRoot;
private final AtomicBoolean gossiped = new AtomicBoolean(false);
Expand All @@ -48,6 +52,54 @@ public class ValidatableAttestation {
private volatile Optional<IndexedAttestation> indexedAttestation = Optional.empty();
private volatile Optional<Bytes32> committeeShufflingSeed = Optional.empty();
private volatile Optional<Int2IntMap> committeesSize = Optional.empty();
private volatile Optional<Attestation> singleAttestation = Optional.empty();

public void convertFromSingleAttestation(final SszBitlist singleAttestationAggregationBits) {
final Attestation localAttestation = attestation;
checkState(localAttestation.isSingleAttestation());
singleAttestation = Optional.of(localAttestation.toSingleAttestationRequired());

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

attestation =
attestationElectraSchema.create(
singleAttestationAggregationBits,
localAttestation.getData(),
localAttestation.getAggregateSignature(),
attestationElectraSchema
.getCommitteeBitsSchema()
.orElseThrow()
.ofBits(localAttestation.getFirstCommitteeIndex().intValue()));
}

public void createSingleAttestation(final UInt64 validatorIndex) {
if (attestation.getCommitteeBits().isEmpty() || singleAttestation.isPresent()) {
return;
}
spec.atSlot(attestation.getData().getSlot())
.getSchemaDefinitions()
.toVersionElectra()
.ifPresent(
schemaDefinitionsElectra -> {
checkState(attestation.getCommitteeBitsRequired().getBitCount() == 1);

singleAttestation =
Optional.of(
schemaDefinitionsElectra
.getSingleAttestationSchema()
.toSingleAttestationSchemaRequired()
.create(
attestation.getFirstCommitteeIndex(),
validatorIndex,
attestation.getData(),
attestation.getAggregateSignature()));
});
}

public static ValidatableAttestation from(final Spec spec, final Attestation attestation) {
return new ValidatableAttestation(
Expand Down Expand Up @@ -220,6 +272,10 @@ public Attestation getAttestation() {
return attestation;
}

public Attestation getSingleAttestation() {
return singleAttestation.orElse(attestation);
}

public SignedAggregateAndProof getSignedAggregateAndProof() {
return maybeAggregate.orElseThrow(
() -> new UnsupportedOperationException("ValidatableAttestation is not an aggregate."));
Expand Down Expand Up @@ -273,6 +329,7 @@ public String toString() {
.add("committeeShufflingSeed", committeeShufflingSeed)
.add("committeesSize", committeesSize)
.add("receivedSubnetId", receivedSubnetId)
.add("singleAttestation", singleAttestation)
.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,16 @@ default List<UInt64> getCommitteeIndicesRequired() {
}

boolean requiresCommitteeBits();

default boolean isSingleAttestation() {
return false;
}

default SingleAttestation toSingleAttestationRequired() {
throw new UnsupportedOperationException("Not a SingleAttestation");
}

default UInt64 getValidatorIndexRequired() {
throw new UnsupportedOperationException("Not a SingleAttestation");
}
}
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 size, final int... indices) {
final SszBitlistSchema<?> bitsSchema = getAggregationBitsSchema();
return bitsSchema.ofBits(size, indices);
}

default Optional<SszBitvector> createEmptyCommitteeBits() {
return getCommitteeBitsSchema().map(SszBitvectorSchema::ofBits);
}
Expand All @@ -54,6 +59,10 @@ default Optional<AttestationElectraSchema> toVersionElectra() {
return Optional.empty();
}

default SingleAttestationSchema toSingleAttestationSchemaRequired() {
throw new UnsupportedOperationException("Not a SingleAttestationSchema");
}

SszBitlistSchema<?> getAggregationBitsSchema();

Optional<SszBitvectorSchema<?>> getCommitteeBitsSchema();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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();
}

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

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

@Override
public boolean isSingleAttestation() {
return true;
}

@Override
public SingleAttestation toSingleAttestationRequired() {
return this;
}

@Override
public UInt64 getValidatorIndexRequired() {
return getField1().get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.infrastructure.unsigned.UInt64;
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);
}

public SingleAttestation create(
final UInt64 committeeIndex,
final UInt64 attesterIndex,
final AttestationData data,
final BLSSignature signature) {
return new SingleAttestation(this, committeeIndex, attesterIndex, data, signature);
}

@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 SingleAttestationSchema toSingleAttestationSchemaRequired() {
return this;
}

@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
Loading