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);
}
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;
});
});
});
}
Aggregations