add claim_id to lbry_file and to get_claim_info

This commit is contained in:
Jack 2016-08-04 18:44:12 -04:00
parent 4705acba55
commit 4570f68727
3 changed files with 62 additions and 11 deletions

View file

@ -324,6 +324,10 @@ class LBRYWallet(object):
for k in ['value', 'txid', 'n', 'height', 'amount']: for k in ['value', 'txid', 'n', '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):
log.info("lbry://%s complies with %s, claimid: %s" % (name, metadata.meta_version, claim_id))
return defer.succeed(None)
if 'error' in result: if 'error' in result:
log.warning("Got an error looking up a name: %s", result['error']) log.warning("Got an error looking up a name: %s", result['error'])
return Failure(UnknownNameError(name)) return Failure(UnknownNameError(name))
@ -335,8 +339,11 @@ class LBRYWallet(object):
except (ValueError, TypeError): except (ValueError, TypeError):
return Failure(InvalidStreamInfoError(name)) return Failure(InvalidStreamInfoError(name))
d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) txid = result['txid']
d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) sd_hash = metadata['sources']['lbry_sd_hash']
d = self._save_name_metadata(name, txid, sd_hash)
d.addCallback(lambda _: self.get_claimid(name, txid))
d.addCallback(lambda cid: _log_success(cid))
d.addCallback(lambda _: metadata) d.addCallback(lambda _: metadata)
return d return d
@ -345,8 +352,10 @@ class LBRYWallet(object):
for k in ['value', 'txid', 'n', 'height', 'amount']: for k in ['value', 'txid', 'n', 'height', 'amount']:
assert k in r, "getvalueforname response missing field %s" % k assert k in r, "getvalueforname response missing field %s" % k
def _build_response(m, result): def _build_response(m, result, claim_id):
result['value'] = m result['value'] = m
result['claim_id'] = claim_id
log.info("lbry://%s complies with %s, claimid: %s" % (name, m.meta_version, claim_id))
return result return result
if 'error' in result: if 'error' in result:
@ -360,9 +369,26 @@ class LBRYWallet(object):
except (ValueError, TypeError): except (ValueError, TypeError):
return Failure(InvalidStreamInfoError(name)) return Failure(InvalidStreamInfoError(name))
d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) sd_hash = metadata['sources']['lbry_sd_hash']
d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) txid = result['txid']
d.addCallback(lambda _: _build_response(metadata, result))
d = self._save_name_metadata(name, txid, sd_hash)
d.addCallback(lambda _: self.get_claimid(name, txid))
d.addCallback(lambda claim_id: _build_response(metadata, result, claim_id))
return d
def get_claimid(self, name, txid):
def _get_id_for_return(claim_id):
if claim_id:
return defer.succeed(claim_id)
else:
d = self.get_claims_from_tx(txid)
d.addCallback(lambda claims: next(c['claimId'] for c in claims if c['name'] == name))
d.addCallback(lambda cid: self._update_claimid(cid, name, txid))
return d
d = self._get_claimid_for_tx(name, txid)
d.addCallback(_get_id_for_return)
return d return d
def get_claim_info(self, name): def get_claim_info(self, name):
@ -370,7 +396,6 @@ class LBRYWallet(object):
d.addCallback(lambda r: self._get_claim_info(r, name)) d.addCallback(lambda r: self._get_claim_info(r, name))
return d return d
def claim_name(self, name, bid, m): def claim_name(self, name, bid, m):
metadata = Metadata(m) metadata = Metadata(m)
@ -534,10 +559,18 @@ class LBRYWallet(object):
def _open_db(self): def _open_db(self):
self.db = adbapi.ConnectionPool('sqlite3', os.path.join(self.db_dir, "blockchainname.db"), self.db = adbapi.ConnectionPool('sqlite3', os.path.join(self.db_dir, "blockchainname.db"),
check_same_thread=False) check_same_thread=False)
return self.db.runQuery("create table if not exists name_metadata (" +
def create_tables(transaction):
transaction.execute("create table if not exists name_metadata (" +
" name text, " + " name text, " +
" txid text, " + " txid text, " +
" sd_hash text)") " sd_hash text)")
transaction.execute("create table if not exists claim_ids (" +
" claimId text, " +
" name text, " +
" txid text)")
return self.db.runInteraction(create_tables)
def _save_name_metadata(self, name, txid, sd_hash): def _save_name_metadata(self, name, txid, sd_hash):
d = self.db.runQuery("select * from name_metadata where name=? and txid=? and sd_hash=?", (name, txid, sd_hash)) d = self.db.runQuery("select * from name_metadata where name=? and txid=? and sd_hash=?", (name, txid, sd_hash))
@ -551,6 +584,17 @@ class LBRYWallet(object):
d.addCallback(lambda r: r[0] if len(r) else None) d.addCallback(lambda r: r[0] if len(r) else None)
return d return d
def _update_claimid(self, claim_id, name, txid):
d = self.db.runQuery("delete from claim_ids where claimId=? and name=?", (claim_id, name))
d.addCallback(lambda r: self.db.runQuery("insert into claim_ids values (?, ?, ?)", (claim_id, name, txid)))
d.addCallback(lambda _: claim_id)
return d
def _get_claimid_for_tx(self, name, txid):
d = self.db.runQuery("select claimId from claim_ids where name=? and txid=?", (name, txid))
d.addCallback(lambda r: None if not r else r[0][0])
return d
######### Must be overridden ######### ######### Must be overridden #########
def get_balance(self): def get_balance(self):

View file

@ -27,6 +27,7 @@ class ManagedLBRYFileDownloader(LBRYFileSaver):
self.sd_hash = None self.sd_hash = None
self.txid = None self.txid = None
self.uri = None self.uri = None
self.claim_id = None
self.rowid = rowid self.rowid = rowid
self.lbry_file_manager = lbry_file_manager self.lbry_file_manager = lbry_file_manager
self.saving_status = False self.saving_status = False
@ -43,10 +44,16 @@ class ManagedLBRYFileDownloader(LBRYFileSaver):
return d return d
def _save_claim_id(claim_id):
self.claim_id = claim_id
return defer.succeed(None)
def _save_claim(name, txid): def _save_claim(name, txid):
self.uri = name self.uri = name
self.txid = txid self.txid = txid
return defer.succeed(None) d = self.wallet.get_claimid(name, txid)
d.addCallback(_save_claim_id)
return d
d.addCallback(_save_sd_hash) d.addCallback(_save_sd_hash)
d.addCallback(lambda r: _save_claim(r[0], r[1]) if r else None) d.addCallback(lambda r: _save_claim(r[0], r[1]) if r else None)

View file

@ -1372,7 +1372,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
'stream_name': f.stream_name, 'stream_name': f.stream_name,
'suggested_file_name': f.suggested_file_name, 'suggested_file_name': f.suggested_file_name,
'upload_allowed': f.upload_allowed, 'sd_hash': f.sd_hash, 'upload_allowed': f.upload_allowed, 'sd_hash': f.sd_hash,
'lbry_uri': f.uri, 'txid': f.txid, 'lbry_uri': f.uri, 'txid': f.txid, 'claim_id': f.claim_id,
'total_bytes': size, 'total_bytes': size,
'written_bytes': written_bytes, 'code': status[0], 'written_bytes': written_bytes, 'code': status[0],
'message': message}) 'message': message})
@ -1384,7 +1384,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
'points_paid': f.points_paid, 'stopped': f.stopped, 'stream_hash': f.stream_hash, 'points_paid': f.points_paid, 'stopped': f.stopped, 'stream_hash': f.stream_hash,
'stream_name': f.stream_name, 'suggested_file_name': f.suggested_file_name, 'stream_name': f.stream_name, 'suggested_file_name': f.suggested_file_name,
'upload_allowed': f.upload_allowed, 'sd_hash': f.sd_hash, 'total_bytes': size, 'upload_allowed': f.upload_allowed, 'sd_hash': f.sd_hash, 'total_bytes': size,
'written_bytes': written_bytes, 'lbry_uri': f.uri, 'txid': f.txid, 'written_bytes': written_bytes, 'lbry_uri': f.uri, 'txid': f.txid, 'claim_id': f.claim_id,
'code': status[0], 'message': status[1]}) 'code': status[0], 'message': status[1]})
return d return d