Search in sources :

Example 1 with SlashingProtectionIncrementalExporter

use of tech.pegasys.teku.data.SlashingProtectionIncrementalExporter 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 2 with SlashingProtectionIncrementalExporter

use of tech.pegasys.teku.data.SlashingProtectionIncrementalExporter in project teku by ConsenSys.

the class ActiveKeyManagerTest method deleteValidator_shouldReportSlashingExportError.

@Test
void deleteValidator_shouldReportSlashingExportError(@TempDir final Path tempDir) throws IOException {
    Files.createFile(tempDir.resolve(publicKey.toBytesCompressed().toUnprefixedHexString() + ".yml"));
    final SlashingProtectionIncrementalExporter exporter = new SlashingProtectionIncrementalExporter(tempDir);
    final Validator activeValidator = mock(Validator.class);
    when(activeValidator.getPublicKey()).thenReturn(publicKey);
    when(activeValidator.isReadOnly()).thenReturn(false);
    when(activeValidator.getSigner()).thenReturn(signer);
    when(validatorLoader.deleteLocalMutableValidator(publicKey)).thenReturn(DeleteKeyResult.success());
    final DeleteKeyResult result = keyManager.deleteValidator(activeValidator, exporter);
    verify(signer).delete();
    verify(validatorLoader).deleteLocalMutableValidator(publicKey);
    assertThat(result.getStatus()).isEqualTo(DeletionStatus.ERROR);
    assertThat(result.getMessage().orElse("")).startsWith("Failed to read from file");
    verify(channel, never()).onValidatorsAdded();
}
Also used : SlashingProtectionIncrementalExporter(tech.pegasys.teku.data.SlashingProtectionIncrementalExporter) DeleteKeyResult(tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeyResult) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) Test(org.junit.jupiter.api.Test)

Aggregations

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