Tests for support and tip

This commit is contained in:
hackrush 2018-09-03 21:11:40 +05:30 committed by Lex Berezhny
parent 6d9947779e
commit 2878716381
3 changed files with 74 additions and 19 deletions

View file

@ -2365,18 +2365,19 @@ class Daemon(AuthJSONRPCServer):
@requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED])
@defer.inlineCallbacks
def jsonrpc_claim_new_support(self, name, claim_id, amount):
def jsonrpc_claim_new_support(self, name, claim_id, amount, account_id=None):
"""
Support a name claim
Usage:
claim_new_support (<name> | --name=<name>) (<claim_id> | --claim_id=<claim_id>)
(<amount> | --amount=<amount>)
(<amount> | --amount=<amount>) [--account_id=<account_id>]
Options:
--name=<name> : (str) name of the claim to support
--claim_id=<claim_id> : (str) claim_id of the claim to support
--amount=<amount> : (float) amount of support
--account_id=<account_id> : (str) id of the account to use
Returns:
(dict) Dictionary containing the transaction information
@ -2391,23 +2392,27 @@ class Daemon(AuthJSONRPCServer):
}
"""
if account_id is not None:
account = self.get_account_or_error("account_id", account_id, lbc_only=True)
amount = self.get_dewies_or_error("amount", amount)
result = yield self.wallet_manager.support_claim(name, claim_id, amount)
result = yield self.wallet_manager.support_claim(name, claim_id, amount, account)
self.analytics_manager.send_claim_action('new_support')
return result
@requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED])
@defer.inlineCallbacks
def jsonrpc_claim_tip(self, claim_id, amount):
def jsonrpc_claim_tip(self, claim_id, amount, account_id=None):
"""
Tip a claim
Usage:
claim_tip (<claim_id> | --claim_id=<claim_id>) (<amount> | --amount=<amount>)
claim_tip (<claim_id> | --claim_id=<claim_id>) (<amount> | --amount=<amount>) [--account_id=<account_id>]
Options:
--claim_id=<claim_id> : (str) claim_id of the claim to support
--amount=<amount> : (float) amount of support
--account_id=<account_id> : (str) id of the account to use
Returns:
(dict) Dictionary containing the transaction information
@ -2422,9 +2427,12 @@ class Daemon(AuthJSONRPCServer):
}
"""
if account_id is not None:
account = self.get_account_or_error("account_id", account_id, lbc_only=True)
amount = self.get_dewies_or_error("amount", amount)
validate_claim_id(claim_id)
result = yield self.wallet.tip_claim(amount, claim_id)
result = yield self.wallet_manager.tip_claim(amount, claim_id, account)
self.analytics_manager.send_claim_action('new_support')
return result

View file

@ -246,16 +246,18 @@ class LbryWalletManager(BaseWalletManager):
}
@defer.inlineCallbacks
def support_claim(self, claim_name, claim_id, amount):
account = self.default_account
def support_claim(self, claim_name, claim_id, amount, account=None):
if account is None:
account = self.default_account
holding_address = yield account.receiving.get_or_create_usable_address()
tx = yield Transaction.support(claim_name, claim_id, amount, holding_address, [account], account)
yield account.ledger.broadcast(tx)
return tx
@defer.inlineCallbacks
def tip_claim(self, amount, claim_id):
account = self.default_account
def tip_claim(self, amount, claim_id, account=None):
if account is None:
account = self.default_account
claim_to_tip = yield self.get_claim_by_claim_id(claim_id)
tx = yield Transaction.support(
claim_to_tip['name'], claim_id, amount, claim_to_tip['address'], [account], account

View file

@ -15,7 +15,8 @@ from lbrynet import conf as lbry_conf
from lbrynet.dht.node import Node
from lbrynet.daemon.Daemon import Daemon
from lbrynet.wallet.manager import LbryWalletManager
from lbrynet.daemon.Components import WalletComponent, DHTComponent, HashAnnouncerComponent, ExchangeRateManagerComponent
from lbrynet.daemon.Components import WalletComponent, DHTComponent, HashAnnouncerComponent, \
ExchangeRateManagerComponent
from lbrynet.daemon.Components import UPnPComponent
from lbrynet.daemon.Components import REFLECTOR_COMPONENT
from lbrynet.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT
@ -167,7 +168,6 @@ class CommandTestCase(IntegrationTestCase):
class EpicAdventuresOfChris45(CommandTestCase):
VERBOSE = False
@defer.inlineCallbacks
@ -307,27 +307,72 @@ class EpicAdventuresOfChris45(CommandTestCase):
result = yield self.daemon.jsonrpc_account_balance()
self.assertEqual(result, Decimal('8.9693585'))
# Amidst all this Chris45 receives a call from his friend Ramsey54
# who says that it is of utmost urgency that Chris45 transfer him
# 1 LBC to which Chris45 readily obliges
ramsey_account_id = (yield self.daemon.jsonrpc_account_create("Ramsey54"))['id']
# Amidst all this Chris receives a call from his friend Ramsey
# who says that it is of utmost urgency that Chris transfer him
# 1 LBC to which Chris readily obliges
ramsey_account_id = (yield self.daemon.jsonrpc_account_create("Ramsey"))['id']
ramsey_account = self.daemon.get_account_or_error('', ramsey_account_id)
ramsey_address = yield ramsey_account.receiving.get_or_create_usable_address()
result = yield self.out(self.daemon.jsonrpc_wallet_send(1, ramsey_address))
self.assertIn("txid", result)
yield self.d_confirm_tx(result['txid'])
# Chris45 then eagerly waits for 6 confirmations to check his balance and then calls Ramsey54 to verify whether
# Chris then eagerly waits for 6 confirmations to check his balance and then calls Ramsey to verify whether
# he received it or not
yield self.d_generate(5)
result = yield self.daemon.jsonrpc_account_balance()
# Chris45's balance was correct
# Chris' balance was correct
self.assertEqual(result, Decimal('7.9692345'))
# Ramsey54 too assured him that he had received the 1 LBC and thanks him
# Ramsey too assured him that he had received the 1 LBC and thanks him
result = yield self.daemon.jsonrpc_account_balance(ramsey_account_id)
self.assertEqual(result, Decimal('1.0'))
# After Chris is done with all the "helping other people" stuff he decides that it's time to
# write a new story and publish it to lbry. All he needed was a fresh start and he came up with:
with tempfile.NamedTemporaryFile() as file:
file.write(b'Amazingly Original First Line')
file.write(b'Super plot for the grand novel')
file.write(b'Totally un-cliched ending')
file.write(b'**Audience Gasps**')
file.flush()
claim3 = yield self.out(self.daemon.jsonrpc_publish(
'fresh-start', 1, file_path=file.name, channel_name='@spam', channel_id=channel['claim_id']
))
self.assertTrue(claim3['success'])
yield self.d_confirm_tx(claim3['tx']['txid'])
yield self.d_generate(5)
# He gives the link of his story to all his friends and hopes that this is the much needed break for him
uri = 'lbry://@spam/fresh-start'
# And voila, and bravo and encore! His Best Friend Ramsey read the story and immediately knew this was a hit
# Now to keep this claim winning on the lbry blockchain he immediately supports the claim
tx = yield self.out(self.daemon.jsonrpc_claim_new_support(
'fresh-start', claim3['claim_id'], '0.2', account_id=ramsey_account_id
))
yield self.d_confirm_tx(tx['txid'])
# And check if his support showed up
resolve_result = yield self.out(self.daemon.jsonrpc_resolve(uri=uri))
# It obviously did! Because, blockchain baby \O/
self.assertEqual(resolve_result[uri]['claim']['supports'][0]['amount'], 0.2)
self.assertEqual(resolve_result[uri]['claim']['supports'][0]['txid'], tx['txid'])
yield self.d_generate(5)
# Now he also wanted to support the original creator of the Award Winning Novel
# So he quickly decides to send a tip to him
tx = yield self.out(
self.daemon.jsonrpc_claim_tip(claim3['claim_id'], '0.3', account_id=ramsey_account_id))
yield self.d_confirm_tx(tx['txid'])
# And again checks if it went to the just right place
resolve_result = yield self.out(self.daemon.jsonrpc_resolve(uri=uri))
# Which it obviously did. Because....?????
self.assertEqual(resolve_result[uri]['claim']['supports'][1]['amount'], 0.3)
self.assertEqual(resolve_result[uri]['claim']['supports'][1]['txid'], tx['txid'])
class AccountManagement(CommandTestCase):