2018-06-12 17:53:29 +02:00
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.trial import unittest
|
2018-07-09 23:04:59 +02:00
|
|
|
from lbrynet.wallet.account import Account
|
2018-06-12 17:53:29 +02:00
|
|
|
from lbrynet.wallet.transaction import Transaction, Output, Input
|
2018-07-09 23:04:59 +02:00
|
|
|
from lbrynet.wallet.ledger import MainNetLedger
|
2018-06-12 17:53:29 +02:00
|
|
|
from torba.wallet import Wallet
|
|
|
|
|
|
|
|
|
|
|
|
class LedgerTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-16 18:40:54 +02:00
|
|
|
super().setUp()
|
|
|
|
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-08-16 18:40:54 +02:00
|
|
|
return self.ledger.db.open()
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2018-08-16 18:40:54 +02:00
|
|
|
super().tearDown()
|
|
|
|
return self.ledger.db.close()
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BasicAccountingTests(LedgerTestCase):
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_empty_state(self):
|
|
|
|
balance = yield self.account.get_balance()
|
|
|
|
self.assertEqual(balance, 0)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_balance(self):
|
2018-07-09 23:04:59 +02:00
|
|
|
address = yield self.account.receiving.get_or_create_usable_address()
|
|
|
|
hash160 = self.ledger.address_to_hash160(address)
|
|
|
|
|
|
|
|
tx = Transaction().add_outputs([Output.pay_pubkey_hash(100, hash160)])
|
|
|
|
yield self.ledger.db.save_transaction_io(
|
2018-09-26 04:49:44 +02:00
|
|
|
'insert', tx, True, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-09 23:04:59 +02:00
|
|
|
)
|
2018-07-15 16:53:52 +02:00
|
|
|
balance = yield self.account.get_balance(0)
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(balance, 100)
|
|
|
|
|
2018-08-04 18:10:41 +02:00
|
|
|
tx = Transaction().add_outputs([Output.pay_claim_name_pubkey_hash(100, 'foo', b'', hash160)])
|
2018-07-09 23:04:59 +02:00
|
|
|
yield self.ledger.db.save_transaction_io(
|
2018-09-26 04:49:44 +02:00
|
|
|
'insert', tx, True, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-09 23:04:59 +02:00
|
|
|
)
|
2018-07-15 16:53:52 +02:00
|
|
|
balance = yield self.account.get_balance(0)
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(balance, 100) # claim names don't count towards balance
|
2018-07-15 16:53:52 +02:00
|
|
|
balance = yield self.account.get_balance(0, include_claims=True)
|
2018-07-09 23:04:59 +02:00
|
|
|
self.assertEqual(balance, 200)
|
|
|
|
|
2018-06-12 17:53:29 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
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)
|
|
|
|
|
|
|
|
tx = Transaction().add_outputs([Output.pay_pubkey_hash(100, hash160)])
|
|
|
|
yield self.ledger.db.save_transaction_io(
|
2018-09-26 04:49:44 +02:00
|
|
|
'insert', tx, True, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-15 16:53:52 +02:00
|
|
|
)
|
2018-06-12 17:53:29 +02:00
|
|
|
|
2018-07-15 16:53:52 +02:00
|
|
|
utxos = yield self.account.get_unspent_outputs()
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(len(utxos), 1)
|
|
|
|
|
2018-07-15 16:53:52 +02:00
|
|
|
tx = Transaction().add_inputs([Input.spend(utxos[0])])
|
|
|
|
yield self.ledger.db.save_transaction_io(
|
2018-09-26 04:49:44 +02:00
|
|
|
'insert', tx, True, address, hash160, '{}:{}:'.format(tx.id, 1)
|
2018-07-15 16:53:52 +02:00
|
|
|
)
|
|
|
|
balance = yield self.account.get_balance(0, include_claims=True)
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(balance, 0)
|
|
|
|
|
2018-07-15 16:53:52 +02:00
|
|
|
utxos = yield self.account.get_unspent_outputs()
|
2018-06-12 17:53:29 +02:00
|
|
|
self.assertEqual(len(utxos), 0)
|
2018-07-15 16:53:52 +02:00
|
|
|
|