more read only calls

This commit is contained in:
Jack Robison 2020-02-25 14:18:09 -05:00
parent 61603ccfce
commit f4645f570c
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
4 changed files with 30 additions and 24 deletions

View file

@ -1492,7 +1492,7 @@ class Daemon(metaclass=JSONRPCServerType):
wallet = self.wallet_manager.get_wallet_or_default(wallet_id) wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
account = wallet.get_account_or_default(account_id) account = wallet.get_account_or_default(account_id)
balance = await account.get_detailed_balance( balance = await account.get_detailed_balance(
confirmations=confirmations, reserved_subtotals=True confirmations=confirmations, reserved_subtotals=True, read_only=True
) )
return dict_values_to_lbc(balance) return dict_values_to_lbc(balance)
@ -1817,7 +1817,7 @@ class Daemon(metaclass=JSONRPCServerType):
""" """
wallet = self.wallet_manager.get_wallet_or_default(wallet_id) wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
account = wallet.get_account_or_default(account_id) account = wallet.get_account_or_default(account_id)
match = await self.ledger.db.get_address(address=address, accounts=[account]) match = await self.ledger.db.get_address(read_only=True, address=address, accounts=[account])
if match is not None: if match is not None:
return True return True
return False return False
@ -1853,7 +1853,7 @@ class Daemon(metaclass=JSONRPCServerType):
return paginate_rows( return paginate_rows(
self.ledger.get_addresses, self.ledger.get_addresses,
self.ledger.get_address_count, self.ledger.get_address_count,
page, page_size, **constraints page, page_size, read_only=True, **constraints
) )
@requires(WALLET_COMPONENT) @requires(WALLET_COMPONENT)
@ -4153,8 +4153,8 @@ class Daemon(metaclass=JSONRPCServerType):
claims = account.get_txos claims = account.get_txos
claim_count = account.get_txo_count claim_count = account.get_txo_count
else: else:
claims = partial(self.ledger.get_txos, wallet=wallet, accounts=wallet.accounts) claims = partial(self.ledger.get_txos, wallet=wallet, accounts=wallet.accounts, read_only=True)
claim_count = partial(self.ledger.get_txo_count, wallet=wallet, accounts=wallet.accounts) claim_count = partial(self.ledger.get_txo_count, wallet=wallet, accounts=wallet.accounts, read_only=True)
constraints = {'resolve': resolve, 'unspent': unspent, 'include_is_received': include_is_received} constraints = {'resolve': resolve, 'unspent': unspent, 'include_is_received': include_is_received}
if is_received is True: if is_received is True:
constraints['is_received'] = True constraints['is_received'] = True

View file

@ -71,6 +71,7 @@ class AddressManager:
def _query_addresses(self, **constraints): def _query_addresses(self, **constraints):
return self.account.ledger.db.get_addresses( return self.account.ledger.db.get_addresses(
read_only=constraints.pop("read_only", False),
accounts=[self.account], accounts=[self.account],
chain=self.chain_number, chain=self.chain_number,
**constraints **constraints
@ -451,13 +452,14 @@ class Account:
def get_public_key(self, chain: int, index: int) -> PubKey: def get_public_key(self, chain: int, index: int) -> PubKey:
return self.address_managers[chain].get_public_key(index) return self.address_managers[chain].get_public_key(index)
def get_balance(self, confirmations: int = 0, include_claims=False, **constraints): def get_balance(self, confirmations: int = 0, include_claims: bool = False,
read_only: bool = False, **constraints):
if not include_claims: if not include_claims:
constraints.update({'txo_type__in': (0, TXO_TYPES['purchase'])}) constraints.update({'txo_type__in': (0, TXO_TYPES['purchase'])})
if confirmations > 0: if confirmations > 0:
height = self.ledger.headers.height - (confirmations-1) height = self.ledger.headers.height - (confirmations-1)
constraints.update({'height__lte': height, 'height__gt': 0}) constraints.update({'height__lte': height, 'height__gt': 0})
return self.ledger.db.get_balance(accounts=[self], **constraints) return self.ledger.db.get_balance(accounts=[self], read_only=read_only, **constraints)
async def get_max_gap(self): async def get_max_gap(self):
change_gap = await self.change.get_max_gap() change_gap = await self.change.get_max_gap()
@ -561,9 +563,10 @@ class Account:
if gap_changed: if gap_changed:
self.wallet.save() self.wallet.save()
async def get_detailed_balance(self, confirmations=0, reserved_subtotals=False): async def get_detailed_balance(self, confirmations=0, reserved_subtotals=False, read_only: bool = False):
tips_balance, supports_balance, claims_balance = 0, 0, 0 tips_balance, supports_balance, claims_balance = 0, 0, 0
get_total_balance = partial(self.get_balance, confirmations=confirmations, include_claims=True) get_total_balance = partial(self.get_balance, read_only=read_only, confirmations=confirmations,
include_claims=True)
total = await get_total_balance() total = await get_total_balance()
if reserved_subtotals: if reserved_subtotals:
claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES) claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES)

View file

@ -671,7 +671,8 @@ class Database(SQLiteMixin):
constraints['is_reserved'] = False constraints['is_reserved'] = False
constraints['txoid__not_in'] = "SELECT txoid FROM txi" constraints['txoid__not_in'] = "SELECT txoid FROM txi"
async def get_txos(self, wallet=None, no_tx=False, unspent=False, include_is_received=False, **constraints): async def get_txos(self, wallet=None, no_tx=False, unspent=False, include_is_received=False,
read_only: bool = False, **constraints):
include_is_received = include_is_received or 'is_received' in constraints include_is_received = include_is_received or 'is_received' in constraints
if unspent: if unspent:
self.constrain_unspent(constraints) self.constrain_unspent(constraints)
@ -687,7 +688,7 @@ class Database(SQLiteMixin):
where account_address.address=txo.address where account_address.address=txo.address
), exists(select 1 from txi where txi.txoid=txo.txoid) ), exists(select 1 from txi where txi.txoid=txo.txoid)
""", """,
wallet=wallet, include_is_received=include_is_received, **constraints wallet=wallet, include_is_received=include_is_received, read_only=read_only, **constraints
) )
txos = [] txos = []
txs = {} txs = {}
@ -736,7 +737,8 @@ class Database(SQLiteMixin):
txo.claim_id: txo for txo in txo.claim_id: txo for txo in
(await self.get_channels( (await self.get_channels(
wallet=wallet, wallet=wallet,
claim_id__in=channel_ids claim_id__in=channel_ids,
read_only=read_only
)) ))
} }
for txo in txos: for txo in txos:
@ -756,18 +758,18 @@ class Database(SQLiteMixin):
count = await self.select_txos('count(*)', **constraints) count = await self.select_txos('count(*)', **constraints)
return count[0][0] return count[0][0]
def get_utxos(self, **constraints): def get_utxos(self, read_only: bool = False, **constraints):
return self.get_txos(unspent=True, **constraints) return self.get_txos(unspent=True, read_only=read_only, **constraints)
def get_utxo_count(self, **constraints): def get_utxo_count(self, **constraints):
return self.get_txo_count(unspent=True, **constraints) return self.get_txo_count(unspent=True, **constraints)
async def get_balance(self, wallet=None, accounts=None, **constraints): async def get_balance(self, wallet=None, accounts=None, read_only: bool = False, **constraints):
assert wallet or accounts, \ assert wallet or accounts, \
"'wallet' or 'accounts' constraints required to calculate balance" "'wallet' or 'accounts' constraints required to calculate balance"
constraints['accounts'] = accounts or wallet.accounts constraints['accounts'] = accounts or wallet.accounts
self.constrain_unspent(constraints) self.constrain_unspent(constraints)
balance = await self.select_txos('SUM(amount)', **constraints) balance = await self.select_txos('SUM(amount)', read_only=read_only, **constraints)
return balance[0][0] or 0 return balance[0][0] or 0
async def select_addresses(self, cols, read_only: bool = False, **constraints): async def select_addresses(self, cols, read_only: bool = False, **constraints):
@ -794,8 +796,8 @@ class Database(SQLiteMixin):
count = await self.select_addresses('count(*)', read_only=read_only, **constraints) count = await self.select_addresses('count(*)', read_only=read_only, **constraints)
return count[0][0] return count[0][0]
async def get_address(self, **constraints): async def get_address(self, read_only: bool = False, **constraints):
addresses = await self.get_addresses(limit=1, **constraints) addresses = await self.get_addresses(read_only=read_only, limit=1, **constraints)
if addresses: if addresses:
return addresses[0] return addresses[0]
@ -859,9 +861,9 @@ class Database(SQLiteMixin):
else: else:
constraints['txo_type__in'] = CLAIM_TYPES constraints['txo_type__in'] = CLAIM_TYPES
async def get_claims(self, **constraints) -> List[Output]: async def get_claims(self, read_only: bool = False, **constraints) -> List[Output]:
self.constrain_claims(constraints) self.constrain_claims(constraints)
return await self.get_utxos(**constraints) return await self.get_utxos(read_only=read_only, **constraints)
def get_claim_count(self, **constraints): def get_claim_count(self, **constraints):
self.constrain_claims(constraints) self.constrain_claims(constraints)
@ -871,9 +873,9 @@ class Database(SQLiteMixin):
def constrain_streams(constraints): def constrain_streams(constraints):
constraints['txo_type'] = TXO_TYPES['stream'] constraints['txo_type'] = TXO_TYPES['stream']
def get_streams(self, **constraints): def get_streams(self, read_only: bool = False, **constraints):
self.constrain_streams(constraints) self.constrain_streams(constraints)
return self.get_claims(**constraints) return self.get_claims(read_only=read_only, **constraints)
def get_stream_count(self, **constraints): def get_stream_count(self, **constraints):
self.constrain_streams(constraints) self.constrain_streams(constraints)

View file

@ -266,9 +266,10 @@ class TestQueries(AsyncioTestCase):
# SQLite is usually compiled with 999 variables limit: https://www.sqlite.org/limits.html # SQLite is usually compiled with 999 variables limit: https://www.sqlite.org/limits.html
# This can be removed when there is a better way. See: https://github.com/lbryio/lbry-sdk/issues/2281 # This can be removed when there is a better way. See: https://github.com/lbryio/lbry-sdk/issues/2281
fetchall = self.ledger.db.db.execute_fetchall fetchall = self.ledger.db.db.execute_fetchall
def check_parameters_length(sql, parameters):
def check_parameters_length(sql, parameters, read_only=False):
self.assertLess(len(parameters or []), 999) self.assertLess(len(parameters or []), 999)
return fetchall(sql, parameters) return fetchall(sql, parameters, read_only)
self.ledger.db.db.execute_fetchall = check_parameters_length self.ledger.db.db.execute_fetchall = check_parameters_length
account = await self.create_account() account = await self.create_account()