Search in sources :

Example 1 with HotUpdater

use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater in project teku by ConsenSys.

the class KvStoreDatabase method updateWeakSubjectivityState.

@Override
public void updateWeakSubjectivityState(WeakSubjectivityUpdate weakSubjectivityUpdate) {
    try (final HotUpdater updater = hotDao.hotUpdater()) {
        Optional<Checkpoint> checkpoint = weakSubjectivityUpdate.getWeakSubjectivityCheckpoint();
        checkpoint.ifPresentOrElse(updater::setWeakSubjectivityCheckpoint, updater::clearWeakSubjectivityCheckpoint);
        updater.commit();
    }
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater)

Example 2 with HotUpdater

use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater in project teku by ConsenSys.

the class KvStoreDatabase method storeInitialAnchor.

@Override
public void storeInitialAnchor(final AnchorPoint anchor) {
    try (final HotUpdater hotUpdater = hotDao.hotUpdater();
        final FinalizedUpdater finalizedUpdater = finalizedDao.finalizedUpdater()) {
        // We should only have a single block / state / checkpoint at anchorpoint initialization
        final Checkpoint anchorCheckpoint = anchor.getCheckpoint();
        final Bytes32 anchorRoot = anchorCheckpoint.getRoot();
        final BeaconState anchorState = anchor.getState();
        final Optional<SignedBeaconBlock> anchorBlock = anchor.getSignedBeaconBlock();
        hotUpdater.setAnchor(anchor.getCheckpoint());
        hotUpdater.setGenesisTime(anchorState.getGenesis_time());
        hotUpdater.setJustifiedCheckpoint(anchorCheckpoint);
        hotUpdater.setBestJustifiedCheckpoint(anchorCheckpoint);
        hotUpdater.setFinalizedCheckpoint(anchorCheckpoint);
        hotUpdater.setLatestFinalizedState(anchorState);
        // We need to store the anchor block in both hot and cold storage so that on restart
        // we're guaranteed to have at least one block / state to load into RecentChainData.
        anchorBlock.ifPresent(block -> {
            // Save to hot storage
            hotUpdater.addHotBlock(new BlockAndCheckpointEpochs(block, new CheckpointEpochs(anchorState.getCurrent_justified_checkpoint().getEpoch(), anchorState.getFinalized_checkpoint().getEpoch())));
            // Save to cold storage
            finalizedUpdater.addFinalizedBlock(block);
        });
        putFinalizedState(finalizedUpdater, anchorRoot, anchorState);
        finalizedUpdater.commit();
        hotUpdater.commit();
    }
}
Also used : BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) FinalizedUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater) BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) CheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs) HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 3 with HotUpdater

use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater 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 4 with HotUpdater

use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater in project teku by ConsenSys.

the class KvStoreDatabase method pruneHotStateRoots.

@Override
public void pruneHotStateRoots(final List<Bytes32> stateRoots) {
    try (final HotUpdater updater = hotDao.hotUpdater()) {
        updater.pruneHotStateRoots(stateRoots);
        updater.commit();
    }
}
Also used : HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater)

Example 5 with HotUpdater

use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater in project teku by ConsenSys.

the class KvStoreDatabase method addHotStateRoots.

@Override
public void addHotStateRoots(final Map<Bytes32, SlotAndBlockRoot> stateRootToSlotAndBlockRootMap) {
    try (final HotUpdater updater = hotDao.hotUpdater()) {
        updater.addHotStateRoots(stateRootToSlotAndBlockRootMap);
        updater.commit();
    }
}
Also used : HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater)

Aggregations

HotUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater)6 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 BlockAndCheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs)1 CheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs)1 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)1 SlotAndExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload)1 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)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 KvStoreHotDao (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao)1