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));
}
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));
}
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));
}
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));
}
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)));
}
Aggregations