diff --git a/lbry/schema/result.py b/lbry/schema/result.py index 13d683b56..3469a9c4c 100644 --- a/lbry/schema/result.py +++ b/lbry/schema/result.py @@ -13,6 +13,16 @@ NOT_FOUND = ErrorMessage.Code.Name(ErrorMessage.NOT_FOUND) BLOCKED = ErrorMessage.Code.Name(ErrorMessage.BLOCKED) +def set_reference(reference, claim_hash, rows): + if claim_hash: + for txo in rows: + if claim_hash == txo['claim_hash']: + reference.tx_hash = txo['txo_hash'][:32] + reference.nout = struct.unpack(' List: ) -def _get_referenced_rows(censor: Censor, txo_rows: List[dict]): +def _get_referenced_rows(txo_rows: List[dict], censor_channels: List[bytes]): + censor = ctx.get().get_resolve_censor() repost_hashes = set(filter(None, map(itemgetter('reposted_claim_hash'), txo_rows))) - channel_hashes = set(filter(None, map(itemgetter('channel_hash'), txo_rows))) + channel_hashes = set(chain( + filter(None, map(itemgetter('channel_hash'), txo_rows)), + censor_channels + )) reposted_txos = [] if repost_hashes: @@ -418,7 +423,7 @@ def search(constraints) -> Tuple[List, List, int, int, Censor]: context = ctx.get() search_censor = context.get_search_censor() txo_rows = search_claims(search_censor, **constraints) - extra_txo_rows = _get_referenced_rows(context.get_resolve_censor(), txo_rows) + extra_txo_rows = _get_referenced_rows(txo_rows, search_censor.censored.keys()) return txo_rows, extra_txo_rows, constraints['offset'], total, search_censor @@ -426,7 +431,8 @@ def search(constraints) -> Tuple[List, List, int, int, Censor]: def resolve(urls) -> Tuple[List, List]: txo_rows = [resolve_url(raw_url) for raw_url in urls] extra_txo_rows = _get_referenced_rows( - ctx.get().get_resolve_censor(), [r for r in txo_rows if isinstance(r, dict)] + [txo for txo in txo_rows if isinstance(txo, dict)], + [txo.censor_hash for txo in txo_rows if isinstance(txo, ResolveCensoredError)] ) return txo_rows, extra_txo_rows @@ -452,7 +458,7 @@ def resolve_url(raw_url): if matches: channel = matches[0] elif censor.censored: - return ResolveCensoredError(raw_url, hexlify(next(iter(censor.censored))[::-1]).decode()) + return ResolveCensoredError(raw_url, next(iter(censor.censored))) else: return LookupError(f'Could not find channel in "{raw_url}".') @@ -472,7 +478,7 @@ def resolve_url(raw_url): if matches: return matches[0] elif censor.censored: - return ResolveCensoredError(raw_url, hexlify(next(iter(censor.censored))[::-1]).decode()) + return ResolveCensoredError(raw_url, next(iter(censor.censored))) else: return LookupError(f'Could not find claim at "{raw_url}".') diff --git a/lbry/wallet/server/db/writer.py b/lbry/wallet/server/db/writer.py index fbccb06ee..db2cda525 100644 --- a/lbry/wallet/server/db/writer.py +++ b/lbry/wallet/server/db/writer.py @@ -241,10 +241,10 @@ class SQLDB: streams, channels = {}, {} if channel_hashes: sql = query( - "SELECT claim.channel_hash, claim.reposted_claim_hash, reposted.claim_type " - "FROM claim JOIN claim AS reposted ON (reposted.claim_hash=claim.reposted_claim_hash)", **{ - 'claim.reposted_claim_hash__is_not_null': 1, - 'claim.channel_hash__in': channel_hashes + "SELECT repost.channel_hash, repost.reposted_claim_hash, target.claim_type " + "FROM claim as repost JOIN claim AS target ON (target.claim_hash=repost.reposted_claim_hash)", **{ + 'repost.reposted_claim_hash__is_not_null': 1, + 'repost.channel_hash__in': channel_hashes } ) for blocked_claim in self.execute(*sql): diff --git a/tests/integration/blockchain/test_claim_commands.py b/tests/integration/blockchain/test_claim_commands.py index cd8593307..24d605dee 100644 --- a/tests/integration/blockchain/test_claim_commands.py +++ b/tests/integration/blockchain/test_claim_commands.py @@ -405,10 +405,10 @@ class ClaimCommands(ClaimTestCase): await self.ledger.wait(channel_tx) r = await self.claim_list(resolve=True) - self.assertEqual('not_found', r[0]['meta']['error']['name']) + self.assertEqual('NOT_FOUND', r[0]['meta']['error']['name']) self.assertTrue(r[1]['meta']['is_controlling']) r = await self.channel_list(resolve=True) - self.assertEqual('not_found', r[0]['meta']['error']['name']) + self.assertEqual('NOT_FOUND', r[0]['meta']['error']['name']) self.assertTrue(r[1]['meta']['is_controlling']) # confirm it @@ -430,10 +430,10 @@ class ClaimCommands(ClaimTestCase): await self.ledger.wait(stream_tx) r = await self.claim_list(resolve=True) - self.assertEqual('not_found', r[0]['meta']['error']['name']) + self.assertEqual('NOT_FOUND', r[0]['meta']['error']['name']) self.assertTrue(r[1]['meta']['is_controlling']) r = await self.stream_list(resolve=True) - self.assertEqual('not_found', r[0]['meta']['error']['name']) + self.assertEqual('NOT_FOUND', r[0]['meta']['error']['name']) self.assertTrue(r[1]['meta']['is_controlling']) # confirm it @@ -845,18 +845,26 @@ class StreamCommands(ClaimTestCase): # search for blocked content directly result = await self.out(self.daemon.jsonrpc_claim_search(name='bad_content')) + blocked = result['blocked'] self.assertEqual([], result['items']) - self.assertEqual({"channels": {filtering_channel_id: 1}, "total": 1}, result['blocked']) + self.assertEqual(1, blocked['total']) + self.assertEqual(1, len(blocked['channels'])) + self.assertEqual(1, blocked['channels'][0]['blocked']) + self.assertTrue(blocked['channels'][0]['channel']['short_url'].startswith('lbry://@filtering#')) # search channel containing blocked content result = await self.out(self.daemon.jsonrpc_claim_search(channel='@some_channel')) + blocked = result['blocked'] self.assertEqual(1, len(result['items'])) - self.assertEqual({"channels": {filtering_channel_id: 1}, "total": 1}, result['blocked']) + self.assertEqual(1, blocked['total']) + self.assertEqual(1, len(blocked['channels'])) + self.assertEqual(1, blocked['channels'][0]['blocked']) + self.assertTrue(blocked['channels'][0]['channel']['short_url'].startswith('lbry://@filtering#')) # content was filtered by not_tag before censoring result = await self.out(self.daemon.jsonrpc_claim_search(channel='@some_channel', not_tags=["good", "bad"])) self.assertEqual(0, len(result['items'])) - self.assertEqual({"channels": {}, "total": 0}, result['blocked']) + self.assertEqual({"channels": [], "total": 0}, result['blocked']) blocking_channel_id = self.get_claim_id( await self.channel_create('@blocking', '1.0') @@ -874,8 +882,9 @@ class StreamCommands(ClaimTestCase): # blocked content is not resolveable result = await self.out(self.daemon.jsonrpc_resolve('lbry://@some_channel/bad_content')) error = result['lbry://@some_channel/bad_content']['error'] - self.assertTrue(error['name'], 'blocked') + self.assertEqual(error['name'], 'BLOCKED') self.assertTrue(error['text'].startswith("Resolve of 'lbry://@some_channel/bad_content' was censored")) + self.assertTrue(error['censor']['short_url'].startswith('lbry://@blocking#')) async def test_publish_updates_file_list(self): tx = await self.stream_create(title='created') diff --git a/tests/integration/blockchain/test_resolve_command.py b/tests/integration/blockchain/test_resolve_command.py index 04e2b2528..1809803d7 100644 --- a/tests/integration/blockchain/test_resolve_command.py +++ b/tests/integration/blockchain/test_resolve_command.py @@ -15,7 +15,7 @@ class BaseResolveTestCase(CommandTestCase): other = (await self.resolve(name))[name] if claim_id is None: self.assertIn('error', other) - self.assertEqual(other['error']['name'], 'not_found') + self.assertEqual(other['error']['name'], 'NOT_FOUND') else: self.assertEqual(claim_id, other['claim_id']) @@ -186,7 +186,7 @@ class ResolveCommand(BaseResolveTestCase): self.assertEqual(response, { 'lbry://@abc/on-channel-claim': { 'error': { - 'name': 'not_found', + 'name': 'NOT_FOUND', 'text': 'Could not find claim at "lbry://@abc/on-channel-claim".', } } @@ -265,7 +265,7 @@ class ResolveCommand(BaseResolveTestCase): self.assertEqual(response, { '@olds/bad_example': { 'error': { - 'name': 'not_found', + 'name': 'NOT_FOUND', 'text': 'Could not find claim at "@olds/bad_example".', } } diff --git a/tests/integration/other/test_chris45.py b/tests/integration/other/test_chris45.py index e71322435..319a0dc21 100644 --- a/tests/integration/other/test_chris45.py +++ b/tests/integration/other/test_chris45.py @@ -88,7 +88,7 @@ class EpicAdventuresOfChris45(CommandTestCase): self.assertEqual( response['lbry://@spam/hovercraft'], {'error': { - 'name': 'not_found', + 'name': 'NOT_FOUND', 'text': 'Could not find claim at "lbry://@spam/hovercraft".' }} ) @@ -192,7 +192,7 @@ class EpicAdventuresOfChris45(CommandTestCase): self.assertEqual( response[uri], {'error': { - 'name': 'not_found', + 'name': 'NOT_FOUND', 'text': f'Could not find claim at "{uri}".' }} )