Search in sources :

Example 1 with Validator

use of tech.pegasys.teku.validator.client.Validator in project teku by ConsenSys.

the class AttestationProductionDutyTest method shouldCreateAttestationForMultipleValidatorsInDifferentCommittees.

@SuppressWarnings("unchecked")
@Test
public void shouldCreateAttestationForMultipleValidatorsInDifferentCommittees() {
    final int committeeIndex1 = 3;
    final int committeeIndex2 = 5;
    final int committeeSize1 = 15;
    final int committeeSize2 = 20;
    final int validator1CommitteePosition = 6;
    final int validator2CommitteePosition = 2;
    final int validator3CommitteePosition = 5;
    final Validator validator1 = createValidator();
    final Validator validator2 = createValidator();
    final Validator validator3 = createValidator();
    final AttestationData unsignedAttestation1 = expectCreateAttestationData(committeeIndex1);
    final AttestationData unsignedAttestation2 = expectCreateAttestationData(committeeIndex2);
    final Attestation expectedAttestation1 = expectSignAttestation(validator1, validator1CommitteePosition, committeeSize1, unsignedAttestation1);
    final Attestation expectedAttestation2 = expectSignAttestation(validator2, validator2CommitteePosition, committeeSize2, unsignedAttestation2);
    final Attestation expectedAttestation3 = expectSignAttestation(validator3, validator3CommitteePosition, committeeSize1, unsignedAttestation1);
    final SafeFuture<Optional<AttestationData>> attestationResult1 = duty.addValidator(validator1, committeeIndex1, validator1CommitteePosition, 10, committeeSize1);
    final SafeFuture<Optional<AttestationData>> attestationResult2 = duty.addValidator(validator2, committeeIndex2, validator2CommitteePosition, 10, committeeSize2);
    final SafeFuture<Optional<AttestationData>> attestationResult3 = duty.addValidator(validator3, committeeIndex1, validator3CommitteePosition, 10, committeeSize1);
    performAndReportDuty();
    assertThat(attestationResult1).isCompletedWithValue(Optional.of(unsignedAttestation1));
    assertThat(attestationResult2).isCompletedWithValue(Optional.of(unsignedAttestation2));
    assertThat(attestationResult3).isCompletedWithValue(Optional.of(unsignedAttestation1));
    ArgumentCaptor<List<Attestation>> argumentCaptor = ArgumentCaptor.forClass(List.class);
    verify(validatorApiChannel).sendSignedAttestations(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue()).containsExactlyInAnyOrder(expectedAttestation1, expectedAttestation2, expectedAttestation3);
    // Need to create an unsigned attestation for each committee
    verify(validatorApiChannel, times(2)).createAttestationData(any(), anyInt());
    verify(validatorLogger).dutyCompleted(TYPE, SLOT, 3, Set.of(unsignedAttestation1.getBeacon_block_root(), unsignedAttestation2.getBeacon_block_root()));
    verifyNoMoreInteractions(validatorLogger);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Optional(java.util.Optional) List(java.util.List) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Validator(tech.pegasys.teku.validator.client.Validator) Test(org.junit.jupiter.api.Test)

Example 2 with Validator

use of tech.pegasys.teku.validator.client.Validator 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 3 with Validator

use of tech.pegasys.teku.validator.client.Validator in project teku by ConsenSys.

the class AttestationProductionDutyTest method shouldPublishProducedAttestationsWhenSomeUnsignedAttestationsFail.

@Test
public void shouldPublishProducedAttestationsWhenSomeUnsignedAttestationsFail() {
    final Validator validator1 = createValidator();
    final Validator validator2 = createValidator();
    final int validator1CommitteeIndex = 0;
    final int validator1CommitteePosition = 5;
    final int validator2CommitteeIndex = 1;
    final int validator2CommitteePosition = 3;
    final int validator2CommitteeSize = 12;
    final RuntimeException failure = new RuntimeException("Golly gee");
    when(validatorApiChannel.createAttestationData(SLOT, validator1CommitteeIndex)).thenReturn(failedFuture(failure));
    final AttestationData attestationData = expectCreateAttestationData(validator2CommitteeIndex);
    final Attestation expectedAttestation = expectSignAttestation(validator2, validator2CommitteePosition, validator2CommitteeSize, attestationData);
    final SafeFuture<Optional<AttestationData>> attestationResult1 = duty.addValidator(validator1, validator1CommitteeIndex, validator1CommitteePosition, 10, 11);
    final SafeFuture<Optional<AttestationData>> attestationResult2 = duty.addValidator(validator2, validator2CommitteeIndex, validator2CommitteePosition, 10, validator2CommitteeSize);
    performAndReportDuty();
    assertThat(attestationResult1).isCompletedExceptionally();
    assertThatThrownBy(attestationResult1::join).hasRootCause(failure);
    assertThat(attestationResult2).isCompletedWithValue(Optional.of(attestationData));
    verify(validatorApiChannel).sendSignedAttestations(List.of(expectedAttestation));
    verify(validatorLogger).dutyCompleted(TYPE, SLOT, 1, Set.of(attestationData.getBeacon_block_root()));
    verify(validatorLogger).dutyFailed(TYPE, SLOT, Set.of(validator1.getPublicKey().toAbbreviatedString()), failure);
    verifyNoMoreInteractions(validatorLogger);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Optional(java.util.Optional) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Validator(tech.pegasys.teku.validator.client.Validator) Test(org.junit.jupiter.api.Test)

Example 4 with Validator

use of tech.pegasys.teku.validator.client.Validator in project teku by ConsenSys.

the class AttestationProductionDutyTest method shouldFailWhenUnsignedAttestationCanNotBeCreated.

@Test
public void shouldFailWhenUnsignedAttestationCanNotBeCreated() {
    final Validator validator = createValidator();
    when(validatorApiChannel.createAttestationData(SLOT, 0)).thenReturn(completedFuture(Optional.empty()));
    final SafeFuture<Optional<AttestationData>> attestationFuture = duty.addValidator(validator, 0, 5, 10, 11);
    performAndReportDuty();
    assertThat(attestationFuture).isCompletedWithValue(Optional.empty());
    verify(validatorLogger).dutyFailed(eq(TYPE), eq(SLOT), eq(Set.of(validator.getPublicKey().toAbbreviatedString())), any(IllegalStateException.class));
    verifyNoMoreInteractions(validatorLogger);
}
Also used : Optional(java.util.Optional) Validator(tech.pegasys.teku.validator.client.Validator) Test(org.junit.jupiter.api.Test)

Example 5 with Validator

use of tech.pegasys.teku.validator.client.Validator in project teku by ConsenSys.

the class AttestationProductionDutyTest method shouldPublishProducedAttestationsWhenSignerFailsForSomeAttestations.

@Test
public void shouldPublishProducedAttestationsWhenSignerFailsForSomeAttestations() {
    final Validator validator1 = createValidator();
    final Validator validator2 = createValidator();
    final int committeeIndex = 0;
    final int validator1CommitteePosition = 5;
    final int validator2CommitteePosition = 3;
    final int validator1CommitteeSize = 23;
    final int validator2CommitteeSize = 39;
    final AttestationData attestationData = expectCreateAttestationData(committeeIndex);
    final RuntimeException signingFailure = new RuntimeException("Gosh darn");
    when(validator1.getSigner().signAttestationData(attestationData, fork)).thenReturn(failedFuture(signingFailure));
    final Attestation expectedAttestation = expectSignAttestation(validator2, validator2CommitteePosition, validator2CommitteeSize, attestationData);
    final SafeFuture<Optional<AttestationData>> attestationResult1 = duty.addValidator(validator1, committeeIndex, validator1CommitteePosition, 10, validator1CommitteeSize);
    final SafeFuture<Optional<AttestationData>> attestationResult2 = duty.addValidator(validator2, committeeIndex, validator2CommitteePosition, 10, validator2CommitteeSize);
    performAndReportDuty();
    assertThat(attestationResult1).isCompletedWithValue(Optional.of(attestationData));
    assertThat(attestationResult2).isCompletedWithValue(Optional.of(attestationData));
    verify(validatorApiChannel).sendSignedAttestations(List.of(expectedAttestation));
    verify(validatorLogger).dutyCompleted(TYPE, SLOT, 1, Set.of(attestationData.getBeacon_block_root()));
    verify(validatorLogger).dutyFailed(TYPE, SLOT, Set.of(validator1.getPublicKey().toAbbreviatedString()), signingFailure);
    verifyNoMoreInteractions(validatorLogger);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Optional(java.util.Optional) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Validator(tech.pegasys.teku.validator.client.Validator) Test(org.junit.jupiter.api.Test)

Aggregations

Validator (tech.pegasys.teku.validator.client.Validator)40 Test (org.junit.jupiter.api.Test)32 Optional (java.util.Optional)15 ValidatorConfig (tech.pegasys.teku.validator.api.ValidatorConfig)10 BLSSignature (tech.pegasys.teku.bls.BLSSignature)7 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)7 AttestationData (tech.pegasys.teku.spec.datastructures.operations.AttestationData)7 List (java.util.List)6 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)6 ForkInfo (tech.pegasys.teku.spec.datastructures.state.ForkInfo)6 ArrayList (java.util.ArrayList)5 SimpleDataDirLayout (tech.pegasys.techu.service.serviceutils.layout.SimpleDataDirLayout)4 Signer (tech.pegasys.teku.core.signatures.Signer)3 ValidatorSigningRecord (tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)3 ValidatorLogger (tech.pegasys.teku.infrastructure.logging.ValidatorLogger)3 DataDirLayout (tech.pegasys.teku.service.serviceutils.layout.DataDirLayout)3 Spec (tech.pegasys.teku.spec.Spec)3 BeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock)3