use of dsl.InterchangeV5Format in project web3signer by ConsenSys.
the class InterchangeExportIntegrationTestBase method exportedEntitiesRepresentTheEntriesStoredInTheDatabase.
@Test
void exportedEntitiesRepresentTheEntriesStoredInTheDatabase() throws IOException {
final Bytes32 gvr = Bytes32.fromHexString(GENESIS_VALIDATORS_ROOT);
final int VALIDATOR_COUNT = 2;
final int TOTAL_BLOCKS_SIGNED = 6;
final int TOTAL_ATTESTATIONS_SIGNED = 8;
for (int i = 0; i < VALIDATOR_COUNT; i++) {
final int validatorId = i + 1;
final Bytes validatorPublicKey = Bytes.of(validatorId);
slashingProtectionContext.getRegisteredValidators().registerValidators(List.of(validatorPublicKey));
for (int b = 0; b < TOTAL_BLOCKS_SIGNED; b++) {
insertBlockAt(UInt64.valueOf(b), validatorId);
}
for (int a = 0; a < TOTAL_ATTESTATIONS_SIGNED; a++) {
insertAttestationAt(UInt64.valueOf(a), UInt64.valueOf(a), validatorId);
}
jdbi.useTransaction(h -> {
lowWatermarkDao.updateSlotWatermarkFor(h, validatorId, UInt64.ZERO);
lowWatermarkDao.updateEpochWatermarksFor(h, validatorId, UInt64.ZERO, UInt64.ZERO);
});
}
final InterchangeV5Format outputObject = getExportObjectFromDatabase();
assertThat(outputObject.getMetadata().getFormatVersion()).isEqualTo("5");
assertThat(outputObject.getMetadata().getGenesisValidatorsRoot()).isEqualTo(gvr);
final List<SignedArtifacts> signedArtifacts = outputObject.getSignedArtifacts();
assertThat(signedArtifacts).hasSize(2);
for (int i = 0; i < VALIDATOR_COUNT; i++) {
final int validatorId = i + 1;
final SignedArtifacts signedArtifact = signedArtifacts.get(i);
assertThat(signedArtifact.getPublicKey()).isEqualTo(String.format("0x0%x", validatorId));
assertThat(signedArtifact.getSignedBlocks()).hasSize(TOTAL_BLOCKS_SIGNED);
for (int b = 0; b < TOTAL_BLOCKS_SIGNED; b++) {
final tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock block = signedArtifact.getSignedBlocks().get(b);
assertThat(block.getSigningRoot()).isEqualTo(Bytes.of(100));
assertThat(block.getSlot()).isEqualTo(UInt64.valueOf(b));
}
assertThat(signedArtifact.getSignedAttestations()).hasSize(TOTAL_ATTESTATIONS_SIGNED);
for (int a = 0; a < TOTAL_ATTESTATIONS_SIGNED; a++) {
final tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation attestation = signedArtifact.getSignedAttestations().get(a);
assertThat(attestation.getSigningRoot()).isEqualTo(Bytes.of(100));
assertThat(attestation.getSourceEpoch()).isEqualTo(UInt64.valueOf(a));
assertThat(attestation.getTargetEpoch()).isEqualTo(UInt64.valueOf(a));
}
}
}
use of dsl.InterchangeV5Format in project web3signer by ConsenSys.
the class InterchangeExportIntegrationTestBase method exportingIncrementallyOnlyExportsSpecifiedValidators.
@Test
void exportingIncrementallyOnlyExportsSpecifiedValidators() throws Exception {
final Bytes32 gvr = Bytes32.fromHexString(GENESIS_VALIDATORS_ROOT);
final int VALIDATOR_COUNT = 6;
final int TOTAL_BLOCKS_SIGNED = 6;
final int TOTAL_ATTESTATIONS_SIGNED = 8;
for (int i = 0; i < VALIDATOR_COUNT; i++) {
final int validatorId = i + 1;
final Bytes validatorPublicKey = Bytes.of(validatorId);
slashingProtectionContext.getRegisteredValidators().registerValidators(List.of(validatorPublicKey));
for (int b = 0; b < TOTAL_BLOCKS_SIGNED; b++) {
insertBlockAt(UInt64.valueOf(b), validatorId);
}
for (int a = 0; a < TOTAL_ATTESTATIONS_SIGNED; a++) {
insertAttestationAt(UInt64.valueOf(a), UInt64.valueOf(a), validatorId);
}
jdbi.useTransaction(h -> {
lowWatermarkDao.updateSlotWatermarkFor(h, validatorId, UInt64.ZERO);
lowWatermarkDao.updateEpochWatermarksFor(h, validatorId, UInt64.ZERO, UInt64.ZERO);
});
}
// incrementally export only the even the public keys
final OutputStream exportOutput = new ByteArrayOutputStream();
final IncrementalExporter incrementalExporter = slashingProtectionContext.getSlashingProtection().createIncrementalExporter(exportOutput);
for (int i = 0; i < VALIDATOR_COUNT; i += 2) {
incrementalExporter.export(String.format("0x0%x", i + 1));
}
incrementalExporter.finalise();
incrementalExporter.close();
final InterchangeV5Format outputObject = mapper.readValue(exportOutput.toString(), InterchangeV5Format.class);
assertThat(outputObject.getMetadata().getFormatVersion()).isEqualTo("5");
assertThat(outputObject.getMetadata().getGenesisValidatorsRoot()).isEqualTo(gvr);
final List<SignedArtifacts> signedArtifacts = outputObject.getSignedArtifacts();
assertThat(signedArtifacts).hasSize(VALIDATOR_COUNT / 2);
for (int i = 0; i < VALIDATOR_COUNT; i += 2) {
final int validatorId = i + 1;
final SignedArtifacts signedArtifact = signedArtifacts.get(i / 2);
assertThat(signedArtifact.getPublicKey()).isEqualTo(String.format("0x0%x", validatorId));
assertThat(signedArtifact.getSignedBlocks()).hasSize(TOTAL_BLOCKS_SIGNED);
for (int b = 0; b < TOTAL_BLOCKS_SIGNED; b++) {
final tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock block = signedArtifact.getSignedBlocks().get(b);
assertThat(block.getSigningRoot()).isEqualTo(Bytes.of(100));
assertThat(block.getSlot()).isEqualTo(UInt64.valueOf(b));
}
assertThat(signedArtifact.getSignedAttestations()).hasSize(TOTAL_ATTESTATIONS_SIGNED);
for (int a = 0; a < TOTAL_ATTESTATIONS_SIGNED; a++) {
final tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation attestation = signedArtifact.getSignedAttestations().get(a);
assertThat(attestation.getSigningRoot()).isEqualTo(Bytes.of(100));
assertThat(attestation.getSourceEpoch()).isEqualTo(UInt64.valueOf(a));
assertThat(attestation.getTargetEpoch()).isEqualTo(UInt64.valueOf(a));
}
}
}
use of dsl.InterchangeV5Format 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 dsl.InterchangeV5Format 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