-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for slashing interchange format tests
- Loading branch information
1 parent
2c2249a
commit f482f88
Showing
8 changed files
with
192 additions
and
6 deletions.
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
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
93 changes: 93 additions & 0 deletions
93
...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,93 @@ | ||
/* | ||
* 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.databind.JsonNode; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
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.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.spec.signatures.LocalSlashingProtector; | ||
|
||
public class SlashingProtectionInterchangeTestExecutor implements TestExecutor { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
//TODO: implement the logic | ||
@Override | ||
public void runTest(final TestDefinition testDefinition) throws Throwable { | ||
final JsonNode testNode = TestDataUtils.loadJson(testDefinition, testDefinition.getTestName()); | ||
|
||
final String testName = testNode.get("name").asText(); | ||
final Bytes32 genesisValidatorsRoot = | ||
Bytes32.fromHexString(testNode.get("genesis_validators_root").asText()); | ||
|
||
LOG.info("Running {}", testName); | ||
|
||
final Path tempDir = Files.createTempDirectory(testName); | ||
|
||
final Path slashProtectionPath = tempDir.resolve("slashprotection"); | ||
|
||
final Path slashProtectionImportFile = tempDir.resolve("import.yml"); | ||
|
||
Files.writeString( | ||
slashProtectionImportFile, | ||
testNode.get("steps").get(0).get("interchange").toString(), | ||
StandardCharsets.UTF_8); | ||
|
||
final BLSPublicKey pubkey = | ||
BLSPublicKey.fromHexString( | ||
"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c"); | ||
|
||
Files.createDirectories(slashProtectionPath); | ||
SlashingProtectionImporter importer = new SlashingProtectionImporter(slashProtectionPath); | ||
importer.initialise(slashProtectionImportFile.toFile()); | ||
final Map<BLSPublicKey, String> errors = importer.updateLocalRecords((__) -> {}); | ||
|
||
assertThat(errors).isEmpty(); | ||
|
||
LocalSlashingProtector localSlashingProtector = | ||
new LocalSlashingProtector( | ||
SyncDataAccessor.create(slashProtectionPath), slashProtectionPath); | ||
assertThat( | ||
localSlashingProtector.maySignBlock(pubkey, genesisValidatorsRoot, UInt64.valueOf(10))) | ||
.isCompletedWithValue(false); | ||
assertThat( | ||
localSlashingProtector.maySignBlock(pubkey, genesisValidatorsRoot, UInt64.valueOf(13))) | ||
.isCompletedWithValue(false); | ||
assertThat( | ||
localSlashingProtector.maySignBlock(pubkey, genesisValidatorsRoot, UInt64.valueOf(14))) | ||
.isCompletedWithValue(true); | ||
assertThat( | ||
localSlashingProtector.maySignAttestation( | ||
pubkey, genesisValidatorsRoot, UInt64.valueOf(0), UInt64.valueOf(2))) | ||
.isCompletedWithValue(false); | ||
assertThat( | ||
localSlashingProtector.maySignAttestation( | ||
pubkey, genesisValidatorsRoot, UInt64.valueOf(1), UInt64.valueOf(3))) | ||
.isCompletedWithValue(false); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
.../main/java/tech/pegasys/teku/ethtests/finder/SlashingProtectionInterchangeTestFinder.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,42 @@ | ||
/* | ||
* 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.ethtests.finder; | ||
|
||
import com.google.errorprone.annotations.MustBeClosed; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
public class SlashingProtectionInterchangeTestFinder implements TestFinder { | ||
|
||
@Override | ||
@MustBeClosed | ||
public Stream<TestDefinition> findTests(final String fork, final String spec, final Path testRoot) | ||
throws IOException { | ||
if (!spec.equals("slashing-protection-interchange")) { | ||
return Stream.empty(); | ||
} | ||
return Files.list(testRoot) | ||
.filter(file -> file.toFile().getName().endsWith(".json")) | ||
.map( | ||
testFile -> | ||
new TestDefinition( | ||
fork, | ||
spec, | ||
spec, | ||
testFile.toFile().getName(), | ||
testRoot.relativize(testFile.getParent()))); | ||
} | ||
} |