From 46d89929bae72d4dbf2e60cfa4a13b6d893cd125 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 12 Aug 2019 02:52:37 -0400 Subject: [PATCH] fixup balance tests and other stuff --- lbry/lbry/extras/daemon/Daemon.py | 54 ++++++----- lbry/lbry/wallet/account.py | 2 +- .../integration/test_transaction_commands.py | 97 +++++++++++++------ 3 files changed, 97 insertions(+), 56 deletions(-) diff --git a/lbry/lbry/extras/daemon/Daemon.py b/lbry/lbry/extras/daemon/Daemon.py index bf57bc530..179bd0be2 100644 --- a/lbry/lbry/extras/daemon/Daemon.py +++ b/lbry/lbry/extras/daemon/Daemon.py @@ -1959,10 +1959,10 @@ class Daemon(metaclass=JSONRPCServerType): txo.generate_channel_private_key() if not preview: - await tx.sign([account]) + await tx.sign(funding_accounts) account.add_channel_private_key(txo.private_key) self.default_wallet.save() - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.storage.save_claims([self._old_get_temp_claim_info( tx, txo, claim_address, claim, name, dewies_to_lbc(amount) )]) @@ -2102,10 +2102,10 @@ class Daemon(metaclass=JSONRPCServerType): new_txo.script.generate() if not preview: - await tx.sign([account]) + await tx.sign(funding_accounts) account.add_channel_private_key(new_txo.private_key) self.default_wallet.save() - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.storage.save_claims([self._old_get_temp_claim_info( tx, new_txo, claim_address, new_txo.claim, new_txo.claim_name, dewies_to_lbc(amount) )]) @@ -2155,7 +2155,7 @@ class Daemon(metaclass=JSONRPCServerType): ) if not preview: - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.analytics_manager.send_claim_action('abandon') else: await account.ledger.release_tx(tx) @@ -2498,10 +2498,10 @@ class Daemon(metaclass=JSONRPCServerType): if channel: new_txo.sign(channel) - await tx.sign([account]) + await tx.sign(funding_accounts) if not preview: - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.storage.save_claims([self._old_get_temp_claim_info( tx, new_txo, claim_address, claim, name, dewies_to_lbc(amount) )]) @@ -2694,10 +2694,10 @@ class Daemon(metaclass=JSONRPCServerType): if channel: new_txo.sign(channel) - await tx.sign([account]) + await tx.sign(funding_accounts) if not preview: - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.storage.save_claims([self._old_get_temp_claim_info( tx, new_txo, claim_address, new_txo.claim, new_txo.claim_name, dewies_to_lbc(amount) )]) @@ -2749,7 +2749,7 @@ class Daemon(metaclass=JSONRPCServerType): ) if not preview: - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.analytics_manager.send_claim_action('abandon') else: await account.ledger.release_tx(tx) @@ -2827,12 +2827,12 @@ class Daemon(metaclass=JSONRPCServerType): Returns: {Transaction} """ - account = self.get_account_or_default(account_id) funding_accounts = self.get_accounts_or_all(funding_account_ids) amount = self.get_dewies_or_error("amount", amount) claim = await self.ledger.get_claim_by_claim_id(claim_id) claim_address = claim.get_address(self.ledger) if not tip: + account = self.get_account_or_default(account_id) claim_address = await account.receiving.get_or_create_usable_address() tx = await Transaction.support( @@ -2840,8 +2840,8 @@ class Daemon(metaclass=JSONRPCServerType): ) if not preview: - await tx.sign([account]) - await self.broadcast_or_release(account, tx, blocking) + await tx.sign(funding_accounts) + await self.broadcast_or_release(tx, blocking) await self.storage.save_supports({claim_id: [{ 'txid': tx.id, 'nout': tx.position, @@ -2851,7 +2851,7 @@ class Daemon(metaclass=JSONRPCServerType): }]}) await self.analytics_manager.send_claim_action('new_support') else: - await account.ledger.release_tx(tx) + await self.ledger.release_tx(tx) return tx @@ -2903,12 +2903,18 @@ class Daemon(metaclass=JSONRPCServerType): Returns: {Transaction} """ - account = self.get_account_or_default(account_id) + if account_id: + account = self.get_account_or_error(account_id) + funding_accounts = [account] + get_supports = account.get_supports + else: + funding_accounts = self.ledger.accounts + get_supports = self.ledger.get_supports if txid is not None and nout is not None: - supports = await account.get_supports(**{'txo.txid': txid, 'txo.position': nout}) + supports = await get_supports(**{'txo.txid': txid, 'txo.position': nout}) elif claim_id is not None: - supports = await account.get_supports(claim_id=claim_id) + supports = await get_supports(claim_id=claim_id) else: raise Exception('Must specify claim_id, or txid and nout') @@ -2929,14 +2935,14 @@ class Daemon(metaclass=JSONRPCServerType): ] tx = await Transaction.create( - [Input.spend(txo) for txo in supports], outputs, [account], account + [Input.spend(txo) for txo in supports], outputs, funding_accounts, funding_accounts[0] ) if not preview: - await self.broadcast_or_release(account, tx, blocking) + await self.broadcast_or_release(tx, blocking) await self.analytics_manager.send_claim_action('abandon') else: - await account.ledger.release_tx(tx) + await self.ledger.release_tx(tx) return tx @@ -3567,13 +3573,13 @@ class Daemon(metaclass=JSONRPCServerType): comment_client.sign_comment(abandon_comment_body, channel, abandon=True) return await comment_client.jsonrpc_post(self.conf.comment_server, 'delete_comment', abandon_comment_body) - async def broadcast_or_release(self, account, tx, blocking=False): + async def broadcast_or_release(self, tx, blocking=False): try: - await account.ledger.broadcast(tx) + await self.ledger.broadcast(tx) if blocking: - await account.ledger.wait(tx) + await self.ledger.wait(tx) except: - await account.ledger.release_tx(tx) + await self.ledger.release_tx(tx) raise def valid_address_or_error(self, address): diff --git a/lbry/lbry/wallet/account.py b/lbry/lbry/wallet/account.py index da066bd13..145df6bee 100644 --- a/lbry/lbry/wallet/account.py +++ b/lbry/lbry/wallet/account.py @@ -82,7 +82,7 @@ class Account(BaseAccount): get_total_balance = partial(self.get_balance, confirmations=confirmations, include_claims=True) total = await get_total_balance() if reserved_subtotals: - claims_balance = await get_total_balance(claim_type__or={'is_claim':True, 'is_update': True}) + claims_balance = await get_total_balance(claim_type__or={'is_claim': True, 'is_update': True}) for amount, spent, from_me, to_me, height in await self.get_support_summary(): if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1): continue diff --git a/lbry/tests/integration/test_transaction_commands.py b/lbry/tests/integration/test_transaction_commands.py index 3fb3c94de..6951ba414 100644 --- a/lbry/tests/integration/test_transaction_commands.py +++ b/lbry/tests/integration/test_transaction_commands.py @@ -38,42 +38,77 @@ class TransactionCommandsTestCase(CommandTestCase): await self.assertBalance(self.account, '11.0') async def test_granular_balances(self): - initial_balance = await self.daemon.jsonrpc_account_balance(reserved_subtotals=True) - self.assertEqual({ + account_balance = self.daemon.jsonrpc_account_balance + + self.assertEqual(await account_balance(reserved_subtotals=False), { + 'total': '10.0', + 'available': '10.0', + 'reserved': '0.0', + 'reserved_subtotals': None + }) + + self.assertEqual(await account_balance(reserved_subtotals=True), { 'total': '10.0', 'available': '10.0', 'reserved': '0.0', 'reserved_subtotals': {'claims': '0.0', 'supports': '0.0', 'tips': '0.0'} - }, initial_balance) - first_claim_id = self.get_claim_id(await self.stream_create('granularity', bid='3.0')) - await self.stream_update(first_claim_id, data=b'news', bid='1.0') - await self.support_create(first_claim_id, bid='2.0') - second_account_id = (await self.out(self.daemon.jsonrpc_account_create("Tip-er")))['id'] - second_accound_address = await self.daemon.jsonrpc_address_unused(second_account_id) - await self.confirm_tx((await self.daemon.jsonrpc_account_send('1.0', second_accound_address)).id) - self.assertEqual({ + }) + + # claim with update + supporting our own claim + stream1 = await self.stream_create('granularity', '3.0') + await self.stream_update(self.get_claim_id(stream1), data=b'news', bid='1.0') + await self.support_create(self.get_claim_id(stream1), '2.0') + self.assertEqual(await account_balance(reserved_subtotals=True), { + 'total': '9.977534', + 'available': '6.977534', + 'reserved': '3.0', + 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} + }) + + account2 = await self.daemon.jsonrpc_account_create("Tip-er") + address2 = await self.daemon.jsonrpc_address_unused(account2.id) + + # send lbc to someone else + tx = await self.daemon.jsonrpc_account_send('1.0', address2) + await self.confirm_tx(tx.id) + self.assertEqual(await account_balance(reserved_subtotals=True), { 'total': '8.97741', 'available': '5.97741', 'reserved': '3.0', 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} - }, await self.daemon.jsonrpc_account_balance(reserved_subtotals=True)) - second_claim_id = self.get_claim_id(await self.stream_create( - name='granularity-is-cool', account_id=second_account_id, bid='0.1')) - await self.daemon.jsonrpc_support_create(second_claim_id, '0.5', tip=True) - first_account_tip_txid = await self.confirm_tx((await self.daemon.jsonrpc_support_create( - first_claim_id, '0.3', tip=True, account_id=second_account_id)).id) - for with_subtotals in (True, False): - self.assertEqual({ - 'total': '8.777264', - 'available': '5.477264', - 'reserved': '3.3', - 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'} if with_subtotals else None - }, await self.daemon.jsonrpc_account_balance(reserved_subtotals=with_subtotals)) - await self.confirm_tx((await self.daemon.jsonrpc_support_abandon(txid=first_account_tip_txid, nout=0)).id) - for with_subtotals in (True, False): - self.assertEqual({ - 'total': '8.777157', - 'available': '5.777157', - 'reserved': '3.0', - 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} if with_subtotals else None - }, await self.daemon.jsonrpc_account_balance(reserved_subtotals=with_subtotals)) + }) + + # tip received + support1 = await self.support_create( + self.get_claim_id(stream1), '0.3', tip=True, funding_account_ids=[account2.id] + ) + self.assertEqual(await account_balance(reserved_subtotals=True), { + 'total': '9.27741', + 'available': '5.97741', + 'reserved': '3.3', + 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'} + }) + + # tip claimed + tx = await self.daemon.jsonrpc_support_abandon(txid=support1['txid'], nout=0) + await self.confirm_tx(tx.id) + self.assertEqual(await account_balance(reserved_subtotals=True), { + 'total': '9.277303', + 'available': '6.277303', + 'reserved': '3.0', + 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} + }) + + stream2 = await self.stream_create( + 'granularity-is-cool', '0.1', account_id=account2.id, funding_account_ids=[account2.id] + ) + + # tip another claim + await self.support_create( + self.get_claim_id(stream2), '0.2', tip=True, funding_account_ids=[self.account.id]) + self.assertEqual(await account_balance(reserved_subtotals=True), { + 'total': '9.077157', + 'available': '6.077157', + 'reserved': '3.0', + 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} + })