From a50d6a133702d12aae8b3e4e6c2111a1e2fd0c4d Mon Sep 17 00:00:00 2001 From: hackrush Date: Wed, 14 Feb 2018 23:31:57 +0530 Subject: [PATCH] Check for max usable balance before updating --- CHANGELOG.md | 10 ---------- lbrynet/core/Wallet.py | 9 +++++++++ lbrynet/daemon/Daemon.py | 6 ++++++ lbrynet/tests/unit/core/test_Wallet.py | 2 ++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69761cbf8a..4e40fd74ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,16 +25,6 @@ at anytime. * `blob_list` failing with --uri parameter (https://github.com/lbryio/lbry/issues/895) * `get` failing with a non-useful error message when given a uri for a channel claim * exception checking in several wallet unit tests - * Fixed unnecessarily verbose exchange rate error (https://github.com/lbryio/lbry/issues/984) - * Merged two separate dht test folders into one - * Fixed value error due to a race condition when saving to the claim cache (https://github.com/lbryio/lbry/issues/1013) - * Fixed being unable to re-download updated content (#951) - * Fixed sending error messages for failed api requests - * Fixed the file manager startup being slow when handling thousands of files - * Fixed handling decryption error for blobs encrypted with an invalid key - * Fixed handling stream with no data blob (https://github.com/lbryio/lbry/issues/905) - * Fixed fetching the external ip - * Fixed API call to blob_list with --uri parameter (https://github.com/lbryio/lbry/issues/895) * Fixed publish command to allow updating claims with bid amount higher than wallet balance(by spending the claimtrietx coin) (https://github.com/lbryio/lbry/issues/748) ### Deprecated diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index fa512907d4..d24ef92009 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -537,6 +537,10 @@ def claim_name(self, name, bid, metadata, certificate_id=None, claim_address=Non decoded = ClaimDict.load_dict(metadata) serialized = decoded.serialized + amt = yield self.get_max_usable_balance_for_claim(name) + if bid > amt: + raise InsufficientFundsError() + claim = yield self._send_name_claim(name, serialized.encode('hex'), bid, certificate_id, claim_address, change_address) @@ -972,6 +976,11 @@ def _update_balance(self): lambda result: Decimal(result['confirmed']) + Decimal(result.get('unconfirmed', 0.0))) return d + @defer.inlineCallbacks + def get_max_usable_balance_for_claim(self, claim_name): + amt = yield self._run_cmd_as_defer_to_thread('get_max_spendable_amt_for_claim', claim_name) + defer.returnValue(amt) + # Always create and return a brand new address def get_new_address(self, for_change=False, account=None): return defer.succeed(self.wallet.create_new_address(account=account, diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index d1531954dc..68c7657ad4 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -2002,6 +2002,12 @@ def jsonrpc_publish(self, name, bid, metadata=None, file_path=None, fee=None, ti if bid <= 0.0: raise Exception("Invalid bid") + amt = yield self.session.wallet.get_max_usable_balance_for_claim(name) + if bid > amt: + raise InsufficientFundsError( + "Please lower the bid value, the max amount you can specify for this claim is {}" + .format(amt)) + metadata = metadata or {} if fee is not None: metadata['fee'] = fee diff --git a/lbrynet/tests/unit/core/test_Wallet.py b/lbrynet/tests/unit/core/test_Wallet.py index 946472d2e0..c2d19e61d7 100644 --- a/lbrynet/tests/unit/core/test_Wallet.py +++ b/lbrynet/tests/unit/core/test_Wallet.py @@ -73,6 +73,8 @@ def get_name_claims(self): def _save_name_metadata(self, name, claim_outpoint, sd_hash): return defer.succeed(True) + def get_max_usable_balance_for_claim(self, name): + return defer.succeed(3) class WalletTest(unittest.TestCase): @defer.inlineCallbacks