Search in sources :

Example 1 with SlotAndExecutionPayload

use of tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload in project teku by ConsenSys.

the class KvStoreDatabase method doUpdate.

private UpdateResult doUpdate(final StorageUpdate update) {
    LOG.trace("Applying finalized updates");
    // Update finalized blocks and states
    final Optional<SlotAndExecutionPayload> finalizedOptimisticExecutionPayload = updateFinalizedData(update.getFinalizedChildToParentMap(), update.getFinalizedBlocks(), update.getFinalizedStates(), update.getDeletedHotBlocks(), update.isFinalizedOptimisticTransitionBlockRootSet(), update.getOptimisticTransitionBlockRoot());
    LOG.trace("Applying hot updates");
    try (final HotUpdater updater = hotDao.hotUpdater()) {
        // Store new hot data
        update.getGenesisTime().ifPresent(updater::setGenesisTime);
        update.getFinalizedCheckpoint().ifPresent(checkpoint -> {
            updater.setFinalizedCheckpoint(checkpoint);
            final int slotsPerEpoch = spec.slotsPerEpoch(checkpoint.getEpoch());
            final UInt64 finalizedSlot = checkpoint.getEpochStartSlot(spec).plus(slotsPerEpoch);
            updater.pruneHotStateRoots(hotDao.getStateRootsBeforeSlot(finalizedSlot));
            updater.deleteHotState(checkpoint.getRoot());
        });
        update.getJustifiedCheckpoint().ifPresent(updater::setJustifiedCheckpoint);
        update.getBestJustifiedCheckpoint().ifPresent(updater::setBestJustifiedCheckpoint);
        update.getLatestFinalizedState().ifPresent(updater::setLatestFinalizedState);
        updater.addHotBlocks(update.getHotBlocks());
        updater.addHotStates(update.getHotStates());
        if (update.getStateRoots().size() > 0) {
            updater.addHotStateRoots(update.getStateRoots());
        }
        // Delete finalized data from hot db
        update.getDeletedHotBlocks().forEach(updater::deleteHotBlock);
        LOG.trace("Committing hot db changes");
        updater.commit();
    }
    LOG.trace("Update complete");
    return new UpdateResult(finalizedOptimisticExecutionPayload);
}
Also used : SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) UpdateResult(tech.pegasys.teku.storage.api.UpdateResult)

Example 2 with SlotAndExecutionPayload

use of tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload in project teku by ConsenSys.

the class KvStoreDatabase method updateFinalizedOptimisticTransitionBlock.

private Optional<SlotAndExecutionPayload> updateFinalizedOptimisticTransitionBlock(final boolean isFinalizedOptimisticBlockRootSet, final Optional<Bytes32> finalizedOptimisticTransitionBlockRoot) {
    if (isFinalizedOptimisticBlockRootSet) {
        final Optional<SignedBeaconBlock> transitionBlock = finalizedOptimisticTransitionBlockRoot.flatMap(this::getHotBlock);
        try (final FinalizedUpdater updater = finalizedDao.finalizedUpdater()) {
            updater.setOptimisticTransitionBlockSlot(transitionBlock.map(SignedBeaconBlock::getSlot));
            updater.commit();
        }
        return transitionBlock.flatMap(SlotAndExecutionPayload::fromBlock);
    } else {
        return Optional.empty();
    }
}
Also used : SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) FinalizedUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)

Example 3 with SlotAndExecutionPayload

use of tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload in project teku by ConsenSys.

the class AbstractDatabaseTest method shouldPersistOptimisticTransitionExecutionPayload.

