2019-04-29 06:39:20 +02:00
|
|
|
import base64
|
|
|
|
import struct
|
2020-01-22 07:55:37 +01:00
|
|
|
from typing import List
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-02-08 00:50:29 +01:00
|
|
|
def set_reference(reference, claim_hash, rows):
|
|
|
|
if claim_hash:
|
|
|
|
for txo in rows:
|
2020-09-11 03:14:31 +02:00
|
|
|
if claim_hash == txo.claim_hash:
|
|
|
|
reference.tx_hash = txo.tx_ref.hash
|
|
|
|
reference.nout = txo.position
|
|
|
|
reference.height = txo.spent_height
|
2020-02-08 00:50:29 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2020-01-10 16:47:57 +01:00
|
|
|
class Censor:
|
|
|
|
|
2020-01-22 07:55:37 +01:00
|
|
|
__slots__ = 'streams', 'channels', 'censored', 'total'
|
|
|
|
|
|
|
|
def __init__(self, streams: dict = None, channels: dict = None):
|
|
|
|
self.streams = streams or {}
|
|
|
|
self.channels = channels or {}
|
|
|
|
self.censored = {}
|
2020-01-10 16:47:57 +01:00
|
|
|
self.total = 0
|
|
|
|
|
|
|
|
def censor(self, row) -> bool:
|
2020-01-22 07:55:37 +01:00
|
|
|
was_censored = False
|
|
|
|
for claim_hash, lookup in (
|
|
|
|
(row['claim_hash'], self.streams),
|
|
|
|
(row['claim_hash'], self.channels),
|
2020-02-22 18:49:20 +01:00
|
|
|
(row['channel_hash'], self.channels),
|
|
|
|
(row['reposted_claim_hash'], self.streams),
|
|
|
|
(row['reposted_claim_hash'], self.channels)):
|
2020-01-22 07:55:37 +01:00
|
|
|
censoring_channel_hash = lookup.get(claim_hash)
|
|
|
|
if censoring_channel_hash:
|
|
|
|
was_censored = True
|
|
|
|
self.censored.setdefault(censoring_channel_hash, 0)
|
|
|
|
self.censored[censoring_channel_hash] += 1
|
|
|
|
break
|
|
|
|
if was_censored:
|
2020-01-10 16:47:57 +01:00
|
|
|
self.total += 1
|
2020-01-22 07:55:37 +01:00
|
|
|
return was_censored
|
2020-01-10 16:47:57 +01:00
|
|
|
|
2020-02-08 00:50:29 +01:00
|
|
|
def to_message(self, outputs: OutputsMessage, extra_txo_rows):
|
2020-01-10 16:47:57 +01:00
|
|
|
outputs.blocked_total = self.total
|
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()
|
|
|
|
blocked.count = count
|
|
|
|
set_reference(blocked.channel, censoring_channel_hash, extra_txo_rows)
|
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,
|
|
|
|
'trending_group': claim.trending_group,
|
|
|
|
'trending_mixed': claim.trending_mixed,
|
|
|
|
'trending_local': claim.trending_local,
|
|
|
|
'trending_global': claim.trending_global,
|
|
|
|
}
|
|
|
|
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
|
2020-04-11 23:27:41 +02:00
|
|
|
txs.add((txo_message.tx_hash, 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
|
|
|
|
)
|
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
|
2020-01-10 16:47:57 +01:00
|
|
|
if blocked is not None:
|
2020-02-08 00:50:29 +01:00
|
|
|
blocked.to_message(page, extra_txo_rows)
|
2019-05-26 05:06:22 +02:00
|
|
|
for row in txo_rows:
|
2019-11-18 21:48:52 +01:00
|
|
|
cls.row_to_message(row, page.txos.add(), extra_txo_rows)
|
2019-05-26 05:06:22 +02:00
|
|
|
for row in extra_txo_rows:
|
2019-11-18 21:48:52 +01:00
|
|
|
cls.row_to_message(row, page.extra_txos.add(), extra_txo_rows)
|
2019-05-26 05:06:22 +02:00
|
|
|
return page.SerializeToString()
|
|
|
|
|
|
|
|
@classmethod
|
2019-11-18 21:48:52 +01:00
|
|
|
def row_to_message(cls, txo, txo_message, extra_txo_rows):
|
2019-05-26 05:06:22 +02:00
|
|
|
if isinstance(txo, Exception):
|
|
|
|
txo_message.error.text = txo.args[0]
|
|
|
|
if isinstance(txo, ValueError):
|
2020-02-01 18:53:39 +01:00
|
|
|
txo_message.error.code = ErrorMessage.INVALID
|
2019-05-26 05:06:22 +02:00
|
|
|
elif isinstance(txo, LookupError):
|
2020-02-01 18:53:39 +01:00
|
|
|
txo_message.error.code = ErrorMessage.NOT_FOUND
|
|
|
|
elif isinstance(txo, ResolveCensoredError):
|
|
|
|
txo_message.error.code = ErrorMessage.BLOCKED
|
2020-02-08 00:50:29 +01:00
|
|
|
set_reference(txo_message.error.blocked.channel, txo.censor_hash, extra_txo_rows)
|
2019-05-26 05:06:22 +02:00
|
|
|
return
|
2020-08-04 16:41:49 +02:00
|
|
|
txo_message.tx_hash = txo.tx_ref.hash
|
|
|
|
txo_message.nout = txo.position
|
|
|
|
txo_message.height = txo.tx_ref.height
|
|
|
|
txo_message.claim.short_url = txo.meta['short_url']
|
|
|
|
txo_message.claim.reposted = txo.meta['reposted_count']
|
|
|
|
if txo.meta['canonical_url'] is not None:
|
|
|
|
txo_message.claim.canonical_url = txo.meta['canonical_url']
|
|
|
|
txo_message.claim.is_controlling = bool(txo.meta['takeover_height'])
|
2020-09-23 05:36:33 +02:00
|
|
|
if txo_message.claim.is_controlling:
|
|
|
|
txo_message.claim.take_over_height = txo.meta['takeover_height']
|
2020-08-04 16:41:49 +02:00
|
|
|
txo_message.claim.creation_height = txo.meta['creation_height']
|
|
|
|
txo_message.claim.activation_height = txo.meta['activation_height']
|
2020-09-23 05:20:08 +02:00
|
|
|
txo_message.claim.expiration_height = txo.meta['expiration_height']
|
2020-08-04 16:41:49 +02:00
|
|
|
if txo.meta['signed_claim_count'] is not None:
|
|
|
|
txo_message.claim.claims_in_channel = txo.meta['signed_claim_count']
|
|
|
|
txo_message.claim.effective_amount = txo.meta['staked_amount']
|
|
|
|
txo_message.claim.support_amount = txo.meta['staked_support_amount']
|
|
|
|
#txo_message.claim.trending_group = txo['trending_group']
|
|
|
|
#txo_message.claim.trending_mixed = txo['trending_mixed']
|
|
|
|
#txo_message.claim.trending_local = txo['trending_local']
|
|
|
|
#txo_message.claim.trending_global = txo['trending_global']
|
2020-09-07 20:51:49 +02:00
|
|
|
if txo.channel:
|
2020-09-11 03:14:31 +02:00
|
|
|
set_reference(txo_message.claim.channel, txo.claim.signing_channel_hash, extra_txo_rows)
|
2020-08-04 16:41:49 +02:00
|
|
|
#set_reference(txo_message.claim.repost, txo['reposted_claim_hash'], extra_txo_rows)
|