Search in sources :

Example 1 with DeleteKeysResponse

use of tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse in project teku by ConsenSys.

the class ActiveKeyManagerTest method deleteValidators_shouldRejectRequestToDeleteReadOnlyValidator.

@Test
void deleteValidators_shouldRejectRequestToDeleteReadOnlyValidator(@TempDir final Path tempDir) {
    final Validator activeValidator = mock(Validator.class);
    when(activeValidator.isReadOnly()).thenReturn(true);
    when(validatorLoader.getOwnedValidators()).thenReturn(new OwnedValidators(Map.of(publicKey, activeValidator)));
    when(activeValidator.getPublicKey()).thenReturn(publicKey);
    final DeleteKeysResponse response = keyManager.deleteValidators(List.of(publicKey), tempDir);
    assertThat(response.getData()).hasSize(1);
    assertThat(response.getData().get(0).getStatus()).isEqualTo(DeletionStatus.ERROR);
    assertThat(response.getData().get(0).getMessage().orElse("")).contains("read-only");
    assertThat(response.getSlashingProtection()).isNotEmpty();
    verify(channel, never()).onValidatorsAdded();
}
Also used : OwnedValidators(tech.pegasys.teku.validator.client.loader.OwnedValidators) DeleteKeysResponse(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) Test(org.junit.jupiter.api.Test)

Example 2 with DeleteKeysResponse

use of tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse in project teku by ConsenSys.

the class ActiveKeyManagerTest method deleteValidators_shouldStopAndDeleteValidator.

@Test
void deleteValidators_shouldStopAndDeleteValidator(@TempDir final Path tempDir) {
    final Validator activeValidator = mock(Validator.class);
    when(activeValidator.getPublicKey()).thenReturn(publicKey);
    when(activeValidator.isReadOnly()).thenReturn(false);
    when(activeValidator.getSigner()).thenReturn(signer);
    when(validatorLoader.getOwnedValidators()).thenReturn(new OwnedValidators(Map.of(publicKey, activeValidator)));
    when(exporter.addPublicKeyToExport(eq(publicKey), any())).thenReturn(Optional.empty());
    when(validatorLoader.deleteLocalMutableValidator(publicKey)).thenReturn(DeleteKeyResult.success());
    final DeleteKeysResponse response = keyManager.deleteValidators(List.of(publicKey), tempDir);
    verify(signer).delete();
    verify(validatorLoader).deleteLocalMutableValidator(publicKey);
    assertThat(response.getData().get(0).getStatus()).isEqualTo(DeletionStatus.DELETED);
    assertThat(response.getData()).hasSize(1);
    assertThat(response.getSlashingProtection()).isNotEmpty();
    verify(channel, never()).onValidatorsAdded();
}
Also used : OwnedValidators(tech.pegasys.teku.validator.client.loader.OwnedValidators) DeleteKeysResponse(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) Test(org.junit.jupiter.api.Test)

Example 3 with DeleteKeysResponse

use of tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse in project teku by ConsenSys.

the class ActiveKeyManager method deleteValidators.

/**
 * Delete a collection of validators
 *
 * <p>The response must be symmetric, the list of validators coming in dictates the response
 * order.
 *
 * <p>An individual deletion failure MUST NOT cancel the operation, but rather cause an error for
 * that specific key Validator keys that are reported as deletionStatus.deleted MUST NOT be active
 * once the result is returned.
 *
 * <p>Each failure should result in a failure message for that specific key Slashing protection
 * data MUST be returned if we have the information
 *
 * <p>- NOT_FOUND should only be returned if the key was not there, and we didn't have slashing
 * protection information
 *
 * <p>- NOT_ACTIVE indicates the key wasn't active, but we had slashing data
 *
 * <p>- DELETED indicates the key was found, and we have stopped using it and removed it.
 *
 * <p>- ERROR indicates we couldn't stop using the key (read-only might be a reason)
 *
 * <p>If an exception is thrown, it will cause a 500 error on the API, so that is an undesirable
 * outcome.
 *
 * @param validators list of validator public keys that should be removed
 * @return The result of each deletion, and slashing protection data
 */
@Override
public synchronized DeleteKeysResponse deleteValidators(final List<BLSPublicKey> validators, final Path slashingProtectionPath) {
    final List<DeleteKeyResult> deletionResults = new ArrayList<>();
    final SlashingProtectionIncrementalExporter exporter = new SlashingProtectionIncrementalExporter(slashingProtectionPath);
    for (final BLSPublicKey publicKey : validators) {
        Optional<Validator> maybeValidator = validatorLoader.getOwnedValidators().getValidator(publicKey);
        // read-only check in a non-destructive manner
        if (maybeValidator.isPresent() && maybeValidator.get().isReadOnly()) {
            deletionResults.add(DeleteKeyResult.error("Cannot remove read-only validator"));
            continue;
        }
        // delete validator from owned validators list
        maybeValidator = validatorLoader.getOwnedValidators().removeValidator(publicKey);
        if (maybeValidator.isPresent()) {
            deletionResults.add(deleteValidator(maybeValidator.get(), exporter));
        } else {
            deletionResults.add(attemptToGetSlashingDataForInactiveValidator(publicKey, exporter));
        }
    }
    String exportedData;
    try {
        exportedData = exporter.finalise();
    } catch (JsonProcessingException e) {
        LOG.error("Failed to serialize slashing export data", e);
        exportedData = EXPORT_FAILED;
    }
    return new DeleteKeysResponse(deletionResults, exportedData);
}
Also used : SlashingProtectionIncrementalExporter(tech.pegasys.teku.data.SlashingProtectionIncrementalExporter) ArrayList(java.util.ArrayList) DeleteKeyResult(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeyResult) DeleteKeysResponse(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator)

Example 4 with DeleteKeysResponse

use of tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse in project teku by ConsenSys.

the class DeleteKeysTest method shouldReturnSuccessIfNoKeysArePassed.

@Test
void shouldReturnSuccessIfNoKeysArePassed() throws JsonProcessingException {
    when(request.getRequestBody()).thenReturn(requestData);
    when(keyManager.deleteValidators(any(), any())).thenReturn(new DeleteKeysResponse(List.of(), ""));
    endpoint.handle(request);
    verify(keyManager).deleteValidators(eq(Collections.emptyList()), any());
    verify(request, never()).respondError(anyInt(), any());
    verify(request, times(1)).respondOk(any(DeleteKeysResponse.class));
}
Also used : DeleteKeysResponse(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse) Test(org.junit.jupiter.api.Test)

Example 5 with DeleteKeysResponse

use of tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse in project teku by ConsenSys.

the class DeleteKeysTest method shouldCallKeyManagerWithListOfKeys.

@Test
void shouldCallKeyManagerWithListOfKeys() throws JsonProcessingException {
    final List<BLSPublicKey> keys = List.of(dataStructureUtil.randomPublicKey(), dataStructureUtil.randomPublicKey());
    requestData.setPublicKeys(keys);
    when(keyManager.deleteValidators(eq(keys), any())).thenReturn(new DeleteKeysResponse(List.of(success(), success()), ""));
    when(request.getRequestBody()).thenReturn(requestData);
    endpoint.handle(request);
    verify(keyManager).deleteValidators(eq(keys), any());
    verify(request, never()).respondError(anyInt(), any());
    verify(request, times(1)).respondOk(any(DeleteKeysResponse.class));
}
Also used : DeleteKeysResponse(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteKeysResponse (tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse)5 Test (org.junit.jupiter.api.Test)4 ExternalValidator (tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator)3 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)2 OwnedValidators (tech.pegasys.teku.validator.client.loader.OwnedValidators)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayList (java.util.ArrayList)1 SlashingProtectionIncrementalExporter (tech.pegasys.teku.data.SlashingProtectionIncrementalExporter)1 DeleteKeyResult (tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeyResult)1