Example #1
0
 def claim(self) -> Claim:
     if self.is_claim:
         if not isinstance(self.script.values['claim'], Claim):
             self.script.values['claim'] = Claim.from_bytes(self.script.values['claim'])
         return self.script.values['claim']
     raise ValueError('Only claim name and claim update have the claim payload.')
Example #2
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]}"))
Example #3
0
    def _make_db(new_db):
        # create the new tables
        new_db.executescript(CREATE_TABLES_QUERY)

        # first migrate the blobs
        blobs = blobs_db_cursor.execute("select * from blobs").fetchall()
        _populate_blobs(blobs)  # pylint: disable=no-value-for-parameter
        log.info("migrated %i blobs",
                 new_db.execute("select count(*) from blob").fetchone()[0])

        # used to store the query arguments if we need to try re-importing the lbry file later
        file_args = {}  # <sd_hash>: args tuple

        file_outpoints = {}  # <outpoint tuple>: sd_hash

        # get the file and stream queries ready
        for (rowid, sd_hash, stream_hash, key, stream_name, suggested_file_name, data_rate, status) in \
            lbryfile_db.execute(
                "select distinct lbry_files.rowid, d.sd_blob_hash, lbry_files.*, o.blob_data_rate, o.status "
                "from lbry_files "
                "inner join lbry_file_descriptors d on lbry_files.stream_hash=d.stream_hash "
                "inner join lbry_file_options o on lbry_files.stream_hash=o.stream_hash"):

            # this is try to link the file to a content claim after we've imported all the files
            if rowid in old_rowid_to_outpoint:
                file_outpoints[old_rowid_to_outpoint[rowid]] = sd_hash
            elif sd_hash in old_sd_hash_to_outpoint:
                file_outpoints[old_sd_hash_to_outpoint[sd_hash]] = sd_hash

            sd_hash_to_stream_hash[sd_hash] = stream_hash
            if stream_hash in stream_hash_to_stream_blobs:
                file_args[sd_hash] = (
                    sd_hash, stream_hash, key, stream_name,
                    suggested_file_name, data_rate or 0.0, status,
                    stream_hash_to_stream_blobs.pop(stream_hash))

        # used to store the query arguments if we need to try re-importing the claim
        claim_queries = {}  # <sd_hash>: claim query tuple

        # get the claim queries ready, only keep those with associated files
        for outpoint, sd_hash in file_outpoints.items():
            if outpoint in claim_outpoint_queries:
                claim_queries[sd_hash] = claim_outpoint_queries[outpoint]

        # insert the claims
        new_db.executemany(
            "insert or ignore into claim values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
            [
                ("%s:%i" % (claim_arg_tup[0], claim_arg_tup[1]),
                 claim_arg_tup[2], claim_arg_tup[3], claim_arg_tup[7],
                 claim_arg_tup[6], claim_arg_tup[8],
                 Claim.from_bytes(claim_arg_tup[8]).signing_channel_id,
                 claim_arg_tup[5], claim_arg_tup[4])
                for sd_hash, claim_arg_tup in claim_queries.items()
                if claim_arg_tup
            ]  # sd_hash,  (txid, nout, claim_id, name, sequence, address, height, amount, serialized)
        )

        log.info("migrated %i claims",
                 new_db.execute("select count(*) from claim").fetchone()[0])

        damaged_stream_sds = []
        # import the files and get sd hashes of streams to attempt recovering
        for sd_hash, file_query in file_args.items():
            failed_sd = _import_file(*file_query)
            if failed_sd:
                damaged_stream_sds.append(failed_sd)

        # recover damaged streams
        if damaged_stream_sds:
            blob_dir = os.path.join(conf.data_dir, "blobfiles")
            damaged_sds_on_disk = [] if not os.path.isdir(blob_dir) else list(
                {p
                 for p in os.listdir(blob_dir) if p in damaged_stream_sds})
            for damaged_sd in damaged_sds_on_disk:
                try:
                    decoded, sd_length = verify_sd_blob(damaged_sd, blob_dir)
                    blobs = decoded['blobs']
                    _add_recovered_blobs(blobs, damaged_sd, sd_length)  # pylint: disable=no-value-for-parameter
                    _import_file(*file_args[damaged_sd])
                    damaged_stream_sds.remove(damaged_sd)
                except (OSError, ValueError, TypeError, IOError,
                        AssertionError, sqlite3.IntegrityError):
                    continue

        log.info("migrated %i files",
                 new_db.execute("select count(*) from file").fetchone()[0])

        # associate the content claims to their respective files
        for claim_arg_tup in claim_queries.values():
            if claim_arg_tup and (claim_arg_tup[0], claim_arg_tup[1]) in file_outpoints \
                    and file_outpoints[(claim_arg_tup[0], claim_arg_tup[1])] in sd_hash_to_stream_hash:
                try:
                    new_db.execute(
                        "insert or ignore into content_claim values (?, ?)",
                        (sd_hash_to_stream_hash.get(
                            file_outpoints.get(
                                (claim_arg_tup[0], claim_arg_tup[1]))),
                         "%s:%i" % (claim_arg_tup[0], claim_arg_tup[1])))
                except sqlite3.IntegrityError:
                    continue

        log.info(
            "migrated %i content claims",
            new_db.execute("select count(*) from content_claim").fetchone()[0])
Example #4
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],
                                           include_purchase_receipt=True),
                    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]}"))