forked from esotericnonsense/bitcoind-ncurses
/
process.py
237 lines (192 loc) · 8.71 KB
/
process.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
import Queue, textwrap, time
import tx
import block
import monitor
import peers
import wallet
import splash
import console
import net
def queue(state, window, interface_queue):
try:
s = interface_queue.get(False)
except Queue.Empty:
return False
if 'stop' in s:
return s['stop']
elif 'resize' in s:
if state['mode'] == 'transaction':
tx.draw_window(state, window)
elif state['mode'] == 'block':
block.draw_window(state, window)
elif state['mode'] == 'peers':
peers.draw_window(state, window)
elif state['mode'] == 'wallet':
wallet.draw_window(state, window)
elif state['mode'] == 'monitor':
monitor.draw_window(state, window)
elif state['mode'] == 'console':
console.draw_window(state, window)
elif state['mode'] == 'net':
net.draw_window(state, window)
# redraw_all_the_things
pass
elif 'getinfo' in s:
state['version'] = str(s['getinfo']['version'] / 1000000)
state['version'] += '.' + str((s['getinfo']['version'] % 1000000) / 10000)
state['version'] += '.' + str((s['getinfo']['version'] % 10000) / 100)
state['version'] += '.' + str((s['getinfo']['version'] % 100))
if s['getinfo']['testnet'] == True:
state['testnet'] = 1
else:
state['testnet'] = 0
state['peers'] = s['getinfo']['connections']
if state['mode'] == "splash":
splash.draw_window(state, window)
elif 'getconnectioncount' in s:
state['peers'] = s['getconnectioncount']
elif 'getblockcount' in s:
state['blockcount'] = s['getblockcount']
if 'browse_height' not in state['blocks']:
state['blocks']['browse_height'] = state['blockcount']
elif 'getbalance' in s:
state['balance'] = s['getbalance']
elif 'getunconfirmedbalance' in s:
state['unconfirmedbalance'] = s['getunconfirmedbalance']
elif 'getblock' in s:
height = s['getblock']['height']
state['blocks'][str(height)] = s['getblock']
if state['mode'] == "monitor":
monitor.draw_window(state, window)
if state['mode'] == "block":
if 'queried' in s['getblock']:
state['blocks'][str(height)].pop('queried')
state['blocks']['browse_height'] = height
state['blocks']['offset'] = 0
state['blocks']['cursor'] = 0
block.draw_window(state, window)
elif 'coinbase' in s:
height = str(s['height'])
if height in state['blocks']:
state['blocks'][height]['coinbase_amount'] = s['coinbase']
elif 'getdifficulty' in s:
state['difficulty'] = s['getdifficulty']
elif 'getnetworkhashps' in s:
blocks = s['getnetworkhashps']['blocks']
state['networkhashps'][blocks] = s['getnetworkhashps']['value']
if state['mode'] == "splash" and blocks == 2016: # initialization complete
state['mode'] = "monitor"
monitor.draw_window(state, window)
elif 'getnettotals' in s:
state['totalbytesrecv'] = s['getnettotals']['totalbytesrecv']
state['totalbytessent'] = s['getnettotals']['totalbytessent']
state['history']['getnettotals'].append(s['getnettotals'])
# ensure getnettotals history does not fill RAM eventually, 300 items is enough
if len(state['history']['getnettotals']) > 500:
state['history']['getnettotals'] = state['history']['getnettotals'][-300:]
if state['mode'] == 'net':
net.draw_window(state, window)
elif 'getrawmempool' in s:
state['rawmempool'] = s['getrawmempool']
elif 'getpeerinfo' in s:
state['peerinfo'] = s['getpeerinfo']
state['peerinfo_offset'] = 0
if state['mode'] == "peers":
peers.draw_window(state, window)
elif 'listsinceblock' in s:
state['wallet'] = s['listsinceblock']
state['wallet']['cursor'] = 0
state['wallet']['offset'] = 0
state['wallet']['view_string'] = []
state['wallet']['transactions'].sort(key=lambda entry: entry['category'], reverse=True)
# add cumulative balance field to transactiosn once ordered by time
state['wallet']['transactions'].sort(key=lambda entry: entry['time'])
state['wallet']['transactions'].sort(key=lambda entry: entry['confirmations'], reverse=True)
cumulative_balance = 0
nonce = 0 # ensures a definitive ordering of transactions for cumulative balance
for entry in state['wallet']['transactions']:
entry['nonce'] = nonce
nonce += 1
if 'amount' in entry:
if 'fee' in entry:
cumulative_balance += entry['fee']
cumulative_balance += entry['amount']
entry['cumulative_balance'] = cumulative_balance
state['wallet']['transactions'].sort(key=lambda entry: entry['nonce'], reverse=True)
unit = 'BTC'
if 'testnet' in state:
if state['testnet']:
unit = 'TNC'
for entry in state['wallet']['transactions']:
if 'txid' in entry:
entry_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(entry['time']))
output_string = entry_time + " %8d" % entry['confirmations'] + " conf"
delta = entry['amount']
if 'fee' in entry:
delta += entry['fee'] # this fails if not all inputs owned by wallet; could be 'too negative'
output_string += "% 17.8f" % delta + unit
output_string += " " + "% 17.8f" % entry['cumulative_balance'] + unit
state['wallet']['view_string'].append(output_string)
output_string = entry['txid'].rjust(74)
state['wallet']['view_string'].append(output_string)
if 'address' in entry: # TODO: more sanity checking here
output_string = " " + entry['category'].ljust(15) + entry['address']
else:
output_string = " unknown transaction type"
state['wallet']['view_string'].append(output_string)
state['wallet']['view_string'].append("")
if state['mode'] == "wallet":
wallet.draw_window(state, window)
elif 'lastblocktime' in s:
state['lastblocktime'] = s['lastblocktime']
elif 'txid' in s:
state['tx'] = {
'txid': s['txid'],
'vin': [],
'vout_string': [],
'cursor': 0,
'offset': 0,
'out_offset': 0,
'loaded': 1,
'mode': 'inputs',
'size': s['size'],
}
for vin in s['vin']:
if 'coinbase' in vin:
state['tx']['vin'].append({'coinbase': vin['coinbase']})
elif 'txid' in vin:
if 'prev_tx' in vin:
state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout'], 'prev_tx': vin['prev_tx']})
else:
state['tx']['vin'].append({'txid': vin['txid'], 'vout': vin['vout']})
state['tx']['total_outputs'] = 0
for vout in s['vout']:
if 'value' in vout:
if vout['scriptPubKey']['type'] == "pubkeyhash":
buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['addresses'][0]
else:
buffer_string = "% 14.8f" % vout['value'] + ": " + vout['scriptPubKey']['asm']
if 'confirmations' in s:
if 'spent' in vout:
if vout['spent'] == 'confirmed':
buffer_string += " [SPENT]"
elif vout['spent'] == 'unconfirmed':
buffer_string += " [UNCONFIRMED SPEND]"
else:
buffer_string += " [UNSPENT]"
state['tx']['total_outputs'] += vout['value']
state['tx']['vout_string'].extend(textwrap.wrap(buffer_string,70)) # change this to scale with window ?
if 'total_inputs' in s:
state['tx']['total_inputs'] = s['total_inputs']
if 'confirmations' in s:
state['tx']['confirmations'] = s['confirmations']
if state['mode'] == "transaction":
tx.draw_window(state, window)
elif 'consolecommand' in s:
state['console']['cbuffer'].append(s['consolecommand'])
state['console']['rbuffer'].append(s['consoleresponse'])
state['console']['offset'] = 0
if state['mode'] == "console":
console.draw_window(state, window)
return False