Search in sources :

Example 1 with BlockProvider

use of tech.pegasys.teku.dataproviders.lookup.BlockProvider 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());
            }
        }
    }
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) FinalizedUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater) ArrayList(java.util.ArrayList) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) 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)

Example 2 with BlockProvider

use of tech.pegasys.teku.dataproviders.lookup.BlockProvider in project teku by ConsenSys.

the class StateGeneratorTest method regenerateStateForBlock_regenerateBaseStateWhenBlockIsMissing.

@Test
public void regenerateStateForBlock_regenerateBaseStateWhenBlockIsMissing() {
    // Build a small chain
    final SignedBlockAndState genesis = chainBuilder.generateGenesis();
    chainBuilder.generateBlocksUpToSlot(5);
    final Map<Bytes32, SignedBeaconBlock> blockMap = chainBuilder.streamBlocksAndStates().map(SignedBlockAndState::getBlock).collect(Collectors.toMap(SignedBeaconBlock::getRoot, Function.identity()));
    final HashTree tree = HashTree.builder().rootHash(genesis.getRoot()).blocks(blockMap.values()).build();
    // Create block provider that is missing the base (genesis) block
    blockMap.remove(genesis.getRoot());
    final BlockProvider blockProvider = BlockProvider.fromMap(blockMap);
    final StateGenerator generator = StateGenerator.create(spec, tree, genesis, blockProvider);
    final SafeFuture<StateAndBlockSummary> result = generator.regenerateStateForBlock(genesis.getRoot());
    final StateAndBlockSummary expected = StateAndBlockSummary.create(genesis.getState());
    assertThat(result).isCompletedWithValue(expected);
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) HashTree(tech.pegasys.teku.spec.datastructures.hashtree.HashTree) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 3 with BlockProvider

use of tech.pegasys.teku.dataproviders.lookup.BlockProvider in project teku by ConsenSys.

the class StateGeneratorTest method regenerateStateForBlock_missingBaseBlock.

@Test
public void regenerateStateForBlock_missingBaseBlock() {
    // Build a small chain
    final SignedBlockAndState genesis = chainBuilder.generateGenesis();
    chainBuilder.generateBlocksUpToSlot(5);
    final Map<Bytes32, SignedBeaconBlock> blockMap = chainBuilder.streamBlocksAndStates().map(SignedBlockAndState::getBlock).collect(Collectors.toMap(SignedBeaconBlock::getRoot, Function.identity()));
    final SignedBlockAndState lastBlockAndState = chainBuilder.getLatestBlockAndState();
    final HashTree tree = HashTree.builder().rootHash(genesis.getRoot()).blocks(blockMap.values()).build();
    // Create block provider that is missing the base (genesis) block
    blockMap.remove(genesis.getRoot());
    final BlockProvider blockProvider = BlockProvider.fromMap(blockMap);
    final StateGenerator generator = StateGenerator.create(spec, tree, genesis, blockProvider);
    final SafeFuture<StateAndBlockSummary> result = generator.regenerateStateForBlock(lastBlockAndState.getRoot());
    assertThat(result).isCompletedWithValue(lastBlockAndState);
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) HashTree(tech.pegasys.teku.spec.datastructures.hashtree.HashTree) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 4 with BlockProvider

use of tech.pegasys.teku.dataproviders.lookup.BlockProvider in project teku by ConsenSys.

the class StateGeneratorTest method testGeneratorWithMissingBlock.

private void testGeneratorWithMissingBlock(BiConsumer<StateGenerator, SignedBeaconBlock> processor) {
    // Build a small chain
    final SignedBlockAndState genesis = chainBuilder.generateGenesis();
    chainBuilder.generateBlocksUpToSlot(5);
    final Map<Bytes32, SignedBeaconBlock> blockMap = chainBuilder.streamBlocksAndStates().map(SignedBlockAndState::getBlock).collect(Collectors.toMap(SignedBeaconBlock::getRoot, Function.identity()));
    final HashTree tree = HashTree.builder().rootHash(genesis.getRoot()).blocks(blockMap.values()).build();
    // Create block provider that is missing some blocks
    final SignedBeaconBlock missingBlock = chainBuilder.getBlockAtSlot(genesis.getSlot().plus(2));
    blockMap.remove(missingBlock.getRoot());
    final BlockProvider blockProvider = BlockProvider.fromMap(blockMap);
    final StateGenerator generator = StateGenerator.create(spec, tree, genesis, blockProvider);
    processor.accept(generator, missingBlock);
}
Also used : HashTree(tech.pegasys.teku.spec.datastructures.hashtree.HashTree) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 5 with BlockProvider

use of tech.pegasys.teku.dataproviders.lookup.BlockProvider in project teku by ConsenSys.

the class AbstractKvStoreDatabaseTest method createMemoryStore_priorToGenesisTime.

@Test
public void createMemoryStore_priorToGenesisTime() {
    database.storeInitialAnchor(genesisAnchor);
    final Optional<StoreBuilder> storeBuilder = ((KvStoreDatabase) database).createMemoryStore(() -> 0L);
    assertThat(storeBuilder).isNotEmpty();
    final UpdatableStore store = storeBuilder.get().asyncRunner(mock(AsyncRunner.class)).blockProvider(mock(BlockProvider.class)).stateProvider(mock(StateAndBlockSummaryProvider.class)).build();
    assertThat(store.getTime()).isEqualTo(genesisTime);
}
Also used : StoreBuilder(tech.pegasys.teku.storage.store.StoreBuilder) UpdatableStore(tech.pegasys.teku.storage.store.UpdatableStore) BlockProvider(tech.pegasys.teku.dataproviders.lookup.BlockProvider) AbstractStorageBackedDatabaseTest(tech.pegasys.teku.storage.server.AbstractStorageBackedDatabaseTest) Test(org.junit.jupiter.api.Test)

Aggregations

BlockProvider (tech.pegasys.teku.dataproviders.lookup.BlockProvider)6 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)5 Test (org.junit.jupiter.api.Test)3 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)3 HashTree (tech.pegasys.teku.spec.datastructures.hashtree.HashTree)3 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)2 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 FinalizedUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater)2 ArrayList (java.util.ArrayList)1 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)1 BeaconBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSummary)1 AbstractStorageBackedDatabaseTest (tech.pegasys.teku.storage.server.AbstractStorageBackedDatabaseTest)1 StateRootRecorder (tech.pegasys.teku.storage.server.state.StateRootRecorder)1 StoreBuilder (tech.pegasys.teku.storage.store.StoreBuilder)1 UpdatableStore (tech.pegasys.teku.storage.store.UpdatableStore)1