-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathaudit.py
executable file
·413 lines (357 loc) · 19.4 KB
/
audit.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/python3
import sys,os,curses,json,time,select,random,threading,urllib.request,contextlib
from datetime import datetime,timedelta,timezone
from collections import namedtuple
from configparser import ConfigParser
import platform,subprocess,re,getopt
import requests,hmac,hashlib,base64
import math,calendar,csv
#-------------------------------------------------------
public_key = None
localhost = 'localhost'
output_file = None
#-------------------------------------------------------
start_day = None
start_month = None
start_year = None
#-------------------------------------------------------
last_day = None
last_month = None
last_year = None
#-------------------------------------------------------
num_decimals = 2
#-------------------------------------------------------
def checkBalance(block):
while True:
try:
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(block)).read())
state_root = block_info['result']['block']['header']['state_root_hash']
break
except:
pass
while True:
try:
query_state = json.loads(os.popen('casper-client query-state -k {} -s {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(public_key, state_root)).read())
main_purse = query_state['result']['stored_value']['Account']['main_purse']
break
except:
pass
while True:
try:
balance_info = json.loads(os.popen('casper-client get-balance --purse-uref {} --state-root-hash {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(main_purse, state_root)).read())
balance = balance_info['result']['balance_value']
break
except:
pass
return int(balance)
#-------------------------------------------------------
def getAuctionInfo(block):
while True:
try:
auction_info = json.loads(os.popen('casper-client get-auction-info -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(block)).read())
auction_info = auction_info['result']['auction_state']
bid_info = auction_info['bids']
break
except:
pass
for item in bid_info:
key = item['public_key'].strip("\"")
value = int(item['bid']['staked_amount'].strip("\""))
if key == public_key:
return int(value)
return 0
def run():
while True:
try:
block_info = json.loads(os.popen('casper-client get-block --node-address https://rpc.mainnet.casperlabs.io/rpc').read())
currentProposerBlock = int(block_info['result']['block']['header']['height'])
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
break
except:
pass
print('\nAll data is gathered at the beginning of each day, and then a final entry is on the last day @11:59pm\n(so you can see total earning from Midnight on first day to Midnight on last day)')
print("\nCasper Blockchain is currently at Block:", currentProposerBlock)
print("\nUsing Public Key", public_key)
today = datetime.now(timezone.utc).date()
first = today.replace(day=1)
lastDayMonth = first - timedelta(days=1)
firstDayMonth = lastDayMonth.replace(day=1)
if event_time.date() > lastDayMonth:
currentProposerBlock -= (event_time.day -1) * 5300
if start_day != None:
a,z = calendar.monthrange(firstDayMonth.year, firstDayMonth.month)
firstDayMonth = firstDayMonth.replace(day=z if start_day > z else start_day)
if start_month != None:
a,z = calendar.monthrange(firstDayMonth.year, start_month)
firstDayMonth = firstDayMonth.replace(day=z if firstDayMonth.day > z else firstDayMonth.day)
firstDayMonth = firstDayMonth.replace(month=start_month)
if start_year != None:
a,z = calendar.monthrange(start_year, firstDayMonth.month)
firstDayMonth = firstDayMonth.replace(day=z if firstDayMonth.day > z else firstDayMonth.day)
firstDayMonth = firstDayMonth.replace(year=start_year)
if last_day != None:
a,z = calendar.monthrange(lastDayMonth.year, lastDayMonth.month)
lastDayMonth = lastDayMonth.replace(day=z if last_day > z else last_day)
if last_month != None:
a,z = calendar.monthrange(lastDayMonth.year, last_month)
lastDayMonth = lastDayMonth.replace(day=z)
lastDayMonth = lastDayMonth.replace(month=last_month)
if last_year != None:
a,z = calendar.monthrange(last_year, lastDayMonth.month)
lastDayMonth = lastDayMonth.replace(day=z)
lastDayMonth = lastDayMonth.replace(year=last_year)
print("\nGetting Info for", firstDayMonth, "to", lastDayMonth, "\n")
lastDayBlock = 0
firstDayBlock = 0
retryMessage = ""
blockAmount = 100
while currentProposerBlock >= 0 and firstDayBlock == 0:
currentProposerBlock -= blockAmount
if currentProposerBlock < 1:
currentProposerBlock = 0
print("\rScanning...", currentProposerBlock, event_time, retryMessage, end =" \r")
retryMessage = ""
try:
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(currentProposerBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
if lastDayBlock == 0 and event_time.date() == lastDayMonth:
lastDayBlock = currentProposerBlock
while True:
currentProposerBlock += 1
print("\rScanning...", currentProposerBlock, event_time.time(), retryMessage, end =" \r")
retryMessage = ""
try:
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(currentProposerBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
if event_time.date() == lastDayMonth:
lastDayBlock = currentProposerBlock
retryMessage = ""
else:
blockAmount = 5000
currentProposerBlock -= 27 * blockAmount
break
except:
currentProposerBlock -= 1
retryMessage = "retrying..."
print("\rFound last block for {} at ".format(lastDayMonth), lastDayBlock, end =" \n")
elif event_time.date() == firstDayMonth:
blockAmount = 100
elif event_time.date() < firstDayMonth:
while True:
currentProposerBlock += 1
print("\rScanning...", currentProposerBlock, event_time.time(), retryMessage, end =" \r")
retryMessage = ""
try:
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(currentProposerBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
if event_time.date() == firstDayMonth or (event_time.date() > firstDayMonth and currentProposerBlock == 0):
firstDayBlock = currentProposerBlock
firstDayMonth = event_time.date()
break
except:
currentProposerBlock -= 1
retryMessage = "retrying..."
print("\rFound first block for {} at ".format(event_time.date()), firstDayBlock, end =" \n")
elif currentProposerBlock == 0:
firstDayBlock = currentProposerBlock
firstDayMonth = event_time.date()
print("\rGenesis Block =", firstDayBlock)
print("\rStart Month =", firstDayMonth)
break
except Exception as e:
currentProposerBlock += blockAmount
retryMessage = "retrying..."
print("\nDone Searching")
if output_file != None:
f = open(output_file, 'w')
writer = csv.writer(f)
header = ['Date', 'Block', 'On Hand (liquid)', 'Auction (bid)', 'Total']
writer.writerow(header)
startMonth = firstDayMonth
startBlock = firstDayBlock
print("Checking Balance... ")
firstBalance = checkBalance(startBlock)
print("Getting Auction Info...")
firstAuction = getAuctionInfo(startBlock)
print("Getting Block Info {}".format(startBlock))
while True:
try:
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
break
except:
print("Getting Block Info {}".format(startBlock), "retrying...")
print("\n\n")
print("Date\t\t\tBlock\tLiquid\t\tAuction\t\t\tTotal")
print("{}\t{}\t{} CSPR\t{} CSPR\t{} CSPR".format(event_time.strftime("%Y-%m-%d %H:%M:%S"), startBlock, round(firstBalance/1000000000,num_decimals), round(firstAuction/1000000000,num_decimals), round((firstBalance+firstAuction)/1000000000),num_decimals))
if output_file != None:
data = [event_time.strftime("%Y-%m-%d %H:%M:%S"), startBlock, firstBalance/1000000000, firstAuction/1000000000, (firstBalance+firstAuction)/1000000000]
writer.writerow(data)
while startMonth < lastDayMonth:
if startBlock != 0:
startBlock += int(5200)
startMonth += timedelta(days=1)
loop = -1
while True:
try:
startBlock += 1
loop += 1
print("\rGetting Block Info ", int(startBlock), event_time.date(), startMonth, retryMessage, end =" \r")
retryMessage = ""
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(int(startBlock))).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
if event_time.date() == startMonth:
if loop == 0:
while event_time.date() == startMonth:
try:
# then we went too far... skip backward to find the first block of the day
startBlock -= 1
print("\rGetting Block Info ", int(startBlock), event_time.date(), startMonth, retryMessage, end =" \r")
retryMessage = ""
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
except:
startBlock += 1
retryMessage = "retrying..."
startBlock += 1
while True:
try:
print("\rGetting Block Info ", int(startBlock), event_time.date(), startMonth, retryMessage, end =" \r")
retryMessage = ""
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
break
except:
retryMessage = "retrying..."
print("\rChecking Balance... ", end =" \r")
balance = checkBalance(startBlock)
print("\rGetting Auction Info...", end =" \r")
auction = getAuctionInfo(startBlock)
print("\r \r{}\t{}\t{:.2f} CSPR\t{:.2f} CSPR\t{} CSPR".format(event_time.strftime("%Y-%m-%d %H:%M:%S"), startBlock, round(balance/1000000000,num_decimals), round(auction/1000000000,num_decimals), round((balance+auction)/1000000000),num_decimals))
if output_file != None:
data = [event_time.strftime("%Y-%m-%d %H:%M:%S"), startBlock, balance/1000000000, auction/1000000000, (balance+auction)/1000000000]
writer.writerow(data)
break
elif event_time.date() > startMonth:
break
except:
startBlock -= 1
loop -= 1
retryMessage = "retrying..."
lastBalance = checkBalance(lastDayBlock)
lastAuction = getAuctionInfo(lastDayBlock)
block_info = json.loads(os.popen('casper-client get-block -b {} --node-address https://rpc.mainnet.casperlabs.io/rpc'.format(lastDayBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
print("{}\t{}\t{} CSPR\t{} CSPR\t{} CSPR".format("{}".format(event_time.strftime("%Y-%m-%d %H:%M:%S")), lastDayBlock, round(lastBalance/1000000000,num_decimals), round(lastAuction/1000000000,num_decimals), round((lastBalance+lastAuction)/1000000000),num_decimals))
if output_file != None:
data = ["{}".format(event_time.strftime("%Y-%m-%d %H:%M:%S")), lastDayBlock, lastBalance/1000000000, lastAuction/1000000000, (lastBalance+lastAuction)/1000000000]
writer.writerow(data)
print("\n\nTotal Increase: {} CSPR\n\n".format(round(((lastBalance+lastAuction) - (firstBalance+firstAuction)) / 1000000000),num_decimals))
if output_file != None:
writer.writerow(['', '', 'Total Diff:', 'End - Start', '{}'.format(((lastBalance+lastAuction) - (firstBalance+firstAuction)) / 1000000000)])
f.close()
#-------------------------------------------------------
def usage():
print('\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),')
print('to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,')
print('and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:')
print('The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.')
print('The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability,')
print('fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other')
print('liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings')
print('in the Software.\n')
print('Usage: '+sys.argv[0]+' [option]\n')
print('options:')
print('\tempty options will do last month')
print('\tPublic Key will be read locally but can be overriden')
print('\t\t-k <key>')
print('\tand if you don\'t have a public key locally you can read it from')
print('\t\t-l <ip-address> (default = localhost)')
print('\tto limit decimals')
print('\t\t-d <num decimals> (default = 2)')
print('\tStart Dates')
print('\t\t--sd= (--sd=21) - Start Date')
print('\t\t--sm= (--sm=5) - Start Month')
print('\t\t--sy= (--sy=2021) - Start Year')
print('\tEnd Dates')
print('\t\t--ed= (--ed=21) - End Date')
print('\t\t--em= (--em=5) - End Month')
print('\t\t--ey= (--ey=2021) - End Year')
print('\tOutput to file')
print('\t\t --f= (--f=august.csv)')
print('\nexample:')
print('\taudit --sm=2 --em=4 --f=feb-apr.csv\n')
#-------------------------------------------------------
def getPublicKey():
global public_key
global localhost
global start_month
global start_day
global start_year
global last_month
global last_day
global last_year
global output_file
global num_decimals
try:
opts, args = getopt.getopt(sys.argv[1:], 'k:h:l:d:' ,['sm=','sd=','sy=','em=','ed=','ey=','help','f='])
for opt, arg in opts:
if opt == '-k':
public_key = arg
elif opt == '-l':
localhost = str(arg)
elif opt == '--sm':
start_month = int(arg)
elif opt == '--sd':
start_day = int(arg)
elif opt == '--sy':
start_year = int(arg)
elif opt == '--em':
last_month = int(arg)
elif opt == '--ed':
last_day = int(arg)
elif opt == '--ey':
last_year = int(arg)
elif opt == '-d':
num_decimals = int(opt)
elif opt == '--f':
output_file = str(arg)
elif opt in ('-h', '--help'):
quit()
except:
usage()
sys.exit(2)
if not public_key:
try:
local_status = json.loads(os.popen('curl -s {}:8888/status'.format(localhost)).read())
public_key = local_status['our_public_signing_key']
except:
reader = open('/etc/casper/validator_keys/public_key_hex')
try:
public_key= reader.read().strip()
finally:
reader.close()
#-------------------------------------------------------
def notFound(ver):
print('\nrequired: Casper-Client version 1.3.2 or greater')
if ver != None:
print('found : Casper-Client version {}'.format(ver))
print('\nClient is incompatible (or not found), please compile (or install) 1.3.2 version or above.\n')
print('If compiling, these instructions might help')
print('\tcd casper-node\n\tgit pull\n\tgit checkout release-1.3.2\n\tmake setup-rs\n\tmake build-client-contracts\n\tcargo build -p casper-client --release\n\n\tsudo cp target/release/casper-client /usr/bin\n')
sys.exit(3)
#-------------------------------------------------------
print('\nAudit - Useful Casper Blockchain tool to get auditable Balance information')
print('The MIT License (MIT)')
print('Copyright (c) 2021 Mark Caldwell (RapidMark)')
try:
ver = os.popen('casper-client --version').read()
if not ver:
notFound(None)
ver = ver.split()
if int(ver[2].replace('.', '').split('-')[0]) < 132:
notFound(ver[2])
except:
sys.exit(3)
getPublicKey()
run()