2019-04-29 06:39:20 +02:00
|
|
|
import base64
|
2021-07-30 04:01:13 +02:00
|
|
|
from typing import List, TYPE_CHECKING, Union, Optional
|
2019-04-29 06:39:20 +02:00
|
|
|
from binascii import hexlify
|
2019-05-26 05:06:22 +02:00
|
|
|
from itertools import chain
|
2019-04-29 06:39:20 +02:00
|
|
|
|
2020-02-01 18:53:39 +01:00
|
|
|
from lbry.error import ResolveCensoredError
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.schema.types.v2.result_pb2 import Outputs as OutputsMessage
|
2020-02-01 18:53:39 +01:00
|
|
|
from lbry.schema.types.v2.result_pb2 import Error as ErrorMessage
|
2021-02-19 19:19:58 +01:00
|
|
|
if TYPE_CHECKING:
|
2022-01-06 18:28:15 +01:00
|
|
|
from lbry.wallet.server.db.common import ResolveResult
|
2020-02-01 18:53:39 +01:00
|
|
|
|
|
|
|
INVALID = ErrorMessage.Code.Name(ErrorMessage.INVALID)
|
|
|
|
NOT_FOUND = ErrorMessage.Code.Name(ErrorMessage.NOT_FOUND)
|
|
|
|
BLOCKED = ErrorMessage.Code.Name(ErrorMessage.BLOCKED)
|
2019-04-29 06:39:20 +02:00
|
|
|
|
|
|
|
|
2021-02-19 19:19:58 +01:00
|
|
|
def set_reference(reference, claim_hash, rows):
|
|
|
|
if claim_hash:
|
|
|
|
for txo in rows:
|
|
|
|
if claim_hash == txo.claim_hash:
|
|
|
|
reference.tx_hash = txo.tx_hash
|
|
|
|
reference.nout = txo.position
|
|
|
|
reference.height = txo.height
|
|
|
|
return
|
2020-02-08 00:50:29 +01:00
|
|
|
|
|
|
|
|
2020-01-10 16:47:57 +01:00
|
|
|
class Censor:
|
|
|
|
|
2021-03-24 09:35:31 +01:00
|
|
|
NOT_CENSORED = 0
|
2021-01-20 05:20:50 +01:00
|
|
|
SEARCH = 1
|
|
|
|
RESOLVE = 2
|
2020-01-22 07:55:37 +01:00
|
|
|
|
2021-01-20 05:20:50 +01:00
|
|
|
__slots__ = 'censor_type', 'censored'
|
|
|
|
|
|
|
|
def __init__(self, censor_type):
|
|
|
|
self.censor_type = censor_type
|
2020-01-22 07:55:37 +01:00
|
|
|
self.censored = {}
|
2021-01-20 05:20:50 +01:00
|
|
|
|
2021-03-24 09:35:31 +01:00
|
|
|
def is_censored(self, row):
|
|
|
|
return (row.get('censor_type') or self.NOT_CENSORED) >= self.censor_type
|
|
|
|
|
2021-01-20 05:20:50 +01:00
|
|
|
def apply(self, rows):
|
|
|
|
return [row for row in rows if not self.censor(row)]
|
2020-01-10 16:47:57 +01:00
|
|
|
|
2021-07-30 04:01:13 +02:00
|
|
|
def censor(self, row) -> Optional[bytes]:
|
2021-03-24 09:35:31 +01:00
|
|
|
if self.is_censored(row):
|
2021-08-06 20:11:28 +02:00
|
|
|
censoring_channel_hash = bytes.fromhex(row['censoring_channel_id'])[::-1]
|
2021-01-20 05:20:50 +01:00
|
|
|
self.censored.setdefault(censoring_channel_hash, set())
|
|
|
|
self.censored[censoring_channel_hash].add(row['tx_hash'])
|
2021-07-30 04:01:13 +02:00
|
|
|
return censoring_channel_hash
|
|
|
|
return None
|
2020-01-10 16:47:57 +01:00
|
|
|
|
2021-03-02 23:58:18 +01:00
|
|
|
def to_message(self, outputs: OutputsMessage, extra_txo_rows: dict):
|
2020-01-22 07:55:37 +01:00
|
|
|
for censoring_channel_hash, count in self.censored.items():
|
2020-02-08 00:50:29 +01:00
|
|
|
blocked = outputs.blocked.add()
|
2021-01-20 05:20:50 +01:00
|
|
|
blocked.count = len(count)
|
2021-07-30 04:01:13 +02:00
|
|
|
set_reference(blocked.channel, censoring_channel_hash, extra_txo_rows)
|
2021-01-20 05:20:50 +01:00
|
|
|
outputs.blocked_total += len(count)
|
2020-01-10 16:47:57 +01:00
|
|
|
|
|
|
|
|
2019-04-29 06:39:20 +02:00
|
|
|
class Outputs:
|
|
|
|
|
2020-01-10 16:47:57 +01:00
|
|
|
__slots__ = 'txos', 'extra_txos', 'txs', 'offset', 'total', 'blocked', 'blocked_total'
|
2019-04-29 06:39:20 +02:00
|
|
|
|
2020-01-10 16:47:57 +01:00
|
|
|
def __init__(self, txos: List, extra_txos: List, txs: set,
|
|
|
|
offset: int, total: int, blocked: List, blocked_total: int):
|
2019-04-29 06:39:20 +02:00
|
|
|
self.txos = txos
|
|
|
|
self.txs = txs
|
2019-05-26 05:06:22 +02:00
|
|
|
self.extra_txos = extra_txos
|
2019-04-29 06:39:20 +02:00
|
|
|
self.offset = offset
|
|
|
|
self.total = total
|
2020-01-10 16:47:57 +01:00
|
|
|
self.blocked = blocked
|
|
|
|
self.blocked_total = blocked_total
|
2019-04-29 06:39:20 +02:00
|
|
|
|
|
|
|
def inflate(self, txs):
|
2019-05-26 05:06:22 +02:00
|
|
|
tx_map = {tx.hash: tx for tx in txs}
|
|
|
|
for txo_message in self.extra_txos:
|
|
|
|
self.message_to_txo(txo_message, tx_map)
|
2020-01-10 16:47:57 +01:00
|
|
|
txos = [self.message_to_txo(txo_message, tx_map) for txo_message in self.txos]
|
2020-02-08 00:50:29 +01:00
|
|
|
return txos, self.inflate_blocked(tx_map)
|
2019-05-26 05:06:22 +02:00
|
|
|
|
2020-02-08 00:50:29 +01:00
|
|
|
def inflate_blocked(self, tx_map):
|
2020-01-22 07:55:37 +01:00
|
|
|
return {
|
|
|
|
"total": self.blocked_total,
|
2020-02-08 00:50:29 +01:00
|
|
|
"channels": [{
|
|
|
|
'channel': self.message_to_txo(blocked.channel, tx_map),
|
|
|
|
'blocked': blocked.count
|
|
|
|
} for blocked in self.blocked]
|
2020-01-22 07:55:37 +01:00
|
|
|
}
|
2019-04-29 06:39:20 +02:00
|
|
|
|
2020-01-19 02:55:27 +01:00
|
|
|
def message_to_txo(self, txo_message, tx_map):
|
2020-01-10 16:47:57 +01:00
|
|
|
if txo_message.WhichOneof('meta') == 'error':
|
2020-02-08 00:50:29 +01:00
|
|
|
error = {
|
2020-02-01 18:53:39 +01:00
|
|
|
'error': {
|
2020-02-08 00:50:29 +01:00
|
|
|
'name': txo_message.error.Code.Name(txo_message.error.code),
|
2020-02-01 18:53:39 +01:00
|
|
|
'text': txo_message.error.text,
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 00:50:29 +01:00
|
|
|
if error['error']['name'] == BLOCKED:
|
|
|
|
error['error']['censor'] = self.message_to_txo(
|
|
|
|
txo_message.error.blocked.channel, tx_map
|
|
|
|
)
|
|
|
|
return error
|
|
|
|
|
|
|
|
tx = tx_map.get(txo_message.tx_hash)
|
|
|
|
if not tx:
|
|
|
|
return
|
|
|
|
txo = tx.outputs[txo_message.nout]
|
2020-01-10 16:47:57 +01:00
|
|
|
if txo_message.WhichOneof('meta') == 'claim':
|
|
|
|
claim = txo_message.claim
|
|
|
|
txo.meta = {
|
|
|
|
'short_url': f'lbry://{claim.short_url}',
|
|
|
|
'canonical_url': f'lbry://{claim.canonical_url or claim.short_url}',
|
|
|
|
'reposted': claim.reposted,
|
|
|
|
'is_controlling': claim.is_controlling,
|
|
|
|
'take_over_height': claim.take_over_height,
|
|
|
|
'creation_height': claim.creation_height,
|
|
|
|
'activation_height': claim.activation_height,
|
|
|
|
'expiration_height': claim.expiration_height,
|
|
|
|
'effective_amount': claim.effective_amount,
|
|
|
|
'support_amount': claim.support_amount,
|
2021-06-02 17:00:27 +02:00
|
|
|
# 'trending_group': claim.trending_group,
|
|
|
|
# 'trending_mixed': claim.trending_mixed,
|
|
|
|
# 'trending_local': claim.trending_local,
|
|
|
|
# 'trending_global': claim.trending_global,
|
2020-01-10 16:47:57 +01:00
|
|
|
}
|
|
|
|
if claim.HasField('channel'):
|
|
|
|
txo.channel = tx_map[claim.channel.tx_hash].outputs[claim.channel.nout]
|
|
|
|
if claim.HasField('repost'):
|
|
|
|
txo.reposted_claim = tx_map[claim.repost.tx_hash].outputs[claim.repost.nout]
|
|
|
|
try:
|
|
|
|
if txo.claim.is_channel:
|
|
|
|
txo.meta['claims_in_channel'] = claim.claims_in_channel
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return txo
|
|
|
|
|
2019-04-29 06:39:20 +02:00
|
|
|
@classmethod
|
|
|
|
def from_base64(cls, data: str) -> 'Outputs':
|
|
|
|
return cls.from_bytes(base64.b64decode(data))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_bytes(cls, data: bytes) -> 'Outputs':
|
|
|
|
outputs = OutputsMessage()
|
|
|
|
outputs.ParseFromString(data)
|
2019-05-26 05:06:22 +02:00
|
|
|
txs = set()
|
|
|
|
for txo_message in chain(outputs.txos, outputs.extra_txos):
|
2019-04-29 06:39:20 +02:00
|
|
|
if txo_message.WhichOneof('meta') == 'error':
|
|
|
|
continue
|
2019-05-26 05:06:22 +02:00
|
|
|
txs.add((hexlify(txo_message.tx_hash[::-1]).decode(), txo_message.height))
|
2020-01-10 16:47:57 +01:00
|
|
|
return cls(
|
|
|
|
outputs.txos, outputs.extra_txos, txs,
|
|
|
|
outputs.offset, outputs.total,
|
|
|
|
outputs.blocked, outputs.blocked_total
|
2021-06-03 06:29:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_grpc(cls, outputs: OutputsMessage) -> 'Outputs':
|
|
|
|
txs = set()
|
|
|
|
for txo_message in chain(outputs.txos, outputs.extra_txos):
|
|
|
|
if txo_message.WhichOneof('meta') == 'error':
|
|
|
|
continue
|
|
|
|
txs.add((hexlify(txo_message.tx_hash[::-1]).decode(), txo_message.height))
|
|
|
|
return cls(
|
|
|
|
outputs.txos, outputs.extra_txos, txs,
|
|
|
|
outputs.offset, outputs.total,
|
|
|
|
outputs.blocked, outputs.blocked_total
|
2020-01-10 16:47:57 +01:00
|
|
|
)
|
2019-04-29 06:39:20 +02:00
|
|
|
|
|
|
|
@classmethod
|
2020-01-10 16:47:57 +01:00
|
|
|
def to_base64(cls, txo_rows, extra_txo_rows, offset=0, total=None, blocked=None) -> str:
|
|
|
|
return base64.b64encode(cls.to_bytes(txo_rows, extra_txo_rows, offset, total, blocked)).decode()
|
2019-04-29 06:39:20 +02:00
|
|
|
|
|
|
|
@classmethod
|
2020-01-10 16:47:57 +01:00
|
|
|
def to_bytes(cls, txo_rows, extra_txo_rows, offset=0, total=None, blocked: Censor = None) -> bytes:
|
2019-04-29 06:39:20 +02:00
|
|
|
page = OutputsMessage()
|
|
|
|
page.offset = offset
|
2019-07-08 05:08:39 +02:00
|
|
|
if total is not None:
|
|
|
|
page.total = total
|
2021-07-30 04:01:13 +02:00
|
|
|
if blocked is not None:
|
|
|
|
blocked.to_message(page, extra_txo_rows)
|
2021-02-19 19:19:58 +01:00
|
|
|
for row in extra_txo_rows:
|
2021-10-08 22:30:26 +02:00
|
|
|
txo_message: 'OutputsMessage' = page.extra_txos.add()
|
|
|
|
if not isinstance(row, Exception):
|
|
|
|
if row.channel_hash:
|
|
|
|
set_reference(txo_message.claim.channel, row.channel_hash, extra_txo_rows)
|
|
|
|
if row.reposted_claim_hash:
|
|
|
|
set_reference(txo_message.claim.repost, row.reposted_claim_hash, extra_txo_rows)
|
|
|
|
cls.encode_txo(txo_message, row)
|
2021-02-19 19:19:58 +01:00
|
|
|
|
2019-05-26 05:06:22 +02:00
|
|
|
for row in txo_rows:
|
2021-02-19 19:19:58 +01:00
|
|
|
# cls.row_to_message(row, page.txos.add(), extra_txo_rows)
|
|
|
|
txo_message: 'OutputsMessage' = page.txos.add()
|
|
|
|
cls.encode_txo(txo_message, row)
|
|
|
|
if not isinstance(row, Exception):
|
|
|
|
if row.channel_hash:
|
|
|
|
set_reference(txo_message.claim.channel, row.channel_hash, extra_txo_rows)
|
|
|
|
if row.reposted_claim_hash:
|
|
|
|
set_reference(txo_message.claim.repost, row.reposted_claim_hash, extra_txo_rows)
|
2021-07-30 04:01:13 +02:00
|
|
|
elif isinstance(row, ResolveCensoredError):
|
2021-08-06 20:11:28 +02:00
|
|
|
set_reference(txo_message.error.blocked.channel, row.censor_id, extra_txo_rows)
|
2019-05-26 05:06:22 +02:00
|
|
|
return page.SerializeToString()
|
|
|
|
|
|
|
|
@classmethod
|
2021-02-19 19:19:58 +01:00
|
|
|
def encode_txo(cls, txo_message, resolve_result: Union['ResolveResult', Exception]):
|
|
|
|
if isinstance(resolve_result, Exception):
|
|
|
|
txo_message.error.text = resolve_result.args[0]
|
|
|
|
if isinstance(resolve_result, ValueError):
|
2020-02-01 18:53:39 +01:00
|
|
|
txo_message.error.code = ErrorMessage.INVALID
|
2021-02-19 19:19:58 +01:00
|
|
|
elif isinstance(resolve_result, LookupError):
|
2020-02-01 18:53:39 +01:00
|
|
|
txo_message.error.code = ErrorMessage.NOT_FOUND
|
2021-02-19 19:19:58 +01:00
|
|
|
elif isinstance(resolve_result, ResolveCensoredError):
|
2020-02-01 18:53:39 +01:00
|
|
|
txo_message.error.code = ErrorMessage.BLOCKED
|
2019-05-26 05:06:22 +02:00
|
|
|
return
|
2021-02-19 19:19:58 +01:00
|
|
|
txo_message.tx_hash = resolve_result.tx_hash
|
|
|
|
txo_message.nout = resolve_result.position
|
|
|
|
txo_message.height = resolve_result.height
|
|
|
|
txo_message.claim.short_url = resolve_result.short_url
|
2021-06-02 17:00:27 +02:00
|
|
|
txo_message.claim.reposted = resolve_result.reposted
|
2021-02-19 19:19:58 +01:00
|
|
|
txo_message.claim.is_controlling = resolve_result.is_controlling
|
|
|
|
txo_message.claim.creation_height = resolve_result.creation_height
|
|
|
|
txo_message.claim.activation_height = resolve_result.activation_height
|
|
|
|
txo_message.claim.expiration_height = resolve_result.expiration_height
|
|
|
|
txo_message.claim.effective_amount = resolve_result.effective_amount
|
|
|
|
txo_message.claim.support_amount = resolve_result.support_amount
|
|
|
|
|
|
|
|
if resolve_result.canonical_url is not None:
|
|
|
|
txo_message.claim.canonical_url = resolve_result.canonical_url
|
2021-05-05 22:17:32 +02:00
|
|
|
if resolve_result.last_takeover_height is not None:
|
|
|
|
txo_message.claim.take_over_height = resolve_result.last_takeover_height
|
2021-02-19 19:19:58 +01:00
|
|
|
if resolve_result.claims_in_channel is not None:
|
|
|
|
txo_message.claim.claims_in_channel = resolve_result.claims_in_channel
|