flesh out the inspection tool a bit more

This commit is contained in:
Jethro Grassie 2019-05-11 22:31:26 -04:00
parent 747b471cd6
commit 2941df1c13
No known key found for this signature in database
GPG key ID: DE8ED755616565BB

74
tools/inspect-data vendored
View file

@ -35,22 +35,56 @@ developers.
''' '''
import argparse import argparse
import sys
import lmdb import lmdb
import struct
from ctypes import * from ctypes import *
from datetime import datetime from datetime import datetime
class share_t(Structure):
_fields_ = [('height', c_longlong),
('difficulty', c_longlong),
('address', c_char*128),
('timestamp', c_longlong)]
class payment_t(Structure):
_fields_ = [('amount', c_longlong),
('timestamp', c_longlong),
('address', c_char*128)]
class block_t(Structure):
_fields_ = [('height', c_longlong),
('hash', c_char*64),
('prev_hash', c_char*64),
('difficulty', c_longlong),
('status', c_long),
('reward', c_longlong),
('timestamp', c_longlong)]
def format_block_status(status):
s = ["LOCKED", "UNLOCKED", "ORPHANED"]
return s[status]
def format_timestamp(timestamp):
dt = datetime.fromtimestamp(timestamp)
return dt.strftime('%Y-%m-%d %H:%M:%S')
def format_amount(amount):
return '{0:.6f}'.format(amount/1e12)
def format_address(address):
return '{}...{}'.format(address[:8], address[-8:])
def address_from_key(key):
return key.decode('utf-8').rstrip('\0')
def print_balance(path): def print_balance(path):
env = lmdb.open(path, readonly=True, max_dbs=1, create=False) env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
balance = env.open_db('balance'.encode()) balance = env.open_db('balance'.encode())
with env.begin(db=balance) as txn: with env.begin(db=balance) as txn:
with txn.cursor() as curs: with txn.cursor() as curs:
for key, value in curs: for key, value in curs:
address = key.decode('utf-8').rstrip('\0') address = format_address(address_from_key(key))
address = '{}...{}'.format(address[:8], address[-8:])
amount = c_longlong.from_buffer_copy(value).value amount = c_longlong.from_buffer_copy(value).value
amount = '{0:.6f}'.format(amount/1e12) amount = format_amount(amount)
print('{}\t{}'.format(address, amount)) print('{}\t{}'.format(address, amount))
env.close() env.close()
@ -60,13 +94,25 @@ def print_payements(path):
with env.begin(db=payments) as txn: with env.begin(db=payments) as txn:
with txn.cursor() as curs: with txn.cursor() as curs:
for key, value in curs: for key, value in curs:
address = key.decode('utf-8').rstrip('\0') address = format_address(address_from_key(key))
address = '{}...{}'.format(address[:8], address[-8:]) p = payment_t.from_buffer_copy(value)
amount, ts, addr = struct.unpack("Q Q 128s", value) amount = format_amount(p.amount)
amount = '{0:.6f}'.format(amount/1e12) dt = format_timestamp(p.timestamp)
dt = datetime.fromtimestamp(ts) print('{}\t{}\t{}'.format(address, amount, dt))
print('{}\t{}\t{}'.format(address, amount, env.close()
dt.strftime('%Y-%m-%d %H:%M:%S')))
def print_mined(path):
env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
blocks = env.open_db('blocks'.encode())
with env.begin(db=blocks) as txn:
with txn.cursor() as curs:
for key, value in curs:
height = c_longlong.from_buffer_copy(key).value
b = block_t.from_buffer_copy(value)
dt = format_timestamp(b.timestamp)
reward = format_amount(b.reward)
status = format_block_status(b.status)
print('{}\t{}\t{}\t{}'.format(height, status, reward, dt))
env.close() env.close()
def main(): def main():
@ -76,12 +122,16 @@ def main():
help='list miner balances') help='list miner balances')
group.add_argument('-p', '--payments', action='store_true', group.add_argument('-p', '--payments', action='store_true',
help='list payments made') help='list payments made')
group.add_argument('-m', '--mined', action='store_true',
help='list mined blocks')
parser.add_argument('database', help='path to database') parser.add_argument('database', help='path to database')
args = parser.parse_args() args = parser.parse_args()
if args.balances: if args.balances:
print_balance(args.database) print_balance(args.database)
elif args.payments: elif args.payments:
print_payements(args.database) print_payements(args.database)
elif args.mined:
print_mined(args.database)
if __name__ == '__main__': if __name__ == '__main__':
main() main()