-
Notifications
You must be signed in to change notification settings - Fork 27
/
monitor.py
175 lines (145 loc) · 7.2 KB
/
monitor.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
import curses, time, math
import global_mod as g
from decimal import Decimal
def draw_window(state, old_window):
# TODO: only draw parts that actually changed
old_window.clear()
old_window.refresh()
window = curses.newwin(19, 76, 0, 0)
if 'version' in state:
if state['testnet'] == 1:
color = curses.color_pair(2)
window.addstr(1, 1, state['version'] + " (testnet)", color + curses.A_BOLD)
unit = 'TNC'
else:
color = curses.color_pair(1)
window.addstr(1, 1, state['version'], color + curses.A_BOLD)
unit = 'BTC'
if "dev" in g.version:
color = curses.color_pair(5) + curses.A_REVERSE
window.addstr(0, 1, "bitcoind-ncurses " + g.version, color + curses.A_BOLD)
if 'peers' in state:
if state['peers'] > 0:
color = 0
else:
color = curses.color_pair(3)
window.addstr(0, 32, str(state['peers']) + " peers ", color + curses.A_BOLD)
if 'balance' in state:
balance_string = "%0.8f" % state['balance'] + " " + unit
if 'unconfirmedbalance' in state:
if state['unconfirmedbalance'] != 0:
balance_string += " (+" + "%0.8f" % state['unconfirmedbalance'] + " unconf)"
window.addstr(1, 32, balance_string, curses.A_BOLD)
if 'mininginfo' in state:
height = str(state['mininginfo']['blocks'])
if height in state['blocks']:
blockdata = state['blocks'][str(height)]
if 'new' in blockdata:
window.attrset(curses.A_REVERSE + curses.color_pair(5) + curses.A_BOLD)
blockdata.pop('new')
window.addstr(3, 1, height.zfill(6) + ": " + str(blockdata['hash']))
window.addstr(4, 1, str(blockdata['size']) + " bytes (" + str(blockdata['size']//1024) + " KB) ")
tx_count = len(blockdata['tx'])
bytes_per_tx = blockdata['size'] // tx_count
window.addstr(5, 1, "Transactions: " + str(tx_count) + " (" + str(bytes_per_tx) + " bytes/tx)")
if 'coinbase_amount' in blockdata:
halvings = state['mininginfo']['blocks'] // 210000
block_subsidy = Decimal(50 * (0.5 ** halvings))
if block_subsidy:
coinbase_amount = blockdata['coinbase_amount']
total_fees = coinbase_amount - block_subsidy # assumption, mostly correct
if coinbase_amount > 0:
fee_percentage = "%0.2f" % ((total_fees / coinbase_amount) * 100)
coinbase_amount_str = "%0.8f" % coinbase_amount
window.addstr(7, 1, "Total block reward: " + coinbase_amount_str + " " + unit + " (" + fee_percentage + "% fees)")
if tx_count > 1:
tx_count -= 1 # the coinbase can't pay a fee
fees_per_tx = (total_fees / tx_count) * 1000
fees_per_kb = ((total_fees * 1024) / blockdata['size']) * 1000
total_fees_str = "%0.8f" % total_fees + " " + unit
fees_per_tx = "%0.5f" % fees_per_tx + " m" + unit + "/tx"
fees_per_kb = "%0.5f" % fees_per_kb + " m" + unit + "/KB"
window.addstr(8, 1, "Fees: " + total_fees_str + " (avg " + fees_per_tx + ", ~" + fees_per_kb + ")")
window.addstr(4, 37, "Block timestamp: " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(blockdata['time'])))
if state['lastblocktime'] == 0:
recvdelta_string = " "
else:
recvdelta = int(time.time() - state['lastblocktime'])
m, s = divmod(recvdelta, 60)
h, m = divmod(m, 60)
recvdelta_string = "{:02d}:{:02d}:{:02d}".format(h,m,s)
stampdelta = int(time.time() - blockdata['time'])
if stampdelta > 3600*3: # probably syncing if it's three hours old
stampdelta_string = " (syncing)"
elif stampdelta > 0:
m, s = divmod(stampdelta, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
stampdelta_string = "({:d}d {:02d}:{:02d}:{:02d} by stamp)".format(d,h,m,s)
else:
stampdelta_string = " (stamp in future)"
window.addstr(5, 37, "Age: " + recvdelta_string + " " + stampdelta_string)
if 'chainwork' in blockdata:
log2_chainwork = math.log(int(blockdata['chainwork'], 16), 2)
window.addstr(14, 1, "Chain work: 2**" + "%0.6f" % log2_chainwork)
diff = int(state['mininginfo']['difficulty'])
window.addstr(10, 1, "Diff: " + "{:,}".format(diff))
for block_avg in state['networkhashps']:
index = 10
if block_avg == 'diff':
pass
elif block_avg == 2016:
index += 1
elif block_avg == 144:
index += 2
else:
break
rate = state['networkhashps'][block_avg]
if block_avg != 'diff':
nextdiff = int((rate*600)//(2**32))
if state['testnet'] == 1:
nextdiff *= 2 # testnet has 1200 est. block interval, not 600
window.addstr(index, 1, "Est (" + str(block_avg).rjust(4) + "): ~" + "{:,}".format(nextdiff))
if rate > 10**19:
rate = int(rate)//10**18
suffix = " EH/s"
if rate > 10**16:
rate = int(rate)//10**15
suffix = " PH/s"
elif rate > 10**13:
rate = int(rate)//10**12
suffix = " TH/s"
else:
rate = int(rate)//10**6
suffix = " MH/s"
rate_string = "{:,}".format(rate) + suffix
window.addstr(index, 37, "Hashrate (" + str(block_avg).rjust(4) + "): " + rate_string.rjust(13))
index += 1
pooledtx = state['mininginfo']['pooledtx']
window.addstr(14, 37, "Mempool transactions: " + "% 5d" % pooledtx)
if 'totalbytesrecv' in state:
recvmb = "%.2f" % (state['totalbytesrecv']*1.0/1048576)
sentmb = "%.2f" % (state['totalbytessent']*1.0/1048576)
recvsent_string = "D/U: " + recvmb + " / " + sentmb + " MB"
window.addstr(0, 43, recvsent_string.rjust(30), curses.A_BOLD)
if 'estimatefee' in state:
string = "estimatefee:"
for blocks in state['estimatefee']:
est = state['estimatefee'][blocks]
if est > 0:
string += " (" + str(blocks) + ")" + "%4.2f" % (est*1000) + "m" + unit
if len(string) > 12:
window.addstr(15, 37, string)
if 'mininginfo' in state:
# errors = state['mininginfo']['errors']
# TODO: Remove this for production.
errors = "This is a development version of bitcoind-ncurses. Expect breakage."
if len(errors):
if state['y'] < 20:
y = state['y'] - 3
else:
y = 17
window.addstr(y, 1, errors[:72].ljust(72), curses.color_pair(5) + curses.A_BOLD + curses.A_REVERSE)
window.addstr(y+1, 1, errors[72:142].rjust(72), curses.color_pair(5) + curses.A_BOLD + curses.A_REVERSE)
window.refresh()