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());
}
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations