lbry-sdk/tests/unit/test_ledger.py

103 lines
3.6 KiB
Python
Raw Normal View History

2018-06-14 02:57:57 +02:00
import six
2018-06-12 16:02:04 +02:00
from binascii import hexlify
2018-06-11 15:33:32 +02:00
from twisted.trial import unittest
from twisted.internet import defer
2018-06-12 16:02:04 +02:00
from torba.coin.bitcoinsegwit import MainNetLedger
2018-06-11 15:33:32 +02:00
2018-06-27 00:31:42 +02:00
from .test_transaction import get_transaction, get_output
2018-06-11 15:33:32 +02:00
2018-06-14 02:57:57 +02:00
if six.PY3:
buffer = memoryview
2018-06-12 16:02:04 +02:00
class MockNetwork:
def __init__(self, history, transaction):
self.history = history
self.transaction = transaction
self.address = None
self.get_history_called = []
self.get_transaction_called = []
def get_history(self, address):
self.get_history_called.append(address)
self.address = address
return defer.succeed(self.history)
2018-06-27 00:31:42 +02:00
def get_merkle(self, txid, height):
return {'merkle': ['abcd01'], 'pos': 1}
2018-06-12 16:02:04 +02:00
def get_transaction(self, tx_hash):
self.get_transaction_called.append(tx_hash)
2018-06-27 00:31:42 +02:00
return defer.succeed(self.transaction[tx_hash.decode()])
class MockHeaders:
def __init__(self, ledger):
self.ledger = ledger
self.height = 1
def __len__(self):
return self.height
def __getitem__(self, height):
return {'merkle_root': 'abcd04'}
2018-06-12 16:02:04 +02:00
2018-07-12 04:37:15 +02:00
class MainNetTestLedger(MainNetLedger):
headers_class = MockHeaders
network_name = 'unittest'
def __init__(self):
super(MainNetLedger, self).__init__({
'db': MainNetLedger.database_class(':memory:')
})
2018-06-12 16:02:04 +02:00
class TestSynchronization(unittest.TestCase):
2018-06-11 15:33:32 +02:00
def setUp(self):
2018-07-12 04:37:15 +02:00
self.ledger = MainNetTestLedger()
2018-06-12 16:02:04 +02:00
return self.ledger.db.start()
2018-06-11 15:33:32 +02:00
@defer.inlineCallbacks
2018-06-12 16:02:04 +02:00
def test_update_history(self):
account = self.ledger.account_class.generate(self.ledger, u"torba")
address = yield account.receiving.get_or_create_usable_address()
address_details = yield self.ledger.db.get_address(address)
self.assertEqual(address_details['history'], None)
2018-06-27 00:31:42 +02:00
self.ledger.headers.height = 3
2018-06-12 16:02:04 +02:00
self.ledger.network = MockNetwork([
2018-06-27 00:31:42 +02:00
{'tx_hash': b'abcd01', 'height': 1},
{'tx_hash': b'abcd02', 'height': 2},
{'tx_hash': b'abcd03', 'height': 3},
2018-06-12 16:02:04 +02:00
], {
2018-06-27 00:31:42 +02:00
'abcd01': hexlify(get_transaction(get_output(1)).raw),
'abcd02': hexlify(get_transaction(get_output(2)).raw),
'abcd03': hexlify(get_transaction(get_output(3)).raw),
2018-06-12 16:02:04 +02:00
})
yield self.ledger.update_history(address)
self.assertEqual(self.ledger.network.get_history_called, [address])
2018-06-27 00:31:42 +02:00
self.assertEqual(self.ledger.network.get_transaction_called, [b'abcd01', b'abcd02', b'abcd03'])
2018-06-12 16:02:04 +02:00
address_details = yield self.ledger.db.get_address(address)
2018-06-27 00:31:42 +02:00
self.assertEqual(address_details['history'], 'abcd01:1:abcd02:2:abcd03:3:')
2018-06-12 16:02:04 +02:00
self.ledger.network.get_history_called = []
self.ledger.network.get_transaction_called = []
yield self.ledger.update_history(address)
self.assertEqual(self.ledger.network.get_history_called, [address])
self.assertEqual(self.ledger.network.get_transaction_called, [])
2018-06-27 00:31:42 +02:00
self.ledger.network.history.append({'tx_hash': b'abcd04', 'height': 4})
self.ledger.network.transaction['abcd04'] = hexlify(get_transaction(get_output(4)).raw)
2018-06-12 16:02:04 +02:00
self.ledger.network.get_history_called = []
self.ledger.network.get_transaction_called = []
yield self.ledger.update_history(address)
self.assertEqual(self.ledger.network.get_history_called, [address])
2018-06-27 00:31:42 +02:00
self.assertEqual(self.ledger.network.get_transaction_called, [b'abcd04'])
2018-06-12 16:02:04 +02:00
address_details = yield self.ledger.db.get_address(address)
2018-06-27 00:31:42 +02:00
self.assertEqual(address_details['history'], 'abcd01:1:abcd02:2:abcd03:3:abcd04:4:')