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