예제 #1
0
 async def resolve(self, accounts, urls, **kwargs):
     resolve = partial(self.network.retriable_call, self.network.resolve)
     txos = (await self._inflate_outputs(resolve(urls), accounts,
                                         **kwargs))[0]
     assert len(urls) == len(
         txos
     ), "Mismatch between urls requested for resolve and responses received."
     result = {}
     for url, txo in zip(urls, txos):
         if txo:
             if isinstance(txo,
                           Output) and URL.parse(url).has_stream_in_channel:
                 if not txo.channel or not txo.is_signed_by(
                         txo.channel, self):
                     txo = {
                         'error': {
                             'name': INVALID,
                             'text': f'{url} has invalid channel signature'
                         }
                     }
         else:
             txo = {
                 'error': {
                     'name': NOT_FOUND,
                     'text': f'{url} did not resolve to a claim'
                 }
             }
         result[url] = txo
     return result
예제 #2
0
 async def resolve(self, urls):
     resolve = partial(self.network.retriable_call, self.network.resolve)
     txos = (await self._inflate_outputs(resolve(urls)))[0]
     assert len(urls) == len(txos), "Mismatch between urls requested for resolve and responses received."
     result = {}
     for url, txo in zip(urls, txos):
         if txo and URL.parse(url).has_stream_in_channel:
             if not txo.channel or not txo.is_signed_by(txo.channel, self):
                 txo = None
         if txo:
             result[url] = txo
         else:
             result[url] = {'error': f'{url} did not resolve to a claim'}
     return result
예제 #3
0
파일: reader.py 프로젝트: gchenfly/lbry-sdk
def resolve_url(raw_url):
    censor = ctx.get().get_resolve_censor()

    try:
        url = URL.parse(raw_url)
    except ValueError as e:
        return e

    channel = None

    if url.has_channel:
        query = url.channel.to_dict()
        if set(query) == {'name'}:
            query['is_controlling'] = True
        else:
            query['order_by'] = ['^creation_height']
        matches = search_claims(censor, **query, limit=1)
        if matches:
            channel = matches[0]
        elif censor.censored:
            return ResolveCensoredError(
                raw_url,
                hexlify(next(iter(censor.censored))[::-1]).decode())
        else:
            return LookupError(f'Could not find channel in "{raw_url}".')

    if url.has_stream:
        query = url.stream.to_dict()
        if channel is not None:
            if set(query) == {'name'}:
                # temporarily emulate is_controlling for claims in channel
                query['order_by'] = ['effective_amount', '^height']
            else:
                query['order_by'] = ['^channel_join']
            query['channel_hash'] = channel['claim_hash']
            query['signature_valid'] = 1
        elif set(query) == {'name'}:
            query['is_controlling'] = 1
        matches = search_claims(censor, **query, limit=1)
        if matches:
            return matches[0]
        elif censor.censored:
            return ResolveCensoredError(
                raw_url,
                hexlify(next(iter(censor.censored))[::-1]).decode())
        else:
            return LookupError(f'Could not find claim at "{raw_url}".')

    return channel
예제 #4
0
    async def _resolve_url(self, raw_url):
        try:
            url = URL.parse(raw_url)
        except ValueError as e:
            return e

        stream = LookupError(f'Could not find claim at "{raw_url}".')

        channel_id = await self.resolve_channel_id(url)
        if isinstance(channel_id, LookupError):
            return channel_id
        stream = (await self.resolve_stream(url, channel_id if isinstance(channel_id, str) else None)) or stream
        if url.has_stream:
            return StreamResolution(stream)
        else:
            return ChannelResolution(channel_id)
예제 #5
0
 async def resolve(self, page, page_size, *uris):
     uris = set(uris)
     try:
         for uri in uris:
             for part in URL.parse(uri).parts:
                 if part.claim_id:
                     validate_claim_id(part.claim_id)
         claim_trie_root = self.ledger.headers.claim_trie_root
         resolutions = await self.network.get_values_for_uris(
             self.ledger.headers.hash().decode(), *uris)
         if len(uris) > 1:
             return await self._batch_handle(resolutions, uris, page,
                                             page_size, claim_trie_root)
         return await self._handle_resolutions(resolutions, uris, page,
                                               page_size, claim_trie_root)
     except ValueError as err:
         return {'error': err.args[0]}
     except Exception as e:
         log.exception(e)
         return {'error': str(e)}
예제 #6
0
파일: test_url.py 프로젝트: zhyniq/lbry-sdk
 def _assert_url(self, url_string, **kwargs):
     url = URL.parse(url_string)
     if url_string.startswith('lbry://'):
         self.assertEqual(url_string, str(url))
     else:
         self.assertEqual(f'lbry://{url_string}', str(url))
     present = {}
     for key in kwargs:
         for segment_name in self.segments:
             if key.startswith(segment_name):
                 present[segment_name] = True
                 break
     for segment_name in self.segments:
         segment = getattr(url, segment_name)
         if segment_name not in present:
             self.assertIsNone(segment)
         else:
             for field in self.fields:
                 self.assertEqual(
                     getattr(segment, field),
                     kwargs.get(f'{segment_name}_{field}', None))
예제 #7
0
def resolve_url(raw_url):

    try:
        url = URL.parse(raw_url)
    except ValueError as e:
        return e

    channel = None

    if url.has_channel:
        query = url.channel.to_dict()
        if set(query) == {'name'}:
            query['is_controlling'] = True
        else:
            query['order_by'] = ['^height']
        matches = _search(**query, limit=1)
        if matches:
            channel = matches[0]
        else:
            return LookupError(f'Could not find channel in "{raw_url}".')

    if url.has_stream:
        query = url.stream.to_dict()
        if channel is not None:
            if set(query) == {'name'}:
                # temporarily emulate is_controlling for claims in channel
                query['order_by'] = ['effective_amount']
            else:
                query['order_by'] = ['^channel_join']
            query['channel_hash'] = channel['claim_hash']
            query['signature_valid'] = 1
        elif set(query) == {'name'}:
            query['is_controlling'] = 1
        matches = _search(**query, limit=1)
        if matches:
            return matches[0]
        else:
            return LookupError(f'Could not find stream in "{raw_url}".')

    return channel
예제 #8
0
    async def download_stream_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManager',
                                       timeout: Optional[float] = None,
                                       file_name: Optional[str] = None,
                                       download_directory: Optional[str] = None,
                                       save_file: Optional[bool] = None,
                                       resolve_timeout: float = 3.0,
                                       wallet: Optional['Wallet'] = None) -> ManagedStream:
        manager = self.wallet_manager
        wallet = wallet or manager.default_wallet
        timeout = timeout or self.config.download_timeout
        start_time = self.loop.time()
        resolved_time = None
        stream = None
        txo: Optional[Output] = None
        error = None
        outpoint = None
        if save_file is None:
            save_file = self.config.save_files
        if file_name and not save_file:
            save_file = True
        if save_file:
            download_directory = download_directory or self.config.download_dir
        else:
            download_directory = None

        payment = None
        try:
            # resolve the claim
            if not URL.parse(uri).has_stream:
                raise ResolveError("cannot download a channel claim, specify a /path")
            try:
                response = await asyncio.wait_for(
                    manager.ledger.resolve(wallet.accounts, [uri]),
                    resolve_timeout
                )
                resolved_result = self._convert_to_old_resolve_output(manager, response)
            except asyncio.TimeoutError:
                raise ResolveTimeoutError(uri)
            except Exception as err:
                if isinstance(err, asyncio.CancelledError):  # TODO: remove when updated to 3.8
                    raise
                log.exception("Unexpected error resolving stream:")
                raise ResolveError(f"Unexpected error resolving stream: {str(err)}")
            await self.storage.save_claims_for_resolve([
                value for value in resolved_result.values() if 'error' not in value
            ])
            resolved = resolved_result.get(uri, {})
            resolved = resolved if 'value' in resolved else resolved.get('claim')
            if not resolved:
                raise ResolveError(f"Failed to resolve stream at '{uri}'")
            if 'error' in resolved:
                raise ResolveError(f"error resolving stream: {resolved['error']}")
            txo = response[uri]

            claim = Claim.from_bytes(binascii.unhexlify(resolved['protobuf']))
            outpoint = f"{resolved['txid']}:{resolved['nout']}"
            resolved_time = self.loop.time() - start_time

            # resume or update an existing stream, if the stream changed: download it and delete the old one after
            updated_stream, to_replace = await self._check_update_or_replace(outpoint, resolved['claim_id'], claim)
            if updated_stream:
                log.info("already have stream for %s", uri)
                if save_file and updated_stream.output_file_exists:
                    save_file = False
                await updated_stream.start(node=self.node, timeout=timeout, save_now=save_file)
                if not updated_stream.output_file_exists and (save_file or file_name or download_directory):
                    await updated_stream.save_file(
                        file_name=file_name, download_directory=download_directory, node=self.node
                    )
                return updated_stream

            if not to_replace and txo.has_price and not txo.purchase_receipt:
                payment = await manager.create_purchase_transaction(
                    wallet.accounts, txo, exchange_rate_manager
                )

            stream = ManagedStream(
                self.loop, self.config, self.blob_manager, claim.stream.source.sd_hash, download_directory,
                file_name, ManagedStream.STATUS_RUNNING, content_fee=payment,
                analytics_manager=self.analytics_manager
            )
            log.info("starting download for %s", uri)

            before_download = self.loop.time()
            await stream.start(self.node, timeout)
            stream.set_claim(resolved, claim)
            if to_replace:  # delete old stream now that the replacement has started downloading
                await self.delete_stream(to_replace)

            if payment is not None:
                await manager.broadcast_or_release(payment)
                payment = None  # to avoid releasing in `finally` later
                log.info("paid fee of %s for %s", dewies_to_lbc(stream.content_fee.outputs[0].amount), uri)
                await self.storage.save_content_fee(stream.stream_hash, stream.content_fee)

            self.streams[stream.sd_hash] = stream
            self.storage.content_claim_callbacks[stream.stream_hash] = lambda: self._update_content_claim(stream)
            await self.storage.save_content_claim(stream.stream_hash, outpoint)
            if save_file:
                await asyncio.wait_for(stream.save_file(node=self.node), timeout - (self.loop.time() - before_download),
                                       loop=self.loop)
            return stream
        except asyncio.TimeoutError:
            error = DownloadDataTimeoutError(stream.sd_hash)
            raise error
        except Exception as err:  # forgive data timeout, don't delete stream
            expected = (DownloadSDTimeoutError, DownloadDataTimeoutError, InsufficientFundsError,
                        KeyFeeAboveMaxAllowedError)
            if isinstance(err, expected):
                log.warning("Failed to download %s: %s", uri, str(err))
            elif isinstance(err, asyncio.CancelledError):
                pass
            else:
                log.exception("Unexpected error downloading stream:")
            error = err
            raise
        finally:
            if payment is not None:
                # payment is set to None after broadcasting, if we're here an exception probably happened
                await manager.ledger.release_tx(payment)
            if self.analytics_manager and (error or (stream and (stream.downloader.time_to_descriptor or
                                                                 stream.downloader.time_to_first_bytes))):
                server = self.wallet_manager.ledger.network.client.server
                self.loop.create_task(
                    self.analytics_manager.send_time_to_first_bytes(
                        resolved_time, self.loop.time() - start_time, None if not stream else stream.download_id,
                        uri, outpoint,
                        None if not stream else len(stream.downloader.blob_downloader.active_connections),
                        None if not stream else len(stream.downloader.blob_downloader.scores),
                        None if not stream else len(stream.downloader.blob_downloader.connection_failures),
                        False if not stream else stream.downloader.added_fixed_peers,
                        self.config.fixed_peer_delay if not stream else stream.downloader.fixed_peers_delay,
                        None if not stream else stream.sd_hash,
                        None if not stream else stream.downloader.time_to_descriptor,
                        None if not (stream and stream.descriptor) else stream.descriptor.blobs[0].blob_hash,
                        None if not (stream and stream.descriptor) else stream.descriptor.blobs[0].length,
                        None if not stream else stream.downloader.time_to_first_bytes,
                        None if not error else error.__class__.__name__,
                        None if not error else str(error),
                        None if not server else f"{server[0]}:{server[1]}"
                    )
                )
예제 #9
0
    async def download_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManager',
                                timeout: Optional[float] = None, file_name: Optional[str] = None,
                                download_directory: Optional[str] = None,
                                save_file: Optional[bool] = None, resolve_timeout: float = 3.0,
                                wallet: Optional['Wallet'] = None) -> ManagedDownloadSource:

        wallet = wallet or self.wallet_manager.default_wallet
        timeout = timeout or self.config.download_timeout
        start_time = self.loop.time()
        resolved_time = None
        stream = None
        claim = None
        error = None
        outpoint = None
        if save_file is None:
            save_file = self.config.save_files
        if file_name and not save_file:
            save_file = True
        if save_file:
            download_directory = download_directory or self.config.download_dir
        else:
            download_directory = None

        payment = None
        try:
            # resolve the claim
            if not URL.parse(uri).has_stream:
                raise ResolveError("cannot download a channel claim, specify a /path")
            try:
                resolved_result = await asyncio.wait_for(
                    self.wallet_manager.ledger.resolve(
                        wallet.accounts, [uri],
                        include_purchase_receipt=True,
                        include_is_my_output=True
                    ), resolve_timeout
                )
            except asyncio.TimeoutError:
                raise ResolveTimeoutError(uri)
            except Exception as err:
                if isinstance(err, asyncio.CancelledError):
                    raise
                log.exception("Unexpected error resolving stream:")
                raise ResolveError(f"Unexpected error resolving stream: {str(err)}")
            if 'error' in resolved_result:
                raise ResolveError(f"Unexpected error resolving uri for download: {resolved_result['error']}")
            if not resolved_result or uri not in resolved_result:
                raise ResolveError(f"Failed to resolve stream at '{uri}'")
            txo = resolved_result[uri]
            if isinstance(txo, dict):
                raise ResolveError(f"Failed to resolve stream at '{uri}': {txo}")
            claim = txo.claim
            outpoint = f"{txo.tx_ref.id}:{txo.position}"
            resolved_time = self.loop.time() - start_time
            await self.storage.save_claim_from_output(self.wallet_manager.ledger, txo)

            ####################
            # update or replace
            ####################

            if claim.stream.source.bt_infohash:
                source_manager = self.source_managers['torrent']
                existing = source_manager.get_filtered(bt_infohash=claim.stream.source.bt_infohash)
            else:
                source_manager = self.source_managers['stream']
                existing = source_manager.get_filtered(sd_hash=claim.stream.source.sd_hash)

            # resume or update an existing stream, if the stream changed: download it and delete the old one after
            to_replace, updated_stream = None, None
            if existing and existing[0].claim_id != txo.claim_id:
                raise ResolveError(f"stream for {existing[0].claim_id} collides with existing download {txo.claim_id}")
            if existing:
                log.info("claim contains a metadata only update to a stream we have")
                if claim.stream.source.bt_infohash:
                    await self.storage.save_torrent_content_claim(
                        existing[0].identifier, outpoint, existing[0].torrent_length, existing[0].torrent_name
                    )
                    claim_info = await self.storage.get_content_claim_for_torrent(existing[0].identifier)
                    existing[0].set_claim(claim_info, claim)
                else:
                    await self.storage.save_content_claim(
                        existing[0].stream_hash, outpoint
                    )
                    await source_manager._update_content_claim(existing[0])
                updated_stream = existing[0]
            else:
                existing_for_claim_id = self.get_filtered(claim_id=txo.claim_id)
                if existing_for_claim_id:
                    log.info("claim contains an update to a stream we have, downloading it")
                    if save_file and existing_for_claim_id[0].output_file_exists:
                        save_file = False
                    if not claim.stream.source.bt_infohash:
                        existing_for_claim_id[0].downloader.node = source_manager.node
                    await existing_for_claim_id[0].start(timeout=timeout, save_now=save_file)
                    if not existing_for_claim_id[0].output_file_exists and (
                            save_file or file_name or download_directory):
                        await existing_for_claim_id[0].save_file(
                            file_name=file_name, download_directory=download_directory
                        )
                    to_replace = existing_for_claim_id[0]

            # resume or update an existing stream, if the stream changed: download it and delete the old one after
            if updated_stream:
                log.info("already have stream for %s", uri)
                if save_file and updated_stream.output_file_exists:
                    save_file = False
                if not claim.stream.source.bt_infohash:
                    updated_stream.downloader.node = source_manager.node
                await updated_stream.start(timeout=timeout, save_now=save_file)
                if not updated_stream.output_file_exists and (save_file or file_name or download_directory):
                    await updated_stream.save_file(
                        file_name=file_name, download_directory=download_directory
                    )
                return updated_stream

            ####################
            # pay fee
            ####################

            needs_purchasing = (
                not to_replace and
                not txo.is_my_output and
                txo.has_price and
                not txo.purchase_receipt
            )

            if needs_purchasing:
                payment = await self.wallet_manager.create_purchase_transaction(
                    wallet.accounts, txo, exchange_rate_manager
                )

            ####################
            # make downloader and wait for start
            ####################

            if not claim.stream.source.bt_infohash:
                # fixme: this shouldnt be here
                stream = ManagedStream(
                    self.loop, self.config, source_manager.blob_manager, claim.stream.source.sd_hash,
                    download_directory, file_name, ManagedStream.STATUS_RUNNING, content_fee=payment,
                    analytics_manager=self.analytics_manager
                )
                stream.downloader.node = source_manager.node
            else:
                stream = TorrentSource(
                    self.loop, self.config, self.storage, identifier=claim.stream.source.bt_infohash,
                    file_name=file_name, download_directory=download_directory or self.config.download_dir,
                    status=ManagedStream.STATUS_RUNNING,
                    analytics_manager=self.analytics_manager,
                    torrent_session=source_manager.torrent_session
                )
            log.info("starting download for %s", uri)

            before_download = self.loop.time()
            await stream.start(timeout, save_file)

            ####################
            # success case: delete to_replace if applicable, broadcast fee payment
            ####################

            if to_replace:  # delete old stream now that the replacement has started downloading
                await source_manager.delete(to_replace)

            if payment is not None:
                await self.wallet_manager.broadcast_or_release(payment)
                payment = None  # to avoid releasing in `finally` later
                log.info("paid fee of %s for %s", dewies_to_lbc(stream.content_fee.outputs[0].amount), uri)
                await self.storage.save_content_fee(stream.stream_hash, stream.content_fee)

            source_manager.add(stream)

            if not claim.stream.source.bt_infohash:
                await self.storage.save_content_claim(stream.stream_hash, outpoint)
            else:
                await self.storage.save_torrent_content_claim(
                    stream.identifier, outpoint, stream.torrent_length, stream.torrent_name
                )
                claim_info = await self.storage.get_content_claim_for_torrent(stream.identifier)
                stream.set_claim(claim_info, claim)
            if save_file:
                await asyncio.wait_for(stream.save_file(), timeout - (self.loop.time() - before_download),
                                       loop=self.loop)
            return stream
        except asyncio.TimeoutError:
            error = DownloadDataTimeoutError(stream.sd_hash)
            raise error
        except Exception as err:  # forgive data timeout, don't delete stream
            expected = (DownloadSDTimeoutError, DownloadDataTimeoutError, InsufficientFundsError,
                        KeyFeeAboveMaxAllowedError)
            if isinstance(err, expected):
                log.warning("Failed to download %s: %s", uri, str(err))
            elif isinstance(err, asyncio.CancelledError):
                pass
            else:
                log.exception("Unexpected error downloading stream:")
            error = err
            raise
        finally:
            if payment is not None:
                # payment is set to None after broadcasting, if we're here an exception probably happened
                await self.wallet_manager.ledger.release_tx(payment)
            if self.analytics_manager and claim and claim.stream.source.bt_infohash:
                # TODO: analytics for torrents
                pass
            elif self.analytics_manager and (error or (stream and (stream.downloader.time_to_descriptor or
                                                                   stream.downloader.time_to_first_bytes))):
                server = self.wallet_manager.ledger.network.client.server
                self.loop.create_task(
                    self.analytics_manager.send_time_to_first_bytes(
                        resolved_time, self.loop.time() - start_time, None if not stream else stream.download_id,
                        uri, outpoint,
                        None if not stream else len(stream.downloader.blob_downloader.active_connections),
                        None if not stream else len(stream.downloader.blob_downloader.scores),
                        None if not stream else len(stream.downloader.blob_downloader.connection_failures),
                        False if not stream else stream.downloader.added_fixed_peers,
                        self.config.fixed_peer_delay if not stream else stream.downloader.fixed_peers_delay,
                        None if not stream else stream.sd_hash,
                        None if not stream else stream.downloader.time_to_descriptor,
                        None if not (stream and stream.descriptor) else stream.descriptor.blobs[0].blob_hash,
                        None if not (stream and stream.descriptor) else stream.descriptor.blobs[0].length,
                        None if not stream else stream.downloader.time_to_first_bytes,
                        None if not error else error.__class__.__name__,
                        None if not error else str(error),
                        None if not server else f"{server[0]}:{server[1]}"
                    )
                )
예제 #10
0
    async def download_stream_from_uri(
            self,
            uri,
            exchange_rate_manager: 'ExchangeRateManager',
            timeout: typing.Optional[float] = None,
            file_name: typing.Optional[str] = None,
            download_directory: typing.Optional[str] = None,
            save_file: typing.Optional[bool] = None,
            resolve_timeout: float = 3.0) -> ManagedStream:
        timeout = timeout or self.config.download_timeout
        start_time = self.loop.time()
        resolved_time = None
        stream = None
        error = None
        outpoint = None
        if save_file is None:
            save_file = self.config.save_files
        if file_name and not save_file:
            save_file = True
        if save_file:
            download_directory = download_directory or self.config.download_dir
        else:
            download_directory = None

        try:
            # resolve the claim
            if not URL.parse(uri).has_stream:
                raise ResolveError(
                    "cannot download a channel claim, specify a /path")
            try:
                resolved_result = self._convert_to_old_resolve_output(
                    await asyncio.wait_for(self.wallet.ledger.resolve([uri]),
                                           resolve_timeout))
            except asyncio.TimeoutError:
                raise ResolveTimeout(uri)
            except Exception as err:
                if isinstance(err, asyncio.CancelledError):
                    raise
                raise ResolveError(
                    f"Unexpected error resolving stream: {str(err)}")
            await self.storage.save_claims_for_resolve([
                value for value in resolved_result.values()
                if 'error' not in value
            ])
            resolved = resolved_result.get(uri, {})
            resolved = resolved if 'value' in resolved else resolved.get(
                'claim')
            if not resolved:
                raise ResolveError(f"Failed to resolve stream at '{uri}'")
            if 'error' in resolved:
                raise ResolveError(
                    f"error resolving stream: {resolved['error']}")

            claim = Claim.from_bytes(binascii.unhexlify(resolved['protobuf']))
            outpoint = f"{resolved['txid']}:{resolved['nout']}"
            resolved_time = self.loop.time() - start_time

            # resume or update an existing stream, if the stream changed download it and delete the old one after
            updated_stream, to_replace = await self._check_update_or_replace(
                outpoint, resolved['claim_id'], claim)
            if updated_stream:
                log.info("already have stream for %s", uri)
                if save_file and updated_stream.output_file_exists:
                    save_file = False
                await updated_stream.start(node=self.node,
                                           timeout=timeout,
                                           save_now=save_file)
                if not updated_stream.output_file_exists and (
                        save_file or file_name or download_directory):
                    await updated_stream.save_file(
                        file_name=file_name,
                        download_directory=download_directory,
                        node=self.node)
                return updated_stream

            content_fee = None
            fee_amount, fee_address = None, None

            # check that the fee is payable
            if not to_replace and claim.stream.has_fee and claim.stream.fee.amount:
                fee_amount = round(
                    exchange_rate_manager.convert_currency(
                        claim.stream.fee.currency, "LBC",
                        claim.stream.fee.amount), 5)
                max_fee_amount = round(
                    exchange_rate_manager.convert_currency(
                        self.config.max_key_fee['currency'], "LBC",
                        Decimal(self.config.max_key_fee['amount'])), 5)
                if fee_amount > max_fee_amount:
                    msg = f"fee of {fee_amount} exceeds max configured to allow of {max_fee_amount}"
                    log.warning(msg)
                    raise KeyFeeAboveMaxAllowed(msg)
                balance = await self.wallet.default_account.get_balance()
                if lbc_to_dewies(str(fee_amount)) > balance:
                    msg = f"fee of {fee_amount} exceeds max available balance"
                    log.warning(msg)
                    raise InsufficientFundsError(msg)
                fee_address = claim.stream.fee.address or resolved['address']

            stream = ManagedStream(self.loop,
                                   self.config,
                                   self.blob_manager,
                                   claim.stream.source.sd_hash,
                                   download_directory,
                                   file_name,
                                   ManagedStream.STATUS_RUNNING,
                                   content_fee=content_fee,
                                   analytics_manager=self.analytics_manager)
            log.info("starting download for %s", uri)

            before_download = self.loop.time()
            await stream.start(self.node, timeout)
            stream.set_claim(resolved, claim)
            if to_replace:  # delete old stream now that the replacement has started downloading
                await self.delete_stream(to_replace)
            elif fee_address:
                stream.content_fee = await self.wallet.send_amount_to_address(
                    lbc_to_dewies(str(fee_amount)),
                    fee_address.encode('latin1'))
                log.info("paid fee of %s for %s", fee_amount, uri)
                await self.storage.save_content_fee(stream.stream_hash,
                                                    stream.content_fee)

            self.streams[stream.sd_hash] = stream
            self.storage.content_claim_callbacks[
                stream.stream_hash] = lambda: self._update_content_claim(stream
                                                                         )
            await self.storage.save_content_claim(stream.stream_hash, outpoint)
            if save_file:
                await asyncio.wait_for(stream.save_file(node=self.node),
                                       timeout -
                                       (self.loop.time() - before_download),
                                       loop=self.loop)
            return stream
        except asyncio.TimeoutError:
            error = DownloadDataTimeout(stream.sd_hash)
            raise error
        except Exception as err:  # forgive data timeout, dont delete stream
            error = err
            raise
        finally:
            if self.analytics_manager and (
                    error or (stream and
                              (stream.downloader.time_to_descriptor
                               or stream.downloader.time_to_first_bytes))):
                server = self.wallet.ledger.network.client.server
                self.loop.create_task(
                    self.analytics_manager.send_time_to_first_bytes(
                        resolved_time,
                        self.loop.time() - start_time,
                        None if not stream else stream.download_id, uri,
                        outpoint, None if not stream else len(
                            stream.downloader.blob_downloader.
                            active_connections), None if not stream else len(
                                stream.downloader.blob_downloader.scores),
                        None if not stream else len(
                            stream.downloader.blob_downloader.
                            connection_failures), False
                        if not stream else stream.downloader.added_fixed_peers,
                        self.config.fixed_peer_delay
                        if not stream else stream.downloader.fixed_peers_delay,
                        None if not stream else stream.sd_hash, None if
                        not stream else stream.downloader.time_to_descriptor,
                        None if not (stream and stream.descriptor) else
                        stream.descriptor.blobs[0].blob_hash,
                        None if not (stream and stream.descriptor) else
                        stream.descriptor.blobs[0].length, None if not stream
                        else stream.downloader.time_to_first_bytes,
                        None if not error else error.__class__.__name__,
                        None if not error else str(error),
                        None if not server else f"{server[0]}:{server[1]}"))
예제 #11
0
 def _fail_url(self, url):
     with self.assertRaisesRegex(ValueError, 'Invalid LBRY URL'):
         URL.parse(url)
예제 #12
0
    async def _handle_resolve_uri_response(self,
                                           uri,
                                           resolution,
                                           claim_trie_root,
                                           page=0,
                                           page_size=10):
        result = {}
        parsed_uri = URL.parse(uri)
        certificate_response = None
        # parse an included certificate
        if 'certificate' in resolution:
            certificate_response = resolution['certificate']['result']
            certificate_resolution_type = resolution['certificate'][
                'resolution_type']
            if certificate_resolution_type == "winning" and certificate_response:
                if 'height' in certificate_response:
                    certificate_response = _verify_proof(
                        parsed_uri.stream.name,
                        claim_trie_root,
                        certificate_response,
                        ledger=self.ledger)
            elif certificate_resolution_type not in [
                    'winning', 'claim_id', 'sequence'
            ]:
                raise Exception(
                    f"unknown response type: {certificate_resolution_type}")
            result['certificate'] = await self.parse_and_validate_claim_result(
                certificate_response)
            result['claims_in_channel'] = len(
                resolution.get('unverified_claims_in_channel', []))

        # if this was a resolution for a name, parse the result
        if 'claim' in resolution:
            claim_response = resolution['claim']['result']
            claim_resolution_type = resolution['claim']['resolution_type']
            if claim_resolution_type == "winning" and claim_response:
                if 'height' in claim_response:
                    claim_response = _verify_proof(parsed_uri.stream.name,
                                                   claim_trie_root,
                                                   claim_response,
                                                   ledger=self.ledger)
            elif claim_resolution_type not in [
                    "sequence", "winning", "claim_id"
            ]:
                raise Exception(
                    f"unknown response type: {claim_resolution_type}")
            result['claim'] = await self.parse_and_validate_claim_result(
                claim_response, certificate_response)

        # if this was a resolution for a name in a channel make sure there is only one valid
        # match
        elif 'unverified_claims_for_name' in resolution and 'certificate' in result:
            unverified_claims_for_name = resolution[
                'unverified_claims_for_name']

            channel_info = await self.get_channel_claims_page(
                unverified_claims_for_name, result['certificate'], page=1)
            claims_in_channel, upper_bound = channel_info

            if not claims_in_channel:
                log.error("No valid claims for this name for this channel")
            elif len(claims_in_channel) > 1:
                log.warning("Multiple signed claims for the same name.")
                winner = pick_winner_from_channel_path_collision(
                    claims_in_channel)
                if winner:
                    result['claim'] = winner
                else:
                    log.error("No valid claims for this name for this channel")
            else:
                result['claim'] = claims_in_channel[0]

        # parse and validate claims in a channel iteratively into pages of results
        elif 'unverified_claims_in_channel' in resolution and 'certificate' in result:
            ids_to_check = resolution['unverified_claims_in_channel']
            channel_info = await self.get_channel_claims_page(
                ids_to_check,
                result['certificate'],
                page=page,
                page_size=page_size)
            claims_in_channel, upper_bound = channel_info

            if claims_in_channel:
                result['total_claims'] = upper_bound
                result['claims_in_channel'] = claims_in_channel
        elif 'error' not in result:
            return {
                'error': 'claim not found',
                'success': False,
                'uri': str(parsed_uri)
            }

        # invalid signatures can only return outside a channel
        if result.get('claim', {}).get('has_signature', False):
            if parsed_uri.has_stream and not result['claim'][
                    'signature_is_valid']:
                return {
                    'error': 'claim not found',
                    'success': False,
                    'uri': str(parsed_uri)
                }
        return result