use of tech.pegasys.teku.storage.server.state.StateRootRecorder in project teku by ConsenSys.
the class KvStoreDatabase method updateFinalizedDataArchiveMode.
private void updateFinalizedDataArchiveMode(Map<Bytes32, Bytes32> finalizedChildToParentMap, final Map<Bytes32, SignedBeaconBlock> finalizedBlocks, final Map<Bytes32, BeaconState> finalizedStates) {
final BlockProvider blockProvider = BlockProvider.withKnownBlocks(roots -> SafeFuture.completedFuture(getHotBlocks(roots)), finalizedBlocks);
final Optional<Checkpoint> initialCheckpoint = hotDao.getAnchor();
final Optional<Bytes32> initialBlockRoot = initialCheckpoint.map(Checkpoint::getRoot);
// Get previously finalized block to build on top of
final BeaconBlockSummary baseBlock = getLatestFinalizedBlockOrSummary();
final List<Bytes32> finalizedRoots = HashTree.builder().rootHash(baseBlock.getRoot()).childAndParentRoots(finalizedChildToParentMap).build().preOrderStream().collect(Collectors.toList());
int i = 0;
UInt64 lastSlot = baseBlock.getSlot();
while (i < finalizedRoots.size()) {
final int start = i;
try (final FinalizedUpdater updater = finalizedDao.finalizedUpdater()) {
final StateRootRecorder recorder = new StateRootRecorder(lastSlot, updater::addFinalizedStateRoot, spec);
while (i < finalizedRoots.size() && (i - start) < TX_BATCH_SIZE) {
final Bytes32 blockRoot = finalizedRoots.get(i);
final Optional<SignedBeaconBlock> maybeBlock = blockProvider.getBlock(blockRoot).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(blockRoot)).isEmpty()) {
throw new IllegalStateException("Missing finalized block");
}
Optional.ofNullable(finalizedStates.get(blockRoot)).or(() -> getHotState(blockRoot)).ifPresent(state -> {
updater.addFinalizedState(blockRoot, state);
recorder.acceptNextState(state);
});
lastSlot = maybeBlock.map(SignedBeaconBlock::getSlot).orElseGet(() -> initialCheckpoint.orElseThrow().getEpochStartSlot(spec));
i++;
}
updater.commit();
if (i >= TX_BATCH_SIZE) {
STATUS_LOG.recordedFinalizedBlocks(i, finalizedRoots.size());
}
}
}
}
Aggregations