use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater in project teku by ConsenSys.
the class KvStoreDatabase method updateFinalizedDataPruneMode.
private void updateFinalizedDataPruneMode(Map<Bytes32, Bytes32> finalizedChildToParentMap, final Map<Bytes32, SignedBeaconBlock> finalizedBlocks) {
final Optional<Bytes32> initialBlockRoot = hotDao.getAnchor().map(Checkpoint::getRoot);
final BlockProvider blockProvider = BlockProvider.withKnownBlocks(roots -> SafeFuture.completedFuture(getHotBlocks(roots)), finalizedBlocks);
final List<Bytes32> finalizedRoots = new ArrayList<>(finalizedChildToParentMap.keySet());
int i = 0;
while (i < finalizedRoots.size()) {
try (final FinalizedUpdater updater = finalizedDao.finalizedUpdater()) {
final int start = i;
while (i < finalizedRoots.size() && (i - start) < TX_BATCH_SIZE) {
final Bytes32 root = finalizedRoots.get(i);
final Optional<SignedBeaconBlock> maybeBlock = blockProvider.getBlock(root).join();
maybeBlock.ifPresent(updater::addFinalizedBlock);
// If block is missing and doesn't match the initial anchor, throw
if (maybeBlock.isEmpty() && initialBlockRoot.filter(r -> r.equals(root)).isEmpty()) {
throw new IllegalStateException("Missing finalized block");
}
i++;
}
updater.commit();
if (i >= TX_BATCH_SIZE) {
STATUS_LOG.recordedFinalizedBlocks(i, finalizedRoots.size());
}
}
}
}
use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater 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();
}
}
use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater in project teku by ConsenSys.
the class KvStoreDatabase method storeNonCanonicalBlocks.
private void storeNonCanonicalBlocks(final Set<SignedBeaconBlock> nonCanonicalBlocks) {
int i = 0;
final Iterator<SignedBeaconBlock> it = nonCanonicalBlocks.iterator();
while (it.hasNext()) {
final Map<UInt64, Set<Bytes32>> nonCanonicalRootsBySlotBuffer = new HashMap<>();
final int start = i;
try (final FinalizedUpdater updater = finalizedDao.finalizedUpdater()) {
while (it.hasNext() && (i - start) < TX_BATCH_SIZE) {
final SignedBeaconBlock block = it.next();
LOG.debug("Non canonical block {}:{}", block.getRoot().toHexString(), block.getSlot());
updater.addNonCanonicalBlock(block);
nonCanonicalRootsBySlotBuffer.computeIfAbsent(block.getSlot(), __ -> new HashSet<>()).add(block.getRoot());
i++;
}
nonCanonicalRootsBySlotBuffer.forEach(updater::addNonCanonicalRootAtSlot);
updater.commit();
}
}
}
use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater in project teku by ConsenSys.
the class KvStoreDatabase method storeFinalizedBlocks.
@Override
public void storeFinalizedBlocks(final Collection<SignedBeaconBlock> blocks) {
if (blocks.isEmpty()) {
return;
}
// Sort blocks and verify that they are contiguous with the oldestBlock
final List<SignedBeaconBlock> sorted = blocks.stream().sorted(Comparator.comparing(SignedBeaconBlock::getSlot).reversed()).collect(Collectors.toList());
// The new block should be just prior to our earliest block if available, and otherwise should
// match our latest finalized block
Bytes32 expectedRoot = getEarliestAvailableBlock().map(SignedBeaconBlock::getParentRoot).orElseGet(() -> this.getLatestFinalizedBlockSummary().getRoot());
for (SignedBeaconBlock block : sorted) {
if (!block.getRoot().equals(expectedRoot)) {
throw new IllegalArgumentException("Blocks must be contiguous with the earliest known block.");
}
expectedRoot = block.getParentRoot();
}
try (final FinalizedUpdater updater = finalizedDao.finalizedUpdater()) {
blocks.forEach(updater::addFinalizedBlock);
updater.commit();
}
}
use of tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater 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();
}
}
Aggregations