Search in sources :

Example 1 with IndexedAttestation

use of tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation in project teku by ConsenSys.

the class AttesterSlashingValidator method validate.

public Optional<OperationInvalidReason> validate(final Fork fork, final BeaconState state, final AttesterSlashing attesterSlashing, final SlashedIndicesCaptor slashedIndicesCaptor) {
    IndexedAttestation attestation_1 = attesterSlashing.getAttestation_1();
    IndexedAttestation attestation_2 = attesterSlashing.getAttestation_2();
    return firstOf(() -> check(attestationUtil.isSlashableAttestationData(attestation_1.getData(), attestation_2.getData()), AttesterSlashingInvalidReason.ATTESTATIONS_NOT_SLASHABLE), () -> check(attestationUtil.isValidIndexedAttestation(fork, state, attestation_1).isSuccessful(), AttesterSlashingInvalidReason.ATTESTATION_1_INVALID), () -> check(attestationUtil.isValidIndexedAttestation(fork, state, attestation_2).isSuccessful(), AttesterSlashingInvalidReason.ATTESTATION_2_INVALID), () -> {
        boolean slashed_any = false;
        Set<UInt64> intersectingIndices = attesterSlashing.getIntersectingValidatorIndices();
        for (UInt64 index : intersectingIndices) {
            if (predicates.isSlashableValidator(state.getValidators().get(toIntExact(index.longValue())), beaconStateAccessors.getCurrentEpoch(state))) {
                slashedIndicesCaptor.captureSlashedValidatorIndex(index);
                slashed_any = true;
            }
        }
        return check(slashed_any, AttesterSlashingInvalidReason.NO_ONE_SLASHED);
    });
}
Also used : IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with IndexedAttestation

use of tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation in project teku by ConsenSys.

the class ActiveValidatorCacheTest method shouldAcceptAttestations.

@Test
void shouldAcceptAttestations() {
    final Attestation attestation = mock(Attestation.class);
    final AttestationData attestationData = mock(AttestationData.class);
    final IndexedAttestation indexedAttestation = mock(IndexedAttestation.class);
    final ValidateableAttestation validateableAttestation = ValidateableAttestation.from(spec, attestation);
    validateableAttestation.setIndexedAttestation(indexedAttestation);
    when(indexedAttestation.getData()).thenReturn(attestationData);
    when(attestationData.getSlot()).thenReturn(UInt64.valueOf(8), UInt64.valueOf(16), UInt64.valueOf(24));
    final SszUInt64List validators = SszUInt64ListSchema.create(2).of(UInt64.valueOf(11), UInt64.valueOf(21));
    when(indexedAttestation.getAttesting_indices()).thenReturn(validators);
    // each attestation will have 2 validators.
    // getSlot will return 8, then 16, then 24; and each validator will be processed
    cache.onAttestation(validateableAttestation);
    cache.onAttestation(validateableAttestation);
    cache.onAttestation(validateableAttestation);
    assertThat(cache.getValidatorEpochs(UInt64.valueOf(11))).containsExactly(THREE, ONE, TWO);
    assertThat(cache.getValidatorEpochs(UInt64.valueOf(21))).containsExactly(THREE, ONE, TWO);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Test(org.junit.jupiter.api.Test)

Example 3 with IndexedAttestation

use of tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation in project teku by ConsenSys.

the class AttestationManagerTest method shouldAddAttestationsThatHaveNotYetReachedTargetSlotToFutureItemsAndPool.

@Test
public void shouldAddAttestationsThatHaveNotYetReachedTargetSlotToFutureItemsAndPool() {
    final int futureSlot = 100;
    final UInt64 currentSlot = UInt64.valueOf(futureSlot).minus(1);
    attestationManager.onSlot(currentSlot);
    ValidateableAttestation attestation = ValidateableAttestation.from(spec, attestationFromSlot(futureSlot));
    IndexedAttestation randomIndexedAttestation = dataStructureUtil.randomIndexedAttestation();
    when(forkChoice.onAttestation(any())).thenReturn(completedFuture(SAVED_FOR_FUTURE));
    attestationManager.onAttestation(attestation).reportExceptions();
    ArgumentCaptor<ValidateableAttestation> captor = ArgumentCaptor.forClass(ValidateableAttestation.class);
    verify(forkChoice).onAttestation(captor.capture());
    final ValidateableAttestation validateableAttestation = captor.getValue();
    attestation.setIndexedAttestation(randomIndexedAttestation);
    verify(attestationPool).add(attestation);
    assertThat(futureAttestations.contains(captor.getValue())).isTrue();
    assertThat(pendingAttestations.size()).isEqualTo(0);
    // Shouldn't try to process the attestation until after it's slot.
    attestationManager.onSlot(UInt64.valueOf(100));
    assertThat(futureAttestations.size()).isEqualTo(1);
    verify(forkChoice, never()).applyIndexedAttestations(any());
    attestationManager.onSlot(UInt64.valueOf(101));
    verify(forkChoice).applyIndexedAttestations(List.of(validateableAttestation));
    assertThat(futureAttestations.size()).isZero();
    assertThat(pendingAttestations.size()).isZero();
}
Also used : IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 4 with IndexedAttestation

use of tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation in project teku by ConsenSys.

the class DataStructureUtil method randomAttesterSlashing.

public AttesterSlashing randomAttesterSlashing(final UInt64... attestingIndices) {
    IndexedAttestation attestation1 = randomIndexedAttestation(attestingIndices);
    IndexedAttestation attestation2 = randomIndexedAttestation(attestingIndices);
    return spec.getGenesisSchemaDefinitions().getAttesterSlashingSchema().create(attestation1, attestation2);
}
Also used : IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation)

