diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index cb53742b8..7f932332f 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -106,7 +106,8 @@ def check_connection(server="lbry.io", port=80, timeout=5): log.debug('Checking connection to %s:%s', server, port) try: server = socket.gethostbyname(server) - socket.create_connection((server, port), timeout) + conn = socket.create_connection((server, port), timeout) + conn.close() log.debug('Connection successful') return True except (socket.gaierror, socket.herror) as ex: diff --git a/lbrynet/daemon/Components.py b/lbrynet/daemon/Components.py index b21246099..e2d731951 100644 --- a/lbrynet/daemon/Components.py +++ b/lbrynet/daemon/Components.py @@ -1,4 +1,5 @@ import os +import asyncio import logging import treq import math @@ -309,6 +310,14 @@ class HeadersComponent(Component): return defer.succeed(None) +def d2f(deferred): + return deferred.asFuture(asyncio.get_event_loop()) + + +def f2d(future): + return defer.Deferred(future) + + class WalletComponent(Component): component_name = WALLET_COMPONENT depends_on = [DATABASE_COMPONENT, HEADERS_COMPONENT] @@ -326,7 +335,7 @@ class WalletComponent(Component): if self.wallet_manager: local_height = self.wallet_manager.network.get_local_height() remote_height = self.wallet_manager.network.get_server_height() - best_hash = yield self.wallet_manager.get_best_blockhash() + best_hash = yield f2d(self.wallet_manager.get_best_blockhash()) defer.returnValue({ 'blocks': max(local_height, 0), 'blocks_behind': max(remote_height - local_height, 0), @@ -341,13 +350,13 @@ class WalletComponent(Component): log.info("Starting torba wallet") storage = self.component_manager.get_component(DATABASE_COMPONENT) lbryschema.BLOCKCHAIN_NAME = conf.settings['blockchain_name'] - self.wallet_manager = yield LbryWalletManager.from_lbrynet_config(conf.settings, storage) + self.wallet_manager = yield f2d(LbryWalletManager.from_lbrynet_config(conf.settings, storage)) self.wallet_manager.old_db = storage - yield self.wallet_manager.start() + yield f2d(self.wallet_manager.start()) @defer.inlineCallbacks def stop(self): - yield self.wallet_manager.stop() + yield f2d(self.wallet_manager.stop()) self.wallet_manager = None diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 3b347a154..1139affb3 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -28,6 +28,7 @@ from lbryschema.decode import smart_decode from lbrynet.core.system_info import get_lbrynet_version from lbrynet import conf from lbrynet.reflector import reupload +from lbrynet.daemon.Components import d2f, f2d from lbrynet.daemon.Components import WALLET_COMPONENT, DATABASE_COMPONENT, DHT_COMPONENT, BLOB_COMPONENT from lbrynet.daemon.Components import STREAM_IDENTIFIER_COMPONENT, FILE_MANAGER_COMPONENT, RATE_LIMITER_COMPONENT from lbrynet.daemon.Components import EXCHANGE_RATE_MANAGER_COMPONENT, PAYMENT_RATE_COMPONENT, UPNP_COMPONENT diff --git a/tests/integration/wallet/test_commands.py b/tests/integration/wallet/test_commands.py index 55d3ce25b..f7fa6a1bd 100644 --- a/tests/integration/wallet/test_commands.py +++ b/tests/integration/wallet/test_commands.py @@ -1,11 +1,12 @@ +from twisted.internet import asyncioreactor +asyncioreactor.install() import json +import asyncio import tempfile import logging -import asyncio from types import SimpleNamespace -from twisted.internet import defer -from orchstr8.testcase import IntegrationTestCase, d2f +from orchstr8.testcase import IntegrationTestCase import lbryschema lbryschema.BLOCKCHAIN_NAME = 'lbrycrd_regtest' @@ -16,9 +17,9 @@ from lbrynet.daemon.Daemon import Daemon from lbrynet.wallet.manager import LbryWalletManager from lbrynet.daemon.Components import WalletComponent, DHTComponent, HashAnnouncerComponent, \ ExchangeRateManagerComponent +from lbrynet.daemon.Components import REFLECTOR_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT from lbrynet.daemon.Components import UPnPComponent -from lbrynet.daemon.Components import REFLECTOR_COMPONENT -from lbrynet.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT +from lbrynet.daemon.Components import d2f from lbrynet.daemon.ComponentManager import ComponentManager from lbrynet.daemon.auth.server import jsonrpc_dumps_pretty @@ -92,8 +93,8 @@ class CommandTestCase(IntegrationTestCase): timeout = 180 WALLET_MANAGER = LbryWalletManager - async def setUp(self): - await super().setUp() + async def asyncSetUp(self): + await super().asyncSetUp() if self.VERBOSE: log.setLevel(logging.DEBUG) @@ -111,8 +112,8 @@ class CommandTestCase(IntegrationTestCase): lbry_conf.settings['known_dht_nodes'] = [] lbry_conf.settings.node_id = None - await d2f(self.account.ensure_address_gap()) - address = (await d2f(self.account.receiving.get_addresses(limit=1, only_usable=True)))[0] + await self.account.ensure_address_gap() + address = (await self.account.receiving.get_addresses(limit=1, only_usable=True))[0] sendtxid = await self.blockchain.send_to_address(address, 10) await self.confirm_tx(sendtxid) await self.generate(5) @@ -140,8 +141,8 @@ class CommandTestCase(IntegrationTestCase): self.daemon.wallet_manager = self.wallet_component.wallet_manager self.manager.old_db = self.daemon.storage - async def tearDown(self): - await super().tearDown() + async def asyncTearDown(self): + await super().asyncTearDown() self.wallet_component._running = False await d2f(self.daemon._shutdown()) @@ -151,35 +152,27 @@ class CommandTestCase(IntegrationTestCase): await self.generate(1) await self.on_transaction_id(txid) - def d_confirm_tx(self, txid): - return defer.Deferred.fromFuture(asyncio.ensure_future(self.confirm_tx(txid))) - async def generate(self, blocks): """ Ask lbrycrd to generate some blocks and wait until ledger has them. """ await self.blockchain.generate(blocks) await self.ledger.on_header.where(self.blockchain.is_expected_block) - def d_generate(self, blocks): - return defer.Deferred.fromFuture(asyncio.ensure_future(self.generate(blocks))) - - def out(self, d): + async def out(self, awaitable): """ Converts Daemon API call results (dictionary) to JSON and then back to a dictionary. """ - d.addCallback(lambda o: json.loads(jsonrpc_dumps_pretty(o, ledger=self.ledger))['result']) - return d + return json.loads(jsonrpc_dumps_pretty(await awaitable, ledger=self.ledger))['result'] class EpicAdventuresOfChris45(CommandTestCase): VERBOSE = False - @defer.inlineCallbacks - def test_no_this_is_not_a_test_its_an_adventure(self): + async def test_no_this_is_not_a_test_its_an_adventure(self): # Chris45 is an avid user of LBRY and this is his story. It's fact and fiction # and everything in between; it's also the setting of some record setting # integration tests. # Chris45 starts everyday by checking his balance. - result = yield self.daemon.jsonrpc_account_balance() + result = await self.daemon.jsonrpc_account_balance() self.assertEqual(result, '10.0') # "10 LBC, yippy! I can do a lot with that.", he thinks to himself, # enthusiastically. But he is hungry so he goes into the kitchen @@ -188,12 +181,12 @@ class EpicAdventuresOfChris45(CommandTestCase): # While making the spamdwich he wonders... has anyone on LBRY # registered the @spam channel yet? "I should do that!" he # exclaims and goes back to his computer to do just that! - channel = yield self.out(self.daemon.jsonrpc_channel_new('@spam', "1.0")) + channel = await self.out(self.daemon.jsonrpc_channel_new('@spam', "1.0")) self.assertTrue(channel['success']) - yield self.d_confirm_tx(channel['tx']['txid']) + await self.confirm_tx(channel['tx']['txid']) # Do we have it locally? - channels = yield self.out(self.daemon.jsonrpc_channel_list()) + channels = await self.out(self.daemon.jsonrpc_channel_list()) self.assertEqual(len(channels), 1) self.assertEqual(channels[0]['name'], '@spam') @@ -201,16 +194,16 @@ class EpicAdventuresOfChris45(CommandTestCase): # way into the mempool and then a block and then into the claimtrie, # Chris doesn't sit idly by: he checks his balance! - result = yield self.daemon.jsonrpc_account_balance() + result = await self.daemon.jsonrpc_account_balance() self.assertEqual(result, '8.989893') # He waits for 6 more blocks (confirmations) to make sure the balance has been settled. - yield self.d_generate(6) - result = yield self.daemon.jsonrpc_account_balance(confirmations=6) + await self.generate(6) + result = await self.daemon.jsonrpc_account_balance(confirmations=6) self.assertEqual(result, '8.989893') # And is the channel resolvable and empty? - response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam')) + response = await self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam')) self.assertIn('lbry://@spam', response) self.assertIn('certificate', response['lbry://@spam']) @@ -227,27 +220,27 @@ class EpicAdventuresOfChris45(CommandTestCase): file.write(b'yada yada yada!') file.write(b'the end') file.flush() - claim1 = yield self.out(self.daemon.jsonrpc_publish( + claim1 = await self.out(self.daemon.jsonrpc_publish( 'hovercraft', '1.0', file_path=file.name, channel_id=channel['claim_id'] )) self.assertTrue(claim1['success']) - yield self.d_confirm_tx(claim1['tx']['txid']) + await self.confirm_tx(claim1['tx']['txid']) # He quickly checks the unconfirmed balance to make sure everything looks # correct. - result = yield self.daemon.jsonrpc_account_balance() + result = await self.daemon.jsonrpc_account_balance() self.assertEqual(result, '7.969786') # Also checks that his new story can be found on the blockchain before # giving the link to all his friends. - response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft')) + response = await self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft')) self.assertIn('lbry://@spam/hovercraft', response) self.assertIn('claim', response['lbry://@spam/hovercraft']) # He goes to tell everyone about it and in the meantime 5 blocks are confirmed. - yield self.d_generate(5) + await self.generate(5) # When he comes back he verifies the confirmed balance. - result = yield self.daemon.jsonrpc_account_balance() + result = await self.daemon.jsonrpc_account_balance() self.assertEqual(result, '7.969786') # As people start reading his story they discover some typos and notify @@ -258,47 +251,47 @@ class EpicAdventuresOfChris45(CommandTestCase): file.write(b'[typo fixing sounds being made]') file.write(b'yada yada yada!') file.flush() - claim2 = yield self.out(self.daemon.jsonrpc_publish( + claim2 = await self.out(self.daemon.jsonrpc_publish( 'hovercraft', '1.0', file_path=file.name, channel_name='@spam' )) self.assertTrue(claim2['success']) self.assertEqual(claim2['claim_id'], claim1['claim_id']) - yield self.d_confirm_tx(claim2['tx']['txid']) + await self.confirm_tx(claim2['tx']['txid']) # After some soul searching Chris decides that his story needs more # heart and a better ending. He takes down the story and begins the rewrite. - abandon = yield self.out(self.daemon.jsonrpc_claim_abandon(claim1['claim_id'])) + abandon = await self.out(self.daemon.jsonrpc_claim_abandon(claim1['claim_id'])) self.assertTrue(abandon['success']) - yield self.d_confirm_tx(abandon['tx']['txid']) + await self.confirm_tx(abandon['tx']['txid']) # And now checks that the claim doesn't resolve anymore. - response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft')) + response = await self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft')) self.assertNotIn('claim', response['lbry://@spam/hovercraft']) # After abandoning he just waits for his LBCs to be returned to his account - yield self.d_generate(5) - result = yield self.daemon.jsonrpc_account_balance() + await self.generate(5) + result = await self.daemon.jsonrpc_account_balance() self.assertEqual(result, '8.9693585') # Amidst all this Chris receives a call from his friend Ramsey # who says that it is of utmost urgency that Chris transfer him # 1 LBC to which Chris readily obliges - ramsey_account_id = (yield self.daemon.jsonrpc_account_create("Ramsey"))['id'] + ramsey_account_id = (await self.daemon.jsonrpc_account_create("Ramsey"))['id'] ramsey_account = self.daemon.get_account_or_error(ramsey_account_id) - ramsey_address = yield self.daemon.jsonrpc_address_unused(ramsey_account_id) - result = yield self.out(self.daemon.jsonrpc_wallet_send('1.0', ramsey_address)) + ramsey_address = await self.daemon.jsonrpc_address_unused(ramsey_account_id) + result = await self.out(self.daemon.jsonrpc_wallet_send('1.0', ramsey_address)) self.assertIn("txid", result) - yield self.d_confirm_tx(result['txid']) + await self.confirm_tx(result['txid']) # Chris then eagerly waits for 6 confirmations to check his balance and then calls Ramsey to verify whether # he received it or not - yield self.d_generate(5) - result = yield self.daemon.jsonrpc_account_balance() + await self.generate(5) + result = await self.daemon.jsonrpc_account_balance() # Chris' balance was correct self.assertEqual(result, '7.9692345') # Ramsey too assured him that he had received the 1 LBC and thanks him - result = yield self.daemon.jsonrpc_account_balance(ramsey_account_id) + result = await self.daemon.jsonrpc_account_balance(ramsey_account_id) self.assertEqual(result, '1.0') # After Chris is done with all the "helping other people" stuff he decides that it's time to @@ -313,50 +306,50 @@ class EpicAdventuresOfChris45(CommandTestCase): 'fresh-start', '1.0', file_path=file.name, channel_name='@spam' )) self.assertTrue(claim3['success']) - yield self.d_confirm_tx(claim3['tx']['txid']) + await self.confirm_tx(claim3['tx']['txid']) - yield self.d_generate(5) + await self.generate(5) # He gives the link of his story to all his friends and hopes that this is the much needed break for him uri = 'lbry://@spam/fresh-start' # And voila, and bravo and encore! His Best Friend Ramsey read the story and immediately knew this was a hit # Now to keep this claim winning on the lbry blockchain he immediately supports the claim - tx = yield self.out(self.daemon.jsonrpc_claim_new_support( + tx = await self.out(self.daemon.jsonrpc_claim_new_support( 'fresh-start', claim3['claim_id'], '0.2', account_id=ramsey_account_id )) - yield self.d_confirm_tx(tx['txid']) + await self.confirm_tx(tx['txid']) # And check if his support showed up - resolve_result = yield self.out(self.daemon.jsonrpc_resolve(uri=uri)) + resolve_result = await self.out(self.daemon.jsonrpc_resolve(uri=uri)) # It obviously did! Because, blockchain baby \O/ self.assertEqual(resolve_result[uri]['claim']['supports'][0]['amount'], 0.2) self.assertEqual(resolve_result[uri]['claim']['supports'][0]['txid'], tx['txid']) - yield self.d_generate(5) + await self.generate(5) # Now he also wanted to support the original creator of the Award Winning Novel # So he quickly decides to send a tip to him - tx = yield self.out( + tx = await self.out( self.daemon.jsonrpc_claim_tip(claim3['claim_id'], '0.3', account_id=ramsey_account_id)) - yield self.d_confirm_tx(tx['txid']) + await self.confirm_tx(tx['txid']) # And again checks if it went to the just right place - resolve_result = yield self.out(self.daemon.jsonrpc_resolve(uri=uri)) + resolve_result = await self.out(self.daemon.jsonrpc_resolve(uri=uri)) # Which it obviously did. Because....????? self.assertEqual(resolve_result[uri]['claim']['supports'][1]['amount'], 0.3) self.assertEqual(resolve_result[uri]['claim']['supports'][1]['txid'], tx['txid']) - yield self.d_generate(5) + await self.generate(5) # Seeing the ravishing success of his novel Chris adds support to his claim too - tx = yield self.out(self.daemon.jsonrpc_claim_new_support('fresh-start', claim3['claim_id'], '0.4')) - yield self.d_confirm_tx(tx['txid']) + tx = await self.out(self.daemon.jsonrpc_claim_new_support('fresh-start', claim3['claim_id'], '0.4')) + await self.confirm_tx(tx['txid']) # And check if his support showed up - resolve_result = yield self.out(self.daemon.jsonrpc_resolve(uri=uri)) + resolve_result = await self.out(self.daemon.jsonrpc_resolve(uri=uri)) # It did! self.assertEqual(resolve_result[uri]['claim']['supports'][2]['amount'], 0.4) self.assertEqual(resolve_result[uri]['claim']['supports'][2]['txid'], tx['txid']) - yield self.d_generate(5) + await self.generate(5) # Now Ramsey who is a singer by profession, is preparing for his new "gig". He has everything in place for that # the instruments, the theatre, the ads, everything, EXCEPT lyrics!! He panicked.. But then he remembered @@ -373,42 +366,41 @@ class EpicAdventuresOfChris45(CommandTestCase): 'hit-song', '1.0', file_path=file.name, channel_id=channel['claim_id'] )) self.assertTrue(claim4['success']) - yield self.d_confirm_tx(claim4['tx']['txid']) + await self.confirm_tx(claim4['tx']['txid']) - yield self.d_generate(5) + await self.generate(5) # He sends the link to Ramsey, all happy and proud uri = 'lbry://@spam/hit-song' # But sadly Ramsey wasn't so pleased. It was hard for him to tell Chris... # Chris, though a bit heartbroken, abandoned the claim for now, but instantly started working on new hit lyrics - abandon = yield self.out(self.daemon.jsonrpc_claim_abandon(txid=claim4['tx']['txid'], nout=0)) + abandon = await self.out(self.daemon.jsonrpc_claim_abandon(txid=claim4['tx']['txid'], nout=0)) self.assertTrue(abandon['success']) - yield self.d_confirm_tx(abandon['tx']['txid']) + await self.confirm_tx(abandon['tx']['txid']) # He them checks that the claim doesn't resolve anymore. - response = yield self.out(self.daemon.jsonrpc_resolve(uri=uri)) + response = await self.out(self.daemon.jsonrpc_resolve(uri=uri)) self.assertNotIn('claim', response[uri]) class AccountManagement(CommandTestCase): - VERBOSE = False + VERBOSE = True - @defer.inlineCallbacks - def test_performing_account_management_commands(self): + async def test_performing_account_management_commands(self): # check initial account - response = yield self.daemon.jsonrpc_account_list() + response = await self.daemon.jsonrpc_account_list() self.assertEqual(len(response['lbc_regtest']), 1) # change account name and gap account_id = response['lbc_regtest'][0]['id'] - yield self.daemon.jsonrpc_account_set( + await self.daemon.jsonrpc_account_set( account_id=account_id, new_name='test account', receiving_gap=95, receiving_max_uses=96, change_gap=97, change_max_uses=98 ) - response = (yield self.daemon.jsonrpc_account_list())['lbc_regtest'][0] + response = (await self.daemon.jsonrpc_account_list())['lbc_regtest'][0] self.assertEqual(response['name'], 'test account') self.assertEqual( response['address_generator']['receiving'], @@ -420,30 +412,30 @@ class AccountManagement(CommandTestCase): ) # create another account - yield self.daemon.jsonrpc_account_create('second account') - response = yield self.daemon.jsonrpc_account_list() + await self.daemon.jsonrpc_account_create('second account') + response = await self.daemon.jsonrpc_account_list() self.assertEqual(len(response['lbc_regtest']), 2) self.assertEqual(response['lbc_regtest'][1]['name'], 'second account') account_id2 = response['lbc_regtest'][1]['id'] # make new account the default self.daemon.jsonrpc_account_set(account_id=account_id2, default=True) - response = yield self.daemon.jsonrpc_account_list(show_seed=True) + response = await self.daemon.jsonrpc_account_list(show_seed=True) self.assertEqual(response['lbc_regtest'][0]['name'], 'second account') account_seed = response['lbc_regtest'][1]['seed'] # remove account - yield self.daemon.jsonrpc_account_remove(response['lbc_regtest'][1]['id']) - response = yield self.daemon.jsonrpc_account_list() + await self.daemon.jsonrpc_account_remove(response['lbc_regtest'][1]['id']) + response = await self.daemon.jsonrpc_account_list() self.assertEqual(len(response['lbc_regtest']), 1) # add account - yield self.daemon.jsonrpc_account_add('recreated account', seed=account_seed) - response = yield self.daemon.jsonrpc_account_list() + await self.daemon.jsonrpc_account_add('recreated account', seed=account_seed) + response = await self.daemon.jsonrpc_account_list() self.assertEqual(len(response['lbc_regtest']), 2) self.assertEqual(response['lbc_regtest'][1]['name'], 'recreated account') # list specific account - response = yield self.daemon.jsonrpc_account_list(account_id, include_claims=True) + response = await self.daemon.jsonrpc_account_list(account_id, include_claims=True) self.assertEqual(response['name'], 'recreated account') diff --git a/tox.ini b/tox.ini index 09f9dd7a8..7a25af791 100644 --- a/tox.ini +++ b/tox.ini @@ -17,6 +17,6 @@ setenv = commands = orchstr8 download coverage run -p --source={envsitepackagesdir}/lbrynet -m twisted.trial --reactor=asyncio integration.cli - coverage run -p --source={envsitepackagesdir}/lbrynet -m twisted.trial --reactor=asyncio integration.wallet.test_transactions.BasicTransactionTest - coverage run -p --source={envsitepackagesdir}/lbrynet -m twisted.trial --reactor=asyncio integration.wallet.test_commands.EpicAdventuresOfChris45 - coverage run -p --source={envsitepackagesdir}/lbrynet -m twisted.trial --reactor=asyncio integration.wallet.test_commands.AccountManagement + coverage run -p --source={envsitepackagesdir}/lbrynet -m unittest integration.wallet.test_transactions.BasicTransactionTest + coverage run -p --source={envsitepackagesdir}/lbrynet -m unittest integration.wallet.test_commands.EpicAdventuresOfChris45 + coverage run -p --source={envsitepackagesdir}/lbrynet -m unittest integration.wallet.test_commands.AccountManagement