@Test
public void shouldPersistOptimisticTransitionExecutionPayload() {
    final SignedBlockAndState transitionBlock = generateChainWithFinalizableTransitionBlock();
    final List<SignedBlockAndState> newBlocks = chainBuilder.streamBlocksAndStates(genesisBlockAndState.getSlot().intValue()).collect(toList());
    // Save all blocks and states in a single transaction
    add(newBlocks);
    // Then finalize
    final Checkpoint finalizedCheckpoint = chainBuilder.getCurrentCheckpointForEpoch(chainBuilder.getLatestEpoch());
    final StoreTransaction tx = recentChainData.startStoreTransaction();
    tx.setFinalizedCheckpoint(finalizedCheckpoint);
    assertThat(tx.commit()).isCompleted();
    final Optional<SlotAndExecutionPayload> transitionPayload = SlotAndExecutionPayload.fromBlock(transitionBlock.getBlock());
    assertThat(transitionPayload).isPresent();
    assertThat(recentChainData.getStore().getFinalizedOptimisticTransitionPayload()).isEqualTo(transitionPayload);
    restartStorage();
    assertThat(recentChainData.getStore().getFinalizedOptimisticTransitionPayload()).isEqualTo(transitionPayload);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Test(org.junit.jupiter.api.Test)

Example 4 with SlotAndExecutionPayload

use of tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload in project teku by ConsenSys.

the class AbstractDatabaseTest method shouldRecordOptimisticTransitionExecutionPayloadWhenFinalized_multiTransaction.

@Test
public void shouldRecordOptimisticTransitionExecutionPayloadWhenFinalized_multiTransaction() {
    final SignedBlockAndState transitionBlock = generateChainWithFinalizableTransitionBlock();
    final List<SignedBlockAndState> newBlocks = chainBuilder.streamBlocksAndStates(genesisBlockAndState.getSlot().intValue()).collect(toList());
    // Save all blocks and states in separate transactions
    for (SignedBlockAndState newBlock : newBlocks) {
        add(List.of(newBlock));
    }
    assertThat(recentChainData.getStore().getFinalizedOptimisticTransitionPayload()).isEmpty();
    // Then finalize
    final Checkpoint finalizedCheckpoint = chainBuilder.getCurrentCheckpointForEpoch(chainBuilder.getLatestEpoch());
    final StoreTransaction tx = recentChainData.startStoreTransaction();
    tx.setFinalizedCheckpoint(finalizedCheckpoint);
    assertThat(tx.commit()).isCompleted();
    final Optional<SlotAndExecutionPayload> transitionPayload = SlotAndExecutionPayload.fromBlock(transitionBlock.getBlock());
    assertThat(transitionPayload).isPresent();
    assertThat(transitionPayload.get().getExecutionPayload().isDefault()).isFalse();
    assertThat(recentChainData.getStore().getFinalizedOptimisticTransitionPayload()).isEqualTo(transitionPayload);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Test(org.junit.jupiter.api.Test)

Example 5 with SlotAndExecutionPayload

use of tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload in project teku by ConsenSys.

the class AbstractDatabaseTest method shouldNotRemoveOptimisticFinalizedExceptionPayloadWhenFinalizedNextUpdated.

@Test
public void shouldNotRemoveOptimisticFinalizedExceptionPayloadWhenFinalizedNextUpdated() {
    final SignedBlockAndState transitionBlock = generateChainWithFinalizableTransitionBlock();
    final List<SignedBlockAndState> newBlocks = chainBuilder.streamBlocksAndStates(genesisBlockAndState.getSlot().intValue()).collect(toList());
    // Save all blocks and states in a single transaction
    add(newBlocks);
    // Then finalize
    final Checkpoint finalizedCheckpoint = chainBuilder.getCurrentCheckpointForEpoch(chainBuilder.getLatestEpoch());
    final StoreTransaction tx = recentChainData.startStoreTransaction();
    tx.setFinalizedCheckpoint(finalizedCheckpoint);
    assertThat(tx.commit()).isCompleted();
    // Finalize the next epoch
    final List<SignedBlockAndState> laterBlocks = chainBuilder.generateBlocksUpToSlot(chainBuilder.getLatestSlot().plus(spec.getSlotsPerEpoch(chainBuilder.getLatestSlot())));
    add(laterBlocks);
    final Checkpoint finalizedCheckpoint2 = chainBuilder.getCurrentCheckpointForEpoch(chainBuilder.getLatestEpoch());
    final StoreTransaction tx2 = recentChainData.startStoreTransaction();
    tx2.setFinalizedCheckpoint(finalizedCheckpoint2);
    assertThat(tx2.commit()).isCompleted();
    final Optional<SlotAndExecutionPayload> transitionPayload = SlotAndExecutionPayload.fromBlock(transitionBlock.getBlock());
    assertThat(transitionPayload).isPresent();
    assertThat(transitionPayload.get().getExecutionPayload().isDefault()).isFalse();
    assertThat(recentChainData.getStore().getFinalizedOptimisticTransitionPayload()).isEqualTo(transitionPayload);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Test(org.junit.jupiter.api.Test)

Aggregations

SlotAndExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload)8 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)7 Test (org.junit.jupiter.api.Test)5 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)5 StoreTransaction (tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction)5 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)2 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HashMap (java.util.HashMap)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 StoredBlockMetadata (tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata)1 BlockAndCheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs)1 CheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs)1 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)1 ExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload)1 VoteTracker (tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 UpdateResult (tech.pegasys.teku.storage.api.UpdateResult)1 FinalizedUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater)1