Example 5 with IndexedAttestation

use of tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation in project teku by ConsenSys.

the class AttestationUtil method isValidIndexedAttestationAsync.

public SafeFuture<AttestationProcessingResult> isValidIndexedAttestationAsync(Fork fork, BeaconState state, IndexedAttestation indexed_attestation, AsyncBLSSignatureVerifier signatureVerifier) {
    SszUInt64List indices = indexed_attestation.getAttesting_indices();
    if (indices.isEmpty() || !Comparators.isInStrictOrder(indices.asListUnboxed(), Comparator.naturalOrder())) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices are not sorted"));
    }
    List<BLSPublicKey> pubkeys = indices.streamUnboxed().flatMap(i -> beaconStateAccessors.getValidatorPubKey(state, i).stream()).collect(toList());
    if (pubkeys.size() < indices.size()) {
        return completedFuture(AttestationProcessingResult.invalid("Attesting indices include non-existent validator"));
    }
    BLSSignature signature = indexed_attestation.getSignature();
    Bytes32 domain = beaconStateAccessors.getDomain(Domain.BEACON_ATTESTER, indexed_attestation.getData().getTarget().getEpoch(), fork, state.getGenesis_validators_root());
    Bytes signing_root = miscHelpers.computeSigningRoot(indexed_attestation.getData(), domain);
    return signatureVerifier.verify(pubkeys, signing_root, signature).thenApply(isValidSignature -> {
        if (isValidSignature) {
            return AttestationProcessingResult.SUCCESSFUL;
        } else {
            LOG.debug("AttestationUtil.is_valid_indexed_attestation: Verify aggregate signature");
            return AttestationProcessingResult.invalid("Signature is invalid");
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) AttestationProcessingResult(tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult) Comparators(com.google.common.collect.Comparators) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) SszBitlist(tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist) IndexedAttestation(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) IndexedAttestationSchema(tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation.IndexedAttestationSchema) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Domain(tech.pegasys.teku.spec.constants.Domain) MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) Logger(org.apache.logging.log4j.Logger) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Bytes(org.apache.tuweni.bytes.Bytes) SszUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Aggregations

IndexedAttestation (tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation)5 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)3 Test (org.junit.jupiter.api.Test)2 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)2 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)2 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Comparators (com.google.common.collect.Comparators)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)1