@Test public void importBlock_latestFinalizedBlock() throws Exception { Constants.SLOTS_PER_EPOCH = 6; final List<SignedBeaconBlock> blocks = new ArrayList<>(); UInt64 currentSlot = recentChainData.getHeadSlot(); for (int i = 0; i < Constants.SLOTS_PER_EPOCH; i++) { currentSlot = currentSlot.plus(UInt64.ONE); final SignedBeaconBlock block = localChain.createAndImportBlockAtSlot(currentSlot); blocks.add(block); } final StoreTransaction tx = recentChainData.startStoreTransaction(); final Bytes32 bestRoot = recentChainData.getBestBlockRoot().orElseThrow(); final UInt64 bestEpoch = compute_epoch_at_slot(recentChainData.getHeadSlot()); assertThat(bestEpoch.longValue()).isEqualTo(Constants.GENESIS_EPOCH + 1L); final Checkpoint finalized = new Checkpoint(bestEpoch, bestRoot); tx.setFinalizedCheckpoint(finalized); tx.commit().join(); final BlockImportResult result = blockImporter.importBlock(blocks.get(blocks.size() - 1)).get(); assertSuccessfulResult(result); }
@CheckReturnValue public SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block) { if (recentChainData.containsBlock(block.getMessage().hash_tree_root())) { LOG.trace( "Importing known block {}. Return successful result without re-processing.", () -> formatBlock(block)); return SafeFuture.completedFuture(BlockImportResult.knownBlock(block)); } if (!weakSubjectivityValidator.isBlockValid(block, getForkChoiceStrategy())) { return SafeFuture.completedFuture(BlockImportResult.FAILED_WEAK_SUBJECTIVITY_CHECKS); } return validateFinalizedCheckpointIsWithinWeakSubjectivityPeriod() .thenCompose(__ -> recentChainData.retrieveBlockState(block.getParent_root())) .thenCompose(preState -> forkChoice.onBlock(block, preState)) .thenApply( result -> { if (!result.isSuccessful()) { LOG.trace( "Failed to import block for reason {}: {}", result::getFailureReason, () -> formatBlock(block)); return result; } LOG.trace("Successfully imported block {}", () -> formatBlock(block)); final Optional<BlockProcessingRecord> record = result.getBlockProcessingRecord(); eventBus.post(new ImportedBlockEvent(block)); if (result.isBlockOnCanonicalChain()) { notifyBlockOperationSubscribers(block); } record.ifPresent(eventBus::post); return result; }) .exceptionally( (e) -> { LOG.error("Internal error while importing block: {}", formatBlock(block), e); return BlockImportResult.internalError(e); }); }
BlockImporter { @CheckReturnValue public SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block) { if (recentChainData.containsBlock(block.getMessage().hash_tree_root())) { LOG.trace( "Importing known block {}. Return successful result without re-processing.", () -> formatBlock(block)); return SafeFuture.completedFuture(BlockImportResult.knownBlock(block)); } if (!weakSubjectivityValidator.isBlockValid(block, getForkChoiceStrategy())) { return SafeFuture.completedFuture(BlockImportResult.FAILED_WEAK_SUBJECTIVITY_CHECKS); } return validateFinalizedCheckpointIsWithinWeakSubjectivityPeriod() .thenCompose(__ -> recentChainData.retrieveBlockState(block.getParent_root())) .thenCompose(preState -> forkChoice.onBlock(block, preState)) .thenApply( result -> { if (!result.isSuccessful()) { LOG.trace( "Failed to import block for reason {}: {}", result::getFailureReason, () -> formatBlock(block)); return result; } LOG.trace("Successfully imported block {}", () -> formatBlock(block)); final Optional<BlockProcessingRecord> record = result.getBlockProcessingRecord(); eventBus.post(new ImportedBlockEvent(block)); if (result.isBlockOnCanonicalChain()) { notifyBlockOperationSubscribers(block); } record.ifPresent(eventBus::post); return result; }) .exceptionally( (e) -> { LOG.error("Internal error while importing block: {}", formatBlock(block), e); return BlockImportResult.internalError(e); }); } }
BlockImporter { @CheckReturnValue public SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block) { if (recentChainData.containsBlock(block.getMessage().hash_tree_root())) { LOG.trace( "Importing known block {}. Return successful result without re-processing.", () -> formatBlock(block)); return SafeFuture.completedFuture(BlockImportResult.knownBlock(block)); } if (!weakSubjectivityValidator.isBlockValid(block, getForkChoiceStrategy())) { return SafeFuture.completedFuture(BlockImportResult.FAILED_WEAK_SUBJECTIVITY_CHECKS); } return validateFinalizedCheckpointIsWithinWeakSubjectivityPeriod() .thenCompose(__ -> recentChainData.retrieveBlockState(block.getParent_root())) .thenCompose(preState -> forkChoice.onBlock(block, preState)) .thenApply( result -> { if (!result.isSuccessful()) { LOG.trace( "Failed to import block for reason {}: {}", result::getFailureReason, () -> formatBlock(block)); return result; } LOG.trace("Successfully imported block {}", () -> formatBlock(block)); final Optional<BlockProcessingRecord> record = result.getBlockProcessingRecord(); eventBus.post(new ImportedBlockEvent(block)); if (result.isBlockOnCanonicalChain()) { notifyBlockOperationSubscribers(block); } record.ifPresent(eventBus::post); return result; }) .exceptionally( (e) -> { LOG.error("Internal error while importing block: {}", formatBlock(block), e); return BlockImportResult.internalError(e); }); } BlockImporter(
      final RecentChainData recentChainData,
      final ForkChoice forkChoice,
      final WeakSubjectivityValidator weakSubjectivityValidator,
      final EventBus eventBus); }
