use of tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs in project teku by ConsenSys.
the class AbstractBlockMetadataStoreTest method processHashesInChain_shouldWalkUpSpecifiedChain.
@Test
void processHashesInChain_shouldWalkUpSpecifiedChain() {
// First chain has all blocks up to 10
// Fork chain has 0-5, skips 6 and then has 7-10
chainBuilder.generateBlocksUpToSlot(5);
final ChainBuilder forkBuilder = chainBuilder.fork();
chainBuilder.generateBlocksUpToSlot(10);
forkBuilder.generateBlockAtSlot(7);
forkBuilder.generateBlocksUpToSlot(10);
final BlockMetadataStore store = createBlockMetadataStore(chainBuilder);
store.applyUpdate(forkBuilder.streamBlocksAndStates().map(BlockAndCheckpointEpochs::fromBlockAndState).collect(toList()), Collections.emptySet(), genesisCheckpoint);
verifyHashesInChain(store, forkBuilder, forkBuilder.getLatestBlockAndState().getRoot(), forkBuilder.streamBlocksAndStates());
verifyHashesInChain(store, chainBuilder, chainBuilder.getLatestBlockAndState().getRoot(), chainBuilder.streamBlocksAndStates());
// And check we can start from part way along the chain
verifyHashesInChain(store, chainBuilder, chainBuilder.getBlockAtSlot(6).getRoot(), chainBuilder.streamBlocksAndStates(0, 6));
}
use of tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs in project teku by ConsenSys.
the class AbstractBlockMetadataStoreTest method contains_shouldContainAddedBlocks.
@Test
void contains_shouldContainAddedBlocks() {
final BlockMetadataStore store = createBlockMetadataStore(chainBuilder);
final BlockAndCheckpointEpochs block1 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(1));
final BlockAndCheckpointEpochs block2 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(2));
final BlockAndCheckpointEpochs block3 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(3));
store.applyUpdate(List.of(block1, block2, block3), Collections.emptySet(), genesisCheckpoint);
chainBuilder.streamBlocksAndStates().forEach(block -> assertThat(store.contains(block.getRoot())).isTrue());
}
use of tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs in project teku by ConsenSys.
the class KvStoreDatabase method storeInitialAnchor.
@Override
public void storeInitialAnchor(final AnchorPoint anchor) {
try (final HotUpdater hotUpdater = hotDao.hotUpdater();
final FinalizedUpdater finalizedUpdater = finalizedDao.finalizedUpdater()) {
// We should only have a single block / state / checkpoint at anchorpoint initialization
final Checkpoint anchorCheckpoint = anchor.getCheckpoint();
final Bytes32 anchorRoot = anchorCheckpoint.getRoot();
final BeaconState anchorState = anchor.getState();
final Optional<SignedBeaconBlock> anchorBlock = anchor.getSignedBeaconBlock();
hotUpdater.setAnchor(anchor.getCheckpoint());
hotUpdater.setGenesisTime(anchorState.getGenesis_time());
hotUpdater.setJustifiedCheckpoint(anchorCheckpoint);
hotUpdater.setBestJustifiedCheckpoint(anchorCheckpoint);
hotUpdater.setFinalizedCheckpoint(anchorCheckpoint);
hotUpdater.setLatestFinalizedState(anchorState);
// We need to store the anchor block in both hot and cold storage so that on restart
// we're guaranteed to have at least one block / state to load into RecentChainData.
anchorBlock.ifPresent(block -> {
// Save to hot storage
hotUpdater.addHotBlock(new BlockAndCheckpointEpochs(block, new CheckpointEpochs(anchorState.getCurrent_justified_checkpoint().getEpoch(), anchorState.getFinalized_checkpoint().getEpoch())));
// Save to cold storage
finalizedUpdater.addFinalizedBlock(block);
});
putFinalizedState(finalizedUpdater, anchorRoot, anchorState);
finalizedUpdater.commit();
hotUpdater.commit();
}
}
use of tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs in project teku by ConsenSys.
the class AbstractBlockMetadataStoreTest method contains_shouldNotContainRemovedBlocks.
@Test
void contains_shouldNotContainRemovedBlocks() {
final BlockMetadataStore store = createBlockMetadataStore(chainBuilder);
final BlockAndCheckpointEpochs block1 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(1));
final BlockAndCheckpointEpochs block2 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(2));
final BlockAndCheckpointEpochs block3 = BlockAndCheckpointEpochs.fromBlockAndState(chainBuilder.generateBlockAtSlot(3));
store.applyUpdate(List.of(block1, block2, block3), Set.of(genesis.getRoot(), block1.getRoot()), new Checkpoint(UInt64.ONE, block2.getRoot()));
assertThat(store.contains(genesis.getRoot())).isFalse();
assertThat(store.contains(block1.getRoot())).isFalse();
assertThat(store.contains(block2.getRoot())).isTrue();
assertThat(store.contains(block3.getRoot())).isTrue();
}
use of tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs in project teku by ConsenSys.
the class AbstractBlockMetadataStoreTest method processAllInOrder_shouldVisitParentBlocksBeforeChildBlocksInForks.
@Test
void processAllInOrder_shouldVisitParentBlocksBeforeChildBlocksInForks() {
// First chain has all blocks up to 10
// Fork chain has 0-5, skips 6 and then has 7-10
chainBuilder.generateBlocksUpToSlot(5);
final ChainBuilder forkBuilder = chainBuilder.fork();
chainBuilder.generateBlocksUpToSlot(10);
forkBuilder.generateBlockAtSlot(7);
forkBuilder.generateBlocksUpToSlot(10);
final BlockMetadataStore store = createBlockMetadataStore(chainBuilder);
store.applyUpdate(forkBuilder.streamBlocksAndStates().map(BlockAndCheckpointEpochs::fromBlockAndState).collect(toList()), Collections.emptySet(), genesisCheckpoint);
final Set<Bytes32> seenBlocks = new HashSet<>();
store.processAllInOrder((childRoot, slot, parentRoot) -> {
assertThat(seenBlocks).doesNotContain(childRoot);
if (!seenBlocks.isEmpty()) {
// First block won't have visited the parent
assertThat(seenBlocks).contains(parentRoot);
}
seenBlocks.add(childRoot);
final SignedBeaconBlock block = chainBuilder.getBlock(childRoot).or(() -> forkBuilder.getBlock(childRoot)).orElseThrow();
assertThat(childRoot).isEqualTo(block.getRoot());
assertThat(slot).isEqualTo(block.getSlot());
assertThat(parentRoot).isEqualTo(block.getParentRoot());
});
// Check every block was seen
final Set<Bytes32> expectedSeenRoots = Stream.concat(chainBuilder.streamBlocksAndStates(), forkBuilder.streamBlocksAndStates()).map(StateAndBlockSummary::getRoot).collect(toSet());
assertThat(seenBlocks).containsExactlyInAnyOrderElementsOf(expectedSeenRoots);
}
Aggregations