2018-07-12 21:44:07 +02:00
|
|
|
import six
|
2018-07-22 03:12:33 +02:00
|
|
|
import asyncio
|
2018-07-12 21:05:24 +02:00
|
|
|
import tempfile
|
2018-07-12 21:44:07 +02:00
|
|
|
from types import SimpleNamespace
|
|
|
|
from binascii import hexlify
|
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
|
|
|
|
from torba.constants import COIN
|
2018-07-22 03:12:33 +02:00
|
|
|
from lbrynet.core.cryptoutils import get_lbry_hash_obj
|
2018-07-05 04:16:02 +02:00
|
|
|
|
|
|
|
import lbryschema
|
|
|
|
lbryschema.BLOCKCHAIN_NAME = 'lbrycrd_regtest'
|
|
|
|
|
|
|
|
from lbrynet import conf as lbry_conf
|
|
|
|
from lbrynet.daemon.Daemon import Daemon
|
|
|
|
from lbrynet.wallet.manager import LbryWalletManager
|
2018-07-12 19:23:18 +02:00
|
|
|
from lbrynet.daemon.Components import WalletComponent, FileManager, SessionComponent, DatabaseComponent
|
2018-07-22 03:12:33 +02:00
|
|
|
from lbrynet.daemon.ComponentManager import ComponentManager
|
2018-07-12 05:18:59 +02:00
|
|
|
from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager
|
2018-07-05 04:16:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeAnalytics:
|
|
|
|
def send_new_channel(self):
|
|
|
|
pass
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
def shutdown(self):
|
|
|
|
pass
|
|
|
|
|
2018-07-12 19:23:18 +02:00
|
|
|
def send_claim_action(self, action):
|
|
|
|
pass
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-07-12 21:44:07 +02:00
|
|
|
class FakeBlob:
|
|
|
|
def __init__(self):
|
|
|
|
self.data = []
|
|
|
|
self.blob_hash = 'abc'
|
|
|
|
self.length = 3
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self.data.append(data)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.data:
|
2018-07-22 03:12:33 +02:00
|
|
|
h = get_lbry_hash_obj()
|
|
|
|
h.update(b'hi')
|
|
|
|
return defer.succeed(h.hexdigest())
|
2018-07-12 21:44:07 +02:00
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
def get_is_verified(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def open_for_reading(self):
|
|
|
|
return six.StringIO('foo')
|
|
|
|
|
|
|
|
|
2018-07-12 21:05:24 +02:00
|
|
|
class FakeBlobManager:
|
|
|
|
def get_blob_creator(self):
|
2018-07-12 21:44:07 +02:00
|
|
|
return FakeBlob()
|
|
|
|
|
|
|
|
def creator_finished(self, blob_info, should_announce):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_blob(self, sd_hash):
|
|
|
|
return FakeBlob()
|
2018-07-12 21:05:24 +02:00
|
|
|
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
class FakeSession:
|
2018-07-12 21:05:24 +02:00
|
|
|
blob_manager = FakeBlobManager()
|
2018-07-12 21:44:07 +02:00
|
|
|
peer_finder = None
|
|
|
|
rate_limiter = None
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def payment_rate_manager(self):
|
|
|
|
obj = SimpleNamespace()
|
|
|
|
obj.min_blob_data_payment_rate = 1
|
|
|
|
return obj
|
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()
|
|
|
|
|
|
|
|
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
|
|
|
|
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-05 04:16:02 +02:00
|
|
|
await self.on_transaction_id(sendtxid)
|
|
|
|
await self.blockchain.generate(1)
|
2018-07-22 03:12:33 +02:00
|
|
|
await self.ledger.on_header.where(lambda n: n == 201)
|
2018-07-05 04:16:02 +02:00
|
|
|
await self.on_transaction_id(sendtxid)
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-22 03:12:33 +02:00
|
|
|
analytics_manager = FakeAnalytics()
|
|
|
|
self.daemon = Daemon(analytics_manager, ComponentManager(analytics_manager, skip_components=[
|
|
|
|
'wallet', 'database', 'session', 'fileManager'
|
|
|
|
]))
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-06 07:17:20 +02:00
|
|
|
wallet_component = WalletComponent(self.daemon.component_manager)
|
|
|
|
wallet_component.wallet = self.manager
|
|
|
|
wallet_component._running = True
|
2018-07-12 21:05:24 +02:00
|
|
|
self.daemon.wallet = self.manager
|
2018-07-06 07:17:20 +02:00
|
|
|
self.daemon.component_manager.components.add(wallet_component)
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-12 21:44:07 +02:00
|
|
|
storage_component = DatabaseComponent(self.daemon.component_manager)
|
|
|
|
await d2f(storage_component.start())
|
|
|
|
self.daemon.storage = storage_component.storage
|
|
|
|
self.daemon.wallet.old_db = self.daemon.storage
|
|
|
|
self.daemon.component_manager.components.add(storage_component)
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
session_component = SessionComponent(self.daemon.component_manager)
|
|
|
|
session_component.session = FakeSession()
|
|
|
|
session_component._running = True
|
2018-07-12 21:05:24 +02:00
|
|
|
self.daemon.session = session_component.session
|
2018-07-12 21:44:07 +02:00
|
|
|
self.daemon.session.storage = self.daemon.storage
|
|
|
|
self.daemon.session.wallet = self.daemon.wallet
|
|
|
|
self.daemon.session.blob_manager.storage = self.daemon.storage
|
2018-07-12 05:18:59 +02:00
|
|
|
self.daemon.component_manager.components.add(session_component)
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
file_manager = FileManager(self.daemon.component_manager)
|
|
|
|
file_manager.file_manager = EncryptedFileManager(session_component.session, True)
|
|
|
|
file_manager._running = True
|
2018-07-12 21:44:07 +02:00
|
|
|
self.daemon.file_manager = file_manager.file_manager
|
2018-07-12 05:18:59 +02:00
|
|
|
self.daemon.component_manager.components.add(file_manager)
|
2018-07-12 21:05:24 +02:00
|
|
|
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
class CommonWorkflowTests(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-17 05:32:37 +02:00
|
|
|
async def test_user_creating_channel_and_publishing_file(self):
|
2018-07-06 07:17:20 +02:00
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
# User checks their balance.
|
|
|
|
result = await d2f(self.daemon.jsonrpc_wallet_balance(include_unconfirmed=True))
|
|
|
|
self.assertEqual(result, 10)
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
# Decides to get a cool new channel.
|
|
|
|
channel = await d2f(self.daemon.jsonrpc_channel_new('@spam', 1))
|
|
|
|
self.assertTrue(channel['success'])
|
|
|
|
await self.on_transaction_id(channel['txid'])
|
|
|
|
await self.blockchain.generate(1)
|
2018-07-22 03:12:33 +02:00
|
|
|
await self.ledger.on_header.where(lambda n: n == 202)
|
2018-07-17 05:32:37 +02:00
|
|
|
await self.on_transaction_id(channel['txid'])
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
# Check balance again.
|
|
|
|
result = await d2f(self.daemon.jsonrpc_wallet_balance(include_unconfirmed=True))
|
|
|
|
self.assertEqual(result, 8.99)
|
2018-07-10 06:20:37 +02:00
|
|
|
|
2018-07-22 03:12:33 +02:00
|
|
|
# Confirmed balance is 0.
|
|
|
|
result = await d2f(self.daemon.jsonrpc_wallet_balance())
|
|
|
|
self.assertEqual(result, 0)
|
|
|
|
|
|
|
|
# Add some confirmations (there is already 1 confirmation, so we add 5 to equal 6 total).
|
|
|
|
await self.blockchain.generate(5)
|
|
|
|
await self.ledger.on_header.where(lambda n: n == 207)
|
|
|
|
|
|
|
|
# Check balance again after some confirmations.
|
|
|
|
result = await d2f(self.daemon.jsonrpc_wallet_balance())
|
|
|
|
self.assertEqual(result, 8.99)
|
|
|
|
|
2018-07-17 05:32:37 +02:00
|
|
|
# Now lets publish a hello world file to the channel.
|
2018-07-12 21:05:24 +02:00
|
|
|
with tempfile.NamedTemporaryFile() as file:
|
|
|
|
file.write(b'hello world!')
|
|
|
|
file.flush()
|
2018-07-17 05:32:37 +02:00
|
|
|
result = await d2f(self.daemon.jsonrpc_publish(
|
|
|
|
'foo', 1, file_path=file.name, channel_name='@spam', channel_id=channel['claim_id']
|
|
|
|
))
|
2018-07-12 21:05:24 +02:00
|
|
|
print(result)
|
2018-07-22 03:12:33 +02:00
|
|
|
# test fails to cleanup on travis
|
|
|
|
await asyncio.sleep(5)
|