### Question:
ChainHeadTracker implements ConnectCallback { @Override public void onPeerConnected(final EthPeer peer) { LOG.debug("Requesting chain head info for {}", peer); GetHeadersFromPeerByHashTask.forSingleHash( protocolSchedule, ethContext, Hash.wrap(peer.chainState().getBestBlock().getHash()), 0, metricsSystem) .assignPeer(peer) .run() .whenComplete( (peerResult, error) -> { if (peerResult != null && !peerResult.getResult().isEmpty()) { final BlockHeader chainHeadHeader = peerResult.getResult().get(0); peer.chainState().update(chainHeadHeader); trailingPeerLimiter.enforceTrailingPeerLimit(); } else { LOG.debug( "Failed to retrieve chain head information. Disconnecting " + peer, error); peer.disconnect(DisconnectReason.USELESS_PEER); } }); } ChainHeadTracker(
      final EthContext ethContext,
      final ProtocolSchedule protocolSchedule,
      final TrailingPeerLimiter trailingPeerLimiter,
      final MetricsSystem metricsSystem); static void trackChainHeadForPeers(
      final EthContext ethContext,
      final ProtocolSchedule protocolSchedule,
      final Blockchain blockchain,
      final Supplier<TrailingPeerRequirements> trailingPeerRequirementsCalculator,
      final MetricsSystem metricsSystem); @Override void onPeerConnected(final EthPeer peer);  }### Answer:
@Test public void shouldRequestHeaderChainHeadWhenNewPeerConnects() { final Responder responder = RespondingEthPeer.blockchainResponder( blockchainSetupUtil.getBlockchain(), blockchainSetupUtil.getWorldArchive(), blockchainSetupUtil.getTransactionPool()); chainHeadTracker.onPeerConnected(respondingPeer.getEthPeer()); Assertions.assertThat(chainHeadState().getEstimatedHeight()).isZero(); respondingPeer.respond(responder); Assertions.assertThat(chainHeadState().getEstimatedHeight()) .isEqualTo(blockchain.getChainHeadBlockNumber()); }
@Test public void shouldIgnoreHeadersIfChainHeadHasAlreadyBeenUpdatedWhileWaiting() { final Responder responder = RespondingEthPeer.blockchainResponder( blockchainSetupUtil.getBlockchain(), blockchainSetupUtil.getWorldArchive(), blockchainSetupUtil.getTransactionPool()); chainHeadTracker.onPeerConnected(respondingPeer.getEthPeer()); respondingPeer.getEthPeer().chainState().statusReceived(Hash.EMPTY_TRIE_HASH, Difficulty.ONE); respondingPeer.respond(responder); Assertions.assertThat(chainHeadState().getEstimatedHeight()).isZero(); }
@Test public void shouldCheckTrialingPeerLimits() { final Responder responder = RespondingEthPeer.blockchainResponder( blockchainSetupUtil.getBlockchain(), blockchainSetupUtil.getWorldArchive(), blockchainSetupUtil.getTransactionPool()); chainHeadTracker.onPeerConnected(respondingPeer.getEthPeer()); Assertions.assertThat(chainHeadState().getEstimatedHeight()).isZero(); respondingPeer.respond(responder); Assertions.assertThat(chainHeadState().getEstimatedHeight()) .isEqualTo(blockchain.getChainHeadBlockNumber()); }