From 205ae8573e48b50ab7e24b18a0274c0d639d4445 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Wed, 15 Jan 2020 21:50:13 +0200 Subject: [PATCH] Get rid of most of direct rpc() calls outside blockchaininterface --- jmclient/jmclient/blockchaininterface.py | 19 +++++++++++++++---- jmclient/jmclient/taker_utils.py | 8 +------- jmclient/jmclient/wallet_service.py | 4 ++-- jmclient/jmclient/wallet_utils.py | 21 +++++++-------------- jmclient/test/test_wallet.py | 2 +- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/jmclient/jmclient/blockchaininterface.py b/jmclient/jmclient/blockchaininterface.py index 4b3b285..8296c9d 100644 --- a/jmclient/jmclient/blockchaininterface.py +++ b/jmclient/jmclient/blockchaininterface.py @@ -275,11 +275,12 @@ class BitcoinCoreInterface(BlockchainInterface): hexval = str(rpcretval["hex"]) return btc.deserialize(hexval) - def list_transactions(self, num): + def list_transactions(self, num, skip=0): """ Return a list of the last `num` transactions seen - in the wallet (under any label/account). + in the wallet (under any label/account), optionally + skipping some. """ - return self.rpc("listtransactions", ["*", num, 0, True]) + return self.rpc("listtransactions", ["*", num, skip, True]) def get_transaction(self, txid): """ Returns a serialized transaction for txid txid, @@ -389,7 +390,17 @@ class BitcoinCoreInterface(BlockchainInterface): return int(Decimal(1e8) * Decimal(estimate)) def get_current_block_height(self): - return self.rpc("getblockchaininfo", [])["blocks"] + return self.rpc("getblockcount", []) + + def get_best_block_hash(self): + return self.rpc('getbestblockhash', []) + + def get_block_time(self, blockhash): + try: + # works with pruning enabled, but only after v0.12 + return self.rpc('getblockheader', [blockhash])['time'] + except JsonRpcError: + return self.rpc('getblock', [blockhash])['time'] class RegtestBitcoinCoreMixin(): diff --git a/jmclient/jmclient/taker_utils.py b/jmclient/jmclient/taker_utils.py index 94ffe53..78c0a72 100644 --- a/jmclient/jmclient/taker_utils.py +++ b/jmclient/jmclient/taker_utils.py @@ -131,15 +131,9 @@ def restart_wait(txid): and confirmed (it must be an in-wallet transaction since it always spends coins from the wallet). """ - try: - res = jm_single().bc_interface.rpc('gettransaction', [txid, True]) - except JsonRpcError as e: - return False + res = jm_single().bc_interface.get_transaction(txid) if not res: return False - if "confirmations" not in res: - log.debug("Malformed gettx result: " + str(res)) - return False if res["confirmations"] == 0: return False if res["confirmations"] < 0: diff --git a/jmclient/jmclient/wallet_service.py b/jmclient/jmclient/wallet_service.py index 186b26b..2f423ad 100644 --- a/jmclient/jmclient/wallet_service.py +++ b/jmclient/jmclient/wallet_service.py @@ -70,7 +70,7 @@ class WalletService(Service): the right height. """ try: - self.current_blockheight = self.bci.rpc("getblockcount", []) + self.current_blockheight = self.bci.get_current_block_height() assert isinstance(self.current_blockheight, Integral) except Exception as e: jlog.error("Failure to get blockheight from Bitcoin Core:") @@ -578,7 +578,7 @@ class WalletService(Service): def sync_unspent(self): st = time.time() # block height needs to be real time for addition to our utxos: - current_blockheight = self.bci.rpc("getblockcount", []) + current_blockheight = self.bci.get_current_block_height() wallet_name = self.get_wallet_name() self.reset_utxos() diff --git a/jmclient/jmclient/wallet_utils.py b/jmclient/jmclient/wallet_utils.py index a6af243..7382796 100644 --- a/jmclient/jmclient/wallet_utils.py +++ b/jmclient/jmclient/wallet_utils.py @@ -308,7 +308,7 @@ def get_tx_info(txid): blocktime: int, blocktime this tx was mined txd: deserialized transaction object (hex-encoded data) """ - rpctx = jm_single().bc_interface.rpc('gettransaction', [txid]) + rpctx = jm_single().bc_interface.get_transaction(txid) txhex = str(rpctx['hex']) txd = btc.deserialize(txhex) output_script_values = {binascii.unhexlify(sv['script']): sv['value'] @@ -616,8 +616,7 @@ def wallet_fetch_history(wallet, options): buf = range(1000) t = 0 while len(buf) == 1000: - buf = jm_single().bc_interface.rpc('listtransactions', ["*", - 1000, t, True]) + buf = jm_single().bc_interface.list_transactions(1000, t) t += len(buf) # confirmed tx_data = ((tx['txid'], tx['blockhash'], tx['blocktime'], 0) for tx @@ -690,10 +689,9 @@ def wallet_fetch_history(wallet, options): rpc_inputs = [] for ins in txd['ins']: - try: - wallet_tx = jm_single().bc_interface.rpc('gettransaction', - [ins['outpoint']['hash']]) - except JsonRpcError: + wallet_tx = jm_single().bc_interface.get_transaction( + ins['outpoint']['hash']) + if wallet_tx is None: continue input_dict = btc.deserialize(str(wallet_tx['hex']))['outs'][ins[ 'outpoint']['index']] @@ -835,13 +833,8 @@ def wallet_fetch_history(wallet, options): cj_batch[7]/n, min(cj_batch[8]), max(cj_batch[9]), '...') - bestblockhash = jm_single().bc_interface.rpc('getbestblockhash', []) - try: - #works with pruning enabled, but only after v0.12 - now = jm_single().bc_interface.rpc('getblockheader', [bestblockhash] - )['time'] - except JsonRpcError: - now = jm_single().bc_interface.rpc('getblock', [bestblockhash])['time'] + bestblockhash = jm_single().bc_interface.get_best_block_hash() + now = jm_single().bc_interface.get_block_time(bestblockhash) jmprint(' %s best block is %s' % (datetime.fromtimestamp(now) .strftime("%Y-%m-%d %H:%M"), bestblockhash)) total_profit = float(balance - sum(deposits)) / float(100000000) diff --git a/jmclient/test/test_wallet.py b/jmclient/test/test_wallet.py index f9f255e..173f926 100644 --- a/jmclient/test/test_wallet.py +++ b/jmclient/test/test_wallet.py @@ -50,7 +50,7 @@ def get_populated_wallet(amount=10**8, num=3): def fund_wallet_addr(wallet, addr, value_btc=1): txin_id = jm_single().bc_interface.grab_coins(addr, value_btc) - txinfo = jm_single().bc_interface.rpc('gettransaction', [txin_id]) + txinfo = jm_single().bc_interface.get_transaction(txin_id) txin = btc.deserialize(unhexlify(txinfo['hex'])) utxo_in = wallet.add_new_utxos_(txin, unhexlify(txin_id), 1) assert len(utxo_in) == 1