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