Search in sources :

Example 1 with SignedArtifacts

use of dsl.SignedArtifacts in project web3signer by ConsenSys.

the class SlashingExportAcceptanceTest method slashingDataIsExported.

@Test
void slashingDataIsExported(@TempDir Path testDirectory) throws IOException {
    setupSigner(testDirectory, true);
    final Eth2SigningRequestBody request = createAttestationRequest(5, 6, UInt64.ZERO);
    final Response initialResponse = signer.eth2Sign(keyPair.getPublicKey().toString(), request);
    assertThat(initialResponse.getStatusCode()).isEqualTo(200);
    assertThat(signer.getMetricsMatching(blockSlashingMetrics)).containsOnly(blockSlashingMetrics.get(0) + " 1.0", blockSlashingMetrics.get(1) + " 0.0");
    final Path exportFile = testDirectory.resolve("dbExport.json");
    final SignerConfigurationBuilder builder = new SignerConfigurationBuilder();
    builder.withMode("eth2");
    builder.withSlashingEnabled(true);
    builder.withSlashingProtectionDbUrl(signer.getSlashingDbUrl());
    builder.withSlashingProtectionDbUsername("postgres");
    builder.withSlashingProtectionDbPassword("postgres");
    builder.withKeyStoreDirectory(testDirectory);
    builder.withSlashingExportPath(exportFile);
    // prevent wait for Ports file in AT
    builder.withHttpPort(12345);
    final Signer exportSigner = new Signer(builder.build(), null);
    exportSigner.start();
    waitFor(() -> assertThat(exportSigner.isRunning()).isFalse());
    final ObjectMapper mapper = new InterchangeJsonProvider().getJsonMapper();
    final InterchangeV5Format mappedData = mapper.readValue(exportFile.toFile(), InterchangeV5Format.class);
    assertThat(mappedData.getMetadata().getFormatVersion()).isEqualTo("5");
    assertThat(mappedData.getMetadata().getGenesisValidatorsRoot()).isEqualTo(Bytes.fromHexString(GENESIS_VALIDATORS_ROOT));
    assertThat(mappedData.getSignedArtifacts()).hasSize(1);
    final SignedArtifacts artifacts = mappedData.getSignedArtifacts().get(0);
    assertThat(artifacts.getSignedBlocks()).hasSize(0);
    assertThat(artifacts.getSignedAttestations()).hasSize(1);
    final SignedAttestation attestation = artifacts.getSignedAttestations().get(0);
    assertThat(attestation.getSourceEpoch().toLong()).isEqualTo(request.getAttestation().source.epoch.longValue());
    assertThat(attestation.getTargetEpoch().toLong()).isEqualTo(request.getAttestation().target.epoch.longValue());
    assertThat(attestation.getSigningRoot()).isNotNull();
}
Also used : Response(io.restassured.response.Response) Path(java.nio.file.Path) Signer(tech.pegasys.web3signer.dsl.signer.Signer) SignerConfigurationBuilder(tech.pegasys.web3signer.dsl.signer.SignerConfigurationBuilder) InterchangeV5Format(dsl.InterchangeV5Format) SignedArtifacts(dsl.SignedArtifacts) Eth2SigningRequestBody(tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.Eth2SigningRequestBody) InterchangeJsonProvider(tech.pegasys.web3signer.slashingprotection.interchange.InterchangeJsonProvider) SignedAttestation(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with SignedArtifacts

use of dsl.SignedArtifacts in project web3signer by ConsenSys.

the class SlashingImportAcceptanceTest method slashingDataIsImported.

@Test
void slashingDataIsImported(@TempDir Path testDirectory) throws URISyntaxException, IOException {
    setupSigner(testDirectory, true);
    final Path importFile = new File(Resources.getResource("slashing/slashingImport.json").toURI()).toPath();
    final SignerConfigurationBuilder builder = new SignerConfigurationBuilder();
    builder.withMode("eth2");
    builder.withSlashingEnabled(true);
    builder.withSlashingProtectionDbUrl(signer.getSlashingDbUrl());
    builder.withSlashingProtectionDbUsername("postgres");
    builder.withSlashingProtectionDbPassword("postgres");
    builder.withKeyStoreDirectory(testDirectory);
    builder.withSlashingImportPath(importFile);
    // prevent wait for Ports file in AT
    builder.withHttpPort(12345);
    final Signer importSigner = new Signer(builder.build(), null);
    importSigner.start();
    waitFor(() -> assertThat(importSigner.isRunning()).isFalse());
    final InterchangeV5Format interchangeData = objectMapper.readValue(importFile.toFile(), InterchangeV5Format.class);
    final Jdbi jdbi = Jdbi.create(signer.getSlashingDbUrl(), DB_USERNAME, DB_PASSWORD);
    final int validatorId = 1;
    final Map<String, Object> metadata = jdbi.withHandle(h -> h.select("SELECT * from metadata").mapToMap().one());
    assertThat(metadata.get("id")).isEqualTo(validatorId);
    assertThat(metadata.get("genesis_validators_root")).isEqualTo(interchangeData.getMetadata().getGenesisValidatorsRoot().toArray());
    final SignedArtifacts artifacts = interchangeData.getSignedArtifacts().get(0);
    final List<Map<String, Object>> validators = jdbi.withHandle(h -> h.select("SELECT * from validators").mapToMap().list());
    assertThat(validators).hasSize(1);
    assertThat(validators.get(0).get("id")).isEqualTo(validatorId);
    assertThat(validators.get(0).get("public_key")).isEqualTo(Bytes.fromHexString(artifacts.getPublicKey()).toArray());
    final List<Map<String, Object>> signedAttestations = jdbi.withHandle(h -> h.select("SELECT * from signed_attestations").mapToMap().list());
    final SignedAttestation attestation = artifacts.getSignedAttestations().get(0);
    assertThat(signedAttestations).hasSize(1);
    assertThat(signedAttestations.get(0).get("validator_id")).isEqualTo(validatorId);
    assertThat(signedAttestations.get(0).get("source_epoch")).isEqualTo(BigDecimal.valueOf(attestation.getSourceEpoch().toLong()));
    assertThat(signedAttestations.get(0).get("target_epoch")).isEqualTo(BigDecimal.valueOf(attestation.getTargetEpoch().toLong()));
    assertThat(signedAttestations.get(0).get("signing_root")).isEqualTo(attestation.getSigningRoot().toArray());
    final List<Map<String, Object>> signedBlocks = jdbi.withHandle(h -> h.select("SELECT * from signed_blocks").mapToMap().list());
    final SignedBlock block = artifacts.getSignedBlocks().get(0);
    assertThat(signedBlocks).hasSize(1);
    assertThat(signedBlocks.get(0).get("validator_id")).isEqualTo(validatorId);
    assertThat(signedBlocks.get(0).get("slot")).isEqualTo(BigDecimal.valueOf(block.getSlot().toLong()));
    assertThat(signedBlocks.get(0).get("signing_root")).isEqualTo(block.getSigningRoot().toArray());
}
Also used : Path(java.nio.file.Path) Jdbi(org.jdbi.v3.core.Jdbi) SignedArtifacts(dsl.SignedArtifacts) SignedBlock(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock) Signer(tech.pegasys.web3signer.dsl.signer.Signer) SignerConfigurationBuilder(tech.pegasys.web3signer.dsl.signer.SignerConfigurationBuilder) InterchangeV5Format(dsl.InterchangeV5Format) SignedAttestation(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation) File(java.io.File) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with SignedArtifacts

use of dsl.SignedArtifacts 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));
        }
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) SignedBlock(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock) InterchangeV5Format(dsl.InterchangeV5Format) SignedArtifacts(dsl.SignedArtifacts) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 4 with SignedArtifacts

