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