Search in sources :

Example 1 with HashTree

use of tech.pegasys.teku.spec.datastructures.hashtree.HashTree 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 2 with HashTree

use of tech.pegasys.teku.spec.datastructures.hashtree.HashTree 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 3 with HashTree

use of tech.pegasys.teku.spec.datastructures.hashtree.HashTree 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 4 with HashTree

use of tech.pegasys.teku.spec.datastructures.hashtree.HashTree in project teku by ConsenSys.

the class StateGenerationTaskTest method createTask.

private StateGenerationTask createTask(final int startSlot, final int endSlot, final Optional<SlotAndBlockRoot> epochBoundaryRoot) {
    final SignedBlockAndState startBlockAndState = chainBuilder.getBlockAndStateAtSlot(startSlot);
    final SignedBeaconBlock endBlock = chainBuilder.getBlockAtSlot(endSlot);
    final HashTree.Builder treeBuilder = HashTree.builder().block(startBlockAndState.getBlock()).rootHash(startBlockAndState.getRoot());
    SignedBeaconBlock block = endBlock;
    while (block.getSlot().isGreaterThan(startBlockAndState.getSlot())) {
        treeBuilder.block(block);
        block = chainBuilder.getBlock(block.getParentRoot()).orElseThrow();
    }
    final HashTree tree = treeBuilder.build();
    return new StateGenerationTask(spec, endBlock.getRoot(), tree, blockProvider, new StateRegenerationBaseSelector(spec, epochBoundaryRoot, () -> Optional.of(new BlockRootAndState(startBlockAndState.getRoot(), startBlockAndState.getState())), getStateAndBlockProvider(), Optional.empty(), REPLAY_TOLERANCE_TO_AVOID_LOADING_IN_EPOCHS));
}
Also used : HashTree(tech.pegasys.teku.spec.datastructures.hashtree.HashTree) BlockRootAndState(tech.pegasys.teku.spec.datastructures.state.BlockRootAndState) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)

Aggregations

SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)4 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)4 HashTree (tech.pegasys.teku.spec.datastructures.hashtree.HashTree)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 BlockProvider (tech.pegasys.teku.dataproviders.lookup.BlockProvider)3 Test (org.junit.jupiter.api.Test)2 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)2 BlockRootAndState (tech.pegasys.teku.spec.datastructures.state.BlockRootAndState)1