Search in sources :

Example 1 with StubStorageUpdateChannelWithDelays

use of tech.pegasys.teku.storage.api.StubStorageUpdateChannelWithDelays 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);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) CheckpointState(tech.pegasys.teku.spec.datastructures.state.CheckpointState) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) StubStorageUpdateChannelWithDelays(tech.pegasys.teku.storage.api.StubStorageUpdateChannelWithDelays)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 CheckpointState (tech.pegasys.teku.spec.datastructures.state.CheckpointState)1 StubStorageUpdateChannelWithDelays (tech.pegasys.teku.storage.api.StubStorageUpdateChannelWithDelays)1 StoreTransaction (tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction)1