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