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