Fix query.py

This commit is contained in:
Neil Booth 2016-10-22 14:19:51 +09:00
parent 2b45698962
commit 76748d2673
3 changed files with 10 additions and 9 deletions

View File

@ -7,15 +7,15 @@ import os
import sys import sys
from server.env import Env from server.env import Env
from server.block_processor import DB from server.block_processor import BlockProcessor
from lib.hash import hash_to_str from lib.hash import hash_to_str
def main(): def main():
env = Env() env = Env()
coin = env.coin
os.chdir(env.db_dir) os.chdir(env.db_dir)
db = DB(env) bp = BlockProcessor(env, None)
coin = db.coin
argc = 1 argc = 1
try: try:
limit = int(sys.argv[argc]) limit = int(sys.argv[argc])
@ -26,19 +26,19 @@ def main():
print('Address: ', addr) print('Address: ', addr)
hash168 = coin.address_to_hash168(addr) hash168 = coin.address_to_hash168(addr)
n = None n = None
for n, (tx_hash, height) in enumerate(db.get_history(hash168, limit)): for n, (tx_hash, height) in enumerate(bp.get_history(hash168, limit)):
print('History #{:d}: hash: {} height: {:d}' print('History #{:d}: hash: {} height: {:d}'
.format(n + 1, hash_to_str(tx_hash), height)) .format(n + 1, hash_to_str(tx_hash), height))
if n is None: if n is None:
print('No history') print('No history')
n = None n = None
for n, utxo in enumerate(db.get_utxos(hash168, limit)): for n, utxo in enumerate(bp.get_utxos(hash168, limit)):
print('UTXOs #{:d}: hash: {} pos: {:d} height: {:d} value: {:d}' print('UTXOs #{:d}: hash: {} pos: {:d} height: {:d} value: {:d}'
.format(n + 1, hash_to_str(utxo.tx_hash), .format(n + 1, hash_to_str(utxo.tx_hash),
utxo.tx_pos, utxo.height, utxo.value)) utxo.tx_pos, utxo.height, utxo.value))
if n is None: if n is None:
print('No UTXOs') print('No UTXOs')
balance = db.get_balance(hash168) balance = bp.get_balance(hash168)
print('Balance: {} {}'.format(coin.decimal_value(balance), print('Balance: {} {}'.format(coin.decimal_value(balance),
coin.SHORTNAME)) coin.SHORTNAME))

View File

@ -6,7 +6,7 @@ import ast
import asyncio import asyncio
import struct import struct
import time import time
from collections import defaultdict from collections import defaultdict, namedtuple
from functools import partial from functools import partial
import plyvel import plyvel
@ -25,6 +25,9 @@ def formatted_time(t):
t // 86400, (t % 86400) // 3600, (t % 3600) // 60, t % 60) t // 86400, (t % 86400) // 3600, (t % 3600) // 60, t % 60)
UTXO = namedtuple("UTXO", "tx_num tx_pos tx_hash height value")
class ChainError(Exception): class ChainError(Exception):
pass pass

View File

@ -6,7 +6,6 @@ import itertools
import os import os
import struct import struct
from bisect import bisect_right from bisect import bisect_right
from collections import namedtuple
from lib.script import ScriptPubKey from lib.script import ScriptPubKey
from lib.util import chunks, LoggedClass from lib.util import chunks, LoggedClass
@ -18,7 +17,6 @@ HIST_ENTRIES_PER_KEY = 1024
HIST_VALUE_BYTES = HIST_ENTRIES_PER_KEY * 4 HIST_VALUE_BYTES = HIST_ENTRIES_PER_KEY * 4
ADDR_TX_HASH_LEN = 4 ADDR_TX_HASH_LEN = 4
UTXO_TX_HASH_LEN = 4 UTXO_TX_HASH_LEN = 4
UTXO = namedtuple("UTXO", "tx_num tx_pos tx_hash height value")
class UTXOCache(LoggedClass): class UTXOCache(LoggedClass):