bug fixes for lbryschema integration

This commit is contained in:
Kay Kurokawa 2017-04-04 19:42:35 -04:00
parent 60d6666abf
commit 931038d478
10 changed files with 39 additions and 36 deletions

View file

@ -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

View file

@ -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),

View file

@ -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

View file

@ -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):

View file

@ -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)

View file

@ -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

View file

@ -193,10 +193,9 @@ 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
}
})
@ -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
}
})

View file

@ -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:

View file

@ -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

View file

@ -11,14 +11,13 @@ from tests import util
class FeeFormatTest(unittest.TestCase):
def test_fee_created_with_correct_inputs(self):
fee_dict = {
'USD': {
'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': {
'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}