Search in sources :

Example 1 with StoredBlockMetadata

use of tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata in project teku by ConsenSys.

the class AbstractStoreTest method createGenesisStore.

protected UpdatableStore createGenesisStore(final StoreConfig pruningOptions) {
    final SignedBlockAndState genesis = chainBuilder.generateGenesis();
    final Checkpoint genesisCheckpoint = chainBuilder.getCurrentCheckpointForEpoch(0);
    return StoreBuilder.create().asyncRunner(SYNC_RUNNER).metricsSystem(new StubMetricsSystem()).specProvider(spec).blockProvider(blockProviderFromChainBuilder()).stateProvider(StateAndBlockSummaryProvider.NOOP).anchor(Optional.empty()).genesisTime(genesis.getState().getGenesis_time()).time(genesis.getState().getGenesis_time()).latestFinalized(AnchorPoint.create(spec, genesisCheckpoint, genesis)).justifiedCheckpoint(genesisCheckpoint).bestJustifiedCheckpoint(genesisCheckpoint).blockInformation(Map.of(genesis.getRoot(), new StoredBlockMetadata(genesis.getSlot(), genesis.getRoot(), genesis.getParentRoot(), genesis.getStateRoot(), genesis.getExecutionBlockHash(), Optional.of(new CheckpointEpochs(UInt64.ZERO, UInt64.ZERO))))).storeConfig(pruningOptions).votes(emptyMap()).build();
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) StubMetricsSystem(tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem) CheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs) StoredBlockMetadata(tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)

Example 2 with StoredBlockMetadata

use of tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata in project teku by ConsenSys.

the class StoreBuilder method forkChoiceStoreBuilder.

