async def _handle_beacon_blocks(self, stream: INetStream) -> None:
    peer_id = stream.mplex_conn.peer_id
    if (peer_id not in self.handshaked_peers):
        self.logger.info('Processing beacon blocks request failed: not handshaked with peer=%s yet', peer_id)
        (await stream.reset())
        return
    self.logger.debug('Waiting for beacon blocks request from the other side')
    try:
        beacon_blocks_request = (await read_req(stream, BeaconBlocksRequest))
        has_error = False
    except (ReadMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
        has_error = True
        if isinstance(error, ReadMessageFailure):
            (await stream.reset())
        elif isinstance(error, MplexStreamEOF):
            (await stream.close())
    finally:
        if has_error:
            return
    self.logger.debug('Received the beacon blocks request message %s', beacon_blocks_request)
    try:
        requested_head_block = self.chain.get_block_by_hash_tree_root(beacon_blocks_request.head_block_root)
    except (BlockNotFound, ValidationError) as error:
        self.logger.info('Sending empty blocks, reason: %s', error)
        requested_beacon_blocks: Tuple[(BaseBeaconBlock, ...)] = tuple()
    else:
        if (requested_head_block.slot < beacon_blocks_request.start_slot):
            reason = f'Invalid request: head block slot({requested_head_block.slot}) lower than `start_slot`({beacon_blocks_request.start_slot})'
            try:
                (await write_resp(stream, reason, ResponseCode.INVALID_REQUEST))
                has_error = False
            except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
                has_error = True
                if isinstance(error, WriteMessageFailure):
                    (await stream.reset())
                elif isinstance(error, MplexStreamEOF):
                    (await stream.close())
            finally:
                if has_error:
                    self.logger.info('Processing beacon blocks request failed: failed to write message %s', reason)
                    return
            (await stream.close())
            return
        else:
            try:
                requested_beacon_blocks = self._get_requested_beacon_blocks(beacon_blocks_request, requested_head_block)
            except ValidationError as val_error:
                reason = ('Invalid request: ' + str(val_error))
                try:
                    (await write_resp(stream, reason, ResponseCode.INVALID_REQUEST))
                    has_error = False
                except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
                    has_error = True
                    if isinstance(error, WriteMessageFailure):
                        (await stream.reset())
                    elif isinstance(error, MplexStreamEOF):
                        (await stream.close())
                finally:
                    if has_error:
                        self.logger.info('Processing beacon blocks request failed: failed to write message %s', reason)
                        return
                (await stream.close())
                return
    beacon_blocks_response = BeaconBlocksResponse(blocks=requested_beacon_blocks)
    self.logger.debug('Sending beacon blocks response %s', beacon_blocks_response)
    try:
        (await write_resp(stream, beacon_blocks_response, ResponseCode.SUCCESS))
        has_error = False
    except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
        has_error = True
        if isinstance(error, WriteMessageFailure):
            (await stream.reset())
        elif isinstance(error, MplexStreamEOF):
            (await stream.close())
    finally:
        if has_error:
            self.logger.info('Processing beacon blocks request failed: failed to write message %s', beacon_blocks_response)
            return
    self.logger.debug('Processing beacon blocks request from %s is finished', peer_id)
    (await stream.close())
async def _handle_beacon_blocks(self, stream: INetStream) -> None:
    peer_id = stream.mplex_conn.peer_id
    if (peer_id not in self.handshaked_peers):
        self.logger.info('Processing beacon blocks request failed: not handshaked with peer=%s yet', peer_id)
        (await stream.reset())
        return
    self.logger.debug('Waiting for beacon blocks request from the other side')
    try:
        beacon_blocks_request = (await read_req(stream, BeaconBlocksRequest))
        has_error = False
    except (ReadMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
        has_error = True
        if isinstance(error, ReadMessageFailure):
            (await stream.reset())
        elif isinstance(error, MplexStreamEOF):
            (await stream.close())
    finally:
        if has_error:
            return
    self.logger.debug('Received the beacon blocks request message %s', beacon_blocks_request)
    try:
        requested_head_block = self.chain.get_block_by_hash_tree_root(beacon_blocks_request.head_block_root)
    except (BlockNotFound, ValidationError) as error:
        self.logger.info('Sending empty blocks, reason: %s', error)
        requested_beacon_blocks: Tuple[(BaseBeaconBlock, ...)] = tuple()
    else:
        if (requested_head_block.slot < beacon_blocks_request.start_slot):
            reason = f'Invalid request: head block slot({requested_head_block.slot}) lower than `start_slot`({beacon_blocks_request.start_slot})'
            try:
                (await write_resp(stream, reason, ResponseCode.INVALID_REQUEST))
                has_error = False
            except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
                has_error = True
                if isinstance(error, WriteMessageFailure):
                    (await stream.reset())
                elif isinstance(error, MplexStreamEOF):
                    (await stream.close())
            finally:
                if has_error:
                    self.logger.info('Processing beacon blocks request failed: failed to write message %s', reason)
                    return
            (await stream.close())
            return
        else:
            try:
                requested_beacon_blocks = self._get_requested_beacon_blocks(beacon_blocks_request, requested_head_block)
            except ValidationError as val_error:
                reason = ('Invalid request: ' + str(val_error))
                try:
                    (await write_resp(stream, reason, ResponseCode.INVALID_REQUEST))
                    has_error = False
                except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
                    has_error = True
                    if isinstance(error, WriteMessageFailure):
                        (await stream.reset())
                    elif isinstance(error, MplexStreamEOF):
                        (await stream.close())
                finally:
                    if has_error:
                        self.logger.info('Processing beacon blocks request failed: failed to write message %s', reason)
                        return
                (await stream.close())
                return
    beacon_blocks_response = BeaconBlocksResponse(blocks=requested_beacon_blocks)
    self.logger.debug('Sending beacon blocks response %s', beacon_blocks_response)
    try:
        (await write_resp(stream, beacon_blocks_response, ResponseCode.SUCCESS))
        has_error = False
    except (WriteMessageFailure, MplexStreamEOF, MplexStreamReset) as error:
        has_error = True
        if isinstance(error, WriteMessageFailure):
            (await stream.reset())
        elif isinstance(error, MplexStreamEOF):
            (await stream.close())
    finally:
        if has_error:
            self.logger.info('Processing beacon blocks request failed: failed to write message %s', beacon_blocks_response)
            return
    self.logger.debug('Processing beacon blocks request from %s is finished', peer_id)
    (await stream.close())