Skip to content

Commit

Permalink
Add delete finalized state interface (#8374)
Browse files Browse the repository at this point in the history
* Add deleteFinalizedState to V4FinalizedState interface

Signed-off-by: Gabriel Fukushima <[email protected]>

* Add test using deleteFinalizedState

Signed-off-by: Gabriel Fukushima <[email protected]>

* Check for state presence before deleting them

Signed-off-by: Gabriel Fukushima <[email protected]>

---------

Signed-off-by: Gabriel Fukushima <[email protected]>
  • Loading branch information
gfukushima authored Jun 13, 2024
1 parent 2c4dc8e commit 3d6b378
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public void addReconstructedFinalizedState(
}
}

@Override
public void deleteFinalizedState(
final KvStoreTransaction transaction, final S schema, final UInt64 slot) {
transaction.delete(schema.getColumnFinalizedStatesBySlot(), slot);
}

@Override
public void commit() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void addFinalizedState(
void addReconstructedFinalizedState(
KvStoreAccessor db, KvStoreTransaction transaction, S schema, BeaconState state);

void deleteFinalizedState(KvStoreTransaction transaction, S schema, UInt64 slot);

void commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public void addReconstructedFinalizedState(
addFinalizedState(db, transaction, schema, state);
}

@Override
public void deleteFinalizedState(
final KvStoreTransaction transaction,
final SchemaCombinedTreeState schema,
final UInt64 slot) {
transaction.delete(schema.getColumnFinalizedStateRootsBySlot(), slot);
}

@Override
public void commit() {
if (nodeStore != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,43 @@ void shouldStoreAndLoadMultipleStates() {
assertStateReloads(state4);
}

@Test
void shouldStoreAndLoadNonDeletedStates() {
final BeaconState state1 = dataStructureUtil.randomBeaconState(UInt64.valueOf(3));
final BeaconState state2 = dataStructureUtil.randomBeaconState(UInt64.valueOf(5));
final BeaconState state3 = dataStructureUtil.randomBeaconState(UInt64.valueOf(7));
final BeaconState state4 = dataStructureUtil.randomBeaconState(UInt64.valueOf(10));
try (final KvStoreTransaction transaction = db.startTransaction()) {
final FinalizedStateUpdater<SchemaCombinedTreeState> updater = logic.updater();
updater.addFinalizedState(db, transaction, schema, state1);
updater.addFinalizedState(db, transaction, schema, state2);
updater.addFinalizedState(db, transaction, schema, state3);
updater.addFinalizedState(db, transaction, schema, state4);
transaction.commit();
}

assertStateReloads(state1);
assertStateReloads(state2);

try (final KvStoreTransaction transaction = db.startTransaction()) {
final FinalizedStateUpdater<SchemaCombinedTreeState> updater = logic.updater();
updater.deleteFinalizedState(transaction, schema, state1.getSlot());
updater.deleteFinalizedState(transaction, schema, state2.getSlot());
transaction.commit();
}

assertStateIsDeleted(state1.getSlot());
assertStateIsDeleted(state2.getSlot());
assertStateReloads(state3);
assertStateReloads(state4);
}

private void assertStateIsDeleted(final UInt64 slot) {
final Optional<BeaconState> loadedState =
logic.getLatestAvailableFinalizedState(db, schema, slot);
assertThat(loadedState).isEmpty();
}

private void assertStateReloads(final BeaconState state) {
assertStateReloads(state, state.getSlot());
}
Expand Down

0 comments on commit 3d6b378

Please sign in to comment.