forked from dsiganos/nano-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_count.py
executable file
·73 lines (54 loc) · 2.13 KB
/
block_count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import requests
import json
import argparse
import time
import datetime
import common
def parse_args():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-b', '--beta', action='store_true', default=False,
help='use beta network')
group.add_argument('-t', '--test', action='store_true', default=False,
help='use test network')
parser.add_argument('--rpc',
help='RPC URL to contact')
parser.add_argument('--csv', action='store_true', default=False,
help='sample every 10 seconds and write a CSV data')
return parser.parse_args()
def get_block_count(session, rpc_url):
params = {'action' : 'block_count'}
result = common.post(session, params, rpc_url)
count = int(result['count'])
unchecked = int(result['unchecked'])
cemented = int(result['cemented'])
return result, count, unchecked, cemented
def produce_csv_values(session, rpc_url):
print('count, timestamp (UTC), count, unchecked, cemented')
i = 0
lasttime = 0
while True:
if time.time() - lasttime > 10:
timestamp = datetime.datetime.utcnow().strftime("%H:%M:%S")
_, count, unchecked, cemented = get_block_count(session, rpc_url)
print('%s, %s, %s, %s, %s' % (i, timestamp, count, unchecked, cemented), flush=True)
i += 1
lasttime = time.time()
time.sleep(0.1)
def single_block_count(session, rpc_url):
result, count, unchecked, cemented = get_block_count(session, rpc_url)
print(json.dumps(result, indent=4))
print('count: {:>15,}'.format(count))
print('unchecked: {:>15,}'.format(unchecked))
print('cemented: {:>15,}'.format(cemented))
print('uncemented: {:>15,}'.format(count - cemented))
print('total: {:>15,}'.format(count + unchecked))
args = parse_args()
rpc_url = common.get_rpc_url(args)
print('RPC URL = %s' % rpc_url)
session = requests.Session()
if args.csv:
produce_csv_values(session, rpc_url)
else:
single_block_count(session, rpc_url)