Search in sources :

Example 1 with ValidatorSigningRecord

use of tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord 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 ValidatorSigningRecord

use of tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord in project teku by ConsenSys.

the class SlashingProtectionImporter method updateLocalRecord.

private Optional<String> updateLocalRecord(final SigningHistory signingHistory, final Consumer<String> statusConsumer) {
    String validatorString = signingHistory.pubkey.toBytes().toUnprefixedHexString().toLowerCase();
    final String hexValidatorPubkey = signingHistory.pubkey.toHexString();
    statusConsumer.accept("Importing " + validatorString);
    Path outputFile = slashingProtectionPath.resolve(validatorString + ".yml");
    Optional<ValidatorSigningRecord> existingRecord = Optional.empty();
    if (outputFile.toFile().exists()) {
        try {
            existingRecord = syncDataAccessor.read(outputFile).map(ValidatorSigningRecord::fromBytes);
        } catch (UncheckedIOException | IOException e) {
            statusConsumer.accept("Failed to read existing file: " + outputFile);
            return Optional.of("unable to load existing record.");
        }
    }
    if (existingRecord.isPresent() && existingRecord.get().getGenesisValidatorsRoot() != null && metadata.genesisValidatorsRoot != null && metadata.genesisValidatorsRoot.compareTo(existingRecord.get().getGenesisValidatorsRoot()) != 0) {
        statusConsumer.accept("Validator " + hexValidatorPubkey + " has a different validators signing root to the data being imported");
        return Optional.of("Genesis validators root did not match what was expected.");
    }
    try {
        syncDataAccessor.syncedWrite(outputFile, signingHistory.toValidatorSigningRecord(existingRecord, metadata.genesisValidatorsRoot).toBytes());
    } catch (IOException e) {
        statusConsumer.accept("Validator " + hexValidatorPubkey + " was not updated.");
        return Optional.of("Failed to update slashing protection record");
    }
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with ValidatorSigningRecord

use of tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord in project teku by ConsenSys.

the class SlashingProtectionRepairer method updateValidatorSigningRecord.

private void updateValidatorSigningRecord(final UInt64 slot, final UInt64 epoch, final SigningHistory historyRecord) {
    final ValidatorSigningRecord currentRecord = historyRecord.toValidatorSigningRecord(Optional.empty(), null);
    final ValidatorSigningRecord updatedRecord = updateSigningRecord(slot, epoch, Optional.of(currentRecord));
    if (!currentRecord.equals(updatedRecord)) {
        writeValidatorSigningRecord(updatedRecord, toDisplayString(historyRecord.pubkey));
    }
}
Also used : ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord)

Example 4 with ValidatorSigningRecord

use of tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord in project teku by ConsenSys.

the class SigningHistory method toValidatorSigningRecord.

public ValidatorSigningRecord toValidatorSigningRecord(final Optional<ValidatorSigningRecord> maybeRecord, final Bytes32 genesisValidatorsRoot) {
    final UInt64 lastSignedBlockSlot = signedBlocks.stream().map(SignedBlock::getSlot).filter(Objects::nonNull).max(UInt64::compareTo).orElse(UInt64.ZERO);
    final UInt64 lastSignedAttestationSourceEpoch = signedAttestations.stream().map(SignedAttestation::getSourceEpoch).filter(Objects::nonNull).max(UInt64::compareTo).orElse(null);
    final UInt64 lastSignedAttestationTargetEpoch = signedAttestations.stream().map(SignedAttestation::getTargetEpoch).filter(Objects::nonNull).max(UInt64::compareTo).orElse(null);
    if (maybeRecord.isPresent()) {
        final ValidatorSigningRecord record = maybeRecord.get();
        return new ValidatorSigningRecord(genesisValidatorsRoot, record.getBlockSlot().max(lastSignedBlockSlot), nullSafeMax(record.getAttestationSourceEpoch(), lastSignedAttestationSourceEpoch), nullSafeMax(record.getAttestationTargetEpoch(), lastSignedAttestationTargetEpoch));
    }
    return new ValidatorSigningRecord(genesisValidatorsRoot, lastSignedBlockSlot, lastSignedAttestationSourceEpoch, lastSignedAttestationTargetEpoch);
}
Also used : Objects(java.util.Objects) MoreObjects(com.google.common.base.MoreObjects) ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 5 with ValidatorSigningRecord

use of tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord in project teku by ConsenSys.

the class SlashingProtectionRepairerTest method updateSigningRecord_shouldUpdateSourceAttestationEpoch.

@Test
void updateSigningRecord_shouldUpdateSourceAttestationEpoch() {
    final ValidatorSigningRecord initialValue = new ValidatorSigningRecord(null, ONE, ZERO, TWO);
    final ValidatorSigningRecord expectedValue = new ValidatorSigningRecord(null, ONE, ONE, TWO);
    assertThat(SlashingProtectionRepairer.updateSigningRecord(ONE, ONE, Optional.of(initialValue))).isEqualTo(expectedValue);
}
Also used : ValidatorSigningRecord(tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord) Test(org.junit.jupiter.api.Test)

Aggregations

ValidatorSigningRecord (tech.pegasys.teku.data.signingrecord.ValidatorSigningRecord)25 Test (org.junit.jupiter.api.Test)12 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)7 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 SigningHistory (tech.pegasys.teku.data.slashinginterchange.SigningHistory)4 Validator (tech.pegasys.teku.validator.client.Validator)4 ArrayList (java.util.ArrayList)3 UncheckedIOException (java.io.UncheckedIOException)2 HashSet (java.util.HashSet)2 Bytes (org.apache.tuweni.bytes.Bytes)2 PublicKeyException (tech.pegasys.teku.api.schema.PublicKeyException)2 SignedBlock (tech.pegasys.teku.data.slashinginterchange.SignedBlock)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 MoreObjects (com.google.common.base.MoreObjects)1 File (java.io.File)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Pair (org.apache.commons.lang3.tuple.Pair)1