Search in sources :

Example 1 with SlashingProtectionImporter

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

the class ImportCommand method run.

@Override
public void run() {
    final Path slashProtectionPath = SlashingProtectionCommandUtils.getSlashingProtectionPath(dataOptions);
    File importFile = new File(fromFileName);
    verifyImportFileExists(importFile);
    prepareOutputPath(slashProtectionPath.toFile());
    SlashingProtectionImporter importer = new SlashingProtectionImporter(slashProtectionPath);
    try {
        SUB_COMMAND_LOG.display("Reading slashing protection data from: " + importFile);
        final Optional<String> errorCondition = importer.initialise(importFile);
        errorCondition.ifPresent(s -> SUB_COMMAND_LOG.exit(1, s));
    } catch (IOException e) {
        String cause = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        SUB_COMMAND_LOG.exit(1, String.format("Failed to read from import file: %s. %s" + importFile, cause));
    }
    SUB_COMMAND_LOG.display("Writing slashing protection data to: " + slashProtectionPath);
    importer.updateLocalRecords(SUB_COMMAND_LOG::display);
}
Also used : Path(java.nio.file.Path) SlashingProtectionImporter(tech.pegasys.teku.data.SlashingProtectionImporter) IOException(java.io.IOException) File(java.io.File)

Example 2 with SlashingProtectionImporter

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

the class PostKeys method readSlashingProtectionDataIfPresent.

private Optional<SlashingProtectionImporter> readSlashingProtectionDataIfPresent(final Optional<String> slashingData) {
    if (slashingData.isPresent()) {
        final InputStream slashingProtectionData = IOUtils.toInputStream(slashingData.get(), StandardCharsets.UTF_8);
        final SlashingProtectionImporter importer = new SlashingProtectionImporter(slashingProtectionPath);
        try {
            final Optional<String> initialiseError = importer.initialise(slashingProtectionData);
            if (initialiseError.isPresent()) {
                throw new BadRequestException(initialiseError.get());
            }
            return Optional.of(importer);
        } catch (IOException ex) {
            throw new BadRequestException(ex.getMessage());
        }
    }
    return Optional.empty();
}
Also used : InputStream(java.io.InputStream) SlashingProtectionImporter(tech.pegasys.teku.data.SlashingProtectionImporter) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) IOException(java.io.IOException)

Example 3 with SlashingProtectionImporter

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

the class PostKeys method handle.

@Override
public void handle(final RestApiRequest request) throws JsonProcessingException {
    final PostKeysRequest body = request.getRequestBody();
    if (body.getPasswords().size() != body.getKeystores().size()) {
        request.respondError(SC_BAD_REQUEST, String.format("Keystores count (%d) and Passwords count (%d) differ, cannot proceed.", body.getKeystores().size(), body.getPasswords().size()));
        return;
    }
    if (body.getKeystores().isEmpty()) {
        request.respondOk(Collections.emptyList());
        return;
    }
    final Optional<SlashingProtectionImporter> slashingData = readSlashingProtectionDataIfPresent(body.getSlashingProtection());
    request.respondOk(keyManager.importValidators(body.getKeystores(), body.getPasswords(), slashingData));
}
Also used : SlashingProtectionImporter(tech.pegasys.teku.data.SlashingProtectionImporter) PostKeysRequest(tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeysRequest)

Aggregations

SlashingProtectionImporter (tech.pegasys.teku.data.SlashingProtectionImporter)3 IOException (java.io.IOException)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 BadRequestException (tech.pegasys.teku.api.exceptions.BadRequestException)1 PostKeysRequest (tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeysRequest)1