Browse Source

Merge #501: Get rid of most of direct rpc() calls outside blockchaininterface

205ae85 Get rid of most of direct rpc() calls outside blockchaininterface (Kristaps Kaupe)
master
Adam Gibson 6 years ago
parent
commit
6259b07d50
No known key found for this signature in database
GPG Key ID: 141001A1AF77F20B
  1. 19
      jmclient/jmclient/blockchaininterface.py
  2. 8
      jmclient/jmclient/taker_utils.py
  3. 4
      jmclient/jmclient/wallet_service.py
  4. 21
      jmclient/jmclient/wallet_utils.py
  5. 2
      jmclient/test/test_wallet.py

19
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():

8
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:

4
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()

21
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)

2
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

Loading…
Cancel
Save