use of tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata in project web3signer by ConsenSys.
the class InterchangeV5Exporter method startInterchangeExport.
private void startInterchangeExport(final JsonGenerator jsonGenerator) throws IOException {
final Optional<Bytes32> gvr = jdbi.inTransaction(metadataDao::findGenesisValidatorsRoot);
if (gvr.isEmpty()) {
throw new RuntimeException("No genesis validators root for slashing protection data");
}
jsonGenerator.writeStartObject();
final Metadata metadata = new Metadata(FORMAT_VERSION, gvr.get());
jsonGenerator.writeFieldName("metadata");
JSON_MAPPER.writeValue(jsonGenerator, metadata);
jsonGenerator.writeArrayFieldStart("data");
}
use of tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata in project web3signer by ConsenSys.
the class InterchangeV5Importer method importDataInternal.
private void importDataInternal(final InputStream input, final Optional<List<String>> pubkeys) throws IOException {
try (final JsonParser jsonParser = JSON_MAPPER.getFactory().createParser(input)) {
final ObjectNode rootNode = JSON_MAPPER.readTree(jsonParser);
final JsonNode metadataJsonNode = rootNode.get("metadata");
final Metadata metadata = JSON_MAPPER.treeToValue(metadataJsonNode, Metadata.class);
if (!metadata.getFormatVersion().equals(FORMAT_VERSION)) {
throw new IllegalStateException("Expecting an interchange_format_version of " + FORMAT_VERSION);
}
final Bytes32 gvr = Bytes32.wrap(metadata.getGenesisValidatorsRoot());
final GenesisValidatorRootValidator genesisValidatorRootValidator = new GenesisValidatorRootValidator(jdbi, metadataDao);
if (!genesisValidatorRootValidator.checkGenesisValidatorsRootAndInsertIfEmpty(gvr)) {
throw new IllegalArgumentException(String.format("Supplied genesis validators root %s does not match value in database", gvr));
}
final ArrayNode dataNode = rootNode.withArray("data");
jdbi.useTransaction(h -> {
for (int i = 0; i < dataNode.size(); i++) {
try {
final JsonNode validatorNode = dataNode.get(i);
parseValidator(h, validatorNode, pubkeys);
} catch (final IllegalArgumentException e) {
LOG.error("Failed to parse validator {}, due to {}", i, e.getMessage());
throw e;
}
}
});
}
}
use of tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata in project web3signer by ConsenSys.
the class InterchangeImportBadLogicalContentIntegrationTestBase method attestationHasSourceGreaterThanTargetEpoch.
@Test
void attestationHasSourceGreaterThanTargetEpoch() throws IOException {
final InterchangeV5Format interchangeData = new InterchangeV5Format(new Metadata("5", Bytes.fromHexString("0x123456")), List.of(new SignedArtifacts("0x12345678", emptyList(), List.of(new SignedAttestation(UInt64.valueOf(6), UInt64.valueOf(5), Bytes.fromHexString("0x01"))))));
final byte[] jsonInput = mapper.writeValueAsBytes(interchangeData);
assertThatThrownBy(() -> slashingProtectionContext.getSlashingProtection().importData(new ByteArrayInputStream(jsonInput))).isInstanceOf(RuntimeException.class).hasMessage("Failed to import database content");
assertDbIsEmpty(jdbi);
}
use of tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata in project web3signer by ConsenSys.
the class InterchangeImportBadLogicalContentIntegrationTestBase method genesisValidatorRootConflictsWithExistingDbGvr.
@Test
void genesisValidatorRootConflictsWithExistingDbGvr() throws JsonProcessingException {
insertGvr(Bytes32.ZERO);
final InterchangeV5Format interchangeData = new InterchangeV5Format(new Metadata("5", Bytes32.leftPad(Bytes.fromHexString("0x123456"))), List.of(new SignedArtifacts("0x12345678", emptyList(), emptyList())));
final byte[] jsonInput = mapper.writeValueAsBytes(interchangeData);
assertThatThrownBy(() -> slashingProtectionContext.getSlashingProtection().importData(new ByteArrayInputStream(jsonInput))).isInstanceOf(RuntimeException.class).hasMessage("Failed to import database content");
assertDbIsEmpty(jdbi);
}
Aggregations