2018-11-04 07:24:41 +01:00
|
|
|
from torba.testcase import AsyncioTestCase
|
|
|
|
from torba.client.wallet import Wallet
|
2018-10-15 23:16:43 +02:00
|
|
|
|
2018-11-04 10:42:47 +01:00
|
|
|
from lbrynet.extras.wallet.account import Account
|
|
|
|
from lbrynet.extras.wallet.transaction import Transaction, Output, Input
|
|
|
|
from lbrynet.extras.wallet.ledger import MainNetLedger
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
class LedgerTestCase(AsyncioTestCase):
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def asyncSetUp(self):
|
2018-08-16 18:40:54 +02:00
|
|
|
self.ledger = MainNetLedger({
|
|
|
|
'db': MainNetLedger.database_class(':memory:'),
|
|
|
|
'headers': MainNetLedger.headers_class(':memory:')
|
|
|
|
})
|
2018-08-09 02:41:29 +02:00
|
|
|
self.account = Account.generate(self.ledger, Wallet(), "lbryum")
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.open()
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def asyncTearDown(self):
|
|
|
|
await self.ledger.db.close()
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BasicAccountingTests(LedgerTestCase):
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def test_empty_state(self):
|
|
|
|
self.assertEqual(await self.account.get_balance(), 0)
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def test_balance(self):
|
|
|
|
address = await self.account.receiving.get_or_create_usable_address()
|
2018-07-09 23:04:59 +02:00
|
|
|
hash160 = self.ledger.address_to_hash160(address)
|
|
|
|
|
2018-10-03 18:00:21 +02:00
|
|
|
tx = Transaction(is_verified=True)\
|
|
|
|
.add_outputs([Output.pay_pubkey_hash(100, hash160)])
|
2018-11-20 01:26:08 +01:00
|
|
|
await self.ledger.db.insert_transaction(tx)
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.save_transaction_io(
|
2018-11-20 01:26:08 +01:00
|
|
|
tx, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-09 23:04:59 +02:00
|
|
|
)
|
2018-10-15 23:16:43 +02:00
|
|
|
self.assertEqual(await self.account.get_balance(), 100)
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-03 18:00:21 +02:00
|
|
|
tx = Transaction(is_verified=True)\
|
|
|
|
.add_outputs([Output.pay_claim_name_pubkey_hash(100, 'foo', b'', hash160)])
|
2018-11-20 01:26:08 +01:00
|
|
|
await self.ledger.db.insert_transaction(tx)
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.save_transaction_io(
|
2018-11-20 01:26:08 +01:00
|
|
|
tx, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-09 23:04:59 +02:00
|
|
|
)
|
2018-10-15 23:16:43 +02:00
|
|
|
self.assertEqual(await self.account.get_balance(), 100) # claim names don't count towards balance
|
|
|
|
self.assertEqual(await self.account.get_balance(include_claims=True), 200)
|
2018-07-09 23:04:59 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def test_get_utxo(self):
|
2018-07-15 16:53:52 +02:00
|
|
|
address = yield self.account.receiving.get_or_create_usable_address()
|
|
|
|
hash160 = self.ledger.address_to_hash160(address)
|
|
|
|
|
2018-10-03 18:00:21 +02:00
|
|
|
tx = Transaction(is_verified=True)\
|
|
|
|
.add_outputs([Output.pay_pubkey_hash(100, hash160)])
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.save_transaction_io(
|
2018-10-03 18:00:21 +02:00
|
|
|
'insert', tx, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-15 16:53:52 +02:00
|
|
|
)
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
utxos = await self.account.get_utxos()
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(len(utxos), 1)
|
|
|
|
|
2018-10-03 18:00:21 +02:00
|
|
|
tx = Transaction(is_verified=True)\
|
|
|
|
.add_inputs([Input.spend(utxos[0])])
|
2018-10-15 23:16:43 +02:00
|
|
|
await self.ledger.db.save_transaction_io(
|
2018-10-03 18:00:21 +02:00
|
|
|
'insert', tx, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-15 16:53:52 +02:00
|
|
|
)
|
2018-10-15 23:16:43 +02:00
|
|
|
self.assertEqual(await self.account.get_balance(include_claims=True), 0)
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
utxos = await self.account.get_utxos()
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(len(utxos), 0)
|