Skip to content

Commit 05fa3ab

Browse files
authored
Merge pull request #175 from yungwine/mytonctrl2_dev
complaints refactor
2 parents 3d274b1 + f9353b7 commit 05fa3ab

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

mytoncore/mytoncore.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,7 @@ def GetComplaints(self, electionId=None, past=False):
21852185
buff = subdata[0] # *complaint*
21862186
item["electionId"] = electionId
21872187
item["hash"] = chash
2188+
item["hash_hex"] = dec2hex(chash)
21882189
pubkey = Dec2HexAddr(buff[0]) # *validator_pubkey*
21892190
adnl = self.GetAdnlFromPubkey(pubkey)
21902191
item["pubkey"] = pubkey
@@ -2324,18 +2325,11 @@ def VoteComplaint(self, electionId, complaintHash):
23242325
validatorKey = self.GetValidatorKey()
23252326
validatorPubkey_b64 = self.GetPubKeyBase64(validatorKey)
23262327
validatorIndex = self.GetValidatorIndex()
2327-
complaint = self.GetComplaint(electionId, complaintHash)
2328-
votedValidators = complaint.get("votedValidators")
2329-
pubkey = complaint.get("pubkey")
2330-
if validatorIndex in votedValidators:
2331-
self.local.add_log("Complaint already has been voted", "info")
2332-
return
23332328
var1 = self.CreateComplaintRequest(electionId, complaintHash, validatorIndex)
23342329
validatorSignature = self.GetValidatorSignature(validatorKey, var1)
23352330
resultFilePath = self.SignComplaintVoteRequestWithValidator(complaintHash, electionId, validatorIndex, validatorPubkey_b64, validatorSignature)
23362331
resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullElectorAddr, 1.5)
23372332
self.SendFile(resultFilePath, wallet)
2338-
self.AddVotedComplaints(complaint)
23392333
#end define
23402334

23412335
def SaveComplaints(self, electionId):
@@ -2377,19 +2371,19 @@ def CheckComplaint(self, file_path: str):
23772371
def complaint_is_valid(self, complaint: dict):
23782372
self.local.add_log("start complaint_is_valid function", "debug")
23792373

2380-
voted_complaints = self.GetVotedComplaints()
2374+
election_id = complaint['electionId']
2375+
voted_complaints = self.GetVotedComplaints(election_id)
23812376
if complaint['pseudohash'] in voted_complaints:
2382-
self.local.add_log(f"skip checking complaint {complaint['hash']}: "
2377+
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
23832378
f"complaint with this pseudohash ({complaint['pseudohash']})"
23842379
f" has already been voted", "debug")
23852380
return False
23862381

23872382
# check that complaint is valid
2388-
election_id = complaint['electionId']
23892383
config32 = self.GetConfig32()
23902384
start = config32.get("startWorkTime")
23912385
if election_id != start:
2392-
self.local.add_log(f"skip checking complaint {complaint['hash']}: "
2386+
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
23932387
f"election_id ({election_id}) doesn't match with "
23942388
f"start work time ({config32.get('startWorkTime')})", "info")
23952389
return False
@@ -2407,15 +2401,15 @@ def complaint_is_valid(self, complaint: dict):
24072401
break
24082402

24092403
if not exists:
2410-
self.local.add_log(f"complaint {complaint['hash']} declined: complaint info was not found", "info")
2404+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint info was not found", "info")
24112405
return False
24122406

24132407
# check complaint fine value
24142408
if complaint['suggestedFine'] != 101: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3708
2415-
self.local.add_log(f"complaint {complaint['hash']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info")
2409+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info")
24162410
return False
24172411
if complaint['suggestedFinePart'] != 0: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3709
2418-
self.local.add_log(f"complaint {complaint['hash']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info")
2412+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info")
24192413
return False
24202414

24212415
return True
@@ -2958,13 +2952,15 @@ def AddSaveOffer(self, offer):
29582952
self.local.save()
29592953
#end define
29602954

2961-
def GetVotedComplaints(self):
2962-
bname = "votedComplaints"
2963-
votedComplaints = self.local.db.get(bname)
2964-
if votedComplaints is None:
2965-
votedComplaints = dict()
2966-
self.local.db[bname] = votedComplaints
2967-
return votedComplaints
2955+
def GetVotedComplaints(self, election_id: int = None):
2956+
complaints = self.GetComplaints(election_id)
2957+
result = {}
2958+
validator_index = self.GetValidatorIndex()
2959+
for pseudohash, complaint in complaints.items():
2960+
votedValidators = complaint.get("votedValidators")
2961+
if validator_index in votedValidators:
2962+
result[pseudohash] = complaint
2963+
return result
29682964
#end define
29692965

29702966
def AddVotedComplaints(self, complaint):

0 commit comments

Comments
 (0)