use of tech.pegasys.teku.spec.datastructures.state.CheckpointState in project teku by ConsenSys.
the class BlockImporterTest method assertWeakSubjectivityWasChecked.
private void assertWeakSubjectivityWasChecked() {
SafeFuture<CheckpointState> finalizedCheckpoint = recentChainData.getStore().retrieveFinalizedCheckpointAndState();
assertThat(finalizedCheckpoint).isCompleted();
UInt64 currentSlot = recentChainData.getCurrentSlot().orElseThrow();
verify(weakSubjectivityValidator).validateLatestFinalizedCheckpoint(finalizedCheckpoint.join(), currentSlot);
}
use of tech.pegasys.teku.spec.datastructures.state.CheckpointState in project teku by ConsenSys.
the class StoreTransactionTest method retrieveFinalizedCheckpointAndState_finalizedCheckpointPruned.
@Test
public void retrieveFinalizedCheckpointAndState_finalizedCheckpointPruned() {
final UpdatableStore store = createGenesisStore();
final SignedBlockAndState finalizedBlockAndState = chainBuilder.generateBlockAtSlot(spec.slotsPerEpoch(ZERO) - 1);
final Checkpoint finalizedCheckpoint = new Checkpoint(UInt64.ONE, finalizedBlockAndState.getRoot());
final SignedBlockAndState newerFinalizedBlockAndState = chainBuilder.generateBlockAtSlot(spec.slotsPerEpoch(ZERO) * 2);
final Checkpoint newerFinalizedCheckpoint = new Checkpoint(UInt64.valueOf(2), newerFinalizedBlockAndState.getRoot());
// Save blocks
final StoreTransaction blockTx = store.startTransaction(new StubStorageUpdateChannel());
blockTx.putBlockAndState(finalizedBlockAndState);
blockTx.putBlockAndState(newerFinalizedBlockAndState);
assertThat(blockTx.commit()).isCompleted();
// Start tx finalizing epoch 1
final StoreTransaction tx = store.startTransaction(new StubStorageUpdateChannel());
tx.setFinalizedCheckpoint(finalizedCheckpoint);
// Finalize epoch 2
final StoreTransaction otherTx = store.startTransaction(new StubStorageUpdateChannel());
otherTx.putBlockAndState(newerFinalizedBlockAndState);
otherTx.setFinalizedCheckpoint(newerFinalizedCheckpoint);
assertThat(otherTx.commit()).isCompleted();
// Check response from tx1 for finalized value
final SafeFuture<CheckpointState> result = tx.retrieveFinalizedCheckpointAndState();
assertThat(result).isCompleted();
assertThat(result.join().getCheckpoint()).isEqualTo(newerFinalizedCheckpoint);
assertThat(result.join().getRoot()).isEqualTo(newerFinalizedBlockAndState.getRoot());
assertThat(result.join().getState()).isEqualTo(newerFinalizedBlockAndState.getState());
}
use of tech.pegasys.teku.spec.datastructures.state.CheckpointState in project teku by ConsenSys.
the class AbstractStoreTest method processCheckpointsWithLimitedCache.
protected void processCheckpointsWithLimitedCache(BiConsumer<UpdatableStore, CheckpointState> chainProcessor) {
final int cacheSize = 3;
final int epochsToProcess = cacheSize * 3;
// Create a new store with a small cache
final StoreConfig pruningOptions = StoreConfig.builder().checkpointStateCacheSize(cacheSize).blockCacheSize(cacheSize).stateCacheSize(cacheSize).build();
final UpdatableStore store = createGenesisStore(pruningOptions);
while (chainBuilder.getLatestEpoch().longValue() < epochsToProcess) {
SignedBlockAndState block = chainBuilder.generateNextBlock();
addBlock(store, block);
}
// Collect checkpoints for each epoch
final List<CheckpointState> allCheckpoints = new ArrayList<>();
for (int i = 0; i <= chainBuilder.getLatestEpoch().intValue(); i++) {
Checkpoint checkpoint = chainBuilder.getCurrentCheckpointForEpoch(i);
SignedBlockAndState blockAndState = chainBuilder.getBlockAndState(checkpoint.getRoot()).get();
allCheckpoints.add(CheckpointState.create(spec, checkpoint, blockAndState.getBlock(), blockAndState.getState()));
}
assertThat(allCheckpoints.size()).isEqualTo(epochsToProcess + 1);
allCheckpoints.forEach(c -> chainProcessor.accept(store, c));
allCheckpoints.forEach(c -> chainProcessor.accept(store, c));
// Process in reverse order
Collections.reverse(allCheckpoints);
allCheckpoints.forEach(c -> chainProcessor.accept(store, c));
allCheckpoints.forEach(c -> chainProcessor.accept(store, c));
}
use of tech.pegasys.teku.spec.datastructures.state.CheckpointState in project teku by ConsenSys.
the class StoreTest method retrieveFinalizedCheckpointAndState.
@Test
public void retrieveFinalizedCheckpointAndState() {
final UpdatableStore store = createGenesisStore();
final SignedBlockAndState finalizedBlockAndState = chainBuilder.generateBlockAtSlot(spec.slotsPerEpoch(UInt64.ZERO) - 1);
final Checkpoint finalizedCheckpoint = new Checkpoint(UInt64.ONE, finalizedBlockAndState.getRoot());
final StoreTransaction tx = store.startTransaction(new StubStorageUpdateChannel());
tx.putBlockAndState(finalizedBlockAndState);
tx.setFinalizedCheckpoint(finalizedCheckpoint);
assertThat(tx.commit()).isCompleted();
final SafeFuture<CheckpointState> result = store.retrieveFinalizedCheckpointAndState();
assertThat(result).isCompleted();
assertThat(result.join().getCheckpoint()).isEqualTo(finalizedCheckpoint);
assertThat(result.join().getRoot()).isEqualTo(finalizedBlockAndState.getRoot());
assertThat(result.join().getState()).isNotEqualTo(finalizedBlockAndState.getState());
assertThat(result.join().getState().getSlot()).isEqualTo(finalizedBlockAndState.getSlot().plus(1));
}
use of tech.pegasys.teku.spec.datastructures.state.CheckpointState in project teku by ConsenSys.
the class StoreTest method testApplyChangesWhenTransactionCommits.
public void testApplyChangesWhenTransactionCommits(final boolean withInterleavedTransaction) {
final UpdatableStore store = createGenesisStore();
final UInt64 epoch3 = UInt64.valueOf(4);
final UInt64 epoch3Slot = spec.computeStartSlotAtEpoch(epoch3);
chainBuilder.generateBlocksUpToSlot(epoch3Slot);
final Checkpoint genesisCheckpoint = store.getFinalizedCheckpoint();
final UInt64 initialTime = store.getTime();
final UInt64 genesisTime = store.getGenesisTime();
final Checkpoint checkpoint1 = chainBuilder.getCurrentCheckpointForEpoch(UInt64.valueOf(1));
final Checkpoint checkpoint2 = chainBuilder.getCurrentCheckpointForEpoch(UInt64.valueOf(2));
final Checkpoint checkpoint3 = chainBuilder.getCurrentCheckpointForEpoch(UInt64.valueOf(3));
// Start transaction
final StubStorageUpdateChannelWithDelays updateChannel = new StubStorageUpdateChannelWithDelays();
final StoreTransaction tx = store.startTransaction(updateChannel);
// Add blocks
chainBuilder.streamBlocksAndStates().forEach(tx::putBlockAndState);
// Update checkpoints
tx.setFinalizedCheckpoint(checkpoint1);
tx.setJustifiedCheckpoint(checkpoint2);
tx.setBestJustifiedCheckpoint(checkpoint3);
// Update time
tx.setTime(initialTime.plus(UInt64.ONE));
tx.setGenesisTime(genesisTime.plus(UInt64.ONE));
// Check that store is not yet updated
// Check blocks
chainBuilder.streamBlocksAndStates(1, chainBuilder.getLatestSlot().longValue()).forEach(b -> assertThat(store.containsBlock(b.getRoot())).isFalse());
// Check checkpoints
assertThat(store.getJustifiedCheckpoint()).isEqualTo(genesisCheckpoint);
assertThat(store.getBestJustifiedCheckpoint()).isEqualTo(genesisCheckpoint);
assertThat(store.getFinalizedCheckpoint()).isEqualTo(genesisCheckpoint);
// Check time
assertThat(store.getTime()).isEqualTo(initialTime);
assertThat(store.getGenesisTime()).isEqualTo(genesisTime);
// Check that transaction is updated
chainBuilder.streamBlocksAndStates(1, chainBuilder.getLatestSlot().longValue()).forEach(b -> assertThat(tx.retrieveBlockAndState(b.getRoot())).isCompletedWithValue(Optional.of(b)));
// Check checkpoints
assertThat(tx.getFinalizedCheckpoint()).isEqualTo(checkpoint1);
assertThat(tx.getJustifiedCheckpoint()).isEqualTo(checkpoint2);
assertThat(tx.getBestJustifiedCheckpoint()).isEqualTo(checkpoint3);
// Check time
assertThat(tx.getTime()).isEqualTo(initialTime.plus(UInt64.ONE));
assertThat(tx.getGenesisTime()).isEqualTo(genesisTime.plus(UInt64.ONE));
// Commit transaction
final SafeFuture<Void> txResult = tx.commit();
final SafeFuture<Void> txResult2;
if (withInterleavedTransaction) {
UInt64 time = store.getTime().plus(UInt64.ONE);
StoreTransaction tx2 = store.startTransaction(updateChannel);
tx2.setTime(time);
txResult2 = tx2.commit();
} else {
txResult2 = SafeFuture.COMPLETE;
}
// Complete transactions
assertThat(updateChannel.getAsyncRunner().countDelayedActions()).isLessThanOrEqualTo(2);
updateChannel.getAsyncRunner().executeUntilDone();
assertThat(txResult).isCompleted();
assertThat(txResult2).isCompleted();
// Check store is updated
chainBuilder.streamBlocksAndStates(checkpoint3.getEpochStartSlot(spec), chainBuilder.getLatestSlot()).forEach(b -> assertThat(store.retrieveBlockAndState(b.getRoot())).isCompletedWithValue(Optional.of(b)));
// Check checkpoints
assertThat(store.getFinalizedCheckpoint()).isEqualTo(checkpoint1);
assertThat(store.getJustifiedCheckpoint()).isEqualTo(checkpoint2);
assertThat(store.getBestJustifiedCheckpoint()).isEqualTo(checkpoint3);
// Extra checks for finalized checkpoint
final SafeFuture<CheckpointState> finalizedCheckpointState = store.retrieveFinalizedCheckpointAndState();
assertThat(finalizedCheckpointState).isCompleted();
assertThat(finalizedCheckpointState.join().getCheckpoint()).isEqualTo(checkpoint1);
// Check time
assertThat(store.getTime()).isEqualTo(initialTime.plus(UInt64.ONE));
assertThat(store.getGenesisTime()).isEqualTo(genesisTime.plus(UInt64.ONE));
// Check store was pruned as expected
final List<Bytes32> expectedBlockRoots = chainBuilder.streamBlocksAndStates(checkpoint1.getEpochStartSlot(spec)).map(SignedBlockAndState::getRoot).collect(Collectors.toList());
assertThat(store.getOrderedBlockRoots()).containsExactlyElementsOf(expectedBlockRoots);
}
Aggregations