use of dsl.SignedArtifacts 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));
        }
    }
}
Also used : SignedArtifacts(dsl.SignedArtifacts) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IncrementalExporter(tech.pegasys.web3signer.slashingprotection.interchange.IncrementalExporter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) SignedBlock(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock) InterchangeV5Format(dsl.InterchangeV5Format) Test(org.junit.jupiter.api.Test)

Example 5 with SignedArtifacts

use of dsl.SignedArtifacts 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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InterchangeV5Format(dsl.InterchangeV5Format) SignedArtifacts(dsl.SignedArtifacts) Metadata(tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata) SignedAttestation(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation) Test(org.junit.jupiter.api.Test)

Aggregations

InterchangeV5Format (dsl.InterchangeV5Format)6 SignedArtifacts (dsl.SignedArtifacts)6 Test (org.junit.jupiter.api.Test)6 SignedAttestation (tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation)3 SignedBlock (tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Path (java.nio.file.Path)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Signer (tech.pegasys.web3signer.dsl.signer.Signer)2 SignerConfigurationBuilder (tech.pegasys.web3signer.dsl.signer.SignerConfigurationBuilder)2 Metadata (tech.pegasys.web3signer.slashingprotection.interchange.model.Metadata)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Response (io.restassured.response.Response)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1 Jdbi (org.jdbi.v3.core.Jdbi)1 Eth2SigningRequestBody (tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.Eth2SigningRequestBody)1