Search in sources :

Example 1 with SafetyState

use of com.radixdlt.hotstuff.safety.SafetyState in project radixdlt by radixdlt.

the class BerkeleySafetyStateStore method get.

@Override
public Optional<SafetyState> get() {
    final var start = System.nanoTime();
    try (com.sleepycat.je.Cursor cursor = this.safetyStore.openCursor(null, null)) {
        DatabaseEntry pKey = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        OperationStatus status = cursor.getLast(pKey, value, LockMode.DEFAULT);
        if (status == OperationStatus.SUCCESS) {
            addBytesRead(pKey.getSize() + value.getSize());
            try {
                final SafetyState deserializedState = serialization.fromDson(value.getData(), SafetyState.class);
                return Optional.of(deserializedState);
            } catch (DeserializeException ex) {
                logger.error("Failed to deserialize persisted SafetyState", ex);
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    } finally {
        addTime(start);
    }
}
Also used : SafetyState(com.radixdlt.hotstuff.safety.SafetyState) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DeserializeException(com.radixdlt.serialization.DeserializeException) Cursor(com.sleepycat.je.Cursor)

Example 2 with SafetyState

use of com.radixdlt.hotstuff.safety.SafetyState in project radixdlt by radixdlt.

the class BerkeleySafetyStateStoreTest method should_be_able_to_restore_committed_state.

@Test
public void should_be_able_to_restore_committed_state() {
    final var db = mock(Database.class);
    final var env = mock(Environment.class);
    final var dbEnv = mock(DatabaseEnvironment.class);
    final var tx = mock(com.sleepycat.je.Transaction.class);
    when(dbEnv.getEnvironment()).thenReturn(env);
    when(env.openDatabase(any(), any(), any())).thenReturn(db);
    final var store = new BerkeleySafetyStateStore(dbEnv, DefaultSerialization.getInstance(), new SystemCountersImpl());
    final var safetyState = new SafetyState(randomView(), Optional.of(randomVote()));
    when(env.beginTransaction(any(), any())).thenReturn(tx);
    when(db.put(any(), any(), any())).thenReturn(OperationStatus.SUCCESS);
    ArgumentCaptor<DatabaseEntry> entryCaptor = ArgumentCaptor.forClass(DatabaseEntry.class);
    store.commitState(safetyState);
    verify(db, times(1)).put(any(), any(), entryCaptor.capture());
    verify(tx, times(1)).commit();
    verifyNoMoreInteractions(tx);
    final var cursor = mock(Cursor.class);
    when(db.openCursor(any(), any())).thenReturn(cursor);
    when(cursor.getLast(any(), any(), any())).thenAnswer(invocation -> {
        DatabaseEntry entry = (DatabaseEntry) invocation.getArguments()[1];
        entry.setData(entryCaptor.getValue().getData());
        return OperationStatus.SUCCESS;
    });
    var state = store.get();
    assertTrue(state.isPresent());
    assertEquals(safetyState, state.get());
}
Also used : SafetyState(com.radixdlt.hotstuff.safety.SafetyState) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Test(org.junit.Test)

Example 3 with SafetyState

use of com.radixdlt.hotstuff.safety.SafetyState in project radixdlt by radixdlt.

the class RecoveryTest method on_reboot_should_load_same_last_vote.

@Test
public void on_reboot_should_load_same_last_vote() {
    // Arrange
    processForCount(processForCount);
    Vote vote = getLastVote();
    // Act
    restartNode();
    // Assert
    SafetyState safetyState = currentInjector.getInstance(SafetyState.class);
    assertThat(safetyState.getLastVotedView().equals(vote.getView()) || (safetyState.getLastVotedView().equals(View.genesis()) && vote.getView().equals(View.of(epochCeilingView + 3)))).isTrue();
}
Also used : Vote(com.radixdlt.hotstuff.Vote) SafetyState(com.radixdlt.hotstuff.safety.SafetyState) Test(org.junit.Test)

Aggregations

SafetyState (com.radixdlt.hotstuff.safety.SafetyState)3 DatabaseEntry (com.sleepycat.je.DatabaseEntry)2 Test (org.junit.Test)2 SystemCountersImpl (com.radixdlt.counters.SystemCountersImpl)1 Vote (com.radixdlt.hotstuff.Vote)1 DeserializeException (com.radixdlt.serialization.DeserializeException)1 Cursor (com.sleepycat.je.Cursor)1 OperationStatus (com.sleepycat.je.OperationStatus)1