forked from esotericnonsense/bitcoind-ncurses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
134 lines (103 loc) · 5.39 KB
/
block.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
#!/usr/bin/env python
import curses, time, calendar
import global_mod as g
import getstr
import footer
def draw_window(state, window):
window.clear()
window.refresh()
win_header = curses.newwin(5, 75, 0, 0)
if 'browse_height' in state['blocks']:
height = str(state['blocks']['browse_height'])
if height in state['blocks']:
blockdata = state['blocks'][height]
win_header.addstr(0, 1, "height: " + height.zfill(6) + " (J/K: browse, HOME/END: quicker, L: latest, G: seek)", curses.A_BOLD)
win_header.addstr(1, 1, "hash: " + blockdata['hash'], curses.A_BOLD)
win_header.addstr(2, 1, "root: " + blockdata['merkleroot'], curses.A_BOLD)
win_header.addstr(3, 1, str(blockdata['size']) + " bytes (" + str(blockdata['size']/1024) + " KB) ", curses.A_BOLD)
win_header.addstr(3, 26, "diff: " + "{:,d}".format(int(blockdata['difficulty'])), curses.A_BOLD)
win_header.addstr(3, 52, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(blockdata['time'])), curses.A_BOLD)
win_header.addstr(4, 51, ("v" + str(blockdata['version'])).rjust(20), curses.A_BOLD)
draw_transactions(state)
state['blocks']['loaded'] = 1
else:
win_header.addstr(0, 1, "no block information loaded", curses.A_BOLD + curses.color_pair(3))
win_header.addstr(1, 1, "press 'G' to enter a block hash, height, or timestamp", curses.A_BOLD)
else:
win_header.addstr(0, 1, "no block information loaded", curses.A_BOLD + curses.color_pair(3))
win_header.addstr(1, 1, "press 'G' to enter a block hash, height, or timestamp", curses.A_BOLD)
win_header.refresh()
footer.draw_window(state)
def draw_transactions(state):
window_height = state['y'] - 6
win_transactions = curses.newwin(window_height, 75, 5, 0)
height = str(state['blocks']['browse_height'])
blockdata = state['blocks'][height]
tx_count = len(blockdata['tx'])
bytes_per_tx = blockdata['size'] / tx_count
win_transactions.addstr(0, 1, "Transactions: " + ("% 4d" % tx_count + " (" + str(bytes_per_tx) + " bytes/tx)").ljust(26) + "(UP/DOWN: scroll, ENTER: view)", curses.A_BOLD + curses.color_pair(5))
# reset cursor if it's been resized off the bottom
if state['blocks']['cursor'] > state['blocks']['offset'] + (window_height-2):
state['blocks']['offset'] = state['blocks']['cursor'] - (window_height-2)
offset = state['blocks']['offset']
for index in xrange(offset, offset+window_height-1):
if index < len(blockdata['tx']):
if index == state['blocks']['cursor']:
win_transactions.addstr(index+1-offset, 1, ">", curses.A_REVERSE + curses.A_BOLD)
condition = (index == offset+window_height-2) and (index+1 < len(blockdata['tx']))
condition = condition or ( (index == offset) and (index > 0) )
if condition:
win_transactions.addstr(index+1-offset, 3, "...")
else:
win_transactions.addstr(index+1-offset, 3, blockdata['tx'][index])
win_transactions.refresh()
def draw_input_window(state, window, rpc_queue):
color = curses.color_pair(2)
if 'testnet' in state:
if state['testnet']: color = curses.color_pair(3)
window.clear()
window.addstr(0, 1, "darkcoind-ncurses " + g.version + " [block input mode]", color + curses.A_BOLD)
window.addstr(1, 1, "please enter block height or hash", curses.A_BOLD)
window.addstr(2, 1, "or timestamp (accepted formats: YYYY-MM-DD hh:mm:ss, YYYY-MM-DD)", curses.A_BOLD)
window.refresh()
entered_block = getstr.getstr(67, 4, 1) # w, y, x
entered_block_timestamp = 0
try:
entered_block_time = time.strptime(entered_block, "%Y-%m-%d")
entered_block_timestamp = calendar.timegm(entered_block_time)
except: pass
try:
entered_block_time = time.strptime(entered_block, "%Y-%m-%d %H:%M:%S")
entered_block_timestamp = calendar.timegm(entered_block_time)
except: pass
if entered_block_timestamp:
s = {'findblockbytimestamp': entered_block_timestamp}
rpc_queue.put(s)
window.addstr(5, 1, "waiting for block (will stall here if not found)", color + curses.A_BOLD)
window.refresh()
state['mode'] = "block"
elif len(entered_block) == 64:
s = {'getblock': entered_block}
rpc_queue.put(s)
window.addstr(5, 1, "waiting for block (will stall here if not found)", color + curses.A_BOLD)
window.refresh()
state['mode'] = "block"
elif (len(entered_block) < 7) and entered_block.isdigit() and (int(entered_block) <= state['mininginfo']['blocks']):
if entered_block in state['blocks']:
state['blocks']['browse_height'] = int(entered_block)
state['mode'] = "block"
draw_window(state, window)
else:
s = {'getblockhash': int(entered_block)}
rpc_queue.put(s)
window.addstr(5, 1, "waiting for block (will stall here if not found)", color + curses.A_BOLD)
window.refresh()
state['mode'] = "block"
state['blocks']['browse_height'] = int(entered_block)
else:
window.addstr(5, 1, "not a valid hash, height, or timestamp format", color + curses.A_BOLD)
window.refresh()
time.sleep(0.5)
window.clear()
window.refresh()
state['mode'] = "overview"