Search in sources :

Example 1 with SigningHistory

use of tech.pegasys.teku.data.slashinginterchange.SigningHistory in project teku by ConsenSys.

the class SlashingProtectionImporter method signingHistoryConverter.

private SigningHistory signingHistoryConverter(final SigningHistory signingHistory) {
    final Optional<UInt64> lastSlot = signingHistory.signedBlocks.stream().map(SignedBlock::getSlot).filter(Objects::nonNull).max(UInt64::compareTo);
    final Optional<UInt64> sourceEpoch = signingHistory.signedAttestations.stream().map(SignedAttestation::getSourceEpoch).filter(Objects::nonNull).max(UInt64::compareTo);
    final Optional<UInt64> targetEpoch = signingHistory.signedAttestations.stream().map(SignedAttestation::getTargetEpoch).filter(Objects::nonNull).max(UInt64::compareTo);
    final ValidatorSigningRecord record = new ValidatorSigningRecord(metadata.genesisValidatorsRoot, lastSlot.orElse(UInt64.ZERO), sourceEpoch.orElse(ValidatorSigningRecord.NEVER_SIGNED), targetEpoch.orElse(ValidatorSigningRecord.NEVER_SIGNED));
    return new SigningHistory(signingHistory.pubkey, record);
}
Also used : SigningHistory(tech.pegasys.teku.data.slashinginterchange.SigningHistory) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) SignedAttestation(tech.pegasys.teku.data.slashinginterchange.SignedAttestation) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBlock(tech.pegasys.teku.data.slashinginterchange.SignedBlock)

Example 2 with SigningHistory

use of tech.pegasys.teku.data.slashinginterchange.SigningHistory in project teku by ConsenSys.

the class SlashingProtectionExporter method readSlashProtectionFile.

// returns an error if there was one
Optional<String> readSlashProtectionFile(final File file, final Consumer<String> infoLogger) {
    try {
        Optional<ValidatorSigningRecord> maybeRecord = syncDataAccessor.read(file.toPath()).map(ValidatorSigningRecord::fromBytes);
        if (maybeRecord.isEmpty()) {
            return Optional.of("Failed to read from file " + file.getName());
        }
        ValidatorSigningRecord validatorSigningRecord = maybeRecord.get();
        if (validatorSigningRecord.getGenesisValidatorsRoot() != null) {
            if (genesisValidatorsRoot == null) {
                this.genesisValidatorsRoot = validatorSigningRecord.getGenesisValidatorsRoot();
            } else if (!genesisValidatorsRoot.equals(validatorSigningRecord.getGenesisValidatorsRoot())) {
                return Optional.of("The genesisValidatorsRoot of " + file.getName() + " does not match the expected " + genesisValidatorsRoot.toHexString());
            }
        }
        final String pubkey = file.getName().substring(0, file.getName().length() - ".yml".length());
        infoLogger.accept("Exporting " + pubkey);
        signingHistoryList.add(new SigningHistory(BLSPubKey.fromHexString(pubkey), validatorSigningRecord));
        return Optional.empty();
    } catch (UncheckedIOException | IOException e) {
        return Optional.of("Failed to read from file " + file);
    } catch (PublicKeyException e) {
        return Optional.of("Public key in file " + file.getName() + " does not appear valid.");
    }
}
Also used : SigningHistory(tech.pegasys.teku.data.slashinginterchange.SigningHistory) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PublicKeyException(tech.pegasys.teku.api.schema.PublicKeyException)

Example 3 with SigningHistory

use of tech.pegasys.teku.data.slashinginterchange.SigningHistory in project teku by ConsenSys.

the class SlashingProtectionRepairer method readSlashProtectionFile.

