async def _handle_recent_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 recent beacon blocks request failed: not handshaked with peer=%s yet', peer_id)
        (await stream.reset())
        return
    self.logger.debug('Waiting for recent beacon blocks request from the other side')
    try:
        recent_beacon_blocks_request = (await read_req(stream, RecentBeaconBlocksRequest))
        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 recent beacon blocks request message %s', recent_beacon_blocks_request)
    recent_beacon_blocks = []
    for block_root in recent_beacon_blocks_request.block_roots:
        try:
            block = self.chain.get_block_by_hash_tree_root(block_root)
        except (BlockNotFound, ValidationError):
            pass
        else:
            recent_beacon_blocks.append(block)
    recent_beacon_blocks_response = RecentBeaconBlocksResponse(blocks=recent_beacon_blocks)
    self.logger.debug('Sending recent beacon blocks response %s', recent_beacon_blocks_response)
    try:
        (await write_resp(stream, recent_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 recent beacon blocks request failed: failed to write message %s', recent_beacon_blocks_response)
            return
    self.logger.debug('Processing recent beacon blocks request from %s is finished', peer_id)
    (await stream.close())
async def _handle_recent_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 recent beacon blocks request failed: not handshaked with peer=%s yet', peer_id)
        (await stream.reset())
        return
    self.logger.debug('Waiting for recent beacon blocks request from the other side')
    try:
        recent_beacon_blocks_request = (await read_req(stream, RecentBeaconBlocksRequest))
        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 recent beacon blocks request message %s', recent_beacon_blocks_request)
    recent_beacon_blocks = []
    for block_root in recent_beacon_blocks_request.block_roots:
        try:
            block = self.chain.get_block_by_hash_tree_root(block_root)
        except (BlockNotFound, ValidationError):
            pass
        else:
            recent_beacon_blocks.append(block)
    recent_beacon_blocks_response = RecentBeaconBlocksResponse(blocks=recent_beacon_blocks)
    self.logger.debug('Sending recent beacon blocks response %s', recent_beacon_blocks_response)
    try:
        (await write_resp(stream, recent_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 recent beacon blocks request failed: failed to write message %s', recent_beacon_blocks_response)
            return
    self.logger.debug('Processing recent beacon blocks request from %s is finished', peer_id)
    (await stream.close())