2019-03-31 00:40:01 +01:00
|
|
|
import sqlite3
|
2018-11-04 10:42:47 +01:00
|
|
|
import struct
|
2019-03-31 00:40:01 +01:00
|
|
|
from typing import Union, Tuple, Set, List
|
|
|
|
from binascii import unhexlify
|
2018-11-04 10:42:47 +01:00
|
|
|
|
|
|
|
from torba.server.db import DB
|
2019-03-31 00:40:01 +01:00
|
|
|
from torba.server.util import class_logger
|
|
|
|
from torba.client.basedatabase import query, constraints_to_sql
|
|
|
|
|
2019-04-29 06:38:58 +02:00
|
|
|
from lbrynet.schema.url import URL, normalize_name
|
2019-05-25 04:40:39 +02:00
|
|
|
from lbrynet.wallet.ledger import MainNetLedger, RegTestLedger
|
2019-03-31 00:40:01 +01:00
|
|
|
from lbrynet.wallet.transaction import Transaction, Output
|
2019-05-24 05:55:57 +02:00
|
|
|
from lbrynet.wallet.server.canonical import register_canonical_functions
|
2019-05-19 21:57:39 +02:00
|
|
|
from lbrynet.wallet.server.trending import (
|
|
|
|
CREATE_TREND_TABLE, calculate_trending, register_trending_functions
|
|
|
|
)
|
2019-03-31 00:40:01 +01:00
|
|
|
|
|
|
|
|
2019-05-06 23:15:25 +02:00
|
|
|
ATTRIBUTE_ARRAY_MAX_LENGTH = 100
|
|
|
|
|
|
|
|
|
|
|
|
def _apply_constraints_for_array_attributes(constraints, attr):
|
|
|
|
any_items = constraints.pop(f'any_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH]
|
|
|
|
if any_items:
|
|
|
|
constraints.update({
|
|
|
|
f'$any_{attr}{i}': item for i, item in enumerate(any_items)
|
|
|
|
})
|
|
|
|
values = ', '.join(
|
|
|
|
f':$any_{attr}{i}' for i in range(len(any_items))
|
|
|
|
)
|
2019-05-18 05:54:03 +02:00
|
|
|
constraints[f'claim.claim_hash__in#_any_{attr}'] = f"""
|
|
|
|
SELECT DISTINCT claim_hash FROM {attr} WHERE {attr} IN ({values})
|
2019-05-06 23:15:25 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
all_items = constraints.pop(f'all_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH]
|
|
|
|
if all_items:
|
|
|
|
constraints[f'$all_{attr}_count'] = len(all_items)
|
|
|
|
constraints.update({
|
|
|
|
f'$all_{attr}{i}': item for i, item in enumerate(all_items)
|
|
|
|
})
|
|
|
|
values = ', '.join(
|
|
|
|
f':$all_{attr}{i}' for i in range(len(all_items))
|
|
|
|
)
|
2019-05-18 05:54:03 +02:00
|
|
|
constraints[f'claim.claim_hash__in#_all_{attr}'] = f"""
|
|
|
|
SELECT claim_hash FROM {attr} WHERE {attr} IN ({values})
|
|
|
|
GROUP BY claim_hash HAVING COUNT({attr}) = :$all_{attr}_count
|
2019-05-06 23:15:25 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
not_items = constraints.pop(f'not_{attr}s', [])[:ATTRIBUTE_ARRAY_MAX_LENGTH]
|
|
|
|
if not_items:
|
|
|
|
constraints.update({
|
|
|
|
f'$not_{attr}{i}': item for i, item in enumerate(not_items)
|
|
|
|
})
|
|
|
|
values = ', '.join(
|
|
|
|
f':$not_{attr}{i}' for i in range(len(not_items))
|
|
|
|
)
|
2019-05-18 05:54:03 +02:00
|
|
|
constraints[f'claim.claim_hash__not_in#_not_{attr}'] = f"""
|
|
|
|
SELECT DISTINCT claim_hash FROM {attr} WHERE {attr} IN ({values})
|
2019-05-06 23:15:25 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
class SQLDB:
|
|
|
|
|
|
|
|
PRAGMAS = """
|
|
|
|
pragma journal_mode=WAL;
|
|
|
|
"""
|
|
|
|
|
|
|
|
CREATE_CLAIM_TABLE = """
|
|
|
|
create table if not exists claim (
|
|
|
|
claim_hash bytes primary key,
|
2019-05-25 04:40:39 +02:00
|
|
|
claim_id text not null,
|
2019-04-29 06:38:58 +02:00
|
|
|
claim_name text not null,
|
2019-05-24 05:55:57 +02:00
|
|
|
normalized text not null,
|
|
|
|
canonical text not null,
|
2019-03-31 00:40:01 +01:00
|
|
|
txo_hash bytes not null,
|
2019-04-29 06:38:58 +02:00
|
|
|
tx_position integer not null,
|
2019-05-25 04:40:39 +02:00
|
|
|
amount integer not null,
|
|
|
|
is_channel bool not null,
|
|
|
|
public_key_bytes bytes,
|
|
|
|
timestamp integer not null,
|
2019-03-31 00:40:01 +01:00
|
|
|
height integer not null,
|
2019-05-25 04:40:39 +02:00
|
|
|
creation_height integer not null,
|
2019-04-29 06:38:58 +02:00
|
|
|
activation_height integer,
|
2019-05-25 04:40:39 +02:00
|
|
|
expiration_height integer not null,
|
|
|
|
release_time integer not null,
|
|
|
|
|
|
|
|
channel_hash bytes,
|
|
|
|
channel_height integer, -- height at which claim got valid signature
|
|
|
|
channel_canonical text, -- canonical URL \w channel
|
|
|
|
is_channel_signature_valid bool,
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
effective_amount integer not null default 0,
|
2019-05-15 05:59:21 +02:00
|
|
|
support_amount integer not null default 0,
|
2019-05-19 01:54:13 +02:00
|
|
|
trending_group integer not null default 0,
|
|
|
|
trending_mixed integer not null default 0,
|
|
|
|
trending_local integer not null default 0,
|
|
|
|
trending_global integer not null default 0
|
2019-03-31 00:40:01 +01:00
|
|
|
);
|
2019-05-19 01:54:13 +02:00
|
|
|
|
2019-04-29 06:38:58 +02:00
|
|
|
create index if not exists claim_normalized_idx on claim (normalized);
|
2019-03-31 00:40:01 +01:00
|
|
|
create index if not exists claim_txo_hash_idx on claim (txo_hash);
|
|
|
|
create index if not exists claim_channel_hash_idx on claim (channel_hash);
|
2019-05-18 05:54:03 +02:00
|
|
|
create index if not exists claim_release_time_idx on claim (release_time);
|
2019-05-25 04:40:39 +02:00
|
|
|
create index if not exists claim_timestamp_idx on claim (timestamp);
|
2019-05-18 05:54:03 +02:00
|
|
|
create index if not exists claim_height_idx on claim (height);
|
2019-04-29 06:38:58 +02:00
|
|
|
create index if not exists claim_activation_height_idx on claim (activation_height);
|
2019-05-19 01:54:13 +02:00
|
|
|
|
|
|
|
create index if not exists claim_trending_group_idx on claim (trending_group);
|
|
|
|
create index if not exists claim_trending_mixed_idx on claim (trending_mixed);
|
|
|
|
create index if not exists claim_trending_local_idx on claim (trending_local);
|
|
|
|
create index if not exists claim_trending_global_idx on claim (trending_global);
|
|
|
|
"""
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
CREATE_SUPPORT_TABLE = """
|
|
|
|
create table if not exists support (
|
|
|
|
txo_hash bytes primary key,
|
2019-04-29 06:38:58 +02:00
|
|
|
tx_position integer not null,
|
2019-03-31 00:40:01 +01:00
|
|
|
height integer not null,
|
2019-04-29 06:38:58 +02:00
|
|
|
claim_hash bytes not null,
|
|
|
|
amount integer not null
|
2019-03-31 00:40:01 +01:00
|
|
|
);
|
2019-04-29 06:38:58 +02:00
|
|
|
create index if not exists support_txo_hash_idx on support (txo_hash);
|
2019-03-31 00:40:01 +01:00
|
|
|
create index if not exists support_claim_hash_idx on support (claim_hash, height);
|
|
|
|
"""
|
|
|
|
|
|
|
|
CREATE_TAG_TABLE = """
|
|
|
|
create table if not exists tag (
|
|
|
|
tag text not null,
|
2019-05-15 05:59:21 +02:00
|
|
|
claim_hash bytes not null,
|
2019-03-31 00:40:01 +01:00
|
|
|
height integer not null
|
|
|
|
);
|
|
|
|
create index if not exists tag_tag_idx on tag (tag);
|
2019-05-15 05:59:21 +02:00
|
|
|
create index if not exists tag_claim_hash_idx on tag (claim_hash);
|
2019-03-31 00:40:01 +01:00
|
|
|
create index if not exists tag_height_idx on tag (height);
|
|
|
|
"""
|
|
|
|
|
|
|
|
CREATE_CLAIMTRIE_TABLE = """
|
|
|
|
create table if not exists claimtrie (
|
2019-04-29 06:38:58 +02:00
|
|
|
normalized text primary key,
|
2019-03-31 00:40:01 +01:00
|
|
|
claim_hash bytes not null,
|
|
|
|
last_take_over_height integer not null
|
|
|
|
);
|
|
|
|
create index if not exists claimtrie_claim_hash_idx on claimtrie (claim_hash);
|
|
|
|
"""
|
|
|
|
|
|
|
|
CREATE_TABLES_QUERY = (
|
|
|
|
PRAGMAS +
|
|
|
|
CREATE_CLAIM_TABLE +
|
2019-05-19 01:54:13 +02:00
|
|
|
CREATE_TREND_TABLE +
|
2019-03-31 00:40:01 +01:00
|
|
|
CREATE_SUPPORT_TABLE +
|
|
|
|
CREATE_CLAIMTRIE_TABLE +
|
|
|
|
CREATE_TAG_TABLE
|
|
|
|
)
|
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def __init__(self, main, path):
|
|
|
|
self.main = main
|
2019-03-31 00:40:01 +01:00
|
|
|
self._db_path = path
|
|
|
|
self.db = None
|
|
|
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
2019-05-25 04:40:39 +02:00
|
|
|
self.ledger = MainNetLedger if self.main.coin.NET == 'mainnet' else RegTestLedger
|
2019-03-31 00:40:01 +01:00
|
|
|
|
|
|
|
def open(self):
|
|
|
|
self.db = sqlite3.connect(self._db_path, isolation_level=None, check_same_thread=False)
|
|
|
|
self.db.row_factory = sqlite3.Row
|
|
|
|
self.db.executescript(self.CREATE_TABLES_QUERY)
|
2019-05-24 05:55:57 +02:00
|
|
|
register_canonical_functions(self.db)
|
2019-05-19 21:57:39 +02:00
|
|
|
register_trending_functions(self.db)
|
2019-03-31 00:40:01 +01:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _insert_sql(table: str, data: dict) -> Tuple[str, list]:
|
|
|
|
columns, values = [], []
|
|
|
|
for column, value in data.items():
|
|
|
|
columns.append(column)
|
|
|
|
values.append(value)
|
|
|
|
sql = (
|
|
|
|
f"INSERT INTO {table} ({', '.join(columns)}) "
|
|
|
|
f"VALUES ({', '.join(['?'] * len(values))})"
|
|
|
|
)
|
|
|
|
return sql, values
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _update_sql(table: str, data: dict, where: str,
|
|
|
|
constraints: Union[list, tuple]) -> Tuple[str, list]:
|
|
|
|
columns, values = [], []
|
|
|
|
for column, value in data.items():
|
|
|
|
columns.append("{} = ?".format(column))
|
|
|
|
values.append(value)
|
|
|
|
values.extend(constraints)
|
|
|
|
return f"UPDATE {table} SET {', '.join(columns)} WHERE {where}", values
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _delete_sql(table: str, constraints: dict) -> Tuple[str, dict]:
|
|
|
|
where, values = constraints_to_sql(constraints)
|
|
|
|
return f"DELETE FROM {table} WHERE {where}", values
|
|
|
|
|
|
|
|
def execute(self, *args):
|
|
|
|
return self.db.execute(*args)
|
|
|
|
|
|
|
|
def begin(self):
|
|
|
|
self.execute('begin;')
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.execute('commit;')
|
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
def _upsertable_claims(self, txos: Set[Output], header, channels, clear_first=False):
|
2019-05-15 05:59:21 +02:00
|
|
|
claim_hashes, claims, tags = [], [], []
|
2019-03-31 00:40:01 +01:00
|
|
|
for txo in txos:
|
|
|
|
tx = txo.tx_ref.tx
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
try:
|
|
|
|
assert txo.claim_name
|
2019-04-29 06:38:58 +02:00
|
|
|
assert txo.normalized_name
|
2019-05-14 04:46:52 +02:00
|
|
|
except:
|
|
|
|
#self.logger.exception(f"Could not decode claim name for {tx.id}:{txo.position}.")
|
2019-03-31 00:40:01 +01:00
|
|
|
continue
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
claim_hash = sqlite3.Binary(txo.claim_hash)
|
|
|
|
claim_hashes.append(claim_hash)
|
2019-04-29 06:38:58 +02:00
|
|
|
claim_record = {
|
2019-05-15 05:59:21 +02:00
|
|
|
'claim_hash': claim_hash,
|
2019-05-25 04:40:39 +02:00
|
|
|
'claim_id': txo.claim_id,
|
2019-04-29 06:38:58 +02:00
|
|
|
'claim_name': txo.claim_name,
|
2019-05-25 04:40:39 +02:00
|
|
|
'normalized': txo.normalized_name,
|
2019-05-15 05:59:21 +02:00
|
|
|
'txo_hash': sqlite3.Binary(txo.ref.hash),
|
2019-04-29 06:38:58 +02:00
|
|
|
'tx_position': tx.position,
|
|
|
|
'amount': txo.amount,
|
2019-05-25 04:40:39 +02:00
|
|
|
'is_channel': False,
|
|
|
|
'public_key_bytes': None,
|
|
|
|
'timestamp': header['timestamp'],
|
|
|
|
'height': tx.height,
|
|
|
|
'release_time': None,
|
2019-04-29 06:38:58 +02:00
|
|
|
'channel_hash': None,
|
2019-05-25 04:40:39 +02:00
|
|
|
'channel_height': None,
|
|
|
|
'is_channel_signature_valid': None
|
2019-04-29 06:38:58 +02:00
|
|
|
}
|
|
|
|
claims.append(claim_record)
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
try:
|
|
|
|
claim = txo.claim
|
2019-05-14 04:46:52 +02:00
|
|
|
except:
|
|
|
|
#self.logger.exception(f"Could not parse claim protobuf for {tx.id}:{txo.position}.")
|
2019-03-31 00:40:01 +01:00
|
|
|
continue
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
if claim.is_stream:
|
|
|
|
if claim.stream.release_time:
|
|
|
|
claim_record['release_time'] = claim.stream.release_time
|
|
|
|
if claim.signing_channel_hash:
|
|
|
|
claim_record['channel_hash'] = sqlite3.Binary(claim.signing_channel_hash)
|
|
|
|
channel_pub_key = channels.get(claim.signing_channel_hash)
|
|
|
|
if channel_pub_key:
|
|
|
|
claim_record['is_channel_signature_valid'] = txo.is_signed_by(
|
|
|
|
None, ledger=self.ledger, public_key_bytes=channel_pub_key
|
|
|
|
)
|
|
|
|
if claim_record['is_channel_signature_valid']:
|
|
|
|
claim_record['channel_height'] = tx.height
|
|
|
|
elif claim.is_channel:
|
|
|
|
claim_record['is_channel'] = True
|
|
|
|
claim_record['public_key_bytes'] = sqlite3.Binary(claim.channel.public_key_bytes)
|
|
|
|
|
2019-04-29 06:38:58 +02:00
|
|
|
for tag in claim.message.tags:
|
2019-05-15 05:59:21 +02:00
|
|
|
tags.append((tag, claim_hash, tx.height))
|
|
|
|
|
|
|
|
if clear_first:
|
|
|
|
self._clear_claim_metadata(claim_hashes)
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
if tags:
|
|
|
|
self.db.executemany(
|
2019-05-15 05:59:21 +02:00
|
|
|
"INSERT INTO tag (tag, claim_hash, height) VALUES (?, ?, ?)", tags
|
2019-03-31 00:40:01 +01:00
|
|
|
)
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
return claims
|
2018-11-04 10:42:47 +01:00
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
def insert_claims(self, txos: Set[Output], header, channels):
|
|
|
|
claims = self._upsertable_claims(txos, header, channels)
|
2019-03-31 00:40:01 +01:00
|
|
|
if claims:
|
2019-05-18 05:54:03 +02:00
|
|
|
self.db.executemany("""
|
|
|
|
INSERT INTO claim (
|
2019-05-25 04:40:39 +02:00
|
|
|
claim_hash, claim_id, claim_name, normalized, txo_hash, tx_position,
|
|
|
|
amount, is_channel, public_key_bytes, timestamp, height, creation_height,
|
|
|
|
channel_hash, channel_height, is_channel_signature_valid, release_time,
|
|
|
|
activation_height, expiration_height, canonical, channel_canonical)
|
2019-05-18 05:54:03 +02:00
|
|
|
VALUES (
|
2019-05-25 04:40:39 +02:00
|
|
|
:claim_hash, :claim_id, :claim_name, :normalized, :txo_hash, :tx_position,
|
|
|
|
:amount, :is_channel, :public_key_bytes, :timestamp, :height, :height,
|
|
|
|
:channel_hash, :channel_height, :is_channel_signature_valid,
|
|
|
|
CASE WHEN :release_time IS NOT NULL THEN :release_time ELSE :timestamp END,
|
2019-05-24 05:55:57 +02:00
|
|
|
CASE WHEN :normalized NOT IN (SELECT normalized FROM claimtrie) THEN :height END,
|
2019-05-25 04:40:39 +02:00
|
|
|
CASE WHEN :height >= 262974 THEN :height+2102400 ELSE :height+262974 END,
|
|
|
|
:normalized||COALESCE(
|
|
|
|
(SELECT shortest_id(claim_id, :claim_id) FROM claim WHERE normalized = :normalized),
|
|
|
|
'#'||substr(:claim_id, 1, 1)
|
|
|
|
),
|
|
|
|
CASE WHEN :is_channel_signature_valid = 1 THEN
|
|
|
|
(SELECT canonical FROM claim WHERE claim_hash=:channel_hash)||'/'||
|
|
|
|
:normalized||COALESCE(
|
|
|
|
(SELECT shortest_id(claim_id, :claim_id) FROM claim
|
|
|
|
WHERE normalized = :normalized AND
|
|
|
|
channel_hash = :channel_hash AND
|
|
|
|
is_channel_signature_valid = 1),
|
|
|
|
'#'||substr(:claim_id, 1, 1)
|
|
|
|
)
|
2019-05-24 05:55:57 +02:00
|
|
|
END
|
2019-05-18 05:54:03 +02:00
|
|
|
)""", claims)
|
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
def update_claims(self, txos: Set[Output], header, channels):
|
|
|
|
claims = self._upsertable_claims(txos, header, channels, clear_first=True)
|
2019-03-31 00:40:01 +01:00
|
|
|
if claims:
|
2019-05-25 04:40:39 +02:00
|
|
|
self.db.executemany("""
|
|
|
|
UPDATE claim SET
|
|
|
|
txo_hash=:txo_hash, tx_position=:tx_position, height=:height, amount=:amount,
|
|
|
|
public_key_bytes=:public_key_bytes, timestamp=:timestamp,
|
|
|
|
release_time=CASE WHEN :release_time IS NOT NULL THEN :release_time ELSE release_time END,
|
|
|
|
channel_hash=:channel_hash, is_channel_signature_valid=:is_channel_signature_valid,
|
|
|
|
channel_height=CASE
|
|
|
|
WHEN channel_hash = :channel_hash AND :is_channel_signature_valid THEN channel_height
|
|
|
|
WHEN :is_channel_signature_valid THEN :height
|
|
|
|
END,
|
|
|
|
channel_canonical=CASE
|
|
|
|
WHEN channel_hash = :channel_hash AND :is_channel_signature_valid THEN channel_canonical
|
|
|
|
WHEN :is_channel_signature_valid THEN
|
|
|
|
(SELECT canonical FROM claim WHERE claim_hash=:channel_hash)||'/'||
|
|
|
|
:normalized||COALESCE(
|
|
|
|
(SELECT shortest_id(claim_id, :claim_id) FROM claim
|
|
|
|
WHERE normalized = :normalized AND
|
|
|
|
channel_hash = :channel_hash AND
|
|
|
|
is_channel_signature_valid = 1),
|
|
|
|
'#'||substr(:claim_id, 1, 1)
|
|
|
|
)
|
|
|
|
END
|
|
|
|
WHERE claim_hash=:claim_hash;
|
|
|
|
""", claims)
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def delete_claims(self, claim_hashes: Set[bytes]):
|
2019-03-31 00:40:01 +01:00
|
|
|
""" Deletes claim supports and from claimtrie in case of an abandon. """
|
|
|
|
if claim_hashes:
|
|
|
|
binary_claim_hashes = [sqlite3.Binary(claim_hash) for claim_hash in claim_hashes]
|
|
|
|
for table in ('claim', 'support', 'claimtrie'):
|
|
|
|
self.execute(*self._delete_sql(table, {'claim_hash__in': binary_claim_hashes}))
|
2019-05-15 05:59:21 +02:00
|
|
|
self._clear_claim_metadata(binary_claim_hashes)
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def _clear_claim_metadata(self, binary_claim_hashes: List[sqlite3.Binary]):
|
|
|
|
if binary_claim_hashes:
|
|
|
|
for table in ('tag',): # 'language', 'location', etc
|
|
|
|
self.execute(*self._delete_sql(table, {'claim_hash__in': binary_claim_hashes}))
|
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
def invalidate_channel_signatures(self, channels):
|
|
|
|
self.execute(f"""
|
|
|
|
UPDATE claim SET
|
|
|
|
channel_height=NULL, channel_canonical=NULL, is_channel_signature_valid=0
|
|
|
|
WHERE channel_hash IN ({','.join('?' for _ in channels)})
|
|
|
|
""", [sqlite3.Binary(channel) for channel in channels])
|
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def split_inputs_into_claims_supports_and_other(self, txis):
|
2019-05-19 01:54:13 +02:00
|
|
|
txo_hashes = {txi.txo_ref.hash for txi in txis}
|
|
|
|
claims = self.execute(*query(
|
|
|
|
"SELECT txo_hash, claim_hash, normalized FROM claim",
|
|
|
|
txo_hash__in=[sqlite3.Binary(txo_hash) for txo_hash in txo_hashes]
|
|
|
|
)).fetchall()
|
|
|
|
txo_hashes -= {r['txo_hash'] for r in claims}
|
2019-05-15 05:59:21 +02:00
|
|
|
supports = {}
|
|
|
|
if txo_hashes:
|
2019-05-19 01:54:13 +02:00
|
|
|
supports = self.execute(*query(
|
2019-05-15 05:59:21 +02:00
|
|
|
"SELECT txo_hash, claim_hash FROM support",
|
|
|
|
txo_hash__in=[sqlite3.Binary(txo_hash) for txo_hash in txo_hashes]
|
2019-05-19 01:54:13 +02:00
|
|
|
)).fetchall()
|
|
|
|
txo_hashes -= {r['txo_hash'] for r in supports}
|
2019-05-15 05:59:21 +02:00
|
|
|
return claims, supports, txo_hashes
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-05-25 04:40:39 +02:00
|
|
|
def get_channel_public_keys_for_outputs(self, txos):
|
|
|
|
channels = set()
|
|
|
|
for txo in txos:
|
|
|
|
try:
|
|
|
|
channel_hash = txo.claim.signing_channel_hash
|
|
|
|
if channel_hash:
|
|
|
|
channels.add(channel_hash)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return dict(self.execute(*query(
|
|
|
|
"SELECT claim_hash, public_key_bytes FROM claim",
|
|
|
|
claim_hash__in=[sqlite3.Binary(channel) for channel in channels]
|
|
|
|
)).fetchall())
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
def insert_supports(self, txos: Set[Output]):
|
|
|
|
supports = []
|
|
|
|
for txo in txos:
|
|
|
|
tx = txo.tx_ref.tx
|
|
|
|
supports.append((
|
2019-04-29 06:38:58 +02:00
|
|
|
sqlite3.Binary(txo.ref.hash), tx.position, tx.height,
|
|
|
|
sqlite3.Binary(txo.claim_hash), txo.amount
|
2019-03-31 00:40:01 +01:00
|
|
|
))
|
|
|
|
if supports:
|
|
|
|
self.db.executemany(
|
2019-04-29 06:38:58 +02:00
|
|
|
"INSERT INTO support ("
|
|
|
|
" txo_hash, tx_position, height, claim_hash, amount"
|
|
|
|
") "
|
|
|
|
"VALUES (?, ?, ?, ?, ?)", supports
|
2019-03-31 00:40:01 +01:00
|
|
|
)
|
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def delete_supports(self, txo_hashes: Set[bytes]):
|
2019-03-31 00:40:01 +01:00
|
|
|
if txo_hashes:
|
|
|
|
self.execute(*self._delete_sql(
|
|
|
|
'support', {'txo_hash__in': [sqlite3.Binary(txo_hash) for txo_hash in txo_hashes]}
|
|
|
|
))
|
|
|
|
|
2019-05-15 05:59:21 +02:00
|
|
|
def _update_support_amount(self, claim_hashes):
|
|
|
|
if claim_hashes:
|
|
|
|
self.execute(f"""
|
|
|
|
UPDATE claim SET
|
|
|
|
support_amount = COALESCE(
|
|
|
|
(SELECT SUM(amount) FROM support WHERE support.claim_hash=claim.claim_hash), 0
|
|
|
|
)
|
|
|
|
WHERE claim_hash IN ({','.join('?' for _ in claim_hashes)})
|
2019-05-16 07:34:18 +02:00
|
|
|
""", claim_hashes)
|
2019-05-15 05:59:21 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
def _update_effective_amount(self, height, claim_hashes=None):
|
|
|
|
self.execute(
|
|
|
|
f"UPDATE claim SET effective_amount = amount + support_amount "
|
|
|
|
f"WHERE activation_height = {height}"
|
|
|
|
)
|
|
|
|
if claim_hashes:
|
|
|
|
self.execute(
|
|
|
|
f"UPDATE claim SET effective_amount = amount + support_amount "
|
|
|
|
f"WHERE activation_height < {height} "
|
|
|
|
f" AND claim_hash IN ({','.join('?' for _ in claim_hashes)})",
|
|
|
|
claim_hashes
|
|
|
|
)
|
2019-05-16 07:34:18 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
def _calculate_activation_height(self, height):
|
|
|
|
last_take_over_height = f"""COALESCE(
|
|
|
|
(SELECT last_take_over_height FROM claimtrie
|
|
|
|
WHERE claimtrie.normalized=claim.normalized),
|
|
|
|
{height}
|
|
|
|
)
|
|
|
|
"""
|
2019-05-16 07:34:18 +02:00
|
|
|
self.execute(f"""
|
|
|
|
UPDATE claim SET activation_height =
|
2019-05-18 05:54:03 +02:00
|
|
|
{height} + min(4032, cast(({height} - {last_take_over_height}) / 32 AS INT))
|
2019-05-16 07:34:18 +02:00
|
|
|
WHERE activation_height IS NULL
|
|
|
|
""")
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-05-19 01:54:13 +02:00
|
|
|
def _perform_overtake(self, height, changed_claim_hashes, deleted_names):
|
|
|
|
deleted_names_sql = claim_hashes_sql = ""
|
|
|
|
if changed_claim_hashes:
|
|
|
|
claim_hashes_sql = f"OR claim_hash IN ({','.join('?' for _ in changed_claim_hashes)})"
|
|
|
|
if deleted_names:
|
|
|
|
deleted_names_sql = f"OR normalized IN ({','.join('?' for _ in deleted_names)})"
|
2019-05-16 07:34:18 +02:00
|
|
|
overtakes = self.execute(f"""
|
2019-05-20 20:11:53 +02:00
|
|
|
SELECT winner.normalized, winner.claim_hash,
|
|
|
|
claimtrie.claim_hash AS current_winner,
|
|
|
|
MAX(winner.effective_amount)
|
|
|
|
FROM (
|
|
|
|
SELECT normalized, claim_hash, effective_amount FROM claim
|
2019-05-19 01:54:13 +02:00
|
|
|
WHERE normalized IN (
|
2019-05-20 20:11:53 +02:00
|
|
|
SELECT normalized FROM claim WHERE activation_height={height} {claim_hashes_sql}
|
|
|
|
) {deleted_names_sql}
|
|
|
|
ORDER BY effective_amount DESC, height ASC, tx_position ASC
|
2019-05-18 05:54:03 +02:00
|
|
|
) AS winner LEFT JOIN claimtrie USING (normalized)
|
|
|
|
GROUP BY winner.normalized
|
2019-05-19 01:54:13 +02:00
|
|
|
HAVING current_winner IS NULL OR current_winner <> winner.claim_hash
|
|
|
|
""", changed_claim_hashes+deleted_names)
|
2019-05-16 07:34:18 +02:00
|
|
|
for overtake in overtakes:
|
2019-05-18 05:54:03 +02:00
|
|
|
if overtake['current_winner']:
|
|
|
|
self.execute(
|
|
|
|
f"UPDATE claimtrie SET claim_hash = ?, last_take_over_height = {height} "
|
|
|
|
f"WHERE normalized = ?",
|
|
|
|
(sqlite3.Binary(overtake['claim_hash']), overtake['normalized'])
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.execute(
|
|
|
|
f"INSERT INTO claimtrie (claim_hash, normalized, last_take_over_height) "
|
|
|
|
f"VALUES (?, ?, {height})",
|
|
|
|
(sqlite3.Binary(overtake['claim_hash']), overtake['normalized'])
|
|
|
|
)
|
2019-03-31 00:40:01 +01:00
|
|
|
self.execute(
|
2019-05-06 01:16:17 +02:00
|
|
|
f"UPDATE claim SET activation_height = {height} WHERE normalized = ? "
|
|
|
|
f"AND (activation_height IS NULL OR activation_height > {height})",
|
2019-04-29 06:38:58 +02:00
|
|
|
(overtake['normalized'],)
|
2019-03-31 00:40:01 +01:00
|
|
|
)
|
|
|
|
|
2019-05-15 17:37:53 +02:00
|
|
|
def _copy(self, height):
|
|
|
|
if height > 50:
|
|
|
|
self.execute(f"DROP TABLE claimtrie{height-50}")
|
|
|
|
self.execute(f"CREATE TABLE claimtrie{height} AS SELECT * FROM claimtrie")
|
|
|
|
|
2019-05-19 01:54:13 +02:00
|
|
|
def update_claimtrie(self, height, changed_claim_hashes, deleted_names, timer):
|
2019-05-19 21:57:39 +02:00
|
|
|
r = timer.run
|
2019-05-18 05:54:03 +02:00
|
|
|
binary_claim_hashes = [
|
2019-05-19 01:54:13 +02:00
|
|
|
sqlite3.Binary(claim_hash) for claim_hash in changed_claim_hashes
|
2019-05-18 05:54:03 +02:00
|
|
|
]
|
2019-05-19 21:57:39 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
r(self._calculate_activation_height, height)
|
|
|
|
r(self._update_support_amount, binary_claim_hashes)
|
2019-05-16 07:34:18 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
r(self._update_effective_amount, height, binary_claim_hashes)
|
2019-05-19 01:54:13 +02:00
|
|
|
r(self._perform_overtake, height, binary_claim_hashes, list(deleted_names))
|
2019-05-16 07:34:18 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
r(self._update_effective_amount, height)
|
2019-05-19 01:54:13 +02:00
|
|
|
r(self._perform_overtake, height, [], [])
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-05-20 02:22:25 +02:00
|
|
|
def advance_txs(self, height, all_txs, header, daemon_height, timer):
|
2019-05-16 07:34:18 +02:00
|
|
|
insert_claims = set()
|
|
|
|
update_claims = set()
|
2019-05-18 05:54:03 +02:00
|
|
|
delete_claim_hashes = set()
|
2019-05-16 07:34:18 +02:00
|
|
|
insert_supports = set()
|
2019-05-19 01:54:13 +02:00
|
|
|
delete_support_txo_hashes = set()
|
|
|
|
recalculate_claim_hashes = set() # added/deleted supports, added/updated claim
|
|
|
|
deleted_claim_names = set()
|
2019-05-16 07:34:18 +02:00
|
|
|
body_timer = timer.add_timer('body')
|
|
|
|
for position, (etx, txid) in enumerate(all_txs):
|
|
|
|
tx = timer.run(
|
|
|
|
Transaction, etx.serialize(), height=height, position=position
|
|
|
|
)
|
2019-05-19 01:54:13 +02:00
|
|
|
# Inputs
|
2019-05-16 07:34:18 +02:00
|
|
|
spent_claims, spent_supports, spent_other = timer.run(
|
|
|
|
self.split_inputs_into_claims_supports_and_other, tx.inputs
|
|
|
|
)
|
|
|
|
body_timer.start()
|
2019-05-19 01:54:13 +02:00
|
|
|
delete_claim_hashes.update({r['claim_hash'] for r in spent_claims})
|
|
|
|
delete_support_txo_hashes.update({r['txo_hash'] for r in spent_supports})
|
|
|
|
deleted_claim_names.update({r['normalized'] for r in spent_claims})
|
|
|
|
recalculate_claim_hashes.update({r['claim_hash'] for r in spent_supports})
|
|
|
|
# Outputs
|
2019-05-16 07:34:18 +02:00
|
|
|
for output in tx.outputs:
|
|
|
|
if output.is_support:
|
|
|
|
insert_supports.add(output)
|
2019-05-19 01:54:13 +02:00
|
|
|
recalculate_claim_hashes.add(output.claim_hash)
|
2019-05-16 07:34:18 +02:00
|
|
|
elif output.script.is_claim_name:
|
|
|
|
insert_claims.add(output)
|
2019-05-19 01:54:13 +02:00
|
|
|
recalculate_claim_hashes.add(output.claim_hash)
|
2019-05-16 07:34:18 +02:00
|
|
|
elif output.script.is_update_claim:
|
|
|
|
claim_hash = output.claim_hash
|
2019-05-18 05:54:03 +02:00
|
|
|
if claim_hash in delete_claim_hashes:
|
|
|
|
delete_claim_hashes.remove(claim_hash)
|
2019-05-16 07:34:18 +02:00
|
|
|
update_claims.add(output)
|
2019-05-19 01:54:13 +02:00
|
|
|
recalculate_claim_hashes.add(output.claim_hash)
|
2019-05-16 07:34:18 +02:00
|
|
|
body_timer.stop()
|
2019-05-25 04:40:39 +02:00
|
|
|
channel_public_keys = self.get_channel_public_keys_for_outputs(insert_claims | update_claims)
|
2019-05-16 07:34:18 +02:00
|
|
|
r = timer.run
|
2019-05-18 05:54:03 +02:00
|
|
|
r(self.delete_claims, delete_claim_hashes)
|
2019-05-19 01:54:13 +02:00
|
|
|
r(self.delete_supports, delete_support_txo_hashes)
|
2019-05-25 04:40:39 +02:00
|
|
|
r(self.invalidate_channel_signatures, recalculate_claim_hashes)
|
|
|
|
r(self.insert_claims, insert_claims, header, channel_public_keys)
|
|
|
|
r(self.update_claims, update_claims, header, channel_public_keys)
|
2019-05-16 07:34:18 +02:00
|
|
|
r(self.insert_supports, insert_supports)
|
2019-05-19 01:54:13 +02:00
|
|
|
r(self.update_claimtrie, height, recalculate_claim_hashes, deleted_claim_names, forward_timer=True)
|
2019-05-20 02:22:25 +02:00
|
|
|
r(calculate_trending, self.db, height, self.main.first_sync, daemon_height)
|
2019-05-16 07:34:18 +02:00
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
def get_claims(self, cols, **constraints):
|
2019-05-18 05:54:03 +02:00
|
|
|
if 'order_by' in constraints:
|
|
|
|
sql_order_by = []
|
|
|
|
for order_by in constraints['order_by']:
|
|
|
|
is_asc = order_by.startswith('^')
|
|
|
|
column = order_by[1:] if is_asc else order_by
|
|
|
|
if column not in self.ORDER_FIELDS:
|
|
|
|
raise NameError(f'{column} is not a valid order_by field')
|
2019-05-18 06:10:24 +02:00
|
|
|
if column == 'name':
|
2019-05-18 05:54:03 +02:00
|
|
|
column = 'normalized'
|
|
|
|
sql_order_by.append(
|
|
|
|
f"claim.{column} ASC" if is_asc else f"claim.{column} DESC"
|
|
|
|
)
|
|
|
|
constraints['order_by'] = sql_order_by
|
|
|
|
|
|
|
|
ops = {'<=': '__lte', '>=': '__gte', '<': '__lt', '>': '__gt'}
|
|
|
|
for constraint in self.INTEGER_PARAMS:
|
|
|
|
if constraint in constraints:
|
|
|
|
value = constraints.pop(constraint)
|
|
|
|
postfix = ''
|
|
|
|
if isinstance(value, str):
|
|
|
|
if len(value) >= 2 and value[:2] in ops:
|
|
|
|
postfix, value = ops[value[:2]], int(value[2:])
|
|
|
|
elif len(value) >= 1 and value[0] in ops:
|
|
|
|
postfix, value = ops[value[0]], int(value[1:])
|
|
|
|
constraints[f'claim.{constraint}{postfix}'] = value
|
|
|
|
|
2019-05-21 00:18:27 +02:00
|
|
|
if constraints.pop('is_controlling', False):
|
2019-05-06 01:16:17 +02:00
|
|
|
if {'sequence', 'amount_order'}.isdisjoint(constraints):
|
|
|
|
constraints['claimtrie.claim_hash__is_not_null'] = ''
|
|
|
|
if 'sequence' in constraints:
|
|
|
|
constraints['order_by'] = 'claim.activation_height ASC'
|
|
|
|
constraints['offset'] = int(constraints.pop('sequence')) - 1
|
|
|
|
constraints['limit'] = 1
|
|
|
|
if 'amount_order' in constraints:
|
|
|
|
constraints['order_by'] = 'claim.effective_amount DESC'
|
|
|
|
constraints['offset'] = int(constraints.pop('amount_order')) - 1
|
|
|
|
constraints['limit'] = 1
|
2019-04-29 06:38:58 +02:00
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
if 'claim_id' in constraints:
|
|
|
|
constraints['claim.claim_hash'] = sqlite3.Binary(
|
|
|
|
unhexlify(constraints.pop('claim_id'))[::-1]
|
|
|
|
)
|
2019-04-29 06:38:58 +02:00
|
|
|
if 'name' in constraints:
|
|
|
|
constraints['claim.normalized'] = normalize_name(constraints.pop('name'))
|
|
|
|
|
|
|
|
if 'channel' in constraints:
|
|
|
|
url = URL.parse(constraints.pop('channel'))
|
|
|
|
if url.channel.claim_id:
|
|
|
|
constraints['channel_id'] = url.channel.claim_id
|
|
|
|
else:
|
|
|
|
constraints['channel_name'] = url.channel.name
|
2019-03-31 00:40:01 +01:00
|
|
|
if 'channel_id' in constraints:
|
2019-04-29 06:38:58 +02:00
|
|
|
constraints['channel_hash'] = unhexlify(constraints.pop('channel_id'))[::-1]
|
|
|
|
if 'channel_hash' in constraints:
|
|
|
|
constraints['channel.claim_hash'] = sqlite3.Binary(constraints.pop('channel_hash'))
|
|
|
|
if 'channel_name' in constraints:
|
|
|
|
constraints['channel.normalized'] = normalize_name(constraints.pop('channel_name'))
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
if 'txid' in constraints:
|
|
|
|
tx_hash = unhexlify(constraints.pop('txid'))[::-1]
|
2019-04-29 06:38:58 +02:00
|
|
|
nout = constraints.pop('nout', 0)
|
|
|
|
constraints['claim.txo_hash'] = sqlite3.Binary(
|
|
|
|
tx_hash + struct.pack('<I', nout)
|
|
|
|
)
|
2019-05-06 04:25:43 +02:00
|
|
|
|
2019-05-06 23:15:25 +02:00
|
|
|
_apply_constraints_for_array_attributes(constraints, 'tag')
|
|
|
|
_apply_constraints_for_array_attributes(constraints, 'language')
|
|
|
|
_apply_constraints_for_array_attributes(constraints, 'location')
|
2019-05-06 22:37:17 +02:00
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
try:
|
|
|
|
return self.db.execute(*query(
|
|
|
|
f"""
|
|
|
|
SELECT {cols} FROM claim
|
|
|
|
LEFT JOIN claimtrie USING (claim_hash)
|
|
|
|
LEFT JOIN claim as channel ON (claim.channel_hash=channel.claim_hash)
|
|
|
|
""", **constraints
|
|
|
|
)).fetchall()
|
|
|
|
except:
|
|
|
|
self.logger.exception('Failed to execute claim search query:')
|
|
|
|
print(query(
|
|
|
|
f"""
|
2019-03-31 00:40:01 +01:00
|
|
|
SELECT {cols} FROM claim
|
|
|
|
LEFT JOIN claimtrie USING (claim_hash)
|
|
|
|
LEFT JOIN claim as channel ON (claim.channel_hash=channel.claim_hash)
|
|
|
|
""", **constraints
|
2019-05-18 05:54:03 +02:00
|
|
|
))
|
|
|
|
raise
|
2019-03-31 00:40:01 +01:00
|
|
|
|
|
|
|
def get_claims_count(self, **constraints):
|
|
|
|
constraints.pop('offset', None)
|
|
|
|
constraints.pop('limit', None)
|
|
|
|
constraints.pop('order_by', None)
|
|
|
|
count = self.get_claims('count(*)', **constraints)
|
|
|
|
return count[0][0]
|
|
|
|
|
2019-04-29 06:38:58 +02:00
|
|
|
def _search(self, **constraints):
|
|
|
|
return self.get_claims(
|
|
|
|
"""
|
|
|
|
claimtrie.claim_hash as is_controlling,
|
2019-05-25 04:40:39 +02:00
|
|
|
claim.claim_hash, claim.txo_hash, claim.height,
|
2019-05-18 05:54:03 +02:00
|
|
|
claim.activation_height, claim.effective_amount, claim.support_amount,
|
2019-05-19 01:54:13 +02:00
|
|
|
claim.trending_group, claim.trending_mixed,
|
|
|
|
claim.trending_local, claim.trending_global,
|
2019-05-25 04:40:39 +02:00
|
|
|
CASE WHEN claim.is_channel_signature_valid = 1
|
|
|
|
THEN claim.channel_canonical
|
|
|
|
ELSE claim.canonical
|
|
|
|
END AS canonical,
|
2019-04-29 06:38:58 +02:00
|
|
|
CASE WHEN claim.is_channel=1 THEN (
|
|
|
|
SELECT COUNT(*) FROM claim as claim_in_channel
|
|
|
|
WHERE claim_in_channel.channel_hash=claim.claim_hash
|
2019-05-18 05:54:03 +02:00
|
|
|
) ELSE 0 END AS claims_in_channel,
|
2019-05-25 04:40:39 +02:00
|
|
|
channel.txo_hash AS channel_txo_hash, channel.height AS channel_height
|
2019-04-29 06:38:58 +02:00
|
|
|
""", **constraints
|
|
|
|
)
|
|
|
|
|
2019-05-18 05:54:03 +02:00
|
|
|
INTEGER_PARAMS = {
|
2019-05-19 01:54:13 +02:00
|
|
|
'height', 'activation_height', 'release_time', 'publish_time',
|
2019-05-18 05:54:03 +02:00
|
|
|
'amount', 'effective_amount', 'support_amount',
|
2019-05-19 01:54:13 +02:00
|
|
|
'trending_group', 'trending_mixed',
|
|
|
|
'trending_local', 'trending_global',
|
2019-05-18 05:54:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 00:40:01 +01:00
|
|
|
SEARCH_PARAMS = {
|
2019-04-29 06:38:58 +02:00
|
|
|
'name', 'claim_id', 'txid', 'nout',
|
|
|
|
'channel', 'channel_id', 'channel_name',
|
2019-05-06 22:37:17 +02:00
|
|
|
'any_tags', 'all_tags', 'not_tags',
|
|
|
|
'any_locations', 'all_locations', 'not_locations',
|
|
|
|
'any_languages', 'all_languages', 'not_languages',
|
2019-05-18 05:54:03 +02:00
|
|
|
'is_controlling', 'limit', 'offset', 'order_by'
|
|
|
|
} | INTEGER_PARAMS
|
|
|
|
|
|
|
|
ORDER_FIELDS = {
|
|
|
|
'name',
|
|
|
|
} | INTEGER_PARAMS
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2019-04-29 06:38:58 +02:00
|
|
|
def search(self, constraints) -> Tuple[List, int, int]:
|
2019-03-31 00:40:01 +01:00
|
|
|
assert set(constraints).issubset(self.SEARCH_PARAMS), \
|
|
|
|
f"Search query contains invalid arguments: {set(constraints).difference(self.SEARCH_PARAMS)}"
|
|
|
|
total = self.get_claims_count(**constraints)
|
|
|
|
constraints['offset'] = abs(constraints.get('offset', 0))
|
|
|
|
constraints['limit'] = min(abs(constraints.get('limit', 10)), 50)
|
2019-05-18 05:54:03 +02:00
|
|
|
if 'order_by' not in constraints:
|
|
|
|
constraints['order_by'] = ["height", "^name"]
|
2019-04-29 06:38:58 +02:00
|
|
|
txo_rows = self._search(**constraints)
|
|
|
|
return txo_rows, constraints['offset'], total
|
|
|
|
|
|
|
|
def resolve(self, urls) -> List:
|
|
|
|
result = []
|
|
|
|
for raw_url in urls:
|
|
|
|
try:
|
|
|
|
url = URL.parse(raw_url)
|
|
|
|
except ValueError as e:
|
|
|
|
result.append(e)
|
|
|
|
continue
|
|
|
|
channel = None
|
|
|
|
if url.has_channel:
|
2019-05-24 05:55:57 +02:00
|
|
|
query = url.channel.to_dict()
|
|
|
|
if set(query) == {'name'}:
|
|
|
|
query['is_controlling'] = True
|
|
|
|
matches = self._search(**query)
|
2019-04-29 06:38:58 +02:00
|
|
|
if matches:
|
|
|
|
channel = matches[0]
|
|
|
|
else:
|
|
|
|
result.append(LookupError(f'Could not find channel in "{raw_url}".'))
|
|
|
|
continue
|
|
|
|
if url.has_stream:
|
|
|
|
query = url.stream.to_dict()
|
|
|
|
if channel is not None:
|
|
|
|
query['channel_hash'] = channel['claim_hash']
|
2019-05-24 05:55:57 +02:00
|
|
|
if set(query) == {'name'}:
|
|
|
|
query['is_controlling'] = True
|
|
|
|
matches = self._search(**query)
|
2019-04-29 06:38:58 +02:00
|
|
|
if matches:
|
|
|
|
result.append(matches[0])
|
|
|
|
else:
|
|
|
|
result.append(LookupError(f'Could not find stream in "{raw_url}".'))
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
result.append(channel)
|
|
|
|
return result
|
2019-03-31 00:40:01 +01:00
|
|
|
|
2018-11-04 10:42:47 +01:00
|
|
|
|
|
|
|
class LBRYDB(DB):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-05-15 05:59:21 +02:00
|
|
|
self.sql = SQLDB(self, 'claims.db')
|
2018-11-04 10:42:47 +01:00
|
|
|
|
2018-12-15 21:31:02 +01:00
|
|
|
def close(self):
|
|
|
|
super().close()
|
2019-03-31 00:40:01 +01:00
|
|
|
self.sql.close()
|
2018-11-04 10:42:47 +01:00
|
|
|
|
2019-04-27 20:27:18 +02:00
|
|
|
async def _open_dbs(self, *args, **kwargs):
|
|
|
|
await super()._open_dbs(*args, **kwargs)
|
2019-03-31 00:40:01 +01:00
|
|
|
self.sql.open()
|