private void readSlashProtectionFile(final File file) {
    final String filePrefix = file.getName().substring(0, file.getName().length() - ".yml".length());
    try {
        BLSPubKey pubkey = BLSPubKey.fromHexString(filePrefix);
        Optional<ValidatorSigningRecord> maybeRecord = syncDataAccessor.read(file.toPath()).map(ValidatorSigningRecord::fromBytes);
        if (maybeRecord.isEmpty() && invalidRecords.add(filePrefix)) {
            log.display(filePrefix + ": Empty slashing protection record");
            return;
        }
        if (updateAllEnabled) {
            log.display(filePrefix + ": looks valid");
        }
        ValidatorSigningRecord validatorSigningRecord = maybeRecord.get();
        signingHistoryList.add(new SigningHistory(pubkey, validatorSigningRecord));
    } catch (PublicKeyException e) {
        log.display(" --- " + file.getName() + " - invalid public key - ignoring file");
    } catch (Exception e) {
        if (invalidRecords.add(filePrefix)) {
            log.display(filePrefix + ": Incomplete or invalid slashing protection data");
        }
    }
}
Also used : SigningHistory(tech.pegasys.teku.data.slashinginterchange.SigningHistory) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) PublicKeyException(tech.pegasys.teku.api.schema.PublicKeyException) BLSPubKey(tech.pegasys.teku.api.schema.BLSPubKey) IOException(java.io.IOException) PublicKeyException(tech.pegasys.teku.api.schema.PublicKeyException)

Example 4 with SigningHistory

use of tech.pegasys.teku.data.slashinginterchange.SigningHistory in project teku by ConsenSys.

the class SlashingProtectionExporterTest method shouldHaveNoSignedAttestationsWhenNoAttestationsSigned.

@Test
void shouldHaveNoSignedAttestationsWhenNoAttestationsSigned(@TempDir Path tempDir) throws Exception {
    final Path exportedFile = tempDir.resolve("exportedFile.json").toAbsolutePath();
    final SlashingProtectionExporter exporter = new SlashingProtectionExporter(tempDir);
    final UInt64 blockSlot = UInt64.ONE;
    final ValidatorSigningRecord signingRecord = new ValidatorSigningRecord(validatorsRoot).maySignBlock(validatorsRoot, blockSlot).orElseThrow();
    final Path recordFile = tempDir.resolve(pubkey + ".yml");
    Files.write(recordFile, signingRecord.toBytes().toArrayUnsafe());
    final Optional<String> error = exporter.readSlashProtectionFile(recordFile.toFile(), LOG::debug);
    assertThat(error).isEmpty();
    assertThat(exportedFile).doesNotExist();
    exporter.saveToFile(exportedFile.toString(), LOG::debug);
    assertThat(exportedFile).exists();
    final SlashingProtectionInterchangeFormat exportedRecords = jsonProvider.jsonToObject(Files.readString(exportedFile), SlashingProtectionInterchangeFormat.class);
    assertThat(exportedRecords.data).hasSize(1);
    final SigningHistory signingHistory = exportedRecords.data.get(0);
    assertThat(signingHistory.signedBlocks).containsExactly(new SignedBlock(blockSlot, null));
    assertThat(signingHistory.signedAttestations).isEmpty();
}
Also used : Path(java.nio.file.Path) SigningHistory(tech.pegasys.teku.data.slashinginterchange.SigningHistory) SlashingProtectionInterchangeFormat(tech.pegasys.teku.data.slashinginterchange.SlashingProtectionInterchangeFormat) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBlock(tech.pegasys.teku.data.slashinginterchange.SignedBlock) Test(org.junit.jupiter.api.Test)

Aggregations

ValidatorSigningRecord (tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord)4 SigningHistory (tech.pegasys.teku.data.slashinginterchange.SigningHistory)4 IOException (java.io.IOException)2 PublicKeyException (tech.pegasys.teku.api.schema.PublicKeyException)2 SignedBlock (tech.pegasys.teku.data.slashinginterchange.SignedBlock)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Test (org.junit.jupiter.api.Test)1 BLSPubKey (tech.pegasys.teku.api.schema.BLSPubKey)1 SignedAttestation (tech.pegasys.teku.data.slashinginterchange.SignedAttestation)1 SlashingProtectionInterchangeFormat (tech.pegasys.teku.data.slashinginterchange.SlashingProtectionInterchangeFormat)1