public static StoreBuilder forkChoiceStoreBuilder(final AsyncRunner asyncRunner, final MetricsSystem metricsSystem, final Spec spec, final BlockProvider blockProvider, final StateAndBlockSummaryProvider stateAndBlockProvider, final AnchorPoint anchor, final UInt64 currentTime) {
    final UInt64 genesisTime = anchor.getState().getGenesis_time();
    final UInt64 slot = anchor.getState().getSlot();
    final UInt64 time = genesisTime.plus(slot.times(spec.getSecondsPerSlot(slot))).max(currentTime);
    Map<Bytes32, StoredBlockMetadata> blockInfo = new HashMap<>();
    blockInfo.put(anchor.getRoot(), new StoredBlockMetadata(slot, anchor.getRoot(), anchor.getParentRoot(), anchor.getState().hashTreeRoot(), anchor.getExecutionBlockHash(), Optional.of(new CheckpointEpochs(anchor.getCheckpoint().getEpoch(), anchor.getCheckpoint().getEpoch()))));
    return create().asyncRunner(asyncRunner).metricsSystem(metricsSystem).specProvider(spec).blockProvider(blockProvider).stateProvider(stateAndBlockProvider).anchor(anchor.getCheckpoint()).time(time).genesisTime(genesisTime).latestFinalized(anchor).justifiedCheckpoint(anchor.getCheckpoint()).bestJustifiedCheckpoint(anchor.getCheckpoint()).blockInformation(blockInfo).votes(new HashMap<>());
}
Also used : HashMap(java.util.HashMap) CheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs) StoredBlockMetadata(tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 3 with StoredBlockMetadata

use of tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata in project teku by ConsenSys.

the class Store method buildProtoArray.

private static ProtoArray buildProtoArray(final Spec spec, final Map<Bytes32, StoredBlockMetadata> blockInfoByRoot, final Optional<Checkpoint> initialCheckpoint, final Checkpoint justifiedCheckpoint, final AnchorPoint finalizedAnchor) {
    final List<StoredBlockMetadata> blocks = new ArrayList<>(blockInfoByRoot.values());
    blocks.sort(Comparator.comparing(StoredBlockMetadata::getBlockSlot));
    final ProtoArray protoArray = ProtoArray.builder().initialCheckpoint(initialCheckpoint).justifiedCheckpoint(justifiedCheckpoint).finalizedCheckpoint(finalizedAnchor.getCheckpoint()).build();
    for (StoredBlockMetadata block : blocks) {
        if (block.getCheckpointEpochs().isEmpty()) {
            throw new IllegalStateException("Incompatible database version detected. The data in this database is too old to be read by Teku. A re-sync will be required.");
        }
        protoArray.onBlock(block.getBlockSlot(), block.getBlockRoot(), block.getParentRoot(), block.getStateRoot(), block.getCheckpointEpochs().get().getJustifiedEpoch(), block.getCheckpointEpochs().get().getFinalizedEpoch(), block.getExecutionBlockHash().orElse(Bytes32.ZERO), spec.isBlockProcessorOptimistic(block.getBlockSlot()));
    }
    return protoArray;
}
Also used : ProtoArray(tech.pegasys.teku.ethereum.forkchoice.ProtoArray) ArrayList(java.util.ArrayList) StoredBlockMetadata(tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata)

Example 4 with StoredBlockMetadata

use of tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata in project teku by ConsenSys.

the class KvStoreDatabase method createMemoryStore.

@VisibleForTesting
Optional<StoreBuilder> createMemoryStore(final Supplier<Long> timeSupplier) {
    Optional<UInt64> maybeGenesisTime = hotDao.getGenesisTime();
    if (maybeGenesisTime.isEmpty()) {
        // If genesis time hasn't been set, genesis hasn't happened and we have no data
        return Optional.empty();
    }
    final UInt64 genesisTime = maybeGenesisTime.get();
    final Optional<Checkpoint> maybeAnchor = hotDao.getAnchor();
    final Checkpoint justifiedCheckpoint = hotDao.getJustifiedCheckpoint().orElseThrow();
    final Checkpoint finalizedCheckpoint = hotDao.getFinalizedCheckpoint().orElseThrow();
    final Checkpoint bestJustifiedCheckpoint = hotDao.getBestJustifiedCheckpoint().orElseThrow();
    final BeaconState finalizedState = hotDao.getLatestFinalizedState().orElseThrow();
    final Map<UInt64, VoteTracker> votes = hotDao.getVotes();
    // Build map with block information
    final Map<Bytes32, StoredBlockMetadata> blockInformation = new HashMap<>();
    try (final Stream<SignedBeaconBlock> hotBlocks = hotDao.streamHotBlocks()) {
        hotBlocks.forEach(b -> {
            final Optional<CheckpointEpochs> checkpointEpochs = hotDao.getHotBlockCheckpointEpochs(b.getRoot());
            blockInformation.put(b.getRoot(), new StoredBlockMetadata(b.getSlot(), b.getRoot(), b.getParentRoot(), b.getStateRoot(), b.getMessage().getBody().getOptionalExecutionPayload().map(ExecutionPayload::getBlockHash), checkpointEpochs));
        });
    }
    // If anchor block is missing, try to pull block info from the anchor state
    final boolean shouldIncludeAnchorBlock = maybeAnchor.isPresent() && finalizedCheckpoint.getEpochStartSlot(spec).equals(maybeAnchor.get().getEpochStartSlot(spec));
    if (shouldIncludeAnchorBlock && !blockInformation.containsKey(maybeAnchor.get().getRoot())) {
        final Checkpoint anchor = maybeAnchor.orElseThrow();
        final StateAndBlockSummary latestFinalized = StateAndBlockSummary.create(finalizedState);
        if (!latestFinalized.getRoot().equals(anchor.getRoot())) {
            throw new IllegalStateException("Anchor state (" + anchor + ") is unavailable");
        }
        blockInformation.put(anchor.getRoot(), StoredBlockMetadata.fromBlockAndState(latestFinalized));
    }
    final Optional<SignedBeaconBlock> finalizedBlock = hotDao.getHotBlock(finalizedCheckpoint.getRoot());
    final AnchorPoint latestFinalized = AnchorPoint.create(spec, finalizedCheckpoint, finalizedState, finalizedBlock);
    final Optional<SlotAndExecutionPayload> finalizedOptimisticTransitionPayload = finalizedDao.getOptimisticTransitionBlockSlot().flatMap(finalizedDao::getFinalizedBlockAtSlot).flatMap(SlotAndExecutionPayload::fromBlock);
    // Make sure time is set to a reasonable value in the case where we start up before genesis when
    // the clock time would be prior to genesis
    final long clockTime = timeSupplier.get();
    final UInt64 slotTime = spec.getSlotStartTime(finalizedState.getSlot(), genesisTime);
    final UInt64 time = slotTime.max(clockTime);
    return Optional.of(StoreBuilder.create().metricsSystem(metricsSystem).specProvider(spec).time(time).anchor(maybeAnchor).genesisTime(genesisTime).latestFinalized(latestFinalized).finalizedOptimisticTransitionPayload(finalizedOptimisticTransitionPayload).justifiedCheckpoint(justifiedCheckpoint).bestJustifiedCheckpoint(bestJustifiedCheckpoint).blockInformation(blockInformation).votes(votes));
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) HashMap(java.util.HashMap) StoredBlockMetadata(tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) VoteTracker(tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) CheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SlotAndExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload) ExecutionPayload(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

StoredBlockMetadata (tech.pegasys.teku.ethereum.forkchoice.StoredBlockMetadata)4 CheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs)3 HashMap (java.util.HashMap)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 ProtoArray (tech.pegasys.teku.ethereum.forkchoice.ProtoArray)1 StubMetricsSystem (tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem)1 BlockAndCheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs)1 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)1 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)1 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)1 ExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload)1 SlotAndExecutionPayload (tech.pegasys.teku.spec.datastructures.execution.SlotAndExecutionPayload)1 VoteTracker (tech.pegasys.teku.spec.datastructures.forkchoice.VoteTracker)1 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1