-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpunisher.py
619 lines (564 loc) · 18.1 KB
/
punisher.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/usr/bin/env python3
# -*- coding: utf_8 -*-
# This script implements receiving complaints about idle or cheating validators, checking complaints and voting for complaints to fine such validators.
# This script is used by validators that, for some reason, do not use mytonctrl https://github.com/igroman787/mytonctrl.
# If you are using mytonctrl (recommended) you do not need to use this script, as the full slashing process is already built into mytonctrl.
# Note that identifying idle or cheating validators and submitting a complaint for such validators is not implemented in the script and it is assumed that this is done by validators using mytonctrl.
#
# This single file script has no dependencies and can be easily reviewed.
#
# Slashing process documentation:
# https://github.com/newton-blockchain/TIPs/issues/13
# https://github.com/newton-blockchain/TIPs/issues/14
# fix me:
lite_client = "/usr/bin/ton/lite-client/lite-client"
config = "/usr/bin/ton/lite-client/ton-lite-client-test1.config.json"
fift = "/usr/bin/ton/crypto/fift"
fiftpath = "/usr/src/ton/crypto/fift/lib/:/usr/src/ton/crypto/smartcont/"
validator_console = "/usr/bin/ton/validator-engine-console/validator-engine-console"
vc_key = "/usr/bin/ton/validator-engine-console/client"
vc_pubkey = "/usr/bin/ton/validator-engine-console/server.pub"
vc_addr = "127.0.0.1:22229" # netstat -ntlup
validator_wallet_name = "/home/ton/.local/share/mytoncore/wallets/validator_wallet_001"
validator_wallet_addr = "kf9X6ObXojpUZza3NiS2TnRJ4KR7ler8cOjMRBt_swy4QiYp"
adnl_addr = "EADD038C8B931BFC802E6725D57581570630C55AEF7181C8748C4A8F7907CDF7"
import os
import time
import json
import crc16
import base64
import struct
import subprocess
config32 = None
config34 = None
class Wallet:
def __init__(self):
self.name = None
self.path = None
self.addrFilePath = None
self.privFilePath = None
self.bocFilePath = None
self.fullAddr = None
self.workchain = None
self.addr_hex = None
self.addr = None
self.addr_init = None
self.oldseqno = None
self.account = None
self.subwallet = None
self.v = None
#end define
def Refresh(self):
buff = self.fullAddr.split(':')
self.workchain = buff[0]
self.addr_hex = buff[1]
self.privFilePath = self.path + ".pk"
if self.v == "v1":
self.addrFilePath = self.path + ".addr"
self.bocFilePath = self.path + "-query.boc"
elif self.v == "hw":
self.addrFilePath = self.path + str(self.subwallet) + ".addr"
self.bocFilePath = self.path + str(self.subwallet) + "-query.boc"
#end define
def Delete(self):
os.remove(self.addrFilePath)
os.remove(self.privFilePath)
#end define
#end class
def LiteClientCmd(cmd):
args = [lite_client, "--global-config", config, "--verbosity", "0", "--cmd", cmd]
ex = None
for i in range(3):
try:
process = subprocess.run(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
except: pass
output = process.stdout.decode("utf-8")
err = process.stderr.decode("utf-8")
if len(err) > 0:
print("LiteClientCmd args: {args}".format(args=args))
raise Exception("LiteClient error: {err}".format(err=err))
return output
#end define
def ValidatorConsoleCmd(cmd):
args = [validator_console, "-k", vc_key, "-p", vc_pubkey, "-a", vc_addr, "-v", "0", "--cmd", cmd]
for i in range(3):
try:
process = subprocess.run(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
except: pass
output = process.stdout.decode("utf-8")
err = process.stderr.decode("utf-8")
if len(err) > 0:
print("ValidatorConsoleCmd args: {args}".format(args=args))
raise Exception("ValidatorConsoleCmd error: {err}".format(err=err))
return output
#end define
def FiftCmd(args):
for i in range(len(args)):
args[i] = str(args[i])
args = [fift, "-I", fiftpath, "-s"] + args
for i in range(3):
try:
process = subprocess.run(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
except: pass
output = process.stdout.decode("utf-8")
err = process.stderr.decode("utf-8")
if len(err) > 0:
print("FiftCmd args: {args}".format(args=args))
raise Exception("FiftCmd error: {err}".format(err=err))
return output
#end define
def GetTimestamp():
return int(time.time())
#end define
def GetElectionId():
config32 = GetConfig32()
return config32["startWorkTime"]
#end define
def GetValidatorIndex():
adnlAddr = adnl_addr
config34 = GetConfig34()
validators = config34.get("validators")
index = 0
for validator in validators:
searchAdnlAddr = validator.get("adnlAddr")
if adnlAddr == searchAdnlAddr:
return index
index += 1
print("GetValidatorIndex warning: index not found.")
return -1
#end define
def GetConfig32():
global config32
if config32:
return config32
config32 = dict()
result = LiteClientCmd("getconfig 32")
config32["totalValidators"] = int(Pars(result, "total:", ' '))
config32["startWorkTime"] = int(Pars(result, "utime_since:", ' '))
config32["endWorkTime"] = int(Pars(result, "utime_until:", ' '))
lines = result.split('\n')
validators = list()
for line in lines:
if "public_key:" in line:
validatorAdnlAddr = Pars(line, "adnl_addr:x", ')')
pubkey = Pars(line, "pubkey:x", ')')
if config32["totalValidators"] > 1:
validatorWeight = int(Pars(line, "weight:", ' '))
else:
validatorWeight = int(Pars(line, "weight:", ')'))
buff = dict()
buff["adnlAddr"] = validatorAdnlAddr
buff["pubkey"] = pubkey
buff["weight"] = validatorWeight
validators.append(buff)
config32["validators"] = validators
return config32
#end define
def GetConfig34():
global config34
if config34:
return config34
config34 = dict()
result = LiteClientCmd("getconfig 34")
config34["totalValidators"] = int(Pars(result, "total:", ' '))
config34["startWorkTime"] = int(Pars(result, "utime_since:", ' '))
config34["endWorkTime"] = int(Pars(result, "utime_until:", ' '))
config34["totalWeight"] = int(Pars(result, "total_weight:", ' '))
lines = result.split('\n')
validators = list()
for line in lines:
if "public_key:" in line:
validatorAdnlAddr = Pars(line, "adnl_addr:x", ')')
pubkey = Pars(line, "pubkey:x", ')')
if config34["totalValidators"] > 1:
validatorWeight = int(Pars(line, "weight:", ' '))
else:
validatorWeight = int(Pars(line, "weight:", ')'))
buff = dict()
buff["adnlAddr"] = validatorAdnlAddr
buff["pubkey"] = pubkey
buff["weight"] = validatorWeight
validators.append(buff)
config34["validators"] = validators
return config34
#end define
def Pars(text, search, search2=None):
if search is None or text is None:
return None
if search not in text:
return None
text = text[text.find(search) + len(search):]
if search2 is not None and search2 in text:
text = text[:text.find(search2)]
return text
#end define
def GetValidatorKey():
data = GetConfigFromValidator()
validators = data["validators"]
for validator in validators:
validatorId = validator["id"]
key_bytes = base64.b64decode(validatorId)
validatorKey = key_bytes.hex().upper()
timestamp = GetTimestamp()
if timestamp > validator["election_date"]:
return validatorKey
raise Exception("GetValidatorKey error: validator key not found. Are you sure you are a validator?")
#end define
def GetConfigFromValidator():
result = ValidatorConsoleCmd("getconfig")
string = Pars(result, "---------", "--------")
vconfig = json.loads(string)
return vconfig
#end define
def GetPubKeyBase64(key):
result = ValidatorConsoleCmd("exportpub " + key)
validatorPubkey_b64 = Pars(result, "got public key: ", '\n')
return validatorPubkey_b64
#end define
def SaveComplaints(electionId):
filePrefix = "/tmp/scp_"
cmd = "savecomplaints {electionId} {filePrefix}".format(electionId=electionId, filePrefix=filePrefix)
result = LiteClientCmd(cmd)
lines = result.split('\n')
complaintsHashes = list()
for line in lines:
if "SAVE_COMPLAINT" in line:
buff = line.split('\t')
chash = buff[2]
validatorPubkey = buff[3]
createdTime = buff[4]
filePath = buff[5]
ok = CheckComplaint(filePath)
if ok is True:
complaintsHashes.append(chash)
return complaintsHashes
#end define
def CheckComplaint(filePath):
cmd = "loadproofcheck {filePath}".format(filePath=filePath)
result = LiteClientCmd(cmd)
lines = result.split('\n')
ok = False
for line in lines:
if "COMPLAINT_VOTE_FOR" in line:
buff = line.split('\t')
chash = buff[1]
ok_buff = buff[2]
if ok_buff == "YES":
ok = True
return ok
#end define
def Result2List(text):
buff = Pars(text, "result:", "\n")
if buff is None or "error" in buff:
return
buff = buff.replace(')', ']')
buff = buff.replace('(', '[')
buff = buff.replace(']', ' ] ')
buff = buff.replace('[', ' [ ')
arr = buff.split()
# Get good raw data
output = ""
arrLen = len(arr)
for i in range(arrLen):
item = arr[i]
# get next item
if i+1 < arrLen:
nextItem = arr[i+1]
else:
nextItem = None
# add item to output
if item == '[':
output += item
elif nextItem == ']':
output += item
elif '{' in item or '}' in item:
output += "\"{item}\", ".format(item=item)
elif i+1 == arrLen:
output += item
else:
output += item + ', '
#end for
data = json.loads(output)
return data
#end define
def GetComplaints(electionId):
complaints = dict()
fullElectorAddr = GetFullElectorAddr()
cmd = "runmethodfull {fullElectorAddr} list_complaints {electionId}".format(fullElectorAddr=fullElectorAddr, electionId=electionId)
result = LiteClientCmd(cmd)
rawComplaints = Result2List(result)
if rawComplaints is None:
return complaints
rawComplaints = rawComplaints[0]
config34 = GetConfig34()
totalWeight = config34.get("totalWeight")
# Get json
for complaint in rawComplaints:
if len(complaint) == 0:
continue
chash = complaint[0]
subdata = complaint[1]
# Create dict
# parser from: https://github.com/ton-blockchain/ton/blob/dab7ee3f9794db5a6d32c895dbc2564f681d9126/crypto/smartcont/elector-code.fc#L1149
item = dict()
buff = subdata[0] # *complaint*
item["electionId"] = electionId
item["hash"] = chash
pubkey = Dec2HexAddr(buff[0]) # *validator_pubkey*
adnl = GetAdnlFromPubkey(pubkey)
item["pubkey"] = pubkey
item["adnl"] = adnl
item["description"] = buff[1] # *description*
item["createdTime"] = buff[2] # *created_at*
item["severity"] = buff[3] # *severity*
rewardAddr = buff[4]
rewardAddr = "-1:" + Dec2HexAddr(rewardAddr)
rewardAddr = HexAddr2Base64Addr(rewardAddr)
item["rewardAddr"] = rewardAddr # *reward_addr*
item["paid"] = buff[5] # *paid*
suggestedFine = buff[6] # *suggested_fine*
item["suggestedFine"] = ng2g(suggestedFine)
item["suggestedFinePart"] = buff[7] # *suggested_fine_part*
votedValidators = subdata[1] # *voters_list*
item["votedValidators"] = votedValidators
item["vsetId"] = subdata[2] # *vset_id*
weightRemaining = subdata[3] # *weight_remaining*
requiredWeight = totalWeight * 2 / 3
if len(votedValidators) == 0:
weightRemaining = requiredWeight
availableWeight = requiredWeight - weightRemaining
item["weightRemaining"] = weightRemaining
item["approvedPercent"] = round(availableWeight / totalWeight * 100, 3)
item["isPassed"] = (weightRemaining < 0)
pseudohash = pubkey + str(electionId)
item["pseudohash"] = pseudohash
complaints[pseudohash] = item
#end for
return complaints
#end define
def HexAddr2Base64Addr(fullAddr, bounceable=True, testnet=True):
buff = fullAddr.split(':')
workchain = int(buff[0])
addr_hex = buff[1]
if len(addr_hex) != 64:
raise Exeption("HexAddr2Base64Addr error: Invalid length of hexadecimal address")
#end if
# Create base64 address
b = bytearray(36)
b[0] = 0x51 - bounceable * 0x40 + testnet * 0x80
b[1] = workchain % 256
b[2:34] = bytearray.fromhex(addr_hex)
buff = bytes(b[:34])
crc = crc16.crc16xmodem(buff)
b[34] = crc >> 8
b[35] = crc & 0xff
result = base64.b64encode(b)
result = result.decode()
result = result.replace('+', '-')
result = result.replace('/', '_')
return result
#end define
def GetAdnlFromPubkey(inputPubkey):
config32 = GetConfig32()
validators = config32["validators"]
for validator in validators:
adnl = validator["adnlAddr"]
pubkey = validator["pubkey"]
if pubkey == inputPubkey:
return adnl
#end define
def VoteComplaint(electionId, complaintHash):
complaintHash = int(complaintHash)
fullElectorAddr = GetFullElectorAddr()
walletName = validator_wallet_name
wallet = GetLocalWallet(walletName)
validatorKey = GetValidatorKey()
validatorPubkey_b64 = GetPubKeyBase64(validatorKey)
validatorIndex = GetValidatorIndex()
complaint = GetComplaint(electionId, complaintHash)
votedValidators = complaint.get("votedValidators")
if validatorIndex in votedValidators:
print("Complaint already has been voted")
return
var1 = CreateComplaintRequest(electionId, complaintHash, validatorIndex)
validatorSignature = GetValidatorSignature(validatorKey, var1)
resultFilePath = SignComplaintVoteRequestWithValidator(complaintHash, electionId, validatorIndex, validatorPubkey_b64, validatorSignature)
resultFilePath = SignFileWithWallet(wallet, resultFilePath, fullElectorAddr, 1.41)
SendFile(resultFilePath, wallet)
#end define
def SendFile(filePath, wallet):
if not os.path.isfile(filePath):
raise Exception("SendFile error: no such file '{filePath}'".format(filePath=filePath))
wallet.oldseqno = GetSeqno(wallet)
result = LiteClientCmd("sendfile " + filePath)
WaitTransaction(wallet)
os.remove(filePath)
#end define
def WaitTransaction(wallet):
for i in range(10): # wait 30 sec
time.sleep(3)
seqno = GetSeqno(wallet)
if seqno != wallet.oldseqno:
return
raise Exception("WaitTransaction error: time out")
#end define
def SignFileWithWallet(wallet, filePath, addr, gram):
seqno = GetSeqno(wallet)
resultFilePath = "/tmp/result"
args = ["wallet.fif", wallet.path, addr, seqno, gram, "-B", filePath, resultFilePath]
result = FiftCmd(args)
resultFilePath = Pars(result, "Saved to file ", ")")
return resultFilePath
#end define
def GetSeqno(wallet):
cmd = "runmethod {addr} seqno".format(addr=wallet.addr)
result = LiteClientCmd(cmd)
if "cannot run any methods" in result:
return None
if "result" not in result:
return 0
seqno = GetVarFromWorkerOutput(result, "result")
seqno = seqno.replace(' ', '')
seqno = Pars(seqno, '[', ']')
seqno = int(seqno)
return seqno
#end define
def SignComplaintVoteRequestWithValidator(complaintHash, electionId, validatorIndex, validatorPubkey_b64, validatorSignature):
fileName = "/tmp/msb.boc"
args = ["complaint-vote-signed.fif", validatorIndex, electionId, complaintHash, validatorPubkey_b64, validatorSignature, fileName]
result = FiftCmd(args)
fileName = Pars(result, "Saved to file ", '\n')
return fileName
#end define
def GetValidatorSignature(validatorKey, var1):
cmd = "sign {validatorKey} {var1}".format(validatorKey=validatorKey, var1=var1)
result = ValidatorConsoleCmd(cmd)
validatorSignature = Pars(result, "got signature ", '\n')
return validatorSignature
#end define
def CreateComplaintRequest(electionId, complaintHash, validatorIndex):
fileName = "/tmp/cvr.boc"
args = ["complaint-vote-req.fif", validatorIndex, electionId, complaintHash, fileName]
result = FiftCmd(args)
fileName = Pars(result, "Saved to file ", '\n')
resultList = result.split('\n')
i = 0
start_index = 0
for item in resultList:
if "Creating a request to vote for complaint" in item:
start_index = i
i += 1
var1 = resultList[start_index + 1]
var2 = resultList[start_index + 2] # var2 not using
return var1
#end define
def GetComplaint(electionId, complaintHash):
complaints = GetComplaints(electionId)
for key, item in complaints.items():
if complaintHash == item.get("hash"):
return item
raise Exception("GetComplaint error: complaint not found.")
#end define
def GetLocalWallet(walletName):
if walletName is None:
return None
filePath = walletName
if (".addr" in filePath):
filePath = filePath.replace(".addr", '')
if (".pk" in filePath):
filePath = filePath.replace(".pk", '')
if os.path.isfile(filePath + ".pk") == False:
raise Exception("GetWalletFromFile error: Private key not found: " + filePath)
#end if
# Create wallet object
wallet = Wallet()
wallet.v = "v1"
wallet.path = filePath
if '/' in filePath:
wallet.name = filePath[filePath.rfind('/')+1:]
else:
wallet.name = filePath
#end if
addrFilePath = filePath + ".addr"
AddrFile2Wallet(wallet, addrFilePath)
return wallet
#end define
def AddrFile2Wallet(wallet, addrFilePath):
file = open(addrFilePath, "rb")
data = file.read()
addr_hex = data[:32].hex()
workchain = struct.unpack("i", data[32:])[0]
wallet.fullAddr = str(workchain) + ":" + addr_hex
wallet.addr = HexAddr2Base64Addr(wallet.fullAddr)
wallet.addr_init = HexAddr2Base64Addr(wallet.fullAddr, False)
wallet.Refresh()
#end define
def GetFullElectorAddr():
result = LiteClientCmd("getconfig 1")
electorAddr_hex = GetVarFromWorkerOutput(result, "elector_addr:x")
fullElectorAddr = "-1:{electorAddr_hex}".format(electorAddr_hex=electorAddr_hex)
return fullElectorAddr
#end define
def GetVarFromWorkerOutput(text, search):
if ':' not in search:
search += ':'
if search is None or text is None:
return None
if search not in text:
return None
start = text.find(search) + len(search)
count = 0
bcount = 0
textLen = len(text)
end = textLen
for i in range(start, textLen):
letter = text[i]
if letter == '(':
count += 1
bcount += 1
elif letter == ')':
count -= 1
if letter == ')' and count < 1:
end = i + 1
break
elif letter == '\n' and count < 1:
end = i
break
result = text[start:end]
if count != 0 and bcount == 0:
result = result.replace(')', '')
return result
#end define
def Dec2HexAddr(dec):
h = dec2hex(dec)
hu = h.upper()
h64 = hu.rjust(64, "0")
return h64
#end define
def dec2hex(dec):
h = hex(dec)[2:]
if len(h) % 2 > 0:
h = '0' + h
return h
#end define
def ng2g(ng):
return int(ng)/10**9
#end define
###
### Start of the program
###
print("start punisher.py")
electionId = GetElectionId()
complaintsHashes = SaveComplaints(electionId)
complaints = GetComplaints(electionId)
for key, item in complaints.items():
complaintHash = item.get("hash")
complaintHash_hex = Dec2HexAddr(complaintHash)
if complaintHash_hex in complaintsHashes:
VoteComplaint(electionId, complaintHash)
os.system("rm -rf /tmp/cvr.boc")
os.system("rm -rf /tmp/msb.boc")
os.system("rm -rf /tmp/result.boc")
#end for
os.system("rm -rf /tmp/scp_*.boc")
print("end punisher.py")