@Override
  protected Optional<List<BlockHeader>> processResponse(
      final boolean streamClosed, final MessageData message, final EthPeer peer) {
    if (streamClosed) {
      // All outstanding requests have been responded to, and we still haven't found the response
      // we wanted. It must have been empty or contain data that didn't match.
      peer.recordUselessResponse("headers");
      return Optional.of(Collections.emptyList());
    }

    final BlockHeadersMessage headersMessage = BlockHeadersMessage.readFrom(message);
    final List<BlockHeader> headers = headersMessage.getHeaders(protocolSchedule);
    if (headers.isEmpty()) {
      // Message contains no data - nothing to do
      LOG.debug("headers.isEmpty. Peer: {}", peer.getLoggableId());
      return Optional.empty();
    }
    if (headers.size() > count) {
      // Too many headers - this isn't our response
      LOG.debug("headers.size()>count. Peer: {}", peer.getLoggableId());
      return Optional.empty();
    }

    final BlockHeader firstHeader = headers.get(0);
    if (!matchesFirstHeader(firstHeader)) {
      // This isn't our message - nothing to do
      LOG.debug("!matchesFirstHeader. Peer: {}", peer.getLoggableId());
      return Optional.empty();
    }

    final List<BlockHeader> headersList = new ArrayList<>(headers.size());
    headersList.add(firstHeader);
    BlockHeader prevBlockHeader = firstHeader;
    updatePeerChainState(peer, firstHeader);
    final int expectedDelta = reverse ? -(skip + 1) : (skip + 1);
    BlockHeader header = null;
    for (int i = 1; i < headers.size(); i++) {
      header = headers.get(i);
      if (header.getNumber() != prevBlockHeader.getNumber() + expectedDelta) {
        // Skip doesn't match, this isn't our data
        LOG.debug("header not matching the expected number. Peer: {}", peer.getLoggableId());
        return Optional.empty();
      }
      // if headers are supposed to be sequential check if a chain is formed
      if (Math.abs(expectedDelta) == 1) {
        final BlockHeader parent = reverse ? header : prevBlockHeader;
        final BlockHeader child = reverse ? prevBlockHeader : header;
        if (!parent.getHash().equals(child.getParentHash())) {
          LOG.debug(
              "Sequential headers must form a chain through hashes (BREACH_OF_PROTOCOL), disconnecting peer: {}",
              peer.getLoggableId());
          peer.disconnect(
              DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL_NON_SEQUENTIAL_HEADERS);
          return Optional.empty();
        }
      }
      prevBlockHeader = header;
      headersList.add(header);
    }
    // if we have received more than one header we have to update the chain state with the last
    // header as well, as the header with the highest block number can be the first or the last
    // header.
    if (headers.size() > 1) {
      updatePeerChainState(peer, header);
    }

    LOG.atTrace()
        .setMessage("Received {} of {} headers requested from peer {}")
        .addArgument(headersList::size)
        .addArgument(count)
        .addArgument(peer::getLoggableId)
        .log();
    return Optional.of(headersList);
  }
Retrieves a sequence of headers from a peer.
ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/task/AbstractGetHeadersFromPeerTask.java