BlockImporter { @CheckReturnValue public SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block) { if (recentChainData.containsBlock(block.getMessage().hash_tree_root())) { LOG.trace( "Importing known block {}. Return successful result without re-processing.", () -> formatBlock(block)); return SafeFuture.completedFuture(BlockImportResult.knownBlock(block)); } if (!weakSubjectivityValidator.isBlockValid(block, getForkChoiceStrategy())) { return SafeFuture.completedFuture(BlockImportResult.FAILED_WEAK_SUBJECTIVITY_CHECKS); } return validateFinalizedCheckpointIsWithinWeakSubjectivityPeriod() .thenCompose(__ -> recentChainData.retrieveBlockState(block.getParent_root())) .thenCompose(preState -> forkChoice.onBlock(block, preState)) .thenApply( result -> { if (!result.isSuccessful()) { LOG.trace( "Failed to import block for reason {}: {}", result::getFailureReason, () -> formatBlock(block)); return result; } LOG.trace("Successfully imported block {}", () -> formatBlock(block)); final Optional<BlockProcessingRecord> record = result.getBlockProcessingRecord(); eventBus.post(new ImportedBlockEvent(block)); if (result.isBlockOnCanonicalChain()) { notifyBlockOperationSubscribers(block); } record.ifPresent(eventBus::post); return result; }) .exceptionally( (e) -> { LOG.error("Internal error while importing block: {}", formatBlock(block), e); return BlockImportResult.internalError(e); }); } BlockImporter(
      final RecentChainData recentChainData,
      final ForkChoice forkChoice,
      final WeakSubjectivityValidator weakSubjectivityValidator,
      final EventBus eventBus); @CheckReturnValue SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block); void subscribeToVerifiedBlockAttestations(
      VerifiedBlockOperationsListener<Attestation> verifiedBlockAttestationsListener); void subscribeToVerifiedBlockAttesterSlashings(
      VerifiedBlockOperationsListener<AttesterSlashing> verifiedBlockAttesterSlashingsListener); void subscribeToVerifiedBlockProposerSlashings(
      VerifiedBlockOperationsListener<ProposerSlashing> verifiedBlockProposerSlashingsListener); void subscribeToVerifiedBlockVoluntaryExits(
      VerifiedBlockOperationsListener<SignedVoluntaryExit> verifiedBlockVoluntaryExitsListener); }
BlockImporter { @CheckReturnValue public SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block) { if (recentChainData.containsBlock(block.getMessage().hash_tree_root())) { LOG.trace( "Importing known block {}. Return successful result without re-processing.", () -> formatBlock(block)); return SafeFuture.completedFuture(BlockImportResult.knownBlock(block)); } if (!weakSubjectivityValidator.isBlockValid(block, getForkChoiceStrategy())) { return SafeFuture.completedFuture(BlockImportResult.FAILED_WEAK_SUBJECTIVITY_CHECKS); } return validateFinalizedCheckpointIsWithinWeakSubjectivityPeriod() .thenCompose(__ -> recentChainData.retrieveBlockState(block.getParent_root())) .thenCompose(preState -> forkChoice.onBlock(block, preState)) .thenApply( result -> { if (!result.isSuccessful()) { LOG.trace( "Failed to import block for reason {}: {}", result::getFailureReason, () -> formatBlock(block)); return result; } LOG.trace("Successfully imported block {}", () -> formatBlock(block)); final Optional<BlockProcessingRecord> record = result.getBlockProcessingRecord(); eventBus.post(new ImportedBlockEvent(block)); if (result.isBlockOnCanonicalChain()) { notifyBlockOperationSubscribers(block); } record.ifPresent(eventBus::post); return result; }) .exceptionally( (e) -> { LOG.error("Internal error while importing block: {}", formatBlock(block), e); return BlockImportResult.internalError(e); }); } BlockImporter(
      final RecentChainData recentChainData,
      final ForkChoice forkChoice,
      final WeakSubjectivityValidator weakSubjectivityValidator,
      final EventBus eventBus); @CheckReturnValue SafeFuture<BlockImportResult> importBlock(SignedBeaconBlock block); void subscribeToVerifiedBlockAttestations(
      VerifiedBlockOperationsListener<Attestation> verifiedBlockAttestationsListener); void subscribeToVerifiedBlockAttesterSlashings(
      VerifiedBlockOperationsListener<AttesterSlashing> verifiedBlockAttesterSlashingsListener); void subscribeToVerifiedBlockProposerSlashings(
      VerifiedBlockOperationsListener<ProposerSlashing> verifiedBlockProposerSlashingsListener); void subscribeToVerifiedBlockVoluntaryExits(
      VerifiedBlockOperationsListener<SignedVoluntaryExit> verifiedBlockVoluntaryExitsListener);  }