2018-07-05 05:16:52 +02:00
|
|
|
# pylint: skip-file
|
2017-12-17 07:00:12 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2017-01-12 21:34:41 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
from collections import defaultdict
|
2016-12-07 15:48:37 +01:00
|
|
|
from twisted.trial import unittest
|
|
|
|
from twisted.internet import threads, defer
|
2018-02-12 20:19:15 +01:00
|
|
|
from lbrynet.database.storage import SQLiteStorage
|
2018-07-06 22:16:58 +02:00
|
|
|
from tests.mocks import FakeNetwork
|
2017-01-12 21:34:41 +01:00
|
|
|
from lbrynet.core.Error import InsufficientFundsError
|
2018-07-05 05:16:52 +02:00
|
|
|
#from lbrynet.core.Wallet import LBRYumWallet, ReservedPoints
|
|
|
|
#from lbryum.commands import Commands
|
|
|
|
#from lbryum.simple_config import SimpleConfig
|
2018-02-12 20:19:15 +01:00
|
|
|
from lbryschema.claim import ClaimDict
|
2016-12-07 15:48:37 +01:00
|
|
|
|
|
|
|
test_metadata = {
|
2018-02-13 20:20:54 +01:00
|
|
|
'license': 'NASA',
|
|
|
|
'version': '_0_1_0',
|
|
|
|
'description': 'test',
|
|
|
|
'language': 'en',
|
|
|
|
'author': 'test',
|
|
|
|
'title': 'test',
|
|
|
|
'nsfw': False,
|
|
|
|
'thumbnail': 'test'
|
2016-12-07 15:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 21:58:20 +02:00
|
|
|
test_claim_dict = {
|
2018-02-13 20:20:54 +01:00
|
|
|
'version': '_0_0_1',
|
|
|
|
'claimType': 'streamType',
|
|
|
|
'stream': {'metadata': test_metadata, 'version': '_0_0_1', 'source':
|
2017-10-02 18:13:45 +02:00
|
|
|
{'source': '8655f713819344980a9a0d67b198344e2c462c90f813e86f'
|
|
|
|
'0c63789ab0868031f25c54d0bb31af6658e997e2041806eb',
|
2017-09-29 12:44:22 +02:00
|
|
|
'sourceType': 'lbry_sd_hash', 'contentType': 'video/mp4', 'version': '_0_0_1'},
|
2018-02-13 20:20:54 +01:00
|
|
|
}}
|
2017-04-03 21:58:20 +02:00
|
|
|
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-07-05 05:16:52 +02:00
|
|
|
#class MocLbryumWallet(LBRYumWallet):
|
|
|
|
# def __init__(self, db_dir, max_usable_balance=3):
|
|
|
|
# LBRYumWallet.__init__(self, SQLiteStorage(db_dir), SimpleConfig(
|
|
|
|
# {"lbryum_path": db_dir, "wallet_path": os.path.join(db_dir, "testwallet")}
|
|
|
|
# ))
|
|
|
|
# self.db_dir = db_dir
|
|
|
|
# self.wallet_balance = Decimal(10.0)
|
|
|
|
# self.total_reserved_points = Decimal(0.0)
|
|
|
|
# self.queued_payments = defaultdict(Decimal)
|
|
|
|
# self.network = FakeNetwork()
|
|
|
|
# self._mock_max_usable_balance = max_usable_balance
|
|
|
|
# assert self.config.get_wallet_path() == os.path.join(self.db_dir, "testwallet")
|
|
|
|
#
|
|
|
|
# @defer.inlineCallbacks
|
|
|
|
# def setup(self, password=None, seed=None):
|
|
|
|
# yield self.storage.setup()
|
|
|
|
# seed = seed or "travel nowhere air position hill peace suffer parent beautiful rise " \
|
|
|
|
# "blood power home crumble teach"
|
|
|
|
# storage = lbryum.wallet.WalletStorage(self.config.get_wallet_path())
|
|
|
|
# self.wallet = lbryum.wallet.NewWallet(storage)
|
|
|
|
# self.wallet.add_seed(seed, password)
|
|
|
|
# self.wallet.create_master_keys(password)
|
|
|
|
# self.wallet.create_main_account()
|
|
|
|
#
|
|
|
|
# @defer.inlineCallbacks
|
|
|
|
# def stop(self):
|
|
|
|
# yield self.storage.stop()
|
|
|
|
# yield threads.deferToThread(shutil.rmtree, self.db_dir)
|
|
|
|
#
|
|
|
|
# def get_least_used_address(self, account=None, for_change=False, max_count=100):
|
|
|
|
# return defer.succeed(None)
|
|
|
|
#
|
|
|
|
# def get_name_claims(self):
|
|
|
|
# return threads.deferToThread(lambda: [])
|
|
|
|
#
|
|
|
|
# def _save_name_metadata(self, name, claim_outpoint, sd_hash):
|
|
|
|
# return defer.succeed(True)
|
|
|
|
#
|
|
|
|
# def get_max_usable_balance_for_claim(self, name):
|
|
|
|
# # The amount is returned on the basis of test_point_reservation_and_claim unittest
|
|
|
|
# # Also affects test_successful_send_name_claim
|
|
|
|
# return defer.succeed(self._mock_max_usable_balance)
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2016-12-07 15:48:37 +01:00
|
|
|
|
|
|
|
class WalletTest(unittest.TestCase):
|
2018-07-05 05:16:52 +02:00
|
|
|
skip = "Needs to be ported to the new wallet."
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-12-17 07:00:12 +01:00
|
|
|
def setUp(self):
|
|
|
|
user_dir = tempfile.mkdtemp()
|
2018-02-13 20:20:54 +01:00
|
|
|
self.wallet = MocLbryumWallet(user_dir)
|
|
|
|
yield self.wallet.setup()
|
|
|
|
self.assertEqual(self.wallet.get_balance(), Decimal(10))
|
2017-12-17 07:00:12 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
2018-02-13 20:20:54 +01:00
|
|
|
return self.wallet.stop()
|
2017-12-17 07:00:12 +01:00
|
|
|
|
2016-12-07 15:48:37 +01:00
|
|
|
def test_failed_send_name_claim(self):
|
|
|
|
def not_enough_funds_send_name_claim(self, name, val, amount):
|
2018-02-13 20:20:54 +01:00
|
|
|
claim_out = {'success': False, 'reason': 'Not enough funds'}
|
2016-12-07 15:48:37 +01:00
|
|
|
return claim_out
|
2017-04-05 04:41:57 +02:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
self.wallet._send_name_claim = not_enough_funds_send_name_claim
|
|
|
|
d = self.wallet.claim_name('test', 1, test_claim_dict)
|
2017-04-05 04:41:57 +02:00
|
|
|
self.assertFailure(d, Exception)
|
2016-12-07 15:48:37 +01:00
|
|
|
return d
|
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-12-07 15:48:37 +01:00
|
|
|
def test_successful_send_name_claim(self):
|
2016-12-14 17:53:41 +01:00
|
|
|
expected_claim_out = {
|
2017-04-07 02:45:05 +02:00
|
|
|
"claim_id": "f43dc06256a69988bdbea09a58c80493ba15dcfa",
|
2016-12-07 15:48:37 +01:00
|
|
|
"fee": "0.00012",
|
|
|
|
"nout": 0,
|
|
|
|
"success": True,
|
2018-02-12 20:19:15 +01:00
|
|
|
"txid": "6f8180002ef4d21f5b09ca7d9648a54d213c666daf8639dc283e2fd47450269e",
|
|
|
|
"value": ClaimDict.load_dict(test_claim_dict).serialized.encode('hex'),
|
|
|
|
"claim_address": "",
|
|
|
|
"channel_claim_id": "",
|
|
|
|
"channel_name": ""
|
|
|
|
}
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2017-06-12 19:32:01 +02:00
|
|
|
def success_send_name_claim(self, name, val, amount, certificate_id=None,
|
|
|
|
claim_address=None, change_address=None):
|
2018-02-13 20:20:54 +01:00
|
|
|
return defer.succeed(expected_claim_out)
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
self.wallet._send_name_claim = success_send_name_claim
|
|
|
|
claim_out = yield self.wallet.claim_name('test', 1, test_claim_dict)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertNotIn('success', claim_out)
|
2018-02-13 20:20:54 +01:00
|
|
|
self.assertEqual(expected_claim_out['claim_id'], claim_out['claim_id'])
|
|
|
|
self.assertEqual(expected_claim_out['fee'], claim_out['fee'])
|
|
|
|
self.assertEqual(expected_claim_out['nout'], claim_out['nout'])
|
|
|
|
self.assertEqual(expected_claim_out['txid'], claim_out['txid'])
|
|
|
|
self.assertEqual(expected_claim_out['value'], claim_out['value'])
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-12-07 15:48:37 +01:00
|
|
|
def test_failed_support(self):
|
2018-02-13 20:20:54 +01:00
|
|
|
# wallet.support_claim will check the balance before calling _support_claim
|
|
|
|
try:
|
|
|
|
yield self.wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1000)
|
|
|
|
except InsufficientFundsError:
|
|
|
|
pass
|
2016-12-07 15:48:37 +01:00
|
|
|
|
|
|
|
def test_succesful_support(self):
|
2016-12-14 17:53:41 +01:00
|
|
|
expected_support_out = {
|
2016-12-07 15:48:37 +01:00
|
|
|
"fee": "0.000129",
|
|
|
|
"nout": 0,
|
|
|
|
"success": True,
|
|
|
|
"txid": "11030a76521e5f552ca87ad70765d0cc52e6ea4c0dc0063335e6cf2a9a85085f"
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
expected_result = {
|
|
|
|
"fee": 0.000129,
|
|
|
|
"nout": 0,
|
|
|
|
"txid": "11030a76521e5f552ca87ad70765d0cc52e6ea4c0dc0063335e6cf2a9a85085f"
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:48:37 +01:00
|
|
|
def check_out(claim_out):
|
2018-02-13 20:20:54 +01:00
|
|
|
self.assertDictEqual(expected_result, claim_out)
|
|
|
|
|
|
|
|
def success_support_claim(name, val, amount):
|
|
|
|
return defer.succeed(expected_support_out)
|
|
|
|
|
|
|
|
self.wallet._support_claim = success_support_claim
|
|
|
|
d = self.wallet.support_claim('test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 1)
|
2016-12-07 15:48:37 +01:00
|
|
|
d.addCallback(lambda claim_out: check_out(claim_out))
|
|
|
|
return d
|
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-12-07 15:48:37 +01:00
|
|
|
def test_failed_abandon(self):
|
2018-02-13 20:20:54 +01:00
|
|
|
try:
|
|
|
|
yield self.wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None)
|
|
|
|
raise Exception("test failed")
|
|
|
|
except Exception as err:
|
|
|
|
self.assertSubstring("claim not found", err.message)
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-12-07 15:48:37 +01:00
|
|
|
def test_successful_abandon(self):
|
2016-12-14 17:53:41 +01:00
|
|
|
expected_abandon_out = {
|
2016-12-07 15:48:37 +01:00
|
|
|
"fee": "0.000096",
|
|
|
|
"success": True,
|
|
|
|
"txid": "0578c161ad8d36a7580c557d7444f967ea7f988e194c20d0e3c42c3cabf110dd"
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
expected_abandon_result = {
|
|
|
|
"fee": 0.000096,
|
|
|
|
"txid": "0578c161ad8d36a7580c557d7444f967ea7f988e194c20d0e3c42c3cabf110dd"
|
|
|
|
}
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
def success_abandon_claim(claim_outpoint, txid, nout):
|
|
|
|
return defer.succeed(expected_abandon_out)
|
2016-12-07 15:48:37 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
self.wallet._abandon_claim = success_abandon_claim
|
|
|
|
claim_out = yield self.wallet.abandon_claim("f43dc06256a69988bdbea09a58c80493ba15dcfa", None, None)
|
|
|
|
self.assertDictEqual(expected_abandon_result, claim_out)
|
2017-01-12 21:34:41 +01:00
|
|
|
|
2018-02-13 20:20:54 +01:00
|
|
|
@defer.inlineCallbacks
|
2017-01-12 21:34:41 +01:00
|
|
|
def test_point_reservation_and_balance(self):
|
|
|
|
# check that point reservations and cancellation changes the balance
|
|
|
|
# properly
|
|
|
|
def update_balance():
|
|
|
|
return defer.succeed(5)
|
2018-02-13 20:20:54 +01:00
|
|
|
|
|
|
|
self.wallet._update_balance = update_balance
|
|
|
|
yield self.wallet.update_balance()
|
|
|
|
self.assertEqual(5, self.wallet.get_balance())
|
|
|
|
|
2017-01-12 21:34:41 +01:00
|
|
|
# test point reservation
|
2018-02-13 20:20:54 +01:00
|
|
|
yield self.wallet.reserve_points('testid', 2)
|
|
|
|
self.assertEqual(3, self.wallet.get_balance())
|
|
|
|
self.assertEqual(2, self.wallet.total_reserved_points)
|
|
|
|
|
2017-01-12 21:34:41 +01:00
|
|
|
# test reserved points cancellation
|
2018-02-13 20:20:54 +01:00
|
|
|
yield self.wallet.cancel_point_reservation(ReservedPoints('testid', 2))
|
|
|
|
self.assertEqual(5, self.wallet.get_balance())
|
|
|
|
self.assertEqual(0, self.wallet.total_reserved_points)
|
|
|
|
|
2017-01-12 21:34:41 +01:00
|
|
|
# test point sending
|
2018-02-13 20:20:54 +01:00
|
|
|
reserve_points = yield self.wallet.reserve_points('testid', 2)
|
|
|
|
yield self.wallet.send_points_to_address(reserve_points, 1)
|
|
|
|
self.assertEqual(3, self.wallet.get_balance())
|
2017-01-12 21:34:41 +01:00
|
|
|
# test failed point reservation
|
2018-02-13 20:20:54 +01:00
|
|
|
out = yield self.wallet.reserve_points('testid', 4)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(out)
|
2017-01-12 21:34:41 +01:00
|
|
|
|
|
|
|
def test_point_reservation_and_claim(self):
|
|
|
|
# check that claims take into consideration point reservations
|
|
|
|
def update_balance():
|
|
|
|
return defer.succeed(5)
|
2018-02-13 20:20:54 +01:00
|
|
|
|
|
|
|
self.wallet._update_balance = update_balance
|
|
|
|
d = self.wallet.update_balance()
|
|
|
|
d.addCallback(lambda _: self.assertEqual(5, self.wallet.get_balance()))
|
|
|
|
d.addCallback(lambda _: self.wallet.reserve_points('testid', 2))
|
|
|
|
d.addCallback(lambda _: self.wallet.claim_name('test', 4, test_claim_dict))
|
2017-09-29 12:44:22 +02:00
|
|
|
self.assertFailure(d, InsufficientFundsError)
|
2017-01-12 21:34:41 +01:00
|
|
|
return d
|
|
|
|
|
|
|
|
def test_point_reservation_and_support(self):
|
|
|
|
# check that supports take into consideration point reservations
|
|
|
|
def update_balance():
|
|
|
|
return defer.succeed(5)
|
2018-02-13 20:20:54 +01:00
|
|
|
|
|
|
|
self.wallet._update_balance = update_balance
|
|
|
|
d = self.wallet.update_balance()
|
|
|
|
d.addCallback(lambda _: self.assertEqual(5, self.wallet.get_balance()))
|
|
|
|
d.addCallback(lambda _: self.wallet.reserve_points('testid', 2))
|
|
|
|
d.addCallback(lambda _: self.wallet.support_claim(
|
2017-09-29 12:44:22 +02:00
|
|
|
'test', "f43dc06256a69988bdbea09a58c80493ba15dcfa", 4))
|
|
|
|
self.assertFailure(d, InsufficientFundsError)
|
2017-01-12 21:34:41 +01:00
|
|
|
return d
|
2017-12-17 07:00:12 +01:00
|
|
|
|