From 84639cfb2e9e1848894ab547e229858dc79cdd7d Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 4 Aug 2020 13:49:59 -0400 Subject: [PATCH] protobuf claim_search --- lbry/db/database.py | 3 +++ lbry/db/queries/search.py | 7 ++++++- lbry/service/api.py | 41 +++++++++++++++++++++++++-------------- lbry/service/full_node.py | 9 ++++++--- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lbry/db/database.py b/lbry/db/database.py index e42b50e3d..050d716c3 100644 --- a/lbry/db/database.py +++ b/lbry/db/database.py @@ -273,6 +273,9 @@ class Database: claims, total, censor = await self.run(q.search_claims, **constraints) return Result(claims, total, censor) + async def protobuf_search_claims(self, **constraints) -> str: + return await self.run(q.protobuf_search_claims, **constraints) + async def search_supports(self, **constraints) -> Result[Output]: return await self.fetch_result(q.search_supports, **constraints) diff --git a/lbry/db/queries/search.py b/lbry/db/queries/search.py index b44962bf7..bd5549683 100644 --- a/lbry/db/queries/search.py +++ b/lbry/db/queries/search.py @@ -8,7 +8,7 @@ from sqlalchemy import func, case from sqlalchemy.future import select, Select from lbry.schema.tags import clean_tags -from lbry.schema.result import Censor +from lbry.schema.result import Censor, Outputs as ResultOutput from lbry.schema.url import normalize_name from lbry.blockchain.transaction import Output @@ -219,6 +219,11 @@ def select_claims(cols: List = None, for_count=False, **constraints) -> Select: ) +def protobuf_search_claims(**constraints) -> str: + txos, _, censor = search_claims(**constraints) + return ResultOutput.to_base64(txos, [], blocked=censor) + + def search_claims(**constraints) -> Tuple[List[Output], Optional[int], Optional[Censor]]: total = None if not constraints.pop('no_totals', False): diff --git a/lbry/service/api.py b/lbry/service/api.py index 9a60d0930..479b6d169 100644 --- a/lbry/service/api.py +++ b/lbry/service/api.py @@ -1590,6 +1590,7 @@ class API: # 'height', 'release_time', 'publish_time', 'amount', 'effective_amount', # 'support_amount', 'trending_group', 'trending_mixed', 'trending_local', # 'trending_global', 'activation_height' + protobuf=False, # protobuf encoded result **claim_filter_and_signed_filter_and_stream_filter_and_pagination_kwargs ) -> Paginated[Output]: # search results """ @@ -1611,29 +1612,39 @@ class API: [--reposted_claim_id=] [--reposted=] [--claim_type=] [--order_by=...] [--wallet_id=] [--include_purchase_receipt] [--include_is_my_output] + [--protobuf] {kwargs} """ - wallet = self.wallets.get_or_default(kwargs.pop('wallet_id', None)) - if {'claim_id', 'claim_ids'}.issubset(kwargs): - raise ValueError("Only 'claim_id' or 'claim_ids' is allowed, not both.") - if kwargs.pop('valid_channel_signature', False): - kwargs['signature_valid'] = 1 - if kwargs.pop('invalid_channel_signature', False): - kwargs['signature_valid'] = 0 - page_num, page_size = abs(kwargs.pop('page', 1)), min(abs(kwargs.pop('page_size', DEFAULT_PAGE_SIZE)), 50) - kwargs.update({'offset': page_size * (page_num - 1), 'limit': page_size}) - txos, total, censored = await self.service.search_claims(wallet.accounts, **kwargs) - result = { - "items": txos, + claim_filter_dict, kwargs = pop_kwargs('claim_filter', claim_filter_kwargs( + **claim_filter_and_signed_filter_and_stream_filter_and_pagination_kwargs + )) + pagination, kwargs = pop_kwargs('pagination', pagination_kwargs(**kwargs)) + wallet = self.wallets.get_or_default(wallet_id) +# if {'claim_id', 'claim_ids'}.issubset(kwargs): +# raise ValueError("Only 'claim_id' or 'claim_ids' is allowed, not both.") +# if kwargs.pop('valid_channel_signature', False): +# kwargs['signature_valid'] = 1 +# if kwargs.pop('invalid_channel_signature', False): +# kwargs['signature_valid'] = 0 + page_num = abs(pagination['page'] or 1) + page_size = min(abs(pagination['page_size'] or DEFAULT_PAGE_SIZE), 50) + claim_filter_dict.update({'offset': page_size * (page_num - 1), 'limit': page_size}) + if protobuf: + return await self.service.protobuf_search_claims(**remove_nulls(claim_filter_dict)) + result = await self.service.search_claims( + wallet.accounts, **remove_nulls(claim_filter_dict) + ) + d = { + "items": result.rows, #"blocked": censored, "page": page_num, "page_size": page_size } if not kwargs.pop('no_totals', False): - result['total_pages'] = int((total + (page_size - 1)) / page_size) - result['total_items'] = total - return result + d['total_pages'] = int((result.total + (page_size - 1)) / page_size) + d['total_items'] = result.total + return d CHANNEL_DOC = """ Create, update, abandon and list your channel claims. diff --git a/lbry/service/full_node.py b/lbry/service/full_node.py index 974a8e8d8..a51f20ec0 100644 --- a/lbry/service/full_node.py +++ b/lbry/service/full_node.py @@ -45,15 +45,18 @@ class FullNode(Service): for tx in await self.db.get_transactions(tx_hashes=tx_hashes) } - async def search_claims(self, accounts, **kwargs): - return await self.db.search_claims(**kwargs) - async def broadcast(self, tx): return await self.chain.send_raw_transaction(hexlify(tx.raw).decode()) async def wait(self, tx: Transaction, height=-1, timeout=1): pass + async def search_claims(self, accounts, **kwargs): + return await self.db.search_claims(**kwargs) + + async def protobuf_search_claims(self, **kwargs): + return await self.db.protobuf_search_claims(**kwargs) + async def resolve(self, urls, **kwargs): return await self.db.resolve(urls, **kwargs)