From 80990b959c352f5ac031cbb8247de3f5a7caa0fa Mon Sep 17 00:00:00 2001
From: Lex Berezhny <lex@damoti.com>
Date: Sat, 26 May 2018 09:25:39 -0400
Subject: [PATCH] moved files around

---
 lbrynet/tests/integration/__init__.py         |   0
 lbrynet/tests/integration/test_wallet.py      |  73 ----------
 lbrynet/tests/integration/wallet/__init__.py  |   0
 .../wallet/test_basic_transactions.py         |  61 ++++++++
 scripts/publish_performance.py                | 133 ++++++++++++++++++
 5 files changed, 194 insertions(+), 73 deletions(-)
 create mode 100644 lbrynet/tests/integration/__init__.py
 delete mode 100644 lbrynet/tests/integration/test_wallet.py
 create mode 100644 lbrynet/tests/integration/wallet/__init__.py
 create mode 100644 lbrynet/tests/integration/wallet/test_basic_transactions.py
 create mode 100644 scripts/publish_performance.py

diff --git a/lbrynet/tests/integration/__init__.py b/lbrynet/tests/integration/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/lbrynet/tests/integration/test_wallet.py b/lbrynet/tests/integration/test_wallet.py
deleted file mode 100644
index f141f5e90..000000000
--- a/lbrynet/tests/integration/test_wallet.py
+++ /dev/null
@@ -1,73 +0,0 @@
-import time
-import shutil
-import logging
-import tempfile
-from binascii import hexlify
-
-from twisted.internet import defer, reactor, threads
-from twisted.trial import unittest
-from orchstr8.services import BaseLbryServiceStack
-
-from lbrynet.core.call_later_manager import CallLaterManager
-from lbrynet.database.storage import SQLiteStorage
-
-from lbrynet.wallet.basecoin import CoinRegistry
-from lbrynet.wallet.manager import WalletManager
-from lbrynet.wallet.constants import COIN
-
-
-class WalletTestCase(unittest.TestCase):
-
-    VERBOSE = False
-
-    def setUp(self):
-        logging.getLogger('lbrynet').setLevel(logging.INFO)
-        self.data_path = tempfile.mkdtemp()
-        self.db = SQLiteStorage(self.data_path)
-        CallLaterManager.setup(reactor.callLater)
-        self.service = BaseLbryServiceStack(self.VERBOSE)
-        return self.service.startup()
-
-    def tearDown(self):
-        CallLaterManager.stop()
-        shutil.rmtree(self.data_path, ignore_errors=True)
-        return self.service.shutdown()
-
-    @property
-    def lbrycrd(self):
-        return self.service.lbrycrd
-
-
-class StartupTests(WalletTestCase):
-
-    VERBOSE = True
-
-    @defer.inlineCallbacks
-    def test_balance(self):
-        coin_id = 'lbc_regtest'
-        manager = WalletManager.from_config({
-            'ledgers': {coin_id: {'default_servers': [('localhost', 50001)]}}
-        })
-        wallet = manager.create_wallet(None, CoinRegistry.get_coin_class(coin_id))
-        ledger = manager.ledgers.values()[0]
-        account = wallet.default_account
-        coin = account.coin
-        yield manager.start_ledgers()
-        address = account.get_least_used_receiving_address()
-        sendtxid = yield self.lbrycrd.sendtoaddress(address, 2.5)
-        yield self.lbrycrd.generate(1)
-        #yield manager.wallet.history.on_transaction.
-        yield threads.deferToThread(time.sleep, 10)
-        utxo = account.get_unspent_utxos()[0]
-        address2 = account.get_least_used_receiving_address()
-        tx_class = ledger.transaction_class
-        Input, Output = tx_class.input_class, tx_class.output_class
-        tx = tx_class()\
-            .add_inputs([Input.spend(utxo)])\
-            .add_outputs([Output.pay_pubkey_hash(2.49*COIN, coin.address_to_hash160(address2))])\
-            .sign(account)
-
-        yield self.lbrycrd.decoderawtransaction(hexlify(tx.raw))
-        yield self.lbrycrd.sendrawtransaction(hexlify(tx.raw))
-
-        yield manager.stop_ledgers()
diff --git a/lbrynet/tests/integration/wallet/__init__.py b/lbrynet/tests/integration/wallet/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/lbrynet/tests/integration/wallet/test_basic_transactions.py b/lbrynet/tests/integration/wallet/test_basic_transactions.py
new file mode 100644
index 000000000..e3f505201
--- /dev/null
+++ b/lbrynet/tests/integration/wallet/test_basic_transactions.py
@@ -0,0 +1,61 @@
+import asyncio
+from binascii import hexlify
+from orchstr8.testcase import IntegrationTestCase
+from torba.constants import COIN
+
+
+class StartupTests(IntegrationTestCase):
+
+    VERBOSE = True
+
+    async def test_balance(self):
+        account = self.wallet.default_account
+        coin = account.coin
+        ledger = self.manager.ledgers[coin.ledger_class]
+        address = account.get_least_used_receiving_address()
+        sendtxid = await self.lbrycrd.sendtoaddress(address.decode(), 2.5)
+        await self.lbrycrd.generate(1)
+        await ledger.on_transaction.where(
+            lambda tx: tx.id.decode() == sendtxid
+        )
+        utxo = account.get_unspent_utxos()[0]
+        address2 = account.get_least_used_receiving_address()
+        tx_class = ledger.transaction_class
+        Input, Output = tx_class.input_class, tx_class.output_class
+        tx = tx_class() \
+            .add_inputs([Input.spend(utxo)]) \
+            .add_outputs([Output.pay_pubkey_hash(int(2.49*COIN), coin.address_to_hash160(address2))]) \
+            .sign(account)
+        await self.lbrycrd.decoderawtransaction(hexlify(tx.raw))
+        sendtxid = await self.lbrycrd.sendrawtransaction(hexlify(tx.raw))
+        await self.lbrycrd.generate(1)
+        await ledger.on_transaction.where(
+            lambda tx: tx.id.decode() == sendtxid
+        )
+
+
+class AbandonClaimLookup(IntegrationTestCase):
+
+    async def skip_test_abandon_claim(self):
+        address = yield self.lbry.wallet.get_least_used_address()
+        yield self.lbrycrd.sendtoaddress(address, 0.0003 - 0.0000355)
+        yield self.lbrycrd.generate(1)
+        yield self.lbry.wallet.update_balance()
+        yield threads.deferToThread(time.sleep, 5)
+        print(self.lbry.wallet.get_balance())
+        claim = yield self.lbry.wallet.claim_new_channel('@test', 0.000096)
+        yield self.lbrycrd.generate(1)
+        print('='*10 + 'CLAIM' + '='*10)
+        print(claim)
+        yield self.lbrycrd.decoderawtransaction(claim['tx'])
+        abandon = yield self.lbry.wallet.abandon_claim(claim['claim_id'], claim['txid'], claim['nout'])
+        print('='*10 + 'ABANDON' + '='*10)
+        print(abandon)
+        yield self.lbrycrd.decoderawtransaction(abandon['tx'])
+        yield self.lbrycrd.generate(1)
+        yield self.lbrycrd.getrawtransaction(abandon['txid'])
+
+        yield self.lbry.wallet.update_balance()
+        yield threads.deferToThread(time.sleep, 5)
+        print('='*10 + 'FINAL BALANCE' + '='*10)
+        print(self.lbry.wallet.get_balance())
diff --git a/scripts/publish_performance.py b/scripts/publish_performance.py
new file mode 100644
index 000000000..d12ae58cf
--- /dev/null
+++ b/scripts/publish_performance.py
@@ -0,0 +1,133 @@
+import os
+import time
+from random import Random
+
+from pyqtgraph.Qt import QtCore, QtGui
+app = QtGui.QApplication([])
+from qtreactor import pyqt4reactor
+pyqt4reactor.install()
+
+from twisted.internet import defer, task, threads
+from orchstr8.services import LbryServiceStack
+
+import pyqtgraph as pg
+
+
+class Profiler:
+    pens = [
+        (230, 25, 75),   # red
+        (60, 180, 75),   # green
+        (255, 225, 25),  # yellow
+        (0, 130, 200),   # blue
+        (245, 130, 48),  # orange
+        (145, 30, 180),  # purple
+        (70, 240, 240),  # cyan
+        (240, 50, 230),  # magenta
+        (210, 245, 60),  # lime
+        (250, 190, 190),  # pink
+        (0, 128, 128),   # teal
+    ]
+
+    def __init__(self, graph=None):
+        self.times = {}
+        self.graph = graph
+
+    def start(self, name):
+        if name in self.times:
+            self.times[name]['start'] = time.time()
+        else:
+            self.times[name] = {
+                'start': time.time(),
+                'data': [],
+                'plot': self.graph.plot(
+                    pen=self.pens[len(self.times)],
+                    symbolBrush=self.pens[len(self.times)],
+                    name=name
+                )
+            }
+
+    def stop(self, name):
+        elapsed = time.time() - self.times[name]['start']
+        self.times[name]['start'] = None
+        self.times[name]['data'].append(elapsed)
+
+    def draw(self):
+        for plot in self.times.values():
+            plot['plot'].setData(plot['data'])
+
+
+class ThePublisherOfThings:
+
+    def __init__(self, blocks=100, txns_per_block=100, seed=2015, start_blocks=110):
+        self.blocks = blocks
+        self.txns_per_block = txns_per_block
+        self.start_blocks = start_blocks
+        self.random = Random(seed)
+        self.profiler = Profiler()
+        self.service = LbryServiceStack(verbose=True, profiler=self.profiler)
+        self.publish_file = None
+
+    @defer.inlineCallbacks
+    def start(self):
+        yield self.service.startup(
+            after_lbrycrd_start=lambda: self.service.lbrycrd.generate(1010)
+        )
+        wallet = self.service.lbry.wallet
+        address = yield wallet.get_least_used_address()
+        sendtxid = yield self.service.lbrycrd.sendtoaddress(address, 100)
+        yield self.service.lbrycrd.generate(1)
+        yield wallet.wait_for_tx_in_wallet(sendtxid)
+        yield wallet.update_balance()
+        self.publish_file = os.path.join(self.service.lbry.download_directory, 'the_file')
+        with open(self.publish_file, 'w') as _publish_file:
+            _publish_file.write('message that will be heard around the world\n')
+        yield threads.deferToThread(time.sleep, 0.5)
+
+    @defer.inlineCallbacks
+    def generate_publishes(self):
+
+        win = pg.GraphicsLayoutWidget(show=True)
+        win.setWindowTitle('orchstr8: performance monitor')
+        win.resize(1800, 600)
+
+        p4 = win.addPlot()
+        p4.addLegend()
+        p4.setDownsampling(mode='peak')
+        p4.setClipToView(True)
+        self.profiler.graph = p4
+
+        for block in range(self.blocks):
+            for txn in range(self.txns_per_block):
+                name = 'block{}txn{}'.format(block, txn)
+                self.profiler.start('total')
+                yield self.service.lbry.daemon.jsonrpc_publish(
+                    name=name, bid=self.random.randrange(1, 5)/1000.0,
+                    file_path=self.publish_file, metadata={
+                        "description": "Some interesting content",
+                        "title": "My interesting content",
+                        "author": "Video shot by me@example.com",
+                        "language": "en", "license": "LBRY Inc", "nsfw": False
+                    }
+                )
+                self.profiler.stop('total')
+                self.profiler.draw()
+
+            yield self.service.lbrycrd.generate(1)
+
+    def stop(self):
+        return self.service.shutdown(cleanup=False)
+
+
+@defer.inlineCallbacks
+def generate_publishes(_):
+    pub = ThePublisherOfThings(50, 10)
+    yield pub.start()
+    yield pub.generate_publishes()
+    yield pub.stop()
+    print('lbrycrd: {}'.format(pub.service.lbrycrd.data_path))
+    print('lbrynet: {}'.format(pub.service.lbry.data_path))
+    print('lbryumserver: {}'.format(pub.service.lbryumserver.data_path))
+
+
+if __name__ == "__main__":
+    task.react(generate_publishes)