From 769ea8cdfe1bb9c506e70bde66740fb6c175a0eb Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 31 Mar 2020 23:08:51 -0400 Subject: [PATCH] added --is_spent filter to txo list/sum commands --- lbry/extras/daemon/daemon.py | 22 ++++++++++++++----- lbry/wallet/database.py | 6 +++-- .../blockchain/test_claim_commands.py | 4 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lbry/extras/daemon/daemon.py b/lbry/extras/daemon/daemon.py index a9ff849cb..6f63acc86 100644 --- a/lbry/extras/daemon/daemon.py +++ b/lbry/extras/daemon/daemon.py @@ -4168,11 +4168,15 @@ class Daemon(metaclass=JSONRPCServerType): @staticmethod def _constrain_txo_from_kwargs( constraints, type=None, txid=None, # pylint: disable=redefined-builtin - claim_id=None, channel_id=None, name=None, unspent=False, reposted_claim_id=None, + claim_id=None, channel_id=None, name=None, reposted_claim_id=None, + is_spent=False, is_not_spent=False, unspent=False, is_my_input_or_output=None, exclude_internal_transfers=False, is_my_output=None, is_not_my_output=None, is_my_input=None, is_not_my_input=None): - constraints['unspent'] = unspent + if unspent or is_not_spent: + constraints['unspent'] = True + elif is_spent: + constraints['is_spent'] = True constraints['exclude_internal_transfers'] = exclude_internal_transfers if is_my_input_or_output is True: constraints['is_my_input_or_output'] = True @@ -4201,7 +4205,8 @@ class Daemon(metaclass=JSONRPCServerType): List my transaction outputs. Usage: - txo_list [--account_id=] [--type=...] [--txid=...] [--unspent] + txo_list [--account_id=] [--type=...] [--txid=...] + [--is_spent] [--is_not_spent] [--unspent] [--claim_id=...] [--channel_id=...] [--name=...] [--is_my_input_or_output | [[--is_my_output | --is_not_my_output] [--is_my_input | --is_not_my_input]] @@ -4217,7 +4222,9 @@ class Daemon(metaclass=JSONRPCServerType): --claim_id= : (str or list) claim id --channel_id= : (str or list) claims in this channel --name= : (str or list) claim name - --unspent : (bool) hide spent outputs, show only unspent ones + --is_spent : (bool) only show spent txos + --is_not_spent : (bool) only show not spent txos + --unspent : (bool) deprecated, alias for --is_not_spent --is_my_input_or_output : (bool) txos which have your inputs or your outputs, if using this flag the other related flags are ignored (--is_my_output, --is_my_input, etc) @@ -4330,7 +4337,8 @@ class Daemon(metaclass=JSONRPCServerType): Usage: txo_list [--account_id=] [--type=...] [--txid=...] - [--claim_id=...] [--name=...] [--unspent] + [--claim_id=...] [--name=...] + [--is_spent] [--is_not_spent] [--unspent] [--is_my_input_or_output | [[--is_my_output | --is_not_my_output] [--is_my_input | --is_not_my_input]] ] @@ -4342,7 +4350,9 @@ class Daemon(metaclass=JSONRPCServerType): --txid= : (str or list) transaction id of outputs --claim_id= : (str or list) claim id --name= : (str or list) claim name - --unspent : (bool) hide spent outputs, show only unspent ones + --is_spent : (bool) only show spent txos + --is_not_spent : (bool) only show not spent txos + --unspent : (bool) deprecated, alias for --is_not_spent --is_my_input_or_output : (bool) txos which have your inputs or your outputs, if using this flag the other related flags are ignored (--is_my_output, --is_my_input, etc) diff --git a/lbry/wallet/database.py b/lbry/wallet/database.py index 949442247..dec280035 100644 --- a/lbry/wallet/database.py +++ b/lbry/wallet/database.py @@ -694,7 +694,7 @@ class Database(SQLiteMixin): self, cols, accounts=None, is_my_input=None, is_my_output=True, is_my_input_or_output=None, exclude_internal_transfers=False, include_is_spent=False, include_is_my_input=False, - read_only=False, **constraints): + is_spent=False, read_only=False, **constraints): for rename_col in ('txid', 'txoid'): for rename_constraint in (rename_col, rename_col+'__in', rename_col+'__not_in'): if rename_constraint in constraints: @@ -737,7 +737,9 @@ class Database(SQLiteMixin): 'txi.address__not_in': my_addresses } sql = [f"SELECT {cols} FROM txo JOIN tx ON (tx.txid=txo.txid)"] - if include_is_spent: + if is_spent: + constraints['spent.txoid__is_not_null'] = True + if include_is_spent or is_spent: sql.append("LEFT JOIN txi AS spent ON (spent.txoid=txo.txoid)") if include_is_my_input: sql.append("LEFT JOIN txi ON (txi.position=0 AND txi.txid=txo.txid)") diff --git a/tests/integration/blockchain/test_claim_commands.py b/tests/integration/blockchain/test_claim_commands.py index 5520f0e88..c5e926434 100644 --- a/tests/integration/blockchain/test_claim_commands.py +++ b/tests/integration/blockchain/test_claim_commands.py @@ -547,6 +547,10 @@ class TransactionOutputCommands(ClaimTestCase): r = await self.txo_list(is_my_input=True, is_my_output=True, type="other", unspent=True) self.assertEqual([change2], r) + # only spent "change" + r = await self.txo_list(is_my_input=True, is_my_output=True, type="other", is_spent=True) + self.assertEqual([change1], r) + # all my unspent stuff r = await self.txo_list(is_my_output=True, unspent=True) self.assertEqual([change2, kept_channel], r)