Search in sources :

Example 1 with SigningWatermark

use of tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark in project web3signer by ConsenSys.

the class DbSlashingProtectionTest method attestationCanBeSignedWithSourceEpochEqualToMin.

@Test
public void attestationCanBeSignedWithSourceEpochEqualToMin() {
    when(lowWatermarkDao.findLowWatermarkForValidator(any(), anyInt())).thenReturn(Optional.of(new SigningWatermark(1, null, SOURCE_EPOCH, UInt64.valueOf(3))));
    assertThat(dbSlashingProtection.maySignAttestation(PUBLIC_KEY1, SIGNING_ROOT, SOURCE_EPOCH, TARGET_EPOCH, GVR)).isTrue();
    verify(lowWatermarkDao).findLowWatermarkForValidator(any(), eq(VALIDATOR_ID));
    final SignedAttestation attestation = new SignedAttestation(VALIDATOR_ID, SOURCE_EPOCH, TARGET_EPOCH, SIGNING_ROOT);
    verify(signedAttestationsDao).insertAttestation(any(), refEq(attestation));
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) SignedAttestation(tech.pegasys.web3signer.slashingprotection.dao.SignedAttestation) Test(org.junit.jupiter.api.Test)

Example 2 with SigningWatermark

use of tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark in project web3signer by ConsenSys.

the class DbSlashingProtectionTest method blockCannotBeSignedIfMatchesBlockBelowWatermark.

@Test
public void blockCannotBeSignedIfMatchesBlockBelowWatermark() {
    final SignedBlock signedBlock = new SignedBlock(VALIDATOR_ID, SLOT, SIGNING_ROOT);
    when(lowWatermarkDao.findLowWatermarkForValidator(any(), anyInt())).thenReturn(Optional.of(new SigningWatermark(VALIDATOR_ID, SLOT.add(1L), null, null)));
    assertThat(dbSlashingProtection.maySignBlock(PUBLIC_KEY1, SIGNING_ROOT, SLOT, GVR)).isFalse();
    verify(signedBlocksDao, never()).insertBlockProposal(any(), refEq(signedBlock));
    verify(lowWatermarkDao).findLowWatermarkForValidator(any(), eq(VALIDATOR_ID));
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) SignedBlock(tech.pegasys.web3signer.slashingprotection.dao.SignedBlock) Test(org.junit.jupiter.api.Test)

Example 3 with SigningWatermark

use of tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark in project web3signer by ConsenSys.

the class DbSlashingProtectionTest method cannotSignAttestationWhichWasPreviouslySignedButBelowSourceWatermark.

@Test
public void cannotSignAttestationWhichWasPreviouslySignedButBelowSourceWatermark() {
    final SignedAttestation attestation = new SignedAttestation(VALIDATOR_ID, SOURCE_EPOCH, TARGET_EPOCH, SIGNING_ROOT);
    when(lowWatermarkDao.findLowWatermarkForValidator(any(), anyInt())).thenReturn(Optional.of(new SigningWatermark(VALIDATOR_ID, null, SOURCE_EPOCH.add(1L), TARGET_EPOCH.subtract(1L))));
    assertThat(dbSlashingProtection.maySignAttestation(PUBLIC_KEY1, SIGNING_ROOT, SOURCE_EPOCH, TARGET_EPOCH, GVR)).isFalse();
    verify(signedAttestationsDao, never()).insertAttestation(any(), refEq(attestation));
    verify(lowWatermarkDao).findLowWatermarkForValidator(any(), eq(VALIDATOR_ID));
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) SignedAttestation(tech.pegasys.web3signer.slashingprotection.dao.SignedAttestation) Test(org.junit.jupiter.api.Test)

Example 4 with SigningWatermark

use of tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark in project web3signer by ConsenSys.

the class DbSlashingProtectionTest method attestationCanSignWhenExactlyMatchesExistingAttestationAndIsAboveWatermark.

@Test
public void attestationCanSignWhenExactlyMatchesExistingAttestationAndIsAboveWatermark() {
    final SignedAttestation attestation = new SignedAttestation(VALIDATOR_ID, SOURCE_EPOCH, TARGET_EPOCH, SIGNING_ROOT);
    when(signedAttestationsDao.findAttestationsForEpochWithDifferentSigningRoot(any(), anyInt(), any(), any())).thenReturn(emptyList());
    when(signedAttestationsDao.findMatchingAttestation(any(), anyInt(), any(), eq(SIGNING_ROOT))).thenReturn(Optional.of(attestation));
    when(lowWatermarkDao.findLowWatermarkForValidator(any(), anyInt())).thenReturn(Optional.of(new SigningWatermark(VALIDATOR_ID, null, UInt64.valueOf(1), UInt64.valueOf(2))));
    assertThat(dbSlashingProtection.maySignAttestation(PUBLIC_KEY1, SIGNING_ROOT, SOURCE_EPOCH, TARGET_EPOCH, GVR)).isTrue();
    verify(signedAttestationsDao).findAttestationsForEpochWithDifferentSigningRoot(any(), eq(VALIDATOR_ID), eq(TARGET_EPOCH), eq(SIGNING_ROOT));
    verify(signedAttestationsDao, never()).insertAttestation(any(), refEq(attestation));
    verify(lowWatermarkDao).findLowWatermarkForValidator(any(), eq(VALIDATOR_ID));
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) SignedAttestation(tech.pegasys.web3signer.slashingprotection.dao.SignedAttestation) Test(org.junit.jupiter.api.Test)

Example 5 with SigningWatermark

use of tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark in project web3signer by ConsenSys.

the class PruningIntegrationTest method prunesDataForRegisteredValidator.

@ParameterizedTest
@CsvSource({ "1, 1, 9, 9", "5, 1, 5, 5", "9, 1, 1, 1, 1", "10, 1, 0, 0", "20, 1, 0, 0", "1, 2, 9, 8", "3, 2, 7, 4" })
void prunesDataForRegisteredValidator(final int amountToKeep, final int slotsPerEpoch, final int expectedLowestPopulatedEpoch, final int expectedLowestPopulatedSlot) {
    final SlashingProtectionContext slashingProtectionContext = SlashingProtectionContextFactory.create(new TestSlashingProtectionParameters(databaseUrl, USERNAME, PASSWORD, amountToKeep, slotsPerEpoch));
    final int size = 10;
    insertValidatorAndCreateSlashingData(slashingProtectionContext.getRegisteredValidators(), size, size, 1);
    final List<SignedAttestation> allAttestations = fetchAttestations(1);
    final List<SignedBlock> allBlocks = fetchBlocks(1);
    slashingProtectionContext.getSlashingProtection().prune();
    final List<SignedAttestation> expectedAttestations = allAttestations.subList(expectedLowestPopulatedEpoch, size);
    final List<SignedAttestation> attestationsInDatabase = fetchAttestations(1);
    assertThat(attestationsInDatabase).usingFieldByFieldElementComparator().isEqualTo(expectedAttestations);
    final List<SignedBlock> expectedBlocks = allBlocks.subList(expectedLowestPopulatedSlot, size);
    final List<SignedBlock> blocks = fetchBlocks(1);
    assertThat(blocks).usingFieldByFieldElementComparator().isEqualTo(expectedBlocks);
    assertThat(getWatermark(1)).isEqualToComparingFieldByField(new SigningWatermark(1, UInt64.valueOf(expectedLowestPopulatedSlot), UInt64.valueOf(expectedLowestPopulatedEpoch), UInt64.valueOf(expectedLowestPopulatedEpoch)));
}
Also used : SigningWatermark(tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark) TestSlashingProtectionParameters(dsl.TestSlashingProtectionParameters) SignedAttestation(tech.pegasys.web3signer.slashingprotection.dao.SignedAttestation) SignedBlock(tech.pegasys.web3signer.slashingprotection.dao.SignedBlock) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SigningWatermark (tech.pegasys.web3signer.slashingprotection.dao.SigningWatermark)22 Test (org.junit.jupiter.api.Test)20 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)5 SignedAttestation (tech.pegasys.web3signer.slashingprotection.dao.SignedAttestation)5 SignedBlock (tech.pegasys.web3signer.slashingprotection.dao.SignedBlock)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 DatabaseUtil (db.DatabaseUtil)1 TestSlashingProtectionParameters (dsl.TestSlashingProtectionParameters)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 UInt64 (org.apache.tuweni.units.bigints.UInt64)1 Jdbi (org.jdbi.v3.core.Jdbi)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1 Validator (tech.pegasys.web3signer.slashingprotection.dao.Validator)1 SignedBlock (tech.pegasys.web3signer.slashingprotection.interchange.model.SignedBlock)1 BlockValidator (tech.pegasys.web3signer.slashingprotection.validator.BlockValidator)1