Merge pull request #513 from lbryio/fix_api_consistency_lbryum

Integrate more consistent dictionary keys and values from lbryum
This commit is contained in:
Umpei Kay Kurokawa 2017-03-10 14:12:04 -05:00 committed by GitHub
commit 7291efac83
3 changed files with 42 additions and 33 deletions

View file

@ -19,6 +19,10 @@ at anytime.
* *
### Fixed ### Fixed
* Added string comparison to ClaimOutpoint (needed to look things up by outpoint)
* Remove unused API commands from daemon
* Fix file filter `outpoint`
* Made dictionary key names in API commmand outputs to be more consistent
* *
* *
* *

View file

@ -476,7 +476,7 @@ class Wallet(object):
def _get_stream_info_from_value(self, result, name): def _get_stream_info_from_value(self, result, name):
def _check_result_fields(r): def _check_result_fields(r):
for k in ['value', 'txid', 'n', 'height', 'amount']: for k in ['value', 'txid', 'nout', 'height', 'amount']:
assert k in r, "getvalueforname response missing field %s" % k assert k in r, "getvalueforname response missing field %s" % k
def _log_success(claim_id): def _log_success(claim_id):
@ -492,9 +492,9 @@ class Wallet(object):
except (TypeError, ValueError, ValidationError): except (TypeError, ValueError, ValidationError):
return Failure(InvalidStreamInfoError(name, result['value'])) return Failure(InvalidStreamInfoError(name, result['value']))
sd_hash = metadata['sources']['lbry_sd_hash'] sd_hash = metadata['sources']['lbry_sd_hash']
claim_outpoint = ClaimOutpoint(result['txid'], result['n']) claim_outpoint = ClaimOutpoint(result['txid'], result['nout'])
d = self._save_name_metadata(name, claim_outpoint, sd_hash) d = self._save_name_metadata(name, claim_outpoint, sd_hash)
d.addCallback(lambda _: self.get_claimid(name, result['txid'], result['n'])) d.addCallback(lambda _: self.get_claimid(name, result['txid'], result['nout']))
d.addCallback(lambda cid: _log_success(cid)) d.addCallback(lambda cid: _log_success(cid))
d.addCallback(lambda _: metadata) d.addCallback(lambda _: metadata)
return d return d
@ -503,7 +503,7 @@ class Wallet(object):
d = self.get_claims_for_name(name) d = self.get_claims_for_name(name)
d.addCallback( d.addCallback(
lambda claims: next( lambda claims: next(
claim for claim in claims['claims'] if claim['claimId'] == claim_id)) claim for claim in claims['claims'] if claim['claim_id'] == claim_id))
return d return d
def get_claimid(self, name, txid, nout): def get_claimid(self, name, txid, nout):
@ -515,10 +515,10 @@ class Wallet(object):
d.addCallback( d.addCallback(
lambda claims: next( lambda claims: next(
c for c in claims if c['name'] == name and c for c in claims if c['name'] == name and
c['nOut'] == claim_outpoint['nout'])) c['nout'] == claim_outpoint['nout']))
d.addCallback( d.addCallback(
lambda claim: self._update_claimid( lambda claim: self._update_claimid(
claim['claimId'], name, ClaimOutpoint(txid, claim['nOut']))) claim['claim_id'], name, ClaimOutpoint(txid, claim['nout'])))
return d return d
claim_outpoint = ClaimOutpoint(txid, nout) claim_outpoint = ClaimOutpoint(txid, nout)
@ -537,7 +537,7 @@ class Wallet(object):
for claim in claims: for claim in claims:
is_unspent = ( is_unspent = (
claim['name'] == name and claim['name'] == name and
not claim['is spent'] and not claim['is_spent'] and
not claim.get('supported_claimid', False) not claim.get('supported_claimid', False)
) )
if is_unspent: if is_unspent:
@ -552,7 +552,7 @@ class Wallet(object):
def get_claim_info(self, name, txid=None, nout=None): def get_claim_info(self, name, txid=None, nout=None):
if txid is None or nout is None: if txid is None or nout is None:
d = self._get_value_for_name(name) d = self._get_value_for_name(name)
d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'], r['n']))) d.addCallback(lambda r: self._get_claim_info(name, ClaimOutpoint(r['txid'], r['nout'])))
else: else:
d = self._get_claim_info(name, ClaimOutpoint(txid, nout)) d = self._get_claim_info(name, ClaimOutpoint(txid, nout))
d.addErrback(lambda _: False) d.addErrback(lambda _: False)
@ -560,15 +560,15 @@ class Wallet(object):
def _format_claim_for_return(self, name, claim, metadata=None, meta_version=None): def _format_claim_for_return(self, name, claim, metadata=None, meta_version=None):
result = {} result = {}
result['claim_id'] = claim['claimId'] result['claim_id'] = claim['claim_id']
result['amount'] = claim['nEffectiveAmount'] result['amount'] = claim['effective_amount']
result['height'] = claim['nHeight'] result['height'] = claim['height']
result['name'] = name result['name'] = name
result['txid'] = claim['txid'] result['txid'] = claim['txid']
result['nout'] = claim['n'] result['nout'] = claim['nout']
result['value'] = metadata if metadata else json.loads(claim['value']) result['value'] = metadata if metadata else json.loads(claim['value'])
result['supports'] = [ result['supports'] = [
{'txid': support['txid'], 'n': support['n']} for support in claim['supports']] {'txid': support['txid'], 'nout': support['nout']} for support in claim['supports']]
result['meta_version'] = ( result['meta_version'] = (
meta_version if meta_version else result['value'].get('ver', '0.0.1')) meta_version if meta_version else result['value'].get('ver', '0.0.1'))
return result return result
@ -591,7 +591,7 @@ class Wallet(object):
meta_version=meta_ver)) meta_version=meta_ver))
log.info( log.info(
"get claim info lbry://%s metadata: %s, claimid: %s", "get claim info lbry://%s metadata: %s, claimid: %s",
name, meta_ver, claim['claimId']) name, meta_ver, claim['claim_id'])
return d return d
d = self.get_claimid(name, claim_outpoint['txid'], claim_outpoint['nout']) d = self.get_claimid(name, claim_outpoint['txid'], claim_outpoint['nout'])
@ -635,10 +635,10 @@ class Wallet(object):
if my_claim: if my_claim:
log.info("Updating claim") log.info("Updating claim")
if self.get_balance() < bid - my_claim['amount']: if self.get_balance() < bid - Decimal(my_claim['amount']):
raise InsufficientFundsError() raise InsufficientFundsError()
new_metadata = yield self.update_metadata(_metadata, my_claim['value']) new_metadata = yield self.update_metadata(_metadata, my_claim['value'])
old_claim_outpoint = ClaimOutpoint(my_claim['txid'], my_claim['nOut']) old_claim_outpoint = ClaimOutpoint(my_claim['txid'], my_claim['nout'])
claim = yield self._send_name_claim_update(name, my_claim['claim_id'], claim = yield self._send_name_claim_update(name, my_claim['claim_id'],
old_claim_outpoint, new_metadata, bid) old_claim_outpoint, new_metadata, bid)
claim['claim_id'] = my_claim['claim_id'] claim['claim_id'] = my_claim['claim_id']

View file

@ -8,7 +8,6 @@ import requests
import urllib import urllib
import simplejson as json import simplejson as json
from requests import exceptions as requests_exceptions from requests import exceptions as requests_exceptions
from decimal import Decimal
import random import random
from twisted.web import server from twisted.web import server
@ -1492,15 +1491,8 @@ class Daemon(AuthJSONRPCServer):
txid, amount, value, n, height txid, amount, value, n, height
""" """
def _convert_amount_to_float(r):
if not r:
return False
else:
r['amount'] = float(r['amount']) / 10 ** 8
return r
d = self.session.wallet.get_claim_info(name, txid, nout) d = self.session.wallet.get_claim_info(name, txid, nout)
d.addCallback(_convert_amount_to_float) d.addCallback(format_json_out_amount_as_float)
d.addCallback(lambda r: self._render_response(r)) d.addCallback(lambda r: self._render_response(r))
return d return d
@ -1826,15 +1818,8 @@ class Daemon(AuthJSONRPCServer):
list of name claims list of name claims
""" """
def _clean(claims):
for c in claims:
for k in c.keys():
if isinstance(c[k], Decimal):
c[k] = float(c[k])
return defer.succeed(claims)
d = self.session.wallet.get_name_claims() d = self.session.wallet.get_name_claims()
d.addCallback(_clean) d.addCallback(format_json_out_amount_as_float)
d.addCallback(lambda claims: self._render_response(claims)) d.addCallback(lambda claims: self._render_response(claims))
return d return d
@ -1861,6 +1846,7 @@ class Daemon(AuthJSONRPCServer):
""" """
d = self.session.wallet.get_claims_for_name(name) d = self.session.wallet.get_claims_for_name(name)
d.addCallback(format_json_out_amount_as_float)
d.addCallback(lambda r: self._render_response(r)) d.addCallback(lambda r: self._render_response(r))
return d return d
@ -2562,3 +2548,22 @@ def get_version_from_tag(tag):
return match.group(1) return match.group(1)
else: else:
raise Exception('Failed to parse version from tag {}'.format(tag)) raise Exception('Failed to parse version from tag {}'.format(tag))
# lbryum returns json loadeable object with amounts as decimal encoded string,
# convert them into floats for the daemon
# TODO: daemon should also use decimal encoded string
def format_json_out_amount_as_float(obj):
if isinstance(obj, dict):
for k, v in obj.iteritems():
if k == 'amount' or k == 'effective_amount':
obj[k] = float(obj[k])
if isinstance(v, dict) or isinstance(v, list):
obj[k] = format_json_out_amount_as_float(v)
elif isinstance(obj, list):
obj = [format_json_out_amount_as_float(o) for o in obj]
return obj