From da5389e212e9999e66ac0d6b0027c1bc86078368 Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Mon, 15 Apr 2024 11:11:09 +0100 Subject: [PATCH] add test + clarifying comment --- .../data/SlashingProtectionImporterTest.java | 33 +++++++++++++++++-- .../src/test/resources/format2_minimal.json | 3 +- ...mal_different_genesis_validators_root.json | 22 +++++++++++++ ...hingProtectionInterchangeTestExecutor.java | 5 ++- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 data/dataexchange/src/test/resources/format2_minimal_different_genesis_validators_root.json diff --git a/data/dataexchange/src/test/java/tech/pegasys/teku/data/SlashingProtectionImporterTest.java b/data/dataexchange/src/test/java/tech/pegasys/teku/data/SlashingProtectionImporterTest.java index 29f443e8e1e..0ac17428386 100644 --- a/data/dataexchange/src/test/java/tech/pegasys/teku/data/SlashingProtectionImporterTest.java +++ b/data/dataexchange/src/test/java/tech/pegasys/teku/data/SlashingProtectionImporterTest.java @@ -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 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()))); @@ -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()); + } } diff --git a/data/dataexchange/src/test/resources/format2_minimal.json b/data/dataexchange/src/test/resources/format2_minimal.json index 9a4f1645fef..89c0bab40c1 100644 --- a/data/dataexchange/src/test/resources/format2_minimal.json +++ b/data/dataexchange/src/test/resources/format2_minimal.json @@ -7,7 +7,8 @@ { "pubkey": "0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed", "signed_blocks": [ - {"slot": "81952" + { + "slot": "81952" } ], "signed_attestations": [ diff --git a/data/dataexchange/src/test/resources/format2_minimal_different_genesis_validators_root.json b/data/dataexchange/src/test/resources/format2_minimal_different_genesis_validators_root.json new file mode 100644 index 00000000000..9a926c08fd6 --- /dev/null +++ b/data/dataexchange/src/test/resources/format2_minimal_different_genesis_validators_root.json @@ -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" + } + ] + } + ] +} \ No newline at end of file diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/slashing_protection_interchange/SlashingProtectionInterchangeTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/slashing_protection_interchange/SlashingProtectionInterchangeTestExecutor.java index 38537f2125f..dbbb9f7eca1 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/slashing_protection_interchange/SlashingProtectionInterchangeTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/slashing_protection_interchange/SlashingProtectionInterchangeTestExecutor.java @@ -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; @@ -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 blocks,