fixing unit tests

This commit is contained in:
Lex Berezhny 2018-07-15 10:53:52 -04:00 committed by Jack Robison
parent 0442770c9d
commit 9636ca22e7
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 106 additions and 87 deletions

View file

@ -78,44 +78,52 @@ def get_test_daemon(data_rate=None, generous=True, with_fee=False):
if with_fee: if with_fee:
metadata.update( metadata.update(
{"fee": {"USD": {"address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75}}}) {"fee": {"USD": {"address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75}}})
daemon._resolve = lambda _: defer.succeed(metadata)
migrated = smart_decode(json.dumps(metadata)) migrated = smart_decode(json.dumps(metadata))
daemon.wallet.resolve = lambda *_: defer.succeed( daemon._resolve = daemon.wallet.resolve = lambda *_: defer.succeed(
{"test": {'claim': {'value': migrated.claim_dict}}}) {"test": {'claim': {'value': migrated.claim_dict}}})
return daemon return daemon
class TestCostEst(unittest.TestCase): class TestCostEst(unittest.TestCase):
def setUp(self): def setUp(self):
mock_conf_settings(self) mock_conf_settings(self)
util.resetTime(self) util.resetTime(self)
@defer.inlineCallbacks
def test_fee_and_generous_data(self): def test_fee_and_generous_data(self):
size = 10000000 size = 10000000
correct_result = 4.5 correct_result = 4.5
daemon = get_test_daemon(generous=True, with_fee=True) daemon = get_test_daemon(generous=True, with_fee=True)
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result) result = yield daemon.get_est_cost("test", size)
self.assertEquals(result, correct_result)
# def test_fee_and_ungenerous_data(self): @defer.inlineCallbacks
# size = 10000000 def test_fee_and_ungenerous_data(self):
# fake_fee_amount = 4.5 size = 10000000
# data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1] fake_fee_amount = 4.5
# correct_result = size / 10 ** 6 * data_rate + fake_fee_amount data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1]
# daemon = get_test_daemon(generous=False, with_fee=True) correct_result = size / 10 ** 6 * data_rate + fake_fee_amount
# self.assertEquals(daemon.get_est_cost("test", size).result, correct_result) daemon = get_test_daemon(generous=False, with_fee=True)
result = yield daemon.get_est_cost("test", size)
self.assertEquals(result, correct_result)
@defer.inlineCallbacks
def test_generous_data_and_no_fee(self): def test_generous_data_and_no_fee(self):
size = 10000000 size = 10000000
correct_result = 0.0 correct_result = 0.0
daemon = get_test_daemon(generous=True) daemon = get_test_daemon(generous=True)
self.assertEquals(daemon.get_est_cost("test", size).result, correct_result) result = yield daemon.get_est_cost("test", size)
# self.assertEquals(result, correct_result)
# def test_ungenerous_data_and_no_fee(self):
# size = 10000000 @defer.inlineCallbacks
# data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1] def test_ungenerous_data_and_no_fee(self):
# correct_result = size / 10 ** 6 * data_rate size = 10000000
# daemon = get_test_daemon(generous=False) data_rate = conf.ADJUSTABLE_SETTINGS['data_rate'][1]
# self.assertEquals(daemon.get_est_cost("test", size).result, correct_result) correct_result = size / 10 ** 6 * data_rate
daemon = get_test_daemon(generous=False)
result = yield daemon.get_est_cost("test", size)
self.assertEquals(result, correct_result)
class TestJsonRpc(unittest.TestCase): class TestJsonRpc(unittest.TestCase):

View file

@ -2,6 +2,7 @@ from twisted.internet import defer
from twisted.trial import unittest from twisted.trial import unittest
from lbrynet import conf from lbrynet import conf
from lbrynet.wallet.account import Account from lbrynet.wallet.account import Account
from lbrynet.wallet.database import WalletDatabase
from lbrynet.wallet.transaction import Transaction, Output, Input from lbrynet.wallet.transaction import Transaction, Output, Input
from lbrynet.wallet.ledger import MainNetLedger from lbrynet.wallet.ledger import MainNetLedger
from torba.wallet import Wallet from torba.wallet import Wallet
@ -19,21 +20,26 @@ class MockHeaders:
return {'merkle_root': 'abcd04'} return {'merkle_root': 'abcd04'}
class MainNetTestLedger(MainNetLedger):
headers_class = MockHeaders
network_name = 'unittest'
def __init__(self):
super(MainNetLedger, self).__init__({
'db': WalletDatabase(':memory:')
})
class LedgerTestCase(unittest.TestCase): class LedgerTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
conf.initialize_settings(False) conf.initialize_settings(False)
self.ledger = MainNetLedger(db=MainNetLedger.database_class(':memory:'), headers_class=MockHeaders) self.ledger = MainNetTestLedger()
self.wallet = Wallet('Main', [Account.from_seed( self.account = Account.generate(self.ledger, u"lbryum")
self.ledger, u'carbon smart garage balance margin twelve chest sword toast envelope botto'
u'm stomach absent', u'lbryum'
)])
self.account = self.wallet.default_account
return self.ledger.db.start() return self.ledger.db.start()
@defer.inlineCallbacks
def tearDown(self): def tearDown(self):
yield self.ledger.db.stop() return self.ledger.db.stop()
class BasicAccountingTests(LedgerTestCase): class BasicAccountingTests(LedgerTestCase):
@ -50,35 +56,40 @@ class BasicAccountingTests(LedgerTestCase):
tx = Transaction().add_outputs([Output.pay_pubkey_hash(100, hash160)]) tx = Transaction().add_outputs([Output.pay_pubkey_hash(100, hash160)])
yield self.ledger.db.save_transaction_io( yield self.ledger.db.save_transaction_io(
'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.hex_id, 1) 'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.id, 1)
) )
balance = yield self.account.get_balance() balance = yield self.account.get_balance(0)
self.assertEqual(balance, 100) self.assertEqual(balance, 100)
tx = Transaction().add_outputs([Output.pay_claim_name_pubkey_hash(100, b'foo', b'', hash160)]) tx = Transaction().add_outputs([Output.pay_claim_name_pubkey_hash(100, b'foo', b'', hash160)])
yield self.ledger.db.save_transaction_io( yield self.ledger.db.save_transaction_io(
'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.hex_id, 1) 'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.id, 1)
) )
balance = yield self.account.get_balance() balance = yield self.account.get_balance(0)
self.assertEqual(balance, 100) # claim names don't count towards balance self.assertEqual(balance, 100) # claim names don't count towards balance
balance = yield self.account.get_balance(include_claims=True) balance = yield self.account.get_balance(0, include_claims=True)
self.assertEqual(balance, 200) self.assertEqual(balance, 200)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_get_utxo(self): def test_get_utxo(self):
tx1 = Transaction().add_outputs([Output.pay_pubkey_hash(100, b'abc1')]) address = yield self.account.receiving.get_or_create_usable_address()
txo = tx1.outputs[0] hash160 = self.ledger.address_to_hash160(address)
yield self.storage.add_tx_output(self.account, txo)
balance = yield self.storage.get_balance_for_account(self.account)
self.assertEqual(balance, 100)
utxos = yield self.storage.get_utxos(self.account, Output) tx = Transaction().add_outputs([Output.pay_pubkey_hash(100, hash160)])
yield self.ledger.db.save_transaction_io(
'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.id, 1)
)
utxos = yield self.account.get_unspent_outputs()
self.assertEqual(len(utxos), 1) self.assertEqual(len(utxos), 1)
txi = Transaction().add_inputs([Input.spend(txo)]).inputs[0] tx = Transaction().add_inputs([Input.spend(utxos[0])])
yield self.storage.add_tx_input(self.account, txi) yield self.ledger.db.save_transaction_io(
balance = yield self.storage.get_balance_for_account(self.account) 'insert', tx, 1, True, address, hash160, '{}:{}:'.format(tx.id, 1)
)
balance = yield self.account.get_balance(0, include_claims=True)
self.assertEqual(balance, 0) self.assertEqual(balance, 0)
utxos = yield self.storage.get_utxos(self.account, Output) utxos = yield self.account.get_unspent_outputs()
self.assertEqual(len(utxos), 0) self.assertEqual(len(utxos), 0)

View file

@ -1,12 +1,13 @@
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import defer
from torba.constants import CENT, COIN from torba.constants import CENT, COIN, NULL_HASH32
from torba.wallet import Wallet from torba.wallet import Wallet
from torba.basetransaction import NULL_HASH
from lbrynet.wallet.account import Account from lbrynet.wallet.account import Account
from lbrynet.wallet.ledger import MainNetLedger from lbrynet.wallet.ledger import MainNetLedger
from lbrynet.wallet.database import WalletDatabase
from lbrynet.wallet.transaction import Transaction, Output, Input from lbrynet.wallet.transaction import Transaction, Output, Input
from lbrynet.wallet.manager import LbryWalletManager from lbrynet.wallet.manager import LbryWalletManager
@ -15,7 +16,7 @@ FEE_PER_BYTE = 50
FEE_PER_CHAR = 200000 FEE_PER_CHAR = 200000
def get_output(amount=CENT, pubkey_hash=NULL_HASH): def get_output(amount=CENT, pubkey_hash=NULL_HASH32):
return Transaction() \ return Transaction() \
.add_outputs([Output.pay_pubkey_hash(amount, pubkey_hash)]) \ .add_outputs([Output.pay_pubkey_hash(amount, pubkey_hash)]) \
.outputs[0] .outputs[0]
@ -28,28 +29,23 @@ def get_input():
def get_transaction(txo=None): def get_transaction(txo=None):
return Transaction() \ return Transaction() \
.add_inputs([get_input()]) \ .add_inputs([get_input()]) \
.add_outputs([txo or Output.pay_pubkey_hash(CENT, NULL_HASH)]) .add_outputs([txo or Output.pay_pubkey_hash(CENT, NULL_HASH32)])
def get_claim_transaction(claim_name, claim=b''): def get_claim_transaction(claim_name, claim=b''):
return get_transaction( return get_transaction(
Output.pay_claim_name_pubkey_hash(CENT, claim_name, claim, NULL_HASH) Output.pay_claim_name_pubkey_hash(CENT, claim_name, claim, NULL_HASH32)
) )
def get_wallet_and_coin():
ledger = LbryWalletManager().get_or_create_ledger(LBC.get_id())
coin = LBC(ledger)
return Wallet('Main', [coin], [Account.generate(coin, u'lbryum')]), coin
class TestSizeAndFeeEstimation(unittest.TestCase): class TestSizeAndFeeEstimation(unittest.TestCase):
def setUp(self): def setUp(self):
self.wallet, self.coin = get_wallet_and_coin() self.ledger = MainNetLedger({'db': WalletDatabase(':memory:')})
return self.ledger.db.start()
def io_fee(self, io): def io_fee(self, io):
return self.coin.get_input_output_fee(io) return self.ledger.get_input_output_fee(io)
def test_output_size_and_fee(self): def test_output_size_and_fee(self):
txo = get_output() txo = get_output()
@ -66,7 +62,7 @@ class TestSizeAndFeeEstimation(unittest.TestCase):
base_size = tx.size - 1 - tx.inputs[0].size base_size = tx.size - 1 - tx.inputs[0].size
self.assertEqual(tx.size, 204) self.assertEqual(tx.size, 204)
self.assertEqual(tx.base_size, base_size) self.assertEqual(tx.base_size, base_size)
self.assertEqual(self.coin.get_transaction_base_fee(tx), FEE_PER_BYTE * base_size) self.assertEqual(self.ledger.get_transaction_base_fee(tx), FEE_PER_BYTE * base_size)
def test_claim_name_transaction_size_and_fee(self): def test_claim_name_transaction_size_and_fee(self):
# fee based on claim name is the larger fee # fee based on claim name is the larger fee
@ -75,14 +71,14 @@ class TestSizeAndFeeEstimation(unittest.TestCase):
base_size = tx.size - 1 - tx.inputs[0].size base_size = tx.size - 1 - tx.inputs[0].size
self.assertEqual(tx.size, 4225) self.assertEqual(tx.size, 4225)
self.assertEqual(tx.base_size, base_size) self.assertEqual(tx.base_size, base_size)
self.assertEqual(self.coin.get_transaction_base_fee(tx), len(claim_name) * FEE_PER_CHAR) self.assertEqual(self.ledger.get_transaction_base_fee(tx), len(claim_name) * FEE_PER_CHAR)
# fee based on total bytes is the larger fee # fee based on total bytes is the larger fee
claim_name = b'a' claim_name = b'a'
tx = get_claim_transaction(claim_name, b'0'*4000) tx = get_claim_transaction(claim_name, b'0'*4000)
base_size = tx.size - 1 - tx.inputs[0].size base_size = tx.size - 1 - tx.inputs[0].size
self.assertEqual(tx.size, 4214) self.assertEqual(tx.size, 4214)
self.assertEqual(tx.base_size, base_size) self.assertEqual(tx.base_size, base_size)
self.assertEqual(self.coin.get_transaction_base_fee(tx), FEE_PER_BYTE * base_size) self.assertEqual(self.ledger.get_transaction_base_fee(tx), FEE_PER_BYTE * base_size)
class TestTransactionSerialization(unittest.TestCase): class TestTransactionSerialization(unittest.TestCase):
@ -100,11 +96,11 @@ class TestTransactionSerialization(unittest.TestCase):
self.assertEqual(len(tx.outputs), 1) self.assertEqual(len(tx.outputs), 1)
coinbase = tx.inputs[0] coinbase = tx.inputs[0]
self.assertEqual(coinbase.output_txid, NULL_HASH) self.assertTrue(coinbase.txo_ref.is_null)
self.assertEqual(coinbase.output_index, 0xFFFFFFFF) self.assertEqual(coinbase.txo_ref.position, 0xFFFFFFFF)
self.assertEqual(coinbase.sequence, 0xFFFFFFFF) self.assertEqual(coinbase.sequence, 0xFFFFFFFF)
self.assertTrue(coinbase.is_coinbase) self.assertIsNotNone(coinbase.coinbase)
self.assertEqual(coinbase.script, None) self.assertIsNone(coinbase.script)
self.assertEqual( self.assertEqual(
hexlify(coinbase.coinbase), hexlify(coinbase.coinbase),
b'04ffff001d010417696e736572742074696d657374616d7020737472696e67' b'04ffff001d010417696e736572742074696d657374616d7020737472696e67'
@ -112,7 +108,7 @@ class TestTransactionSerialization(unittest.TestCase):
out = tx.outputs[0] out = tx.outputs[0]
self.assertEqual(out.amount, 40000000000000000) self.assertEqual(out.amount, 40000000000000000)
self.assertEqual(out.index, 0) self.assertEqual(out.position, 0)
self.assertTrue(out.script.is_pay_pubkey_hash) self.assertTrue(out.script.is_pay_pubkey_hash)
self.assertFalse(out.script.is_pay_script_hash) self.assertFalse(out.script.is_pay_script_hash)
self.assertFalse(out.script.is_claim_involved) self.assertFalse(out.script.is_claim_involved)
@ -133,11 +129,11 @@ class TestTransactionSerialization(unittest.TestCase):
self.assertEqual(len(tx.outputs), 1) self.assertEqual(len(tx.outputs), 1)
coinbase = tx.inputs[0] coinbase = tx.inputs[0]
self.assertEqual(coinbase.output_txid, NULL_HASH) self.assertTrue(coinbase.txo_ref.is_null)
self.assertEqual(coinbase.output_index, 0xFFFFFFFF) self.assertEqual(coinbase.txo_ref.position, 0xFFFFFFFF)
self.assertEqual(coinbase.sequence, 0) self.assertEqual(coinbase.sequence, 0)
self.assertTrue(coinbase.is_coinbase) self.assertIsNotNone(coinbase.coinbase)
self.assertEqual(coinbase.script, None) self.assertIsNone(coinbase.script)
self.assertEqual( self.assertEqual(
hexlify(coinbase.coinbase), hexlify(coinbase.coinbase),
b'034d520504f89ac55a086032d217bf0700000d2f6e6f64655374726174756d2f' b'034d520504f89ac55a086032d217bf0700000d2f6e6f64655374726174756d2f'
@ -145,7 +141,7 @@ class TestTransactionSerialization(unittest.TestCase):
out = tx.outputs[0] out = tx.outputs[0]
self.assertEqual(out.amount, 36600100000) self.assertEqual(out.amount, 36600100000)
self.assertEqual(out.index, 0) self.assertEqual(out.position, 0)
self.assertTrue(out.script.is_pay_pubkey_hash) self.assertTrue(out.script.is_pay_pubkey_hash)
self.assertFalse(out.script.is_pay_script_hash) self.assertFalse(out.script.is_pay_script_hash)
self.assertFalse(out.script.is_claim_involved) self.assertFalse(out.script.is_claim_involved)
@ -176,12 +172,12 @@ class TestTransactionSerialization(unittest.TestCase):
txin = tx.inputs[0] txin = tx.inputs[0]
self.assertEqual( self.assertEqual(
hexlify(txin.output_txid[::-1]), txin.txo_ref.id,
b'1dfd535b6c8550ebe95abceb877f92f76f30e5ba4d3483b043386027b3e13324' '1dfd535b6c8550ebe95abceb877f92f76f30e5ba4d3483b043386027b3e13324:0'
) )
self.assertEqual(txin.output_index, 0) self.assertEqual(txin.txo_ref.position, 0)
self.assertEqual(txin.sequence, 0xFFFFFFFF) self.assertEqual(txin.sequence, 0xFFFFFFFF)
self.assertFalse(txin.is_coinbase) self.assertIsNone(txin.coinbase)
self.assertEqual(txin.script.template.name, 'pubkey_hash') self.assertEqual(txin.script.template.name, 'pubkey_hash')
self.assertEqual( self.assertEqual(
hexlify(txin.script.values['pubkey']), hexlify(txin.script.values['pubkey']),
@ -196,7 +192,7 @@ class TestTransactionSerialization(unittest.TestCase):
# Claim # Claim
out0 = tx.outputs[0] out0 = tx.outputs[0]
self.assertEqual(out0.amount, 10000000) self.assertEqual(out0.amount, 10000000)
self.assertEqual(out0.index, 0) self.assertEqual(out0.position, 0)
self.assertTrue(out0.script.is_pay_pubkey_hash) self.assertTrue(out0.script.is_pay_pubkey_hash)
self.assertTrue(out0.script.is_claim_name) self.assertTrue(out0.script.is_claim_name)
self.assertTrue(out0.script.is_claim_involved) self.assertTrue(out0.script.is_claim_involved)
@ -209,7 +205,7 @@ class TestTransactionSerialization(unittest.TestCase):
# Change # Change
out1 = tx.outputs[1] out1 = tx.outputs[1]
self.assertEqual(out1.amount, 189977100) self.assertEqual(out1.amount, 189977100)
self.assertEqual(out1.index, 1) self.assertEqual(out1.position, 1)
self.assertTrue(out1.script.is_pay_pubkey_hash) self.assertTrue(out1.script.is_pay_pubkey_hash)
self.assertFalse(out1.script.is_claim_involved) self.assertFalse(out1.script.is_claim_involved)
self.assertEqual( self.assertEqual(
@ -223,24 +219,28 @@ class TestTransactionSerialization(unittest.TestCase):
class TestTransactionSigning(unittest.TestCase): class TestTransactionSigning(unittest.TestCase):
def test_sign(self): def setUp(self):
ledger = LbryWalletManager().get_or_create_ledger(LBC.get_id()) self.ledger = MainNetLedger({'db': WalletDatabase(':memory:')})
coin = LBC(ledger) return self.ledger.db.start()
wallet = Wallet('Main', [coin], [Account.from_seed(
coin, u'carbon smart garage balance margin twelve chest sword toast envelope bottom sto'
u'mach absent', u'lbryum'
)])
account = wallet.default_account
address1 = account.receiving_keys.generate_next_address() @defer.inlineCallbacks
address2 = account.receiving_keys.generate_next_address() def test_sign(self):
pubkey_hash1 = account.coin.address_to_hash160(address1) account = self.ledger.account_class.from_seed(
pubkey_hash2 = account.coin.address_to_hash160(address2) self.ledger,
u"carbon smart garage balance margin twelve chest sword toast envelope bottom stomach ab"
u"sent", u"lbryum"
)
yield account.ensure_address_gap()
address1, address2 = yield account.receiving.get_addresses(2)
pubkey_hash1 = self.ledger.address_to_hash160(address1)
pubkey_hash2 = self.ledger.address_to_hash160(address2)
tx = Transaction() \ tx = Transaction() \
.add_inputs([Input.spend(get_output(int(2*COIN), pubkey_hash1))]) \ .add_inputs([Input.spend(get_output(int(2*COIN), pubkey_hash1))]) \
.add_outputs([Output.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)]) \ .add_outputs([Output.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)])
.sign(account)
yield tx.sign([account])
self.assertEqual( self.assertEqual(
hexlify(tx.inputs[0].script.values['signature']), hexlify(tx.inputs[0].script.values['signature']),