diff --git a/slashing-protection/src/main/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDao.java b/slashing-protection/src/main/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDao.java index a5a5864fa..b52d6f26e 100644 --- a/slashing-protection/src/main/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDao.java +++ b/slashing-protection/src/main/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDao.java @@ -43,7 +43,7 @@ public Optional findHighWatermark(Handle handle) { "SELECT high_watermark_epoch as epoch, high_watermark_slot as slot FROM metadata WHERE id = ?") .bind(0, METADATA_ROW_ID) .mapToBean(HighWatermark.class) - .filter(h -> h.getEpoch() != null && h.getSlot() != null) + .filter(h -> h.getEpoch() != null || h.getSlot() != null) .findFirst(); } diff --git a/slashing-protection/src/test/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDaoTest.java b/slashing-protection/src/test/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDaoTest.java index 15e268f69..2a7e730c5 100644 --- a/slashing-protection/src/test/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDaoTest.java +++ b/slashing-protection/src/test/java/tech/pegasys/web3signer/slashingprotection/dao/MetadataDaoTest.java @@ -92,6 +92,26 @@ public void findsExistingHighWatermark(final Handle handle) { .contains(new HighWatermark(UInt64.valueOf(2), UInt64.valueOf(1))); } + @Test + public void findsExistingHighWatermarkWithOnlyEpoch(final Handle handle) { + insertGvr(handle, Bytes32.leftPad(Bytes.of(3))); + updateHighWatermark(handle, 1, null); + + final Optional existingHighWatermark = metadataDao.findHighWatermark(handle); + + assertThat(existingHighWatermark).contains(new HighWatermark(null, UInt64.valueOf(1))); + } + + @Test + public void findsExistingHighWatermarkWithOnlySlot(final Handle handle) { + insertGvr(handle, Bytes32.leftPad(Bytes.of(3))); + updateHighWatermark(handle, null, 2); + + final Optional existingHighWatermark = metadataDao.findHighWatermark(handle); + + assertThat(existingHighWatermark).contains(new HighWatermark(UInt64.valueOf(2), null)); + } + @Test public void returnsEmptyForNonExistingHighWatermark(final Handle handle) { assertThat(metadataDao.findHighWatermark(handle)).isEmpty(); @@ -110,15 +130,27 @@ public void insertsHighWatermark(final Handle handle) { int updateCount = metadataDao.updateHighWatermark(handle, highWatermark); - assertThat(updateCount).isEqualTo(1); - final List highWatermarks = - handle - .createQuery( - "SELECT high_watermark_epoch as epoch, high_watermark_slot as slot FROM metadata") - .mapToBean(HighWatermark.class) - .list(); - assertThat(highWatermarks.size()).isEqualTo(1); - assertThat(highWatermarks.get(0)).isEqualTo(highWatermark); + assertHighWatermarkUpdatedSuccessfully(handle, updateCount, highWatermark); + } + + @Test + public void insertsOnlyEpochHighWatermark(final Handle handle) { + insertGvr(handle, Bytes32.leftPad(Bytes.of(3))); + HighWatermark highWatermark = new HighWatermark(null, UInt64.valueOf(1)); + + int updateCount = metadataDao.updateHighWatermark(handle, highWatermark); + + assertHighWatermarkUpdatedSuccessfully(handle, updateCount, highWatermark); + } + + @Test + public void insertsOnlySlotHighWatermark(final Handle handle) { + insertGvr(handle, Bytes32.leftPad(Bytes.of(3))); + HighWatermark highWatermark = new HighWatermark(UInt64.valueOf(1), null); + + int updateCount = metadataDao.updateHighWatermark(handle, highWatermark); + + assertHighWatermarkUpdatedSuccessfully(handle, updateCount, highWatermark); } @Test @@ -129,6 +161,11 @@ public void updatesHighWatermark(final Handle handle) { int updateCount = metadataDao.updateHighWatermark(handle, highWatermark); + assertHighWatermarkUpdatedSuccessfully(handle, updateCount, highWatermark); + } + + private void assertHighWatermarkUpdatedSuccessfully( + Handle handle, int updateCount, HighWatermark highWatermark) { assertThat(updateCount).isEqualTo(1); final List highWatermarks = handle @@ -220,7 +257,7 @@ private void insertLowWatermarks(Handle handle) { MAX_LOW_WATERMARK_SOURCE_EPOCH); } - private void updateHighWatermark(final Handle handle, final int epoch, final int slot) { + private void updateHighWatermark(final Handle handle, final Integer epoch, final Integer slot) { handle .createUpdate("UPDATE metadata set high_watermark_epoch=:epoch, high_watermark_slot=:slot") .bind("epoch", epoch)