diff --git a/lbrynet/analytics/events.py b/lbrynet/analytics/events.py index 203f24973..14cdabcf0 100644 --- a/lbrynet/analytics/events.py +++ b/lbrynet/analytics/events.py @@ -10,7 +10,7 @@ def get_sd_hash(stream_info): if not stream_info: return None try: - return stream_info['sources']['lbry_sd_hash'] + return stream_info['stream']['source']['source'] except (KeyError, TypeError, ValueError): log.debug('Failed to get sd_hash from %s', stream_info, exc_info=True) return None diff --git a/lbrynet/conf.py b/lbrynet/conf.py index a12e7e588..c9cae41a0 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -182,7 +182,7 @@ ADJUSTABLE_SETTINGS = { # # TODO: writing json on the cmd line is a pain, come up with a nicer # parser for this data structure. (maybe MAX_KEY_FEE': USD:25 - 'max_key_fee': (json.loads, {'USD': {'amount': 25.0, 'address': ''}}), + 'max_key_fee': (json.loads, {'currency':'USD', 'amount': 25.0, 'address':''}), 'max_search_results': (int, 25), 'max_upload': (float, 0.0), diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index 63bd24fb9..1353c592d 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -1,6 +1,5 @@ import datetime import logging -import json import os from twisted.internet import threads, reactor, defer, task @@ -19,6 +18,7 @@ from lbryschema.claim import ClaimDict from lbryschema.decode import smart_decode from lbryschema.error import DecodeError +from lbrynet.core import utils from lbrynet.core.sqlite_helpers import rerun_if_locked from lbrynet.interfaces import IRequestCreator, IQueryHandlerFactory, IQueryHandler, IWallet from lbrynet.core.client.ClientRequest import ClientRequest @@ -495,7 +495,7 @@ class Wallet(object): return Failure(InvalidStreamInfoError(name, result['value'])) #TODO: what if keys don't exist here, # probablly need get_sd_hash() function fro ClaimDict - sd_hash = claim_dict.claim_dict['stream']['source']['source'] + sd_hash = utils.get_sd_hash(claim_dict.claim_dict) claim_outpoint = ClaimOutpoint(result['txid'], result['nout']) d = self._save_name_metadata(name, claim_outpoint, sd_hash) d.addCallback(lambda _: self.get_claimid(name, result['txid'], result['nout'])) @@ -534,7 +534,7 @@ class Wallet(object): def _get_claim_for_return(claim): if not claim: return False - claim['value'] = json.loads(claim['value']) + claim['value'] = smart_decode(claim['value'].decode('hex')).claim_dict return claim def _get_my_unspent_claim(claims): @@ -559,6 +559,7 @@ class Wallet(object): d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'], r['nout']))) else: d = self._get_claim_info(name, ClaimOutpoint(txid, nout)) + # TODO: this catches every exception, fix this d.addErrback(lambda _: False) return d @@ -579,9 +580,9 @@ class Wallet(object): def _get_claim_info(self, name, claim_outpoint): def _build_response(claim): try: - claim_dict = smart_decode(claim['value'].decode('hex')) - meta_ver = claim_dict.claim_dict['stream']['metadata']['version'] - sd_hash = claim_dict.claim_dict['stream']['source']['source'] + claim_dict = smart_decode(claim['value'].decode('hex')).claim_dict + meta_ver = claim_dict['stream']['metadata']['version'] + sd_hash = utils.get_sd_hash(claim_dict) d = self._save_name_metadata(name, claim_outpoint, sd_hash) except (TypeError, ValueError, KeyError, DecodeError): claim_dict = claim['value'] @@ -651,7 +652,7 @@ class Wallet(object): claim_outpoint = ClaimOutpoint(claim['txid'], claim['nout']) log.info("Saving metadata for claim %s %d", claim['txid'], claim['nout']) yield self._save_name_metadata(name, claim_outpoint, - claim_dict.claim_dict['stream']['source']['source']) + utils.get_sd_hash(claim_dict.claim_dict)) defer.returnValue(claim) @defer.inlineCallbacks diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index 6136c9626..f4e79b65e 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -112,7 +112,7 @@ def short_hash(hash_str): def get_sd_hash(stream_info): if not stream_info: return None - return stream_info['sources']['lbry_sd_hash'] + return stream_info['stream']['source']['source'] def json_dumps_pretty(obj, **kwargs): diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 786af1d12..03f2e5054 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -24,7 +24,8 @@ from lbrynet.conf import LBRYCRD_WALLET, LBRYUM_WALLET, PTC_WALLET from lbrynet.reflector import reupload from lbrynet.reflector import ServerFactory as reflector_server_factory from lbrynet.metadata.Fee import FeeValidator -from lbrynet.metadata.Metadata import verify_name_characters, Metadata +from lbrynet.metadata.Metadata import verify_name_characters +from lbryschema.decode import smart_decode from lbrynet.lbryfile.client.EncryptedFileDownloader import EncryptedFileSaverFactory from lbrynet.lbryfile.client.EncryptedFileDownloader import EncryptedFileOpenerFactory from lbrynet.lbryfile.client.EncryptedFileOptions import add_lbry_file_to_sd_identifier @@ -828,7 +829,7 @@ class Daemon(AuthJSONRPCServer): return d def _get_est_cost_from_metadata(self, metadata, name): - d = self.get_est_cost_from_sd_hash(metadata['sources']['lbry_sd_hash']) + d = self.get_est_cost_from_sd_hash(utils.get_sd_hash(metadata)) def _handle_err(err): if isinstance(err, Failure): @@ -1426,9 +1427,10 @@ class Daemon(AuthJSONRPCServer): """ def _get_claim(_claim_id, _claims): + #TODO: do this in Wallet class for claim in _claims['claims']: if claim['claim_id'] == _claim_id: - return Metadata(json.loads(claim['value'])) + return smart_decode(claim['value']).claim_dict log.info("Received request to get %s", name) @@ -1477,9 +1479,11 @@ class Daemon(AuthJSONRPCServer): result = yield self._get_lbry_file_dict(self.streams[name].downloader, full_status=True) except Exception as e: + # TODO: should reraise here, instead of returning e.message log.warning('Failed to get %s', name) self.analytics_manager.send_download_errored(download_id, name, stream_info) result = e.message + response = yield self._render_response(result) defer.returnValue(response) diff --git a/lbrynet/lbrynet_daemon/Downloader.py b/lbrynet/lbrynet_daemon/Downloader.py index 20067a93a..de7852125 100644 --- a/lbrynet/lbrynet_daemon/Downloader.py +++ b/lbrynet/lbrynet_daemon/Downloader.py @@ -3,6 +3,7 @@ import os from twisted.internet import defer from twisted.internet.task import LoopingCall +from lbrynet.core import utils from lbrynet.core.Error import InsufficientFundsError, KeyFeeAboveMaxAllowed from lbrynet.core.StreamDescriptor import download_sd_blob from lbrynet.metadata.Fee import FeeValidator @@ -158,9 +159,9 @@ class GetStream(object): @defer.inlineCallbacks def download(self, stream_info, name): self.set_status(INITIALIZING_CODE, name) - self.sd_hash = stream_info['sources']['lbry_sd_hash'] - if 'fee' in stream_info: - fee = self.check_fee(stream_info['fee']) + self.sd_hash = utils.get_sd_hash(stream_info) + if 'fee' in stream_info['stream']['metadata']: + fee = self.check_fee(stream_info['stream']['metadata']['fee']) else: fee = None diff --git a/lbrynet/lbrynet_daemon/ExchangeRateManager.py b/lbrynet/lbrynet_daemon/ExchangeRateManager.py index 46f4b0d22..dee5af0fb 100644 --- a/lbrynet/lbrynet_daemon/ExchangeRateManager.py +++ b/lbrynet/lbrynet_daemon/ExchangeRateManager.py @@ -193,11 +193,10 @@ class ExchangeRateManager(object): fee_in = fee return FeeValidator({ - fee_in.currency_symbol: { + 'currency':fee_in.currency_symbol, 'amount': self.convert_currency(fee_in.currency_symbol, "LBC", fee_in.amount), 'address': fee_in.address - } - }) + }) class DummyBTCLBCFeed(MarketFeed): @@ -252,8 +251,7 @@ class DummyExchangeRateManager(object): fee_in = fee return FeeValidator({ - fee_in.currency_symbol: { + 'currency':fee_in.currency_symbol, 'amount': self.convert_currency(fee_in.currency_symbol, "LBC", fee_in.amount), 'address': fee_in.address - } - }) + }) diff --git a/lbrynet/metadata/Fee.py b/lbrynet/metadata/Fee.py index 0e77b3cfe..c9bc97a55 100644 --- a/lbrynet/metadata/Fee.py +++ b/lbrynet/metadata/Fee.py @@ -14,12 +14,12 @@ class FeeValidator(StructuredDict): StructuredDict.__init__(self, fee, fee.get('ver', '0.0.1')) - self.currency_symbol = self.keys()[0] + self.currency_symbol = self['currency'] self.amount = self._get_amount() - self.address = self[self.currency_symbol]['address'] + self.address = self['address'] def _get_amount(self): - amt = self[self.currency_symbol]['amount'] + amt = self['amount'] try: return float(amt) except TypeError: diff --git a/tests/unit/lbrynet_daemon/test_Daemon.py b/tests/unit/lbrynet_daemon/test_Daemon.py index dd06247a7..3c17d572f 100644 --- a/tests/unit/lbrynet_daemon/test_Daemon.py +++ b/tests/unit/lbrynet_daemon/test_Daemon.py @@ -80,7 +80,7 @@ def get_test_daemon(data_rate=None, generous=True, with_fee=False): } if with_fee: metadata.update( - {"fee": {"USD": {"address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75}}}) + {"fee": {"currency":"USD", "address":"bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75}}) daemon._resolve_name = lambda _: defer.succeed(metadata) return daemon diff --git a/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py b/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py index 34529c18c..8a0ba3792 100644 --- a/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py +++ b/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py @@ -11,14 +11,13 @@ from tests import util class FeeFormatTest(unittest.TestCase): def test_fee_created_with_correct_inputs(self): fee_dict = { - 'USD': { - 'amount': 10.0, - 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" - } + 'currency':'USD', + 'amount': 10.0, + 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" } fee = Fee.FeeValidator(fee_dict) - self.assertEqual(10.0, fee['USD']['amount']) - + self.assertEqual(10.0, fee['amount']) + self.assertEqual('USD', fee['currency']) class FeeTest(unittest.TestCase): def setUp(self): @@ -26,11 +25,11 @@ class FeeTest(unittest.TestCase): def test_fee_converts_to_lbc(self): fee_dict = { - 'USD': { - 'amount': 10.0, - 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" + 'currency':'USD', + 'amount': 10.0, + 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" } - } + rates = { 'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1}, 'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}