forked from matsuro-hadouken/casper-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.py
executable file
·335 lines (284 loc) · 15.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
#!/usr/bin/python3
import sys,os,curses,json,time,select,random,threading,urllib.request,contextlib
from datetime import datetime,timedelta
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):
pass
block_info = json.loads(os.popen('casper-client get-block -b {}'.format(block)).read())
state_root = block_info['result']['block']['header']['state_root_hash']
query_state = json.loads(os.popen('casper-client query-state -k {} -s {}'.format(public_key, state_root)).read())
main_purse = query_state['result']['stored_value']['Account']['main_purse']
balance_info = json.loads(os.popen('casper-client get-balance --purse-uref {} --state-root-hash {}'.format(main_purse, state_root)).read())
balance = balance_info['result']['balance_value']
return int(balance)
#-------------------------------------------------------
def getAuctionInfo(block):
auction_info = json.loads(os.popen('casper-client get-auction-info -b {}'.format(block)).read())
auction_info = auction_info['result']['auction_state']
bid_info = auction_info['bids']
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():
block_info = json.loads(os.popen('casper-client get-block').read())
currentProposerBlock = int(block_info['result']['block']['header']['height'])
lastBlock = currentProposerBlock - (1319 * 60) # 60 is the max days to search back (in case we're on the last day of the month and need to go back to the 1st of last month)
if lastBlock < 0:
lastBlock = 0
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.utcnow().date()
first = today.replace(day=1)
lastDayMonth = first - timedelta(days=1)
firstDayMonth = lastDayMonth.replace(day=1)
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
while currentProposerBlock >= 0 and firstDayBlock == 0:
currentProposerBlock -= 1319
if currentProposerBlock < 1:
currentProposerBlock = 0
print("\rScanning...", currentProposerBlock, end =" ")
try:
block_info = json.loads(os.popen('casper-client get-block -b {}'.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, end =" ")
block_info = json.loads(os.popen('casper-client get-block -b {}'.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
else:
break
print("\rFound 1ast block for {} at ".format(lastDayMonth), lastDayBlock)
elif event_time.date() == firstDayMonth:
currentProposerBlock -= 1319
if currentProposerBlock < 1:
currentProposerBlock = -1
while True:
currentProposerBlock += 1
print("\rScanning...", currentProposerBlock, end =" ")
block_info = json.loads(os.popen('casper-client get-block -b {}'.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
print("\rFound first block for {} at ".format(event_time.date()), firstDayBlock)
elif currentProposerBlock == 0:
firstDayBlock = currentProposerBlock
firstDayMonth = event_time.date()
print("\rGenesis Block =", firstDayBlock)
print("\rStart Month =", firstDayMonth)
break;
except:
pass
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
firstBalance = checkBalance(startBlock)
firstAuction = getAuctionInfo(startBlock)
block_info = json.loads(os.popen('casper-client get-block -b {}'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
print("\n\n")
print("Date\tBlock\tLiquid\tAuction\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 += 1300
startMonth += timedelta(days=1)
loop = -1
while True:
startBlock += 1
loop += 1
block_info = json.loads(os.popen('casper-client get-block -b {}'.format(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:
# then we went too far... skip backward to find the first block of the day
startBlock -= 1
block_info = json.loads(os.popen('casper-client get-block -b {}'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
startBlock += 1
block_info = json.loads(os.popen('casper-client get-block -b {}'.format(startBlock)).read())
event_time = datetime.strptime(block_info['result']['block']['header']['timestamp'],'%Y-%m-%dT%H:%M:%S.%fZ')
balance = checkBalance(startBlock)
auction = getAuctionInfo(startBlock)
print("{}\t{}\t{} CSPR\t{} 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;
lastBalance = checkBalance(lastDayBlock)
lastAuction = getAuctionInfo(lastDayBlock)
block_info = json.loads(os.popen('casper-client get-block -b {}'.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('.', '')) < 132:
notFound(ver[2])
except:
sys.exit(3)
getPublicKey()
run()