Search in sources :

Example 1 with StateRootRecorder

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());
            }
        }
    }
}
Also used : FinalizedUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) StateRootRecorder(tech.pegasys.teku.storage.server.state.StateRootRecorder) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)1 BlockProvider (tech.pegasys.teku.dataproviders.lookup.BlockProvider)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 BeaconBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary)1 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)1 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 FinalizedUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater)1 StateRootRecorder (tech.pegasys.teku.storage.server.state.StateRootRecorder)1