diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index daadfdb92..430cb7f06 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -1492,7 +1492,7 @@ class Daemon(metaclass=JSONRPCServerType): wallet = self.wallet_manager.get_wallet_or_default(wallet_id) account = wallet.get_account_or_default(account_id) 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) @@ -1817,7 +1817,7 @@ class Daemon(metaclass=JSONRPCServerType): """ wallet = self.wallet_manager.get_wallet_or_default(wallet_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: return True return False @@ -1853,7 +1853,7 @@ class Daemon(metaclass=JSONRPCServerType): return paginate_rows( self.ledger.get_addresses, self.ledger.get_address_count, - page, page_size, **constraints + page, page_size, read_only=True, **constraints ) @requires(WALLET_COMPONENT) @@ -4153,8 +4153,8 @@ class Daemon(metaclass=JSONRPCServerType): claims = account.get_txos claim_count = account.get_txo_count else: - claims = partial(self.ledger.get_txos, wallet=wallet, accounts=wallet.accounts) - claim_count = partial(self.ledger.get_txo_count, 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, read_only=True) constraints = {'resolve': resolve, 'unspent': unspent, 'include_is_received': include_is_received} if is_received is True: constraints['is_received'] = True diff --git a/lbry/wallet/account.py b/lbry/wallet/account.py index 831d45e96..a315ea239 100644 --- a/lbry/wallet/account.py +++ b/lbry/wallet/account.py @@ -71,6 +71,7 @@ class AddressManager: def _query_addresses(self, **constraints): return self.account.ledger.db.get_addresses( + read_only=constraints.pop("read_only", False), accounts=[self.account], chain=self.chain_number, **constraints @@ -451,13 +452,14 @@ class Account: def get_public_key(self, chain: int, index: int) -> PubKey: 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: constraints.update({'txo_type__in': (0, TXO_TYPES['purchase'])}) if confirmations > 0: height = self.ledger.headers.height - (confirmations-1) 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): change_gap = await self.change.get_max_gap() @@ -561,9 +563,10 @@ class Account: if gap_changed: 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 - 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() if reserved_subtotals: claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES) diff --git a/lbry/wallet/database.py b/lbry/wallet/database.py index 571a27823..00cc8423d 100644 --- a/lbry/wallet/database.py +++ b/lbry/wallet/database.py @@ -671,7 +671,8 @@ class Database(SQLiteMixin): constraints['is_reserved'] = False 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 if unspent: self.constrain_unspent(constraints) @@ -687,7 +688,7 @@ class Database(SQLiteMixin): where account_address.address=txo.address ), 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 = [] txs = {} @@ -736,7 +737,8 @@ class Database(SQLiteMixin): txo.claim_id: txo for txo in (await self.get_channels( wallet=wallet, - claim_id__in=channel_ids + claim_id__in=channel_ids, + read_only=read_only )) } for txo in txos: @@ -756,18 +758,18 @@ class Database(SQLiteMixin): count = await self.select_txos('count(*)', **constraints) return count[0][0] - def get_utxos(self, **constraints): - return self.get_txos(unspent=True, **constraints) + def get_utxos(self, read_only: bool = False, **constraints): + return self.get_txos(unspent=True, read_only=read_only, **constraints) def get_utxo_count(self, **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, \ "'wallet' or 'accounts' constraints required to calculate balance" constraints['accounts'] = accounts or wallet.accounts 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 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) return count[0][0] - async def get_address(self, **constraints): - addresses = await self.get_addresses(limit=1, **constraints) + async def get_address(self, read_only: bool = False, **constraints): + addresses = await self.get_addresses(read_only=read_only, limit=1, **constraints) if addresses: return addresses[0] @@ -859,9 +861,9 @@ class Database(SQLiteMixin): else: 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) - return await self.get_utxos(**constraints) + return await self.get_utxos(read_only=read_only, **constraints) def get_claim_count(self, **constraints): self.constrain_claims(constraints) @@ -871,9 +873,9 @@ class Database(SQLiteMixin): def constrain_streams(constraints): constraints['txo_type'] = TXO_TYPES['stream'] - def get_streams(self, **constraints): + def get_streams(self, read_only: bool = False, **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): self.constrain_streams(constraints) diff --git a/tests/unit/wallet/test_database.py b/tests/unit/wallet/test_database.py index 29ef58b46..50a40ad91 100644 --- a/tests/unit/wallet/test_database.py +++ b/tests/unit/wallet/test_database.py @@ -266,9 +266,10 @@ class TestQueries(AsyncioTestCase): # 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 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) - return fetchall(sql, parameters) + return fetchall(sql, parameters, read_only) self.ledger.db.db.execute_fetchall = check_parameters_length account = await self.create_account()