From 4385b90cca09397e8b37d882dc7e9fe389ff8270 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 17 Apr 2016 23:23:20 -0400 Subject: [PATCH] add get transaction and history functions to daemon --- lbrynet/core/LBRYcrdWallet.py | 35 +++++++++++++++- lbrynet/lbrynet_daemon/LBRYDaemon.py | 44 +++++++++++++++++++++ lbrynet/lbrynet_daemon/LBRYDaemonControl.py | 23 +++++++---- 3 files changed, 93 insertions(+), 9 deletions(-) diff --git a/lbrynet/core/LBRYcrdWallet.py b/lbrynet/core/LBRYcrdWallet.py index 895dbfa49..3ff1008fb 100644 --- a/lbrynet/core/LBRYcrdWallet.py +++ b/lbrynet/core/LBRYcrdWallet.py @@ -1003,7 +1003,7 @@ class LBRYumWallet(LBRYWallet): self.max_behind = self.blocks_behind_alert self.catchup_progress = int(100 * (self.blocks_behind_alert / (5 + self.max_behind))) if self._caught_up_counter == 0: - alert.info('Catching up to the blockchain...showing blocks left...') + alert.info('Catching up with the blockchain...showing blocks left...') if self._caught_up_counter % 30 == 0: alert.info('%d...', (remote_height - local_height)) alert.info("Catching up: " + str(self.catchup_progress) + "%") @@ -1128,6 +1128,39 @@ class LBRYumWallet(LBRYWallet): func = getattr(self.cmd_runner, cmd.name) return threads.deferToThread(func) + def get_history(self): + cmd = known_commands['history'] + func = getattr(self.cmd_runner, cmd.name) + return threads.deferToThread(func) + + def get_tx_json(self, txid): + def _decode(raw_tx): + tx = Transaction(raw_tx).deserialize() + decoded_tx = {} + for txkey in tx.keys(): + if isinstance(tx[txkey], list): + decoded_tx[txkey] = [] + for i in tx[txkey]: + tmp = {} + for k in i.keys(): + if isinstance(i[k], Decimal): + tmp[k] = float(i[k] / 1e8) + else: + tmp[k] = i[k] + decoded_tx[txkey].append(tmp) + else: + decoded_tx[txkey] = tx[txkey] + return decoded_tx + + d = self._get_raw_tx(txid) + d.addCallback(_decode) + return d + + def get_pub_keys(self, wallet): + cmd = known_commands['getpubkeys'] + func = getattr(self.cmd_runner, cmd.name) + return threads.deferToThread(func, wallet) + def _save_wallet(self, val): d = threads.deferToThread(self.wallet.storage.write) d.addCallback(lambda _: val) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 95340be0d..3b658c880 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -1459,6 +1459,50 @@ class LBRYDaemon(jsonrpc.JSONRPC): return d + def jsonrpc_get_transaction_history(self): + """ + Get transaction history + + Args: + None + Returns: + list of transactions + """ + + d = self.session.wallet.get_history() + d.addCallback(lambda r: self._render_response(r, OK_CODE)) + return d + + def jsonrpc_get_transaction(self, p): + """ + Get a decoded transaction from a txid + + Args: + txid: txid hex string + Returns: + JSON formatted transaction + """ + + + txid = p['txid'] + d = self.session.wallet.get_tx_json(txid) + d.addCallback(lambda r: self._render_response(r, OK_CODE)) + return d + + def jsonrpc_get_public_key_from_wallet(self, p): + """ + Get public key from wallet address + + Args: + wallet: wallet address, base58 + Returns: + public key + """ + + wallet = p['wallet'] + d = self.session.wallet.get_pub_keys(wallet) + d.addCallback(lambda r: self._render_response(r, OK_CODE)) + def jsonrpc_get_time_behind_blockchain(self): """ Get number of blocks behind the blockchain diff --git a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py index 5fc6f98ff..2f0f00154 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py @@ -72,7 +72,13 @@ def start(): help="Branch of lbry-web-ui repo to use, defaults on HEAD", default="HEAD") parser.add_argument('--no-launch', dest='launchui', action="store_false") - parser.set_defaults(launchui=True) + parser.add_argument('--log-to-console', dest='logtoconsole', action="store_true") + parser.add_argument('--quiet', dest='quiet', action="store_true") + parser.set_defaults(launchui=True, logtoconsole=False, quiet=False) + args = parser.parse_args() + + if args.logtoconsole: + logging.basicConfig(level=logging.INFO) try: JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running() @@ -82,13 +88,13 @@ def start(): pass log.info("Starting lbrynet-daemon from command line") - print "Starting lbrynet-daemon from command line" - print "To view activity, view the log file here: " + LOG_FILENAME - print "Web UI is available at http://%s:%i" %(API_INTERFACE, API_PORT) - print "JSONRPC API is available at " + API_CONNECTION_STRING - print "To quit press ctrl-c or call 'stop' via the API" - args = parser.parse_args() + if not args.logtoconsole and not args.quiet: + print "Starting lbrynet-daemon from command line" + print "To view activity, view the log file here: " + LOG_FILENAME + print "Web UI is available at http://%s:%i" %(API_INTERFACE, API_PORT) + print "JSONRPC API is available at " + API_CONNECTION_STRING + print "To quit press ctrl-c or call 'stop' via the API" if args.branch == "HEAD": GIT_CMD_STRING = "git ls-remote https://github.com/lbryio/lbry-web-ui.git | grep %s | cut -f 1" % args.branch @@ -171,7 +177,8 @@ def start(): if args.launchui: d.addCallback(lambda _: webbrowser.open(UI_ADDRESS)) reactor.run() - print "\nClosing lbrynet-daemon" + if not args.logtoconsole and not args.quiet: + print "\nClosing lbrynet-daemon" else: log.info("Not connected to internet, unable to start") print "Not connected to internet, unable to start"