Search in sources :

Example 1 with SignedBlock

use of tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock in project web3signer by ConsenSys.

the class BlockImporter method importFrom.

public void importFrom(final ArrayNode signedBlocksNode) throws JsonProcessingException {
    for (int i = 0; i < signedBlocksNode.size(); i++) {
        final SignedBlock jsonBlock = mapper.treeToValue(signedBlocksNode.get(i), SignedBlock.class);
        final BlockValidator blockValidator = new BlockValidator(handle, jsonBlock.getSigningRoot(), jsonBlock.getSlot(), validator.getId(), signedBlocksDao, lowWatermarkDao);
        final String blockIdentifierString = String.format("Block with index %d for validator %s", i, validator.getPublicKey());
        if (jsonBlock.getSigningRoot() == null) {
            if (nullBlockAlreadyExistsInSlot(jsonBlock.getSlot())) {
                LOG.warn("{} - already exists in database, not imported", blockIdentifierString);
            } else {
                persist(jsonBlock);
            }
        } else {
            if (blockValidator.directlyConflictsWithExistingEntry()) {
                LOG.warn("{} - conflicts with an existing entry, not imported", blockIdentifierString);
            } else if (blockValidator.alreadyExists()) {
                LOG.debug("{} - already exists in database, not imported", blockIdentifierString);
            } else {
                persist(jsonBlock);
            }
        }
    }
    final Optional<SigningWatermark> watermark = lowWatermarkDao.findLowWatermarkForValidator(handle, validator.getId());
    if (minSlotTracker.compareTrackedValueTo(watermark.map(SigningWatermark::getSlot)) > 0) {
        LOG.warn("Updating Block slot low watermark to {}", minSlotTracker.getTrackedMinValue().get());
        lowWatermarkDao.updateSlotWatermarkFor(handle, validator.getId(), minSlotTracker.getTrackedMinValue().get());
    }
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) BlockValidator(tech.pegasys.web3signer.slashingprotection.validator.BlockValidator) SignedBlock(tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock)

Example 2 with SignedBlock

use of tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock 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)

Aggregations

SignedBlock (tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock)2 InterchangeV5Format (dsl.InterchangeV5Format)1 SignedArtifacts (dsl.SignedArtifacts)1 File (java.io.File)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Jdbi (org.jdbi.v3.core.Jdbi)1 Test (org.junit.jupiter.api.Test)1 Signer (tech.pegasys.web3signer.dsl.signer.Signer)1 SignerConfigurationBuilder (tech.pegasys.web3signer.dsl.signer.SignerConfigurationBuilder)1 SigningWatermark (tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark)1 SignedAttestation (tech.pegasys.web3signer.slashingprotection.interchange.model.SignedAttestation)1 BlockValidator (tech.pegasys.web3signer.slashingprotection.validator.BlockValidator)1