Search in sources :

Example 1 with BlockAndCheckpointEpochs

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));
}
Also used : ChainBuilder(tech.pegasys.teku.core.ChainBuilder) BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) Test(org.junit.jupiter.api.Test)

Example 2 with BlockAndCheckpointEpochs

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());
}
Also used : BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) Test(org.junit.jupiter.api.Test)

Example 3 with BlockAndCheckpointEpochs

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();
    }
}
Also used : BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) FinalizedUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater) BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) CheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs) HotUpdater(tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 4 with BlockAndCheckpointEpochs

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();
}
Also used : BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 5 with BlockAndCheckpointEpochs

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);
}
Also used : ChainBuilder(tech.pegasys.teku.core.ChainBuilder) BlockAndCheckpointEpochs(tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

BlockAndCheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.BlockAndCheckpointEpochs)5 Test (org.junit.jupiter.api.Test)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 ChainBuilder (tech.pegasys.teku.core.ChainBuilder)2 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)2 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)2 HashSet (java.util.HashSet)1 CheckpointEpochs (tech.pegasys.teku.spec.datastructures.blocks.CheckpointEpochs)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 FinalizedUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreFinalizedDao.FinalizedUpdater)1 HotUpdater (tech.pegasys.teku.storage.server.kvstore.dataaccess.KvStoreHotDao.HotUpdater)1