From 4570f687276186216bfa74569f25224ec71ee61d Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 4 Aug 2016 18:44:12 -0400 Subject: [PATCH] add claim_id to lbry_file and to get_claim_info --- lbrynet/core/LBRYWallet.py | 60 ++++++++++++++++--- lbrynet/lbryfilemanager/LBRYFileDownloader.py | 9 ++- lbrynet/lbrynet_daemon/LBRYDaemon.py | 4 +- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/lbrynet/core/LBRYWallet.py b/lbrynet/core/LBRYWallet.py index 2de6f622e..7cfa22717 100644 --- a/lbrynet/core/LBRYWallet.py +++ b/lbrynet/core/LBRYWallet.py @@ -324,6 +324,10 @@ class LBRYWallet(object): for k in ['value', 'txid', 'n', 'height', 'amount']: 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: log.warning("Got an error looking up a name: %s", result['error']) return Failure(UnknownNameError(name)) @@ -335,8 +339,11 @@ class LBRYWallet(object): except (ValueError, TypeError): return Failure(InvalidStreamInfoError(name)) - d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) - d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) + txid = result['txid'] + 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) return d @@ -345,8 +352,10 @@ class LBRYWallet(object): for k in ['value', 'txid', 'n', 'height', 'amount']: 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['claim_id'] = claim_id + log.info("lbry://%s complies with %s, claimid: %s" % (name, m.meta_version, claim_id)) return result if 'error' in result: @@ -360,9 +369,26 @@ class LBRYWallet(object): except (ValueError, TypeError): return Failure(InvalidStreamInfoError(name)) - d = self._save_name_metadata(name, str(result['txid']), metadata['sources']['lbry_sd_hash']) - d.addCallback(lambda _: log.info("lbry://%s complies with %s" % (name, metadata.meta_version))) - d.addCallback(lambda _: _build_response(metadata, result)) + sd_hash = metadata['sources']['lbry_sd_hash'] + txid = result['txid'] + + 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 def get_claim_info(self, name): @@ -370,7 +396,6 @@ class LBRYWallet(object): d.addCallback(lambda r: self._get_claim_info(r, name)) return d - def claim_name(self, name, bid, m): metadata = Metadata(m) @@ -534,10 +559,18 @@ class LBRYWallet(object): def _open_db(self): self.db = adbapi.ConnectionPool('sqlite3', os.path.join(self.db_dir, "blockchainname.db"), 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, " + " txid 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): 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) 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 ######### def get_balance(self): diff --git a/lbrynet/lbryfilemanager/LBRYFileDownloader.py b/lbrynet/lbryfilemanager/LBRYFileDownloader.py index b01a84708..be6232aa5 100644 --- a/lbrynet/lbryfilemanager/LBRYFileDownloader.py +++ b/lbrynet/lbryfilemanager/LBRYFileDownloader.py @@ -27,6 +27,7 @@ class ManagedLBRYFileDownloader(LBRYFileSaver): self.sd_hash = None self.txid = None self.uri = None + self.claim_id = None self.rowid = rowid self.lbry_file_manager = lbry_file_manager self.saving_status = False @@ -43,10 +44,16 @@ class ManagedLBRYFileDownloader(LBRYFileSaver): return d + def _save_claim_id(claim_id): + self.claim_id = claim_id + return defer.succeed(None) + def _save_claim(name, txid): self.uri = name 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(lambda r: _save_claim(r[0], r[1]) if r else None) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 51f3b0b66..c564705a8 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1372,7 +1372,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): 'stream_name': f.stream_name, 'suggested_file_name': f.suggested_file_name, '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, 'written_bytes': written_bytes, 'code': status[0], 'message': message}) @@ -1384,7 +1384,7 @@ class LBRYDaemon(jsonrpc.JSONRPC): '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, '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]}) return d