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