2019-03-24 21:55:04 +01:00
|
|
|
from typing import List
|
|
|
|
|
2019-12-31 20:52:57 +01:00
|
|
|
from lbry.wallet.client.basedatabase import BaseDatabase
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.wallet.transaction import Output
|
2019-11-13 23:50:35 +01:00
|
|
|
from lbry.wallet.constants import TXO_TYPES, CLAIM_TYPES
|
2019-03-24 21:55:04 +01:00
|
|
|
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
class WalletDatabase(BaseDatabase):
|
|
|
|
|
2019-10-29 06:26:25 +01:00
|
|
|
SCHEMA_VERSION = f"{BaseDatabase.SCHEMA_VERSION}+1"
|
|
|
|
|
|
|
|
CREATE_TX_TABLE = """
|
|
|
|
create table if not exists tx (
|
|
|
|
txid text primary key,
|
|
|
|
raw blob not null,
|
|
|
|
height integer not null,
|
|
|
|
position integer not null,
|
|
|
|
is_verified boolean not null default 0,
|
|
|
|
purchased_claim_id text
|
|
|
|
);
|
|
|
|
create index if not exists tx_purchased_claim_id_idx on tx (purchased_claim_id);
|
|
|
|
"""
|
|
|
|
|
2018-07-09 15:55:07 +02:00
|
|
|
CREATE_TXO_TABLE = """
|
|
|
|
create table if not exists txo (
|
2018-07-15 05:02:19 +02:00
|
|
|
txid text references tx,
|
|
|
|
txoid text primary key,
|
|
|
|
address text references pubkey_address,
|
2018-07-09 15:55:07 +02:00
|
|
|
position integer not null,
|
|
|
|
amount integer not null,
|
|
|
|
script blob not null,
|
|
|
|
is_reserved boolean not null default 0,
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2019-09-13 15:16:17 +02:00
|
|
|
txo_type integer not null default 0,
|
2018-07-15 05:02:19 +02:00
|
|
|
claim_id text,
|
2019-09-13 15:16:17 +02:00
|
|
|
claim_name text
|
2018-07-09 15:55:07 +02:00
|
|
|
);
|
2019-10-29 06:26:25 +01:00
|
|
|
create index if not exists txo_txid_idx on txo (txid);
|
2019-09-20 06:05:37 +02:00
|
|
|
create index if not exists txo_address_idx on txo (address);
|
2018-10-10 05:58:32 +02:00
|
|
|
create index if not exists txo_claim_id_idx on txo (claim_id);
|
2019-09-13 15:16:17 +02:00
|
|
|
create index if not exists txo_txo_type_idx on txo (txo_type);
|
2018-07-09 15:55:07 +02:00
|
|
|
"""
|
|
|
|
|
2018-06-12 17:53:29 +02:00
|
|
|
CREATE_TABLES_QUERY = (
|
2019-09-20 06:05:37 +02:00
|
|
|
BaseDatabase.PRAGMAS +
|
|
|
|
BaseDatabase.CREATE_ACCOUNT_TABLE +
|
|
|
|
BaseDatabase.CREATE_PUBKEY_ADDRESS_TABLE +
|
2019-10-29 06:26:25 +01:00
|
|
|
CREATE_TX_TABLE +
|
2019-09-20 06:05:37 +02:00
|
|
|
CREATE_TXO_TABLE +
|
|
|
|
BaseDatabase.CREATE_TXI_TABLE
|
2018-06-12 17:53:29 +02:00
|
|
|
)
|
2018-07-09 15:55:07 +02:00
|
|
|
|
2019-10-29 06:26:25 +01:00
|
|
|
def tx_to_row(self, tx):
|
|
|
|
row = super().tx_to_row(tx)
|
|
|
|
txos = tx.outputs
|
|
|
|
if len(txos) >= 2 and txos[1].can_decode_purchase_data:
|
|
|
|
txos[0].purchase = txos[1]
|
|
|
|
row['purchased_claim_id'] = txos[1].purchase_data.claim_id
|
|
|
|
return row
|
|
|
|
|
2018-07-09 15:55:07 +02:00
|
|
|
def txo_to_row(self, tx, address, txo):
|
2018-07-22 00:34:59 +02:00
|
|
|
row = super().txo_to_row(tx, address, txo)
|
2019-09-13 15:16:17 +02:00
|
|
|
if txo.is_claim:
|
2019-09-13 15:32:34 +02:00
|
|
|
if txo.can_decode_claim:
|
|
|
|
row['txo_type'] = TXO_TYPES.get(txo.claim.claim_type, TXO_TYPES['stream'])
|
|
|
|
else:
|
|
|
|
row['txo_type'] = TXO_TYPES['stream']
|
2019-09-13 15:16:17 +02:00
|
|
|
elif txo.is_support:
|
|
|
|
row['txo_type'] = TXO_TYPES['support']
|
2019-10-29 06:26:25 +01:00
|
|
|
elif txo.purchase is not None:
|
|
|
|
row['txo_type'] = TXO_TYPES['purchase']
|
|
|
|
row['claim_id'] = txo.purchased_claim_id
|
2018-07-09 15:55:07 +02:00
|
|
|
if txo.script.is_claim_involved:
|
2018-08-04 18:10:41 +02:00
|
|
|
row['claim_id'] = txo.claim_id
|
|
|
|
row['claim_name'] = txo.claim_name
|
2018-07-09 15:55:07 +02:00
|
|
|
return row
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2019-10-29 06:26:25 +01:00
|
|
|
async def get_transactions(self, **constraints):
|
|
|
|
txs = await super().get_transactions(**constraints)
|
|
|
|
for tx in txs:
|
|
|
|
txos = tx.outputs
|
|
|
|
if len(txos) >= 2 and txos[1].can_decode_purchase_data:
|
|
|
|
txos[0].purchase = txos[1]
|
|
|
|
return txs
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def constrain_purchases(constraints):
|
|
|
|
accounts = constraints.pop('accounts', None)
|
|
|
|
assert accounts, "'accounts' argument required to find purchases"
|
|
|
|
if not {'purchased_claim_id', 'purchased_claim_id__in'}.intersection(constraints):
|
|
|
|
constraints['purchased_claim_id__is_not_null'] = True
|
|
|
|
constraints.update({
|
|
|
|
f'$account{i}': a.public_key.address for i, a in enumerate(accounts)
|
|
|
|
})
|
|
|
|
account_values = ', '.join([f':$account{i}' for i in range(len(accounts))])
|
|
|
|
constraints['txid__in'] = f"""
|
|
|
|
SELECT txid FROM txi JOIN account_address USING (address)
|
|
|
|
WHERE account_address.account IN ({account_values})
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def get_purchases(self, **constraints):
|
|
|
|
self.constrain_purchases(constraints)
|
|
|
|
return [tx.outputs[0] for tx in await self.get_transactions(**constraints)]
|
|
|
|
|
|
|
|
def get_purchase_count(self, **constraints):
|
|
|
|
self.constrain_purchases(constraints)
|
|
|
|
return self.get_transaction_count(**constraints)
|
|
|
|
|
2019-09-20 06:05:37 +02:00
|
|
|
async def get_txos(self, wallet=None, no_tx=False, **constraints) -> List[Output]:
|
|
|
|
txos = await super().get_txos(wallet=wallet, no_tx=no_tx, **constraints)
|
2018-10-05 15:02:02 +02:00
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
channel_ids = set()
|
2018-10-05 15:02:02 +02:00
|
|
|
for txo in txos:
|
2019-06-07 05:42:31 +02:00
|
|
|
if txo.is_claim and txo.can_decode_claim:
|
2019-03-20 06:46:23 +01:00
|
|
|
if txo.claim.is_signed:
|
2019-03-22 07:18:34 +01:00
|
|
|
channel_ids.add(txo.claim.signing_channel_id)
|
2019-09-20 06:05:37 +02:00
|
|
|
if txo.claim.is_channel and wallet:
|
|
|
|
for account in wallet.accounts:
|
2019-08-12 06:40:05 +02:00
|
|
|
private_key = account.get_channel_private_key(
|
|
|
|
txo.claim.channel.public_key_bytes
|
|
|
|
)
|
|
|
|
if private_key:
|
|
|
|
txo.private_key = private_key
|
|
|
|
break
|
2018-10-05 15:02:02 +02:00
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
if channel_ids:
|
2018-10-05 15:02:02 +02:00
|
|
|
channels = {
|
|
|
|
txo.claim_id: txo for txo in
|
2019-12-10 00:05:47 +01:00
|
|
|
(await self.get_channels(
|
2019-09-20 06:05:37 +02:00
|
|
|
wallet=wallet,
|
2018-10-08 16:41:07 +02:00
|
|
|
claim_id__in=channel_ids
|
2018-10-05 15:02:02 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
for txo in txos:
|
2019-07-09 07:00:05 +02:00
|
|
|
if txo.is_claim and txo.can_decode_claim:
|
2019-03-22 07:18:34 +01:00
|
|
|
txo.channel = channels.get(txo.claim.signing_channel_id, None)
|
2018-10-05 15:02:02 +02:00
|
|
|
|
|
|
|
return txos
|
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
@staticmethod
|
|
|
|
def constrain_claims(constraints):
|
2019-11-18 21:48:52 +01:00
|
|
|
claim_type = constraints.pop('claim_type', None)
|
|
|
|
if claim_type is not None:
|
|
|
|
constraints['txo_type'] = TXO_TYPES[claim_type]
|
|
|
|
else:
|
|
|
|
constraints['txo_type__in'] = CLAIM_TYPES
|
2019-11-12 18:17:35 +01:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
async def get_claims(self, **constraints) -> List[Output]:
|
2018-10-08 16:41:07 +02:00
|
|
|
self.constrain_claims(constraints)
|
2019-03-24 21:55:04 +01:00
|
|
|
return await self.get_utxos(**constraints)
|
2018-09-19 04:23:41 +02:00
|
|
|
|
2018-10-09 20:46:44 +02:00
|
|
|
def get_claim_count(self, **constraints):
|
2018-10-08 16:41:07 +02:00
|
|
|
self.constrain_claims(constraints)
|
|
|
|
return self.get_utxo_count(**constraints)
|
|
|
|
|
2019-03-25 23:30:43 +01:00
|
|
|
@staticmethod
|
|
|
|
def constrain_streams(constraints):
|
2019-09-13 15:16:17 +02:00
|
|
|
constraints['txo_type'] = TXO_TYPES['stream']
|
2019-03-25 23:30:43 +01:00
|
|
|
|
|
|
|
def get_streams(self, **constraints):
|
|
|
|
self.constrain_streams(constraints)
|
|
|
|
return self.get_claims(**constraints)
|
|
|
|
|
|
|
|
def get_stream_count(self, **constraints):
|
|
|
|
self.constrain_streams(constraints)
|
|
|
|
return self.get_claim_count(**constraints)
|
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
@staticmethod
|
|
|
|
def constrain_channels(constraints):
|
2019-09-13 15:16:17 +02:00
|
|
|
constraints['txo_type'] = TXO_TYPES['channel']
|
2018-10-08 16:41:07 +02:00
|
|
|
|
|
|
|
def get_channels(self, **constraints):
|
|
|
|
self.constrain_channels(constraints)
|
2018-10-03 18:00:21 +02:00
|
|
|
return self.get_claims(**constraints)
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-10-09 20:46:44 +02:00
|
|
|
def get_channel_count(self, **constraints):
|
2018-10-08 16:41:07 +02:00
|
|
|
self.constrain_channels(constraints)
|
2018-10-09 20:46:44 +02:00
|
|
|
return self.get_claim_count(**constraints)
|
2018-10-08 16:41:07 +02:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
@staticmethod
|
|
|
|
def constrain_supports(constraints):
|
2019-09-13 15:16:17 +02:00
|
|
|
constraints['txo_type'] = TXO_TYPES['support']
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
def get_supports(self, **constraints):
|
|
|
|
self.constrain_supports(constraints)
|
|
|
|
return self.get_utxos(**constraints)
|
|
|
|
|
|
|
|
def get_support_count(self, **constraints):
|
|
|
|
self.constrain_supports(constraints)
|
2019-11-12 18:17:35 +01:00
|
|
|
return self.get_utxo_count(**constraints)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def constrain_collections(constraints):
|
|
|
|
constraints['txo_type'] = TXO_TYPES['collection']
|
|
|
|
|
|
|
|
def get_collections(self, **constraints):
|
2019-11-13 23:50:35 +01:00
|
|
|
self.constrain_collections(constraints)
|
2019-11-12 18:17:35 +01:00
|
|
|
return self.get_utxos(**constraints)
|
|
|
|
|
|
|
|
def get_collection_count(self, **constraints):
|
2019-11-13 23:50:35 +01:00
|
|
|
self.constrain_collections(constraints)
|
2019-03-24 21:55:04 +01:00
|
|
|
return self.get_utxo_count(**constraints)
|
2019-01-04 08:49:29 +01:00
|
|
|
|
|
|
|
async def release_all_outputs(self, account):
|
2019-09-17 16:26:38 +02:00
|
|
|
await self.db.execute_fetchall(
|
2019-01-04 08:49:29 +01:00
|
|
|
"UPDATE txo SET is_reserved = 0 WHERE"
|
|
|
|
" is_reserved = 1 AND txo.address IN ("
|
2019-09-20 15:25:50 +02:00
|
|
|
" SELECT address from account_address WHERE account = ?"
|
2019-09-17 16:26:38 +02:00
|
|
|
" )", (account.public_key.address, )
|
2019-01-04 08:49:29 +01:00
|
|
|
)
|
2019-07-30 05:34:12 +02:00
|
|
|
|
|
|
|
def get_supports_summary(self, account_id):
|
2019-09-13 15:16:17 +02:00
|
|
|
return self.db.execute_fetchall(f"""
|
2019-07-30 05:34:12 +02:00
|
|
|
select txo.amount, exists(select * from txi where txi.txoid=txo.txoid) as spent,
|
|
|
|
(txo.txid in
|
2019-09-20 15:25:50 +02:00
|
|
|
(select txi.txid from txi join account_address a on txi.address = a.address
|
2019-07-30 05:34:12 +02:00
|
|
|
where a.account = ?)) as from_me,
|
2019-09-20 15:25:50 +02:00
|
|
|
(txo.address in (select address from account_address where account=?)) as to_me,
|
2019-07-30 05:34:12 +02:00
|
|
|
tx.height
|
2019-09-13 15:16:17 +02:00
|
|
|
from txo join tx using (txid) where txo_type={TXO_TYPES['support']}
|
2019-07-30 05:34:12 +02:00
|
|
|
""", (account_id, account_id))
|