Search in sources :

Example 1 with SafeFuture.completedFuture

use of tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture in project teku by ConsenSys.

the class AttestationProductionDutyTest method shouldReportFailureWhenAttestationIsInvalid.

@Test
void shouldReportFailureWhenAttestationIsInvalid() {
    final int committeeIndex = 3;
    final int committeePosition = 6;
    final int committeeSize = 22;
    final Validator validator = createValidator();
    final AttestationData attestationData = expectCreateAttestationData(committeeIndex);
    final Attestation expectedAttestation = expectSignAttestation(validator, committeePosition, committeeSize, attestationData);
    when(validatorApiChannel.sendSignedAttestations(List.of(expectedAttestation))).thenReturn(SafeFuture.completedFuture(List.of(new SubmitDataError(UInt64.ZERO, "Naughty attestation"))));
    final SafeFuture<Optional<AttestationData>> attestationResult = duty.addValidator(validator, committeeIndex, committeePosition, 10, committeeSize);
    performAndReportDuty();
    assertThat(attestationResult).isCompletedWithValue(Optional.of(attestationData));
    verify(validatorApiChannel).sendSignedAttestations(List.of(expectedAttestation));
    verify(validatorLogger).dutyFailed(eq(TYPE), eq(SLOT), eq(Set.of(validator.getPublicKey().toAbbreviatedString())), argThat(error -> error instanceof RestApiReportedException && error.getMessage().equals("Naughty attestation")));
    verifyNoMoreInteractions(validatorLogger);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AttestationSchema(tech.pegasys.teku.spec.datastructures.operations.Attestation.AttestationSchema) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SszBitlist(tech.pegasys.teku.infrastructure.ssz.collections.SszBitlist) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Signer(tech.pegasys.teku.core.signatures.Signer) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ForkProvider(tech.pegasys.teku.validator.client.ForkProvider) BatchAttestationSendingStrategy(tech.pegasys.teku.validator.client.duties.attestations.BatchAttestationSendingStrategy) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) SafeFuture.failedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.failedFuture) Spec(tech.pegasys.teku.spec.Spec) FileBackedGraffitiProvider(tech.pegasys.teku.validator.api.FileBackedGraffitiProvider) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidatorLogger(tech.pegasys.teku.infrastructure.logging.ValidatorLogger) BLSSignature(tech.pegasys.teku.bls.BLSSignature) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AttestationProductionDuty(tech.pegasys.teku.validator.client.duties.attestations.AttestationProductionDuty) List(java.util.List) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) ValidatorApiChannel(tech.pegasys.teku.validator.api.ValidatorApiChannel) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Optional(java.util.Optional) Collections(java.util.Collections) SubmitDataError(tech.pegasys.teku.validator.api.SubmitDataError) Mockito.mock(org.mockito.Mockito.mock) Validator(tech.pegasys.teku.validator.client.Validator) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Optional(java.util.Optional) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) SubmitDataError(tech.pegasys.teku.validator.api.SubmitDataError) Validator(tech.pegasys.teku.validator.client.Validator) Test(org.junit.jupiter.api.Test)

Example 2 with SafeFuture.completedFuture

use of tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture in project teku by ConsenSys.

the class AggregateAttestationValidator method validate.

public SafeFuture<InternalValidationResult> validate(final ValidateableAttestation attestation) {
    final SignedAggregateAndProof signedAggregate = attestation.getSignedAggregateAndProof();
    final AggregateAndProof aggregateAndProof = signedAggregate.getMessage();
    final Attestation aggregate = aggregateAndProof.getAggregate();
    final UInt64 aggregateSlot = aggregate.getData().getSlot();
    final SpecVersion specVersion = spec.atSlot(aggregateSlot);
    final AggregatorIndexAndEpoch aggregatorIndexAndEpoch = new AggregatorIndexAndEpoch(aggregateAndProof.getIndex(), spec.computeEpochAtSlot(aggregateSlot));
    if (receivedAggregatorIndexAndEpochs.contains(aggregatorIndexAndEpoch)) {
        return completedFuture(ignore("Ignoring duplicate aggregate"));
    }
    if (receivedValidAggregations.contains(attestation.hash_tree_root())) {
        return completedFuture(ignore("Ignoring duplicate aggregate based on hash tree root"));
    }
    final AsyncBatchBLSSignatureVerifier signatureVerifier = new AsyncBatchBLSSignatureVerifier(this.signatureVerifier);
    return singleOrAggregateAttestationChecks(signatureVerifier, attestation, OptionalInt.empty()).thenCompose(aggregateInternalValidationResult -> {
        if (aggregateInternalValidationResult.isNotProcessable()) {
            LOG.trace("Rejecting aggregate because attestation failed validation");
            return completedFuture(aggregateInternalValidationResult);
        }
        return recentChainData.retrieveBlockState(aggregate.getData().getBeacon_block_root()).thenCompose(maybeState -> maybeState.isEmpty() ? completedFuture(Optional.empty()) : attestationValidator.resolveStateForAttestation(aggregate, maybeState.get())).thenCompose(maybeState -> {
            if (maybeState.isEmpty()) {
                return SafeFuture.completedFuture(InternalValidationResult.SAVE_FOR_FUTURE);
            }
            final BeaconState state = maybeState.get();
            final Optional<BLSPublicKey> aggregatorPublicKey = spec.getValidatorPubKey(state, aggregateAndProof.getIndex());
            if (aggregatorPublicKey.isEmpty()) {
                return SafeFuture.completedFuture(reject("Rejecting aggregate with invalid index"));
            }
            if (!isSelectionProofValid(signatureVerifier, aggregateSlot, state, aggregatorPublicKey.get(), aggregateAndProof.getSelection_proof())) {
                return SafeFuture.completedFuture(reject("Rejecting aggregate with incorrect selection proof"));
            }
            final IntList beaconCommittee = spec.getBeaconCommittee(state, aggregateSlot, aggregate.getData().getIndex());
            final int aggregatorModulo = specVersion.getValidatorsUtil().getAggregatorModulo(beaconCommittee.size());
            if (!specVersion.getValidatorsUtil().isAggregator(aggregateAndProof.getSelection_proof(), aggregatorModulo)) {
                return SafeFuture.completedFuture(reject("Rejecting aggregate because selection proof does not select validator as aggregator"));
            }
            if (!beaconCommittee.contains(toIntExact(aggregateAndProof.getIndex().longValue()))) {
                return SafeFuture.completedFuture(reject("Rejecting aggregate because attester is not in committee. Should have been one of %s", beaconCommittee));
            }
            if (!validateSignature(signatureVerifier, signedAggregate, state, aggregatorPublicKey.get())) {
                return SafeFuture.completedFuture(reject("Rejecting aggregate with invalid signature"));
            }
            return signatureVerifier.batchVerify().thenApply(signatureValid -> {
                if (!signatureValid) {
                    return reject("Rejecting aggregate with invalid batch signature");
                }
                if (!receivedAggregatorIndexAndEpochs.add(aggregatorIndexAndEpoch)) {
                    return ignore("Ignoring duplicate aggregate");
                }
                if (!receivedValidAggregations.add(attestation.hash_tree_root())) {
                    return ignore("Ignoring duplicate aggregate based on hash tree root");
                }
                return aggregateInternalValidationResult;
            });
        });
    });
}
Also used : AsyncBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBLSSignatureVerifier) AggregateAndProof(tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) Bytes(org.apache.tuweni.bytes.Bytes) SafeFuture.completedFuture(tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture) OptionalInt(java.util.OptionalInt) BLSSignatureVerifier(tech.pegasys.teku.bls.BLSSignatureVerifier) SpecVersion(tech.pegasys.teku.spec.SpecVersion) LimitedSet(tech.pegasys.teku.infrastructure.collections.LimitedSet) SignedAggregateAndProof(tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof) VALID_AGGREGATE_SET_SIZE(tech.pegasys.teku.spec.config.Constants.VALID_AGGREGATE_SET_SIZE) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Math.toIntExact(java.lang.Math.toIntExact) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Domain(tech.pegasys.teku.spec.constants.Domain) BLSSignature(tech.pegasys.teku.bls.BLSSignature) InternalValidationResult.ignore(tech.pegasys.teku.statetransition.validation.InternalValidationResult.ignore) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Set(java.util.Set) Objects(java.util.Objects) InternalValidationResult.reject(tech.pegasys.teku.statetransition.validation.InternalValidationResult.reject) IntList(it.unimi.dsi.fastutil.ints.IntList) Logger(org.apache.logging.log4j.Logger) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Optional(java.util.Optional) AsyncBatchBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBatchBLSSignatureVerifier) LogManager(org.apache.logging.log4j.LogManager) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) IntList(it.unimi.dsi.fastutil.ints.IntList) SpecVersion(tech.pegasys.teku.spec.SpecVersion) AsyncBatchBLSSignatureVerifier(tech.pegasys.teku.spec.logic.common.util.AsyncBatchBLSSignatureVerifier) SignedAggregateAndProof(tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) AggregateAndProof(tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof) SignedAggregateAndProof(tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof)

Aggregations

Optional (java.util.Optional)2 Set (java.util.Set)2 BLSSignature (tech.pegasys.teku.bls.BLSSignature)2 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)2 SafeFuture.completedFuture (tech.pegasys.teku.infrastructure.async.SafeFuture.completedFuture)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 Spec (tech.pegasys.teku.spec.Spec)2 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)2 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Math.toIntExact (java.lang.Math.toIntExact)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 OptionalInt (java.util.OptionalInt)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 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1