180 lines
7.0 KiB
Python
Executable File
Vendored
180 lines
7.0 KiB
Python
Executable File
Vendored
#!/usr/bin/env python
|
|
|
|
'''
|
|
Copyright (c) 2018, The Monero Project
|
|
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors
|
|
may be used to endorse or promote products derived from this software without
|
|
specific prior written permission.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
'''
|
|
|
|
import argparse
|
|
import lmdb
|
|
from ctypes import *
|
|
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_hash(block_hash):
|
|
#return '{}...{}'.format(block_hash[:4], block_hash[-4:])
|
|
return str(block_hash)
|
|
|
|
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, scale):
|
|
return '{0:.6f}'.format(amount/scale)
|
|
|
|
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(aux, path):
|
|
env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
|
|
balance = env.open_db(('aux-balance' if aux else 'balance').encode())
|
|
with env.begin(db=balance) as txn:
|
|
with txn.cursor() as curs:
|
|
for key, value in curs:
|
|
address = format_address(address_from_key(key))
|
|
amount = c_longlong.from_buffer_copy(value).value
|
|
amount = format_amount(amount, 1e8 if aux else 1e12)
|
|
print('{}\t{}'.format(address, amount))
|
|
env.close()
|
|
|
|
def print_payements(aux, path):
|
|
env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
|
|
payments = env.open_db(('aux-payments' if aux else 'payments').encode())
|
|
with env.begin(db=payments) as txn:
|
|
with txn.cursor() as curs:
|
|
for key, value in curs:
|
|
address = format_address(address_from_key(key))
|
|
p = payment_t.from_buffer_copy(value)
|
|
amount = format_amount(p.amount, 1e8 if aux else 1e12)
|
|
dt = format_timestamp(p.timestamp)
|
|
print('{}\t{}\t{}'.format(address, amount, dt))
|
|
env.close()
|
|
|
|
def print_mined(aux, path):
|
|
env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
|
|
blocks = env.open_db(('aux-blocks' if aux else '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)
|
|
bh = format_block_hash(b.hash.decode('utf-8'))
|
|
dt = format_timestamp(b.timestamp)
|
|
reward = format_amount(b.reward, 1e8 if aux else 1e12)
|
|
status = format_block_status(b.status)
|
|
print('{}\t{}\t{}\t{}\t{}'.format(height, bh, status,
|
|
reward, dt))
|
|
env.close()
|
|
|
|
def print_shares(aux, path):
|
|
env = lmdb.open(path, readonly=True, max_dbs=1, create=False)
|
|
shares = env.open_db(('aux-shares' if aux else 'shares').encode(), dupsort=True)
|
|
with env.begin(db=shares) as txn:
|
|
with txn.cursor() as curs:
|
|
curs.last()
|
|
for i in range(25):
|
|
key, value = curs.item()
|
|
height = c_longlong.from_buffer_copy(key).value
|
|
share = share_t.from_buffer_copy(value)
|
|
address = format_address(address_from_key(share.address))
|
|
difficulty = share.difficulty
|
|
dt = format_timestamp(share.timestamp)
|
|
print('{}\t{}\t{}\t{}'.format(height, address, difficulty, dt))
|
|
if not curs.prev():
|
|
break
|
|
env.close()
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
group.add_argument('-b', '--balances', action='store_true',
|
|
help='list miner balances')
|
|
group.add_argument('-B', '--aux-balances', action='store_true',
|
|
help='list auxiliary miner balances')
|
|
group.add_argument('-p', '--payments', action='store_true',
|
|
help='list payments made')
|
|
group.add_argument('-P', '--aux-payments', action='store_true',
|
|
help='list auxiliary payments made')
|
|
group.add_argument('-m', '--mined', action='store_true',
|
|
help='list mined blocks')
|
|
group.add_argument('-M', '--aux-mined', action='store_true',
|
|
help='list auxiliary mined blocks')
|
|
group.add_argument('-s', '--shares', action='store_true',
|
|
help='list recent shares')
|
|
group.add_argument('-S', '--aux-shares', action='store_true',
|
|
help='list recent auxiliary shares')
|
|
parser.add_argument('database', help='path to database')
|
|
args = parser.parse_args()
|
|
if args.balances:
|
|
print_balance(False, args.database)
|
|
if args.aux_balances:
|
|
print_balance(True, args.database)
|
|
elif args.payments:
|
|
print_payements(False, args.database)
|
|
elif args.aux_payments:
|
|
print_payements(True, args.database)
|
|
elif args.mined:
|
|
print_mined(False, args.database)
|
|
elif args.aux_mined:
|
|
print_mined(True, args.database)
|
|
elif args.shares:
|
|
print_shares(False, args.database)
|
|
elif args.aux_shares:
|
|
print_shares(True, args.database)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|