Search in sources :

Example 1 with SignedBlockAndState

use of tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState in project teku by ConsenSys.

the class BatchSyncTest method shouldHandleBatchWithNoSyncSourceMarkedCompleteBecauseOfLaterBatch.

@Test
void shouldHandleBatchWithNoSyncSourceMarkedCompleteBecauseOfLaterBatch() {
    final SafeFuture<SyncResult> syncFuture = sync.syncToChain(targetChain);
    assertThat(syncFuture).isNotDone();
    final Batch batch0 = batches.get(0);
    final Batch batch1 = batches.get(1);
    final Batch batch2 = batches.get(2);
    final Batch batch3 = batches.get(3);
    final Batch batch4 = batches.get(4);
    // Found an old common ancestor so we already have blocks up to the start of batch 2
    final SignedBlockAndState bestBlock = storageSystem.chainUpdater().advanceChainUntil(batch4.getFirstSlot().longValue());
    storageSystem.chainUpdater().updateBestBlock(bestBlock);
    // We receive a block from in batch4 which is a child of an existing block
    // but it's not the common ancestor sync started from
    final SignedBeaconBlock batch4Block = chainBuilder.getBlockAtSlot(batch4.getFirstSlot());
    assertThat(recentChainData.containsBlock(batch4Block.getParentRoot())).isTrue();
    batches.receiveBlocks(batch4, batch4Block);
    // None of the batches should be complete
    assertThatBatch(batch0).isNotComplete();
    assertThatBatch(batch1).isNotComplete();
    assertThatBatch(batch2).isNotComplete();
    assertThatBatch(batch3).isNotComplete();
    assertThatBatch(batch4).isNotComplete();
}
Also used : BatchAssert.assertThatBatch(tech.pegasys.teku.beacon.sync.forward.multipeer.batches.BatchAssert.assertThatBatch) Batch(tech.pegasys.teku.beacon.sync.forward.multipeer.batches.Batch) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Test(org.junit.jupiter.api.Test)

Example 2 with SignedBlockAndState

use of tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState in project teku by ConsenSys.

the class BatchSyncTest method shouldDelaySwitchingToNewChainUntilCurrentImportCompletes.

@Test
void shouldDelaySwitchingToNewChainUntilCurrentImportCompletes() {
    assertThat(sync.syncToChain(targetChain)).isNotDone();
    final Batch batch0 = batches.get(0);
    final Batch batch1 = batches.get(1);
    batches.receiveBlocks(batch0, chainBuilder.generateBlockAtSlot(1).getBlock());
    batches.receiveBlocks(batch1, chainBuilder.generateBlockAtSlot(batch1.getFirstSlot()).getBlock());
    assertBatchImported(batch0);
    final Batch batch4 = batches.get(4);
    // Switch to a new chain
    targetChain = chainWith(dataStructureUtil.randomSlotAndBlockRoot(), syncSource);
    assertThat(sync.syncToChain(targetChain)).isNotDone();
    // And return blocks so the new chain doesn't match up.
    batches.receiveBlocks(batch4, chainBuilder.generateBlockAtSlot(batch4.getLastSlot()).getBlock());
    final Batch batch5 = batches.get(5);
    batches.receiveBlocks(batch5, dataStructureUtil.randomSignedBeaconBlock(batch5.getFirstSlot()));
    assertBatchNotActive(batch0);
    // All batches should have been dropped and none started until the import completes
    batches.forEach(this::assertBatchNotActive);
    batches.clearBatchList();
    final SignedBlockAndState finalizedBlock = storageSystem.chainUpdater().finalizeEpoch(1);
    batches.getImportResult(batch0).complete(IMPORT_FAILED);
    // Now we should start downloading from the latest finalized checkpoint
    assertThat(batches.get(0).getFirstSlot()).isEqualTo(finalizedBlock.getSlot());
}
Also used : BatchAssert.assertThatBatch(tech.pegasys.teku.beacon.sync.forward.multipeer.batches.BatchAssert.assertThatBatch) Batch(tech.pegasys.teku.beacon.sync.forward.multipeer.batches.Batch) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Test(org.junit.jupiter.api.Test)

Example 3 with SignedBlockAndState

use of tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState in project teku by ConsenSys.

the class ValidatorApiHandlerTest method createAttestationData_shouldFailWhenHeadIsOptimistic.

@Test
public void createAttestationData_shouldFailWhenHeadIsOptimistic() {
    final UInt64 slot = spec.computeStartSlotAtEpoch(EPOCH).plus(ONE);
    when(chainDataClient.getCurrentSlot()).thenReturn(slot);
    final BeaconState state = createStateWithActiveValidators(EPOCH_START_SLOT);
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(state.getSlot(), state);
    final SignedBlockAndState blockAndState = new SignedBlockAndState(block, state);
    final SafeFuture<Optional<SignedBlockAndState>> blockAndStateResult = completedFuture(Optional.of(blockAndState));
    when(chainDataClient.getSignedBlockAndStateInEffectAtSlot(slot)).thenReturn(blockAndStateResult);
    when(forkChoiceTrigger.prepareForAttestationProduction(slot)).thenReturn(SafeFuture.COMPLETE);
    when(chainDataClient.isOptimisticBlock(blockAndState.getRoot())).thenReturn(true);
    final int committeeIndex = 0;
    final SafeFuture<Optional<AttestationData>> result = validatorApiHandler.createAttestationData(slot, committeeIndex);
    assertThat(result).isCompletedExceptionally();
    assertThatThrownBy(result::get).hasRootCauseInstanceOf(NodeSyncingException.class);
}
Also used : Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 4 with SignedBlockAndState

use of tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState in project teku by ConsenSys.

the class ValidatorApiHandlerTest method createAttestationData_shouldUseCorrectSourceWhenEpochTransitionRequired.

@Test
public void createAttestationData_shouldUseCorrectSourceWhenEpochTransitionRequired() {
    final UInt64 slot = spec.computeStartSlotAtEpoch(EPOCH);
    when(chainDataClient.getCurrentSlot()).thenReturn(slot);
    // Slot is from before the current epoch, so we need to ensure we process the epoch transition
    final UInt64 blockSlot = slot.minus(1);
    final BeaconState wrongState = createStateWithActiveValidators(blockSlot);
    final BeaconState rightState = createStateWithActiveValidators(slot);
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(wrongState.getSlot(), wrongState);
    final SignedBlockAndState blockAndState = new SignedBlockAndState(block, wrongState);
    final SafeFuture<Optional<SignedBlockAndState>> blockAndStateResult = completedFuture(Optional.of(blockAndState));
    when(chainDataClient.getSignedBlockAndStateInEffectAtSlot(slot)).thenReturn(blockAndStateResult);
    when(chainDataClient.getCheckpointState(EPOCH, blockAndState)).thenReturn(SafeFuture.completedFuture(CheckpointState.create(spec, new Checkpoint(EPOCH, block.getRoot()), block, rightState)));
    when(forkChoiceTrigger.prepareForAttestationProduction(slot)).thenReturn(SafeFuture.COMPLETE);
    final int committeeIndex = 0;
    final SafeFuture<Optional<AttestationData>> result = validatorApiHandler.createAttestationData(slot, committeeIndex);
    assertThat(result).isCompleted();
    final Optional<AttestationData> maybeAttestation = result.join();
    assertThat(maybeAttestation).isPresent();
    final AttestationData attestationData = maybeAttestation.orElseThrow();
    assertThat(attestationData).isEqualTo(spec.getGenericAttestationData(slot, rightState, block.getMessage(), UInt64.valueOf(committeeIndex)));
    assertThat(attestationData.getSlot()).isEqualTo(slot);
}
Also used : AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 5 with SignedBlockAndState

use of tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState in project teku by ConsenSys.

the class AbstractDataBackedRestAPIIntegrationTest method createBlocksAtSlots.

public ArrayList<SignedBlockAndState> createBlocksAtSlots(UInt64... slots) {
    final ArrayList<SignedBlockAndState> results = new ArrayList<>();
    for (UInt64 slot : slots) {
        final SignedBlockAndState block = chainUpdater.advanceChain(slot);
        chainUpdater.updateBestBlock(block);
        results.add(block);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Aggregations

SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)291 Test (org.junit.jupiter.api.Test)208 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)107 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)71 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)41 Optional (java.util.Optional)39 StoreTransaction (tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction)38 ChainBuilder (tech.pegasys.teku.core.ChainBuilder)35 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 Bytes32 (org.apache.tuweni.bytes.Bytes32)24 AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)21 StorageSystem (tech.pegasys.teku.storage.storageSystem.StorageSystem)19 MethodSource (org.junit.jupiter.params.provider.MethodSource)14 CheckpointState (tech.pegasys.teku.spec.datastructures.state.CheckpointState)14 UpdatableStore (tech.pegasys.teku.storage.store.UpdatableStore)12 ArrayList (java.util.ArrayList)11 BlockOptions (tech.pegasys.teku.core.ChainBuilder.BlockOptions)10 ReadOnlyForkChoiceStrategy (tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy)10 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)10