Get query.py working

This commit is contained in:
Neil Booth 2018-08-02 10:51:25 +09:00
parent 9e3893b065
commit 147989a0a6
2 changed files with 76 additions and 41 deletions

View File

@ -404,7 +404,7 @@ class HOdlcoin(Coin):
class BitcoinCash(BitcoinMixin, Coin): class BitcoinCash(BitcoinMixin, Coin):
NAME = "BitcoinCash" NAME = "BitcoinCash"
SHORTNAME = "BCC" SHORTNAME = "BCH"
TX_COUNT = 246362688 TX_COUNT = 246362688
TX_COUNT_HEIGHT = 511484 TX_COUNT_HEIGHT = 511484
TX_PER_BLOCK = 400 TX_PER_BLOCK = 400

115
query.py
View File

@ -12,64 +12,99 @@
Not currently documented; might become easier to use in future. Not currently documented; might become easier to use in future.
''' '''
import argparse
import asyncio
import sys import sys
from electrumx import Env from electrumx import Env
from electrumx.server.db import DB from electrumx.server.db import DB
from electrumx.lib.hash import hash_to_hex_str from electrumx.lib.hash import hash_to_hex_str, Base58Error
def count_entries(hist_db, utxo_db): async def print_stats(hist_db, utxo_db):
utxos = 0 count = 0
for key in utxo_db.iterator(prefix=b'u', include_value=False): for key in utxo_db.iterator(prefix=b'u', include_value=False):
utxos += 1 count += 1
print("UTXO count:", utxos) print(f'UTXO count: {utxos}')
hashX = 0 count = 0
for key in utxo_db.iterator(prefix=b'h', include_value=False): for key in utxo_db.iterator(prefix=b'h', include_value=False):
hashX += 1 count += 1
print("HashX count:", hashX) print(f'HashX count: {count}')
hist = 0 hist = 0
hist_len = 0 hist_len = 0
for key, value in hist_db.iterator(prefix=b'H'): for key, value in hist_db.iterator(prefix=b'H'):
hist += 1 hist += 1
hist_len += len(value) // 4 hist_len += len(value) // 4
print("History rows {:,d} entries {:,d}".format(hist, hist_len)) print(f'History rows {hist:,d} entries {hist_len:,d}')
def arg_to_hashX(coin, arg):
try:
script = bytes.fromhex(arg)
print(f'Script: {arg}')
return coin.hashX_from_script(script)
except ValueError:
pass
try:
hashX = coin.address_to_hashX(arg)
print(f'Address: {arg}')
return hashX
except Base58Error:
print(f'Ingoring unknown arg: {arg}')
return None
async def query(args):
env = Env()
db = DB(env)
coin = env.coin
await db._open_dbs(False)
if not args.scripts:
await print_stats(db.hist_db, db.utxo_db)
return
limit = args.limit
for arg in args.scripts:
hashX = arg_to_hashX(coin, arg)
if not hashX:
continue
n = None
for n, (tx_hash, height) in enumerate(db.get_history(hashX, limit),
start=1):
print(f'History #{n:,d}: height {height:,d} '
f'tx_hash {hash_to_hex_str(tx_hash)}')
if n is None:
print('No history found')
n = None
for n, utxo in enumerate(db.get_utxos(hashX, limit), start=1):
print(f'UTXO #{n:,d}: tx_hash {hash_to_hex_str(utxo.tx_hash)} '
f'tx_pos {utxo.tx_pos:,d} height {utxo.height:,d} '
f'value {utxo.value:,d}')
if n is None:
print('No UTXOs found')
balance = db.get_balance(hashX)
print(f'Balance: {coin.decimal_value(balance):,f} {coin.SHORTNAME}')
def main(): def main():
env = Env() default_limit = 10
bp = DB(env) parser = argparse.ArgumentParser(
coin = env.coin 'query.py',
if len(sys.argv) == 1: description='Invoke with COIN and DB_DIRECTORY set in the '
count_entries(bp.hist_db, bp.utxo_db) 'environment as they would be invoking electrumx_server'
return )
argc = 1 parser.add_argument('-l', '--limit', metavar='limit', type=int,
try: default=10, help=f'maximum number of entries to '
limit = int(sys.argv[argc]) f'return (default: {default_limit})')
argc += 1 parser.add_argument('scripts', nargs='*', default=[], type=str,
except Exception: help='hex scripts to query')
limit = 10 args = parser.parse_args()
for addr in sys.argv[argc:]: loop = asyncio.get_event_loop()
print('Address: ', addr) loop.run_until_complete(query(args))
hashX = coin.address_to_hashX(addr)
for n, (tx_hash, height) in enumerate(bp.get_history(hashX, limit)):
print('History #{:d}: hash: {} height: {:d}'
.format(n + 1, hash_to_hex_str(tx_hash), height))
n = None
for n, utxo in enumerate(bp.get_utxos(hashX, limit)):
print('UTXOs #{:d}: hash: {} pos: {:d} height: {:d} value: {:d}'
.format(n + 1, hash_to_hex_str(utxo.tx_hash),
utxo.tx_pos, utxo.height, utxo.value))
if n is None:
print('No UTXOs')
balance = bp.get_balance(hashX)
print('Balance: {} {}'.format(coin.decimal_value(balance),
coin.SHORTNAME))
if __name__ == '__main__': if __name__ == '__main__':
main() main()