-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for slashing interchange format tests #8185
Merged
StefanBratanov
merged 12 commits into
Consensys:master
from
StefanBratanov:add_support_slashing_format_tests
Apr 15, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fad8dcf
Add support for slashing interchange format tests
StefanBratanov a7850ff
fix spotless
StefanBratanov cfca406
implement logic for testing
StefanBratanov d0546fa
cleaner
StefanBratanov cb2ab0b
nit
StefanBratanov 1cfdfa9
small nits
StefanBratanov 460be01
small refactor
StefanBratanov 29cd6b6
add dot
StefanBratanov f1dc151
sanity check
StefanBratanov f7c451d
nit
StefanBratanov 7882df3
add test + clarifying comment
StefanBratanov fc743f2
nit
StefanBratanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
data/dataexchange/src/test/resources/format2_minimal_different_genesis_validators_root.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...nce/phase0/slashing_protection_interchange/SlashingProtectionInterchangeTestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.reference.phase0.slashing_protection_interchange; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.bls.BLSPublicKey; | ||
import tech.pegasys.teku.data.SlashingProtectionImporter; | ||
import tech.pegasys.teku.data.slashinginterchange.SlashingProtectionInterchangeFormat; | ||
import tech.pegasys.teku.ethtests.finder.TestDefinition; | ||
import tech.pegasys.teku.infrastructure.io.SyncDataAccessor; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.reference.TestDataUtils; | ||
import tech.pegasys.teku.reference.TestExecutor; | ||
import tech.pegasys.teku.reference.phase0.slashing_protection_interchange.SlashingProtectionInterchangeTestExecutor.TestData.Step; | ||
import tech.pegasys.teku.spec.signatures.LocalSlashingProtector; | ||
|
||
public class SlashingProtectionInterchangeTestExecutor implements TestExecutor { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
@Override | ||
public void runTest(final TestDefinition testDefinition) throws Throwable { | ||
final TestData testData = | ||
TestDataUtils.loadJson(testDefinition, testDefinition.getTestName(), TestData.class); | ||
|
||
// 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. This case is covered by | ||
// SlashingProtectionImporterTest#shouldFailImportingIfValidatorExistingRecordHasDifferentGenesisValidatorsRoot() | ||
if (testData.name.startsWith("wrong_genesis_validators_root")) { | ||
LOG.info("Skipping {}", testData.name); | ||
return; | ||
} | ||
|
||
LOG.info("Running {}", testData.name); | ||
|
||
final Path slashingProtectionPath = Files.createTempDirectory("slashprotection"); | ||
try { | ||
runTest(testData, slashingProtectionPath); | ||
} finally { | ||
deleteDirectory(slashingProtectionPath); | ||
} | ||
} | ||
|
||
private void runTest(final TestData testData, final Path slashingProtectionPath) { | ||
final SlashingProtectionImporter importer = | ||
new SlashingProtectionImporter(slashingProtectionPath); | ||
final LocalSlashingProtector slashingProtector = | ||
new LocalSlashingProtector( | ||
SyncDataAccessor.create(slashingProtectionPath), slashingProtectionPath); | ||
testData.steps.forEach(step -> runStep(step, importer, slashingProtector)); | ||
} | ||
|
||
private void runStep( | ||
final Step step, | ||
final SlashingProtectionImporter importer, | ||
final LocalSlashingProtector slashingProtector) { | ||
final Map<BLSPublicKey, String> importErrors = importInterchange(importer, step.interchange); | ||
if (step.shouldSucceed) { | ||
assertThat(importErrors).isEmpty(); | ||
} else { | ||
assertThat(importErrors).isNotEmpty(); | ||
} | ||
final Bytes32 genesisValidatorsRoot = step.interchange.metadata.genesisValidatorsRoot; | ||
step.blocks.forEach( | ||
block -> | ||
assertThat( | ||
slashingProtector.maySignBlock(block.pubkey, genesisValidatorsRoot, block.slot)) | ||
.isCompletedWithValue(block.shouldSucceed)); | ||
step.attestations.forEach( | ||
attestation -> | ||
assertThat( | ||
slashingProtector.maySignAttestation( | ||
attestation.pubkey, | ||
genesisValidatorsRoot, | ||
attestation.sourceEpoch, | ||
attestation.targetEpoch)) | ||
.isCompletedWithValue(attestation.shouldSucceed)); | ||
} | ||
|
||
private Map<BLSPublicKey, String> importInterchange( | ||
final SlashingProtectionImporter importer, | ||
final SlashingProtectionInterchangeFormat interchange) { | ||
try { | ||
final Path importFile = Files.createTempFile("import", ".json"); | ||
TestDataUtils.writeJsonToFile(interchange, importFile); | ||
final Optional<String> initialiseError = importer.initialise(importFile.toFile()); | ||
assertThat(initialiseError).isEmpty(); | ||
// cleanup | ||
Files.delete(importFile); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
return importer.updateLocalRecords(status -> LOG.info("Import status: " + status)); | ||
} | ||
|
||
private void deleteDirectory(final Path dir) { | ||
try (DirectoryStream<Path> files = Files.newDirectoryStream(dir)) { | ||
for (Path file : files) { | ||
if (Files.isRegularFile(file)) { | ||
Files.delete(file); | ||
} | ||
} | ||
Files.delete(dir); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
} | ||
|
||
public record TestData( | ||
String name, | ||
@JsonProperty("genesis_validators_root") Bytes32 genesisValidatorsRoot, | ||
List<Step> steps) { | ||
|
||
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, | ||
List<Attestation> attestations) {} | ||
|
||
public record Block( | ||
BLSPublicKey pubkey, | ||
UInt64 slot, | ||
@JsonProperty("signing_root") Bytes32 signingRoot, | ||
@JsonProperty("should_succeed") boolean shouldSucceed) {} | ||
|
||
public record Attestation( | ||
BLSPublicKey pubkey, | ||
@JsonProperty("source_epoch") UInt64 sourceEpoch, | ||
@JsonProperty("target_epoch") UInt64 targetEpoch, | ||
@JsonProperty("signing_root") Bytes32 signingRoot, | ||
@JsonProperty("should_succeed") boolean shouldSucceed) {} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add the name of the test which covers this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually there is no test for this, will add it.