forked from esotericnonsense/bitcoind-ncurses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
277 lines (228 loc) · 9.96 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
import Queue, textwrap, time
import tx
import block
import monitor
import peers
import wallet
import splash
import console
import net
import forks
import mnode
def resize(s, state, window):
if state['mode'] == 'tx':
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'] == 'overview':
monitor.draw_window(state, window)
elif state['mode'] == 'console':
console.draw_window(state, window)
elif state['mode'] == 'net':
net.draw_window(state, window)
elif state['mode'] == 'forks':
forks.draw_window(state, window)
elif state['mode'] == 'mnodes':
mnode.draw_window(state, window)
def getinfo(s, state, window):
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
if state['mode'] == "splash":
splash.draw_window(state, window)
def getconnectioncount(s, state, window):
state['peers'] = s['getconnectioncount']
def getbalance(s, state, window):
state['balance'] = s['getbalance']
def getunconfirmedbalance(s, state, window):
state['unconfirmedbalance'] = s['getunconfirmedbalance']
def getblock(s, state, window):
height = s['getblock']['height']
state['blocks'][str(height)] = s['getblock']
if state['mode'] == "overview":
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)
def coinbase(s, state, window):
height = str(s['height'])
if height in state['blocks']:
state['blocks'][height]['coinbase_amount'] = s['coinbase']
def getnetworkhashps(s, state, window):
blocks = s['getnetworkhashps']['blocks']
state['networkhashps'][blocks] = s['getnetworkhashps']['value']
if state['mode'] == "splash" and blocks == 576: # initialization complete
state['mode'] = "overview"
monitor.draw_window(state, window)
def getnettotals(s, state, window):
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)
def getmininginfo(s, state, window):
state['mininginfo'] = s['getmininginfo']
if 'browse_height' not in state['blocks']:
state['blocks']['browse_height'] = s['getmininginfo']['blocks']
state['networkhashps']['diff'] = (int(s['getmininginfo']['difficulty'])*2**32)/150
def getpeerinfo(s, state, window):
state['peerinfo'] = s['getpeerinfo']
state['peerinfo_offset'] = 0
if state['mode'] == "peers":
peers.draw_window(state, window)
def getchaintips(s, state, window):
state['chaintips'] = s['getchaintips']
state['chaintips_offset'] = 0
if state['mode'] == 'forks':
forks.draw_window(state, window)
def masternode(s, state, window):
state['mnodes'] = s['masternode']
state['mnodes_offset'] = 0
if state['mode'] == 'mnodes':
mnode.draw_window(state, window)
def listsinceblock(s, state, window):
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 = 'DRK'
if 'testnet' in state:
if state['testnet']:
unit = 'tDRK'
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)
def lastblocktime(s, state, window):
state['lastblocktime'] = s['lastblocktime']
def txid(s, state, window):
if s['size'] < 0:
if 'tx' in state:
state.pop('tx')
if state['mode'] == 'tx':
tx.draw_window(state, window)
return False
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'] == 'tx':
tx.draw_window(state, window)
def consolecommand(s, state, window):
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)
def estimatefee(s, state, window):
state['estimatefee'] = s['estimatefee']
def queue(state, window, interface_queue):
while True:
try:
s = interface_queue.get(False)
except Queue.Empty:
return False
if 'resize' in s: resize(s, state, window)
elif 'getinfo' in s: getinfo(s, state, window)
elif 'getconnectioncount' in s: getconnectioncount(s, state, window)
elif 'getbalance' in s: getbalance(s, state, window)
elif 'getunconfirmedbalance' in s: getunconfirmedbalance(s, state, window)
elif 'getblock' in s: getblock(s, state, window)
elif 'coinbase' in s: coinbase(s, state, window)
elif 'getnetworkhashps' in s: getnetworkhashps(s, state, window)
elif 'getnettotals' in s: getnettotals(s, state, window)
elif 'getmininginfo' in s: getmininginfo(s, state, window)
elif 'getpeerinfo' in s: getpeerinfo(s, state, window)
elif 'getchaintips' in s: getchaintips(s, state, window)
elif 'masternode' in s: masternode(s, state, window)
elif 'listsinceblock' in s: listsinceblock(s, state, window)
elif 'lastblocktime' in s: lastblocktime(s, state, window)
elif 'txid' in s: txid(s, state, window)
elif 'consolecommand' in s: consolecommand(s, state, window)
elif 'estimatefee' in s: estimatefee(s, state, window)
elif 'stop' in s:
return s['stop']