Skip to content

Commit

Permalink
add test + clarifying comment
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Apr 15, 2024
1 parent 696b73b commit da5389e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ void shouldImportFileOverRepairedRecords(@TempDir Path tempDir) throws Exception
repairedEpoch));
}

@Test
void shouldFailImportingIfValidatorExistingRecordHasDifferentGenesisValidatorsRoot(
@TempDir Path tempDir) throws URISyntaxException, IOException {
final SlashingProtectionImporter importer = new SlashingProtectionImporter(tempDir);

final File slashProtection = getResourceFile("format2_minimal.json");

importer.initialise(slashProtection);

Map<BLSPublicKey, String> errors = importer.updateLocalRecords(__ -> {});

assertThat(errors).isEmpty();

final File slashProtectionWithDifferentGvr =
getResourceFile("format2_minimal_different_genesis_validators_root.json");

importer.initialise(slashProtectionWithDifferentGvr);

errors = importer.updateLocalRecords(__ -> {});

assertThat(errors)
.hasSize(1)
.containsEntry(publicKey, "Genesis validators root did not match what was expected.");
}

private ValidatorSigningRecord loadSigningRecord(final File repairedRuleFile) throws IOException {
return ValidatorSigningRecord.fromBytes(
Bytes.wrap(Files.readAllBytes(repairedRuleFile.toPath())));
Expand All @@ -182,9 +207,11 @@ private File usingResourceFile(final String resourceFileName, final Path tempDir
throws URISyntaxException, IOException {
final Path tempFile = tempDir.resolve(pubkey + ".yml").toAbsolutePath();
Files.copy(
new File(Resources.getResource(resourceFileName).toURI()).toPath(),
tempFile,
StandardCopyOption.REPLACE_EXISTING);
getResourceFile(resourceFileName).toPath(), tempFile, StandardCopyOption.REPLACE_EXISTING);
return tempFile.toFile();
}

private File getResourceFile(final String resourceFileName) throws URISyntaxException {
return new File(Resources.getResource(resourceFileName).toURI());
}
}
3 changes: 2 additions & 1 deletion data/dataexchange/src/test/resources/format2_minimal.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{
"pubkey": "0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed",
"signed_blocks": [
{"slot": "81952"
{
"slot": "81952"
}
],
"signed_attestations": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"metadata": {
"interchange_format_version": "5",
"genesis_validators_root": "0x0000000000000000000000000000000000000000000000000000000000123457"
},
"data": [
{
"pubkey": "0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed",
"signed_blocks": [
{
"slot": "81952"
}
],
"signed_attestations": [
{
"source_epoch": "2290",
"target_epoch": "3007"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void runTest(final TestDefinition testDefinition) throws Throwable {

// our implementation fails when importing one of the keys in an interchange, which is already
// in our slashprotection directory with a different genesis validators root. However, the test
// does not import any keys.
// does not import any keys. This case is covered by
// SlashingProtectionImporterTest#shouldFailImportingIfValidatorExistingRecordHasDifferentGenesisValidatorsRoot()
if (testData.name.startsWith("wrong_genesis_validators_root")) {
LOG.info("Skipping {}", testData.name);
return;
Expand Down Expand Up @@ -137,6 +138,8 @@ public record TestData(

public record Step(
@JsonProperty("should_succeed") boolean shouldSucceed,
// we don't fail importing when the interchange contains slashable data, so can safely
// ignore this field in the tests
@JsonProperty("contains_slashable_data") boolean containsSlashableData,
SlashingProtectionInterchangeFormat interchange,
List<Block> blocks,
Expand Down

0 comments on commit da5389e

Please sign in to comment.