2018-08-17 03:42:38 +02:00
|
|
|
import json
|
2018-07-12 21:05:24 +02:00
|
|
|
import tempfile
|
2018-07-30 01:15:06 +02:00
|
|
|
import logging
|
2018-07-31 15:48:57 +02:00
|
|
|
import asyncio
|
2018-07-12 21:44:07 +02:00
|
|
|
from types import SimpleNamespace
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-06 07:17:20 +02:00
|
|
|
from twisted.internet import defer
|
2018-07-05 04:16:02 +02:00
|
|
|
from orchstr8.testcase import IntegrationTestCase, d2f
|
|
|
|
|
|
|
|
import lbryschema
|
|
|
|
lbryschema.BLOCKCHAIN_NAME = 'lbrycrd_regtest'
|
|
|
|
|
|
|
|
from lbrynet import conf as lbry_conf
|
2018-07-31 15:48:57 +02:00
|
|
|
from lbrynet.dht.node import Node
|
2018-07-05 04:16:02 +02:00
|
|
|
from lbrynet.daemon.Daemon import Daemon
|
|
|
|
from lbrynet.wallet.manager import LbryWalletManager
|
2018-07-31 15:48:57 +02:00
|
|
|
from lbrynet.daemon.Components import WalletComponent, DHTComponent, HashAnnouncerComponent, ExchangeRateManagerComponent
|
2018-08-05 06:55:22 +02:00
|
|
|
from lbrynet.daemon.Components import UPnPComponent
|
2018-08-17 03:42:38 +02:00
|
|
|
from lbrynet.daemon.Components import REFLECTOR_COMPONENT
|
|
|
|
from lbrynet.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT
|
2018-07-22 03:12:33 +02:00
|
|
|
from lbrynet.daemon.ComponentManager import ComponentManager
|
2018-08-17 03:42:38 +02:00
|
|
|
from lbrynet.daemon.auth.server import jsonrpc_dumps_pretty
|
2018-07-05 04:16:02 +02:00
|
|
|
|
|
|
|
|
2018-07-30 01:15:06 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-08-05 06:55:22 +02:00
|
|
|
class FakeUPnP(UPnPComponent):
|
|
|
|
|
|
|
|
def __init__(self, component_manager):
|
|
|
|
self.component_manager = component_manager
|
|
|
|
self._running = False
|
|
|
|
self.use_upnp = False
|
|
|
|
self.upnp_redirects = {}
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
class FakeDHT(DHTComponent):
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def start(self):
|
|
|
|
self.dht_node = Node()
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-07-12 19:23:18 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
class FakeExchangeRateComponent(ExchangeRateManagerComponent):
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def start(self):
|
|
|
|
self.exchange_rate_manager = SimpleNamespace()
|
2018-07-12 21:44:07 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def stop(self):
|
|
|
|
pass
|
2018-07-12 21:44:07 +02:00
|
|
|
|
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
class FakeHashAnnouncerComponent(HashAnnouncerComponent):
|
2018-07-12 21:44:07 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def start(self):
|
|
|
|
self.hash_announcer = SimpleNamespace()
|
2018-07-12 21:44:07 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def stop(self):
|
|
|
|
pass
|
2018-07-12 21:44:07 +02:00
|
|
|
|
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
class FakeAnalytics:
|
2018-07-12 21:44:07 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
@property
|
|
|
|
def is_started(self):
|
|
|
|
return True
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def send_new_channel(self):
|
|
|
|
pass
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def shutdown(self):
|
|
|
|
pass
|
2018-07-12 21:44:07 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def send_claim_action(self, action):
|
|
|
|
pass
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-07-05 04:16:02 +02:00
|
|
|
|
|
|
|
class CommandTestCase(IntegrationTestCase):
|
|
|
|
|
|
|
|
WALLET_MANAGER = LbryWalletManager
|
|
|
|
|
|
|
|
async def setUp(self):
|
|
|
|
await super().setUp()
|
|
|
|
|
2018-07-30 01:15:06 +02:00
|
|
|
if self.VERBOSE:
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
logging.getLogger('lbrynet.core').setLevel(logging.DEBUG)
|
|
|
|
|
2018-07-05 04:16:02 +02:00
|
|
|
lbry_conf.settings = None
|
|
|
|
lbry_conf.initialize_settings(load_conf_file=False)
|
|
|
|
lbry_conf.settings['data_dir'] = self.stack.wallet.data_path
|
|
|
|
lbry_conf.settings['lbryum_wallet_dir'] = self.stack.wallet.data_path
|
|
|
|
lbry_conf.settings['download_directory'] = self.stack.wallet.data_path
|
|
|
|
lbry_conf.settings['use_upnp'] = False
|
2018-07-31 15:48:57 +02:00
|
|
|
lbry_conf.settings['reflect_uploads'] = False
|
2018-07-05 04:16:02 +02:00
|
|
|
lbry_conf.settings['blockchain_name'] = 'lbrycrd_regtest'
|
|
|
|
lbry_conf.settings['lbryum_servers'] = [('localhost', 50001)]
|
|
|
|
lbry_conf.settings['known_dht_nodes'] = []
|
|
|
|
lbry_conf.settings.node_id = None
|
|
|
|
|
|
|
|
await d2f(self.account.ensure_address_gap())
|
2018-07-15 21:50:32 +02:00
|
|
|
address = (await d2f(self.account.receiving.get_addresses(1, only_usable=True)))[0]
|
|
|
|
sendtxid = await self.blockchain.send_to_address(address, 10)
|
2018-07-30 01:15:06 +02:00
|
|
|
await self.confirm_tx(sendtxid)
|
2018-08-01 04:59:51 +02:00
|
|
|
await self.generate(5)
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
def wallet_maker(component_manager):
|
|
|
|
self.wallet_component = WalletComponent(component_manager)
|
2018-08-26 05:20:43 +02:00
|
|
|
self.wallet_component.wallet_manager = self.manager
|
2018-07-31 15:48:57 +02:00
|
|
|
self.wallet_component._running = True
|
|
|
|
return self.wallet_component
|
|
|
|
|
2018-08-05 06:55:22 +02:00
|
|
|
skip = [
|
|
|
|
#UPNP_COMPONENT,
|
|
|
|
PEER_PROTOCOL_SERVER_COMPONENT,
|
|
|
|
REFLECTOR_COMPONENT
|
|
|
|
]
|
2018-07-22 03:12:33 +02:00
|
|
|
analytics_manager = FakeAnalytics()
|
2018-07-31 15:48:57 +02:00
|
|
|
self.daemon = Daemon(analytics_manager, ComponentManager(
|
2018-08-05 06:55:22 +02:00
|
|
|
analytics_manager=analytics_manager,
|
|
|
|
skip_components=skip, wallet=wallet_maker,
|
|
|
|
dht=FakeDHT, hash_announcer=FakeHashAnnouncerComponent,
|
|
|
|
exchange_rate_manager=FakeExchangeRateComponent,
|
|
|
|
upnp=FakeUPnP
|
2018-07-31 15:48:57 +02:00
|
|
|
))
|
2018-08-05 06:55:22 +02:00
|
|
|
#for component in skip:
|
|
|
|
# self.daemon.component_attributes.pop(component, None)
|
2018-07-31 15:48:57 +02:00
|
|
|
await d2f(self.daemon.setup())
|
2018-08-26 06:00:45 +02:00
|
|
|
self.daemon.wallet_manager = self.wallet_component.wallet_manager
|
2018-08-05 06:55:22 +02:00
|
|
|
self.manager.old_db = self.daemon.storage
|
2018-07-31 15:48:57 +02:00
|
|
|
|
|
|
|
async def tearDown(self):
|
|
|
|
await super().tearDown()
|
|
|
|
self.wallet_component._running = False
|
|
|
|
await d2f(self.daemon._shutdown())
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-30 01:15:06 +02:00
|
|
|
async def confirm_tx(self, txid):
|
|
|
|
""" Wait for tx to be in mempool, then generate a block, wait for tx to be in a block. """
|
|
|
|
await self.on_transaction_id(txid)
|
|
|
|
await self.generate(1)
|
|
|
|
await self.on_transaction_id(txid)
|
2018-07-31 15:48:57 +02:00
|
|
|
|
|
|
|
def d_confirm_tx(self, txid):
|
|
|
|
return defer.Deferred.fromFuture(asyncio.ensure_future(self.confirm_tx(txid)))
|
2018-07-30 01:15:06 +02:00
|
|
|
|
|
|
|
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)
|
2018-07-31 15:48:57 +02:00
|
|
|
|
|
|
|
def d_generate(self, blocks):
|
|
|
|
return defer.Deferred.fromFuture(asyncio.ensure_future(self.generate(blocks)))
|
2018-07-30 01:15:06 +02:00
|
|
|
|
2018-08-17 03:42:38 +02:00
|
|
|
def out(self, d):
|
|
|
|
""" 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
|
|
|
|
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
class EpicAdventuresOfChris45(CommandTestCase):
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
VERBOSE = False
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-31 15:48:57 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-08-01 04:59:51 +02:00
|
|
|
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.
|
2018-07-06 07:17:20 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# Chris45 starts everyday by checking his balance.
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-07-17 05:32:37 +02:00
|
|
|
self.assertEqual(result, 10)
|
2018-08-01 04:59:51 +02:00
|
|
|
# "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
|
|
|
|
# to make himself a spamdwich.
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# 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!
|
2018-08-17 03:42:38 +02:00
|
|
|
channel = yield self.out(self.daemon.jsonrpc_channel_new('@spam', 1))
|
2018-07-17 05:32:37 +02:00
|
|
|
self.assertTrue(channel['success'])
|
2018-08-17 03:42:38 +02:00
|
|
|
yield self.d_confirm_tx(channel['tx']['txid'])
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-08-04 03:39:48 +02:00
|
|
|
# Do we have it locally?
|
2018-08-17 03:42:38 +02:00
|
|
|
channels = yield self.out(self.daemon.jsonrpc_channel_list())
|
2018-08-04 03:39:48 +02:00
|
|
|
self.assertEqual(len(channels), 1)
|
2018-08-04 18:10:41 +02:00
|
|
|
self.assertEqual(channels[0]['name'], '@spam')
|
2018-08-04 03:39:48 +02:00
|
|
|
self.assertTrue(channels[0]['have_certificate'])
|
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# As the new channel claim travels through the intertubes and makes its
|
|
|
|
# way into the mempool and then a block and then into the claimtrie,
|
|
|
|
# Chris doesn't sit idly by: he checks his balance!
|
|
|
|
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-08-01 04:59:51 +02:00
|
|
|
self.assertEqual(result, 0)
|
|
|
|
|
|
|
|
# "Oh! No! It's all gone? Did I make a mistake in entering the amount?"
|
|
|
|
# exclaims Chris, then he remembers there is a 6 block confirmation window
|
|
|
|
# to make sure the TX is really going to stay in the blockchain. And he only
|
|
|
|
# had one UTXO that morning.
|
|
|
|
|
|
|
|
# To get the unconfirmed balance he has to pass the '--include-unconfirmed'
|
|
|
|
# flag to lbrynet:
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance(include_unconfirmed=True)
|
2018-07-17 05:32:37 +02:00
|
|
|
self.assertEqual(result, 8.99)
|
2018-08-01 04:59:51 +02:00
|
|
|
# "Well, that's a relief." he thinks to himself as he exhales a sigh of relief.
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# He waits for a block
|
|
|
|
yield self.d_generate(1)
|
|
|
|
# and checks the confirmed balance again.
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-07-22 03:12:33 +02:00
|
|
|
self.assertEqual(result, 0)
|
2018-08-01 04:59:51 +02:00
|
|
|
# Still zero.
|
2018-07-22 03:12:33 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# But it's only at 2 confirmations, so he waits another 3
|
|
|
|
yield self.d_generate(3)
|
|
|
|
# and checks again.
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-08-01 04:59:51 +02:00
|
|
|
self.assertEqual(result, 0)
|
|
|
|
# Still zero.
|
2018-07-22 03:12:33 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# Just one more confirmation
|
|
|
|
yield self.d_generate(1)
|
|
|
|
# and it should be 6 total, enough to get the correct balance!
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-07-22 03:12:33 +02:00
|
|
|
self.assertEqual(result, 8.99)
|
2018-08-01 04:59:51 +02:00
|
|
|
# Like a Swiss watch (right niko?) the blockchain never disappoints! We're
|
|
|
|
# at 6 confirmations and the total is correct.
|
|
|
|
|
2018-08-04 18:10:41 +02:00
|
|
|
# And is the channel resolvable and empty?
|
2018-08-17 03:42:38 +02:00
|
|
|
response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam'))
|
2018-08-04 18:10:41 +02:00
|
|
|
self.assertIn('lbry://@spam', response)
|
|
|
|
self.assertIn('certificate', response['lbry://@spam'])
|
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# "What goes well with spam?" ponders Chris...
|
|
|
|
# "A hovercraft with eels!" he exclaims.
|
|
|
|
# "That's what goes great with spam!" he further confirms.
|
2018-07-22 03:12:33 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# And so, many hours later, Chris is finished writing his epic story
|
|
|
|
# about eels driving a hovercraft across the wetlands while eating spam
|
|
|
|
# and decides it's time to publish it to the @spam channel.
|
2018-07-12 21:05:24 +02:00
|
|
|
with tempfile.NamedTemporaryFile() as file:
|
2018-08-01 04:59:51 +02:00
|
|
|
file.write(b'blah blah blah...')
|
|
|
|
file.write(b'[insert long story about eels driving hovercraft]')
|
|
|
|
file.write(b'yada yada yada!')
|
|
|
|
file.write(b'the end')
|
2018-07-12 21:05:24 +02:00
|
|
|
file.flush()
|
2018-08-17 03:42:38 +02:00
|
|
|
claim1 = yield self.out(self.daemon.jsonrpc_publish(
|
2018-07-30 01:15:06 +02:00
|
|
|
'hovercraft', 1, file_path=file.name, channel_name='@spam', channel_id=channel['claim_id']
|
2018-08-17 03:42:38 +02:00
|
|
|
))
|
2018-08-01 04:59:51 +02:00
|
|
|
self.assertTrue(claim1['success'])
|
2018-08-17 03:42:38 +02:00
|
|
|
yield self.d_confirm_tx(claim1['tx']['txid'])
|
2018-07-30 01:15:06 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# He quickly checks the unconfirmed balance to make sure everything looks
|
|
|
|
# correct.
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance(include_unconfirmed=True)
|
2018-07-30 01:15:06 +02:00
|
|
|
self.assertEqual(round(result, 2), 7.97)
|
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# Also checks that his new story can be found on the blockchain before
|
|
|
|
# giving the link to all his friends.
|
2018-08-17 03:42:38 +02:00
|
|
|
response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft'))
|
2018-07-30 01:15:06 +02:00
|
|
|
self.assertIn('lbry://@spam/hovercraft', response)
|
2018-08-04 03:39:48 +02:00
|
|
|
self.assertIn('claim', response['lbry://@spam/hovercraft'])
|
2018-07-30 01:15:06 +02:00
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# He goes to tell everyone about it and in the meantime 5 blocks are confirmed.
|
2018-07-31 15:48:57 +02:00
|
|
|
yield self.d_generate(5)
|
2018-08-01 04:59:51 +02:00
|
|
|
# When he comes back he verifies the confirmed balance.
|
2018-08-26 06:00:45 +02:00
|
|
|
result = yield self.daemon.jsonrpc_account_balance()
|
2018-07-30 01:15:06 +02:00
|
|
|
self.assertEqual(round(result, 2), 7.97)
|
|
|
|
|
2018-08-01 04:59:51 +02:00
|
|
|
# As people start reading his story they discover some typos and notify
|
|
|
|
# Chris who explains in despair "Oh! Noooooos!" but then remembers
|
|
|
|
# "No big deal! I can update my claim." And so he updates his claim.
|
2018-07-30 01:15:06 +02:00
|
|
|
with tempfile.NamedTemporaryFile() as file:
|
2018-08-01 04:59:51 +02:00
|
|
|
file.write(b'blah blah blah...')
|
|
|
|
file.write(b'[typo fixing sounds being made]')
|
|
|
|
file.write(b'yada yada yada!')
|
2018-07-30 01:15:06 +02:00
|
|
|
file.flush()
|
2018-08-17 03:42:38 +02:00
|
|
|
claim2 = yield self.out(self.daemon.jsonrpc_publish(
|
2018-07-30 01:15:06 +02:00
|
|
|
'hovercraft', 1, file_path=file.name, channel_name='@spam', channel_id=channel['claim_id']
|
2018-08-17 03:42:38 +02:00
|
|
|
))
|
2018-08-01 04:59:51 +02:00
|
|
|
self.assertTrue(claim2['success'])
|
2018-08-04 18:10:41 +02:00
|
|
|
self.assertEqual(claim2['claim_id'], claim1['claim_id'])
|
2018-08-17 03:42:38 +02:00
|
|
|
yield self.d_confirm_tx(claim2['tx']['txid'])
|
2018-08-01 04:59:51 +02:00
|
|
|
|
|
|
|
# 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.
|
2018-08-17 03:42:38 +02:00
|
|
|
abandon = yield self.out(self.daemon.jsonrpc_claim_abandon(claim1['claim_id']))
|
2018-08-01 04:59:51 +02:00
|
|
|
self.assertTrue(abandon['success'])
|
2018-08-17 03:42:38 +02:00
|
|
|
yield self.d_confirm_tx(abandon['tx']['txid'])
|
2018-08-04 03:39:48 +02:00
|
|
|
|
2018-08-04 18:10:41 +02:00
|
|
|
# And now check that the claim doesn't resolve anymore.
|
2018-08-17 03:42:38 +02:00
|
|
|
response = yield self.out(self.daemon.jsonrpc_resolve(uri='lbry://@spam/hovercraft'))
|
2018-08-04 03:39:48 +02:00
|
|
|
self.assertNotIn('claim', response['lbry://@spam/hovercraft'])
|