Skip to content

Commit 2fbc372

Browse files
authored
Merge pull request #206 from yungwine/mytonctrl2_dev
refactor complaints validation
2 parents 4ee3a79 + cfd23bd commit 2fbc372

File tree

2 files changed

+59
-74
lines changed

2 files changed

+59
-74
lines changed

mytoncore/functions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,12 @@ def Complaints(local, ton):
506506

507507
# Voting for complaints
508508
config32 = ton.GetConfig32()
509-
electionId = config32.get("startWorkTime")
510-
complaints = ton.GetComplaints(electionId) # get complaints from Elector
511-
for c in complaints.values():
509+
election_id = config32.get("startWorkTime")
510+
complaints = ton.GetComplaints(election_id) # get complaints from Elector
511+
valid_complaints = ton.get_valid_complaints(complaints, election_id)
512+
for c in valid_complaints.values():
512513
complaint_hash = c.get("hash")
513-
if ton.complaint_is_valid(c):
514-
ton.VoteComplaint(electionId, complaint_hash)
514+
ton.VoteComplaint(election_id, complaint_hash)
515515
# end define
516516

517517

mytoncore/mytoncore.py

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,7 @@ def GetComplaints(self, electionId=None, past=False):
22382238
item["isPassed"] = (weightRemaining < 0)
22392239
pseudohash = pubkey + str(electionId)
22402240
item["pseudohash"] = pseudohash
2241-
complaints[pseudohash] = item
2241+
complaints[chash] = item
22422242
#end for
22432243

22442244
# Set buffer
@@ -2280,29 +2280,17 @@ def GetComplaintsNumber(self):
22802280
self.local.add_log("start GetComplaintsNumber function", "debug")
22812281
result = dict()
22822282
complaints = self.GetComplaints()
2283-
votedComplaints = self.GetVotedComplaints()
2283+
voted_complaints = self.GetVotedComplaints(complaints)
22842284
buff = 0
2285-
for key, item in complaints.items():
2286-
pubkey = item.get("pubkey")
2287-
electionId = item.get("electionId")
2288-
pseudohash = pubkey + str(electionId)
2289-
if pseudohash in votedComplaints:
2285+
for chash, item in complaints.values():
2286+
if chash in voted_complaints:
22902287
continue
22912288
buff += 1
22922289
result["all"] = len(complaints)
22932290
result["new"] = buff
22942291
return result
22952292
#end define
22962293

2297-
def GetComplaint(self, electionId, complaintHash):
2298-
self.local.add_log("start GetComplaint function", "debug")
2299-
complaints = self.GetComplaints(electionId)
2300-
for key, item in complaints.items():
2301-
if complaintHash == item.get("hash"):
2302-
return item
2303-
raise Exception("GetComplaint error: complaint not found.")
2304-
#end define
2305-
23062294
def SignProposalVoteRequestWithValidator(self, offerHash, validatorIndex, validatorPubkey_b64, validatorSignature):
23072295
self.local.add_log("start SignProposalVoteRequestWithValidator function", "debug")
23082296
fileName = self.tempDir + self.nodeName + "proposal_vote-msg-body.boc"
@@ -2391,51 +2379,56 @@ def CheckComplaint(self, file_path: str):
23912379
return ok
23922380
#end define
23932381

2394-
def complaint_is_valid(self, complaint: dict):
2395-
self.local.add_log("start complaint_is_valid function", "debug")
2396-
2397-
election_id = complaint['electionId']
2398-
voted_complaints = self.GetVotedComplaints(election_id)
2399-
if complaint['pseudohash'] in voted_complaints:
2400-
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
2401-
f"complaint with this pseudohash ({complaint['pseudohash']})"
2402-
f" has already been voted", "debug")
2403-
return False
2404-
2405-
# check that complaint is valid
2382+
def get_valid_complaints(self, complaints: dict, election_id: int):
2383+
self.local.add_log("start get_valid_complaints function", "debug")
24062384
config32 = self.GetConfig32()
24072385
start = config32.get("startWorkTime")
2408-
if election_id != start:
2409-
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
2410-
f"election_id ({election_id}) doesn't match with "
2411-
f"start work time ({config32.get('startWorkTime')})", "info")
2412-
return False
2386+
assert start == election_id, 'provided election_id != election_id from config32'
24132387
end = config32.get("endWorkTime")
2414-
data = self.GetValidatorsLoad(start, end - 60, saveCompFiles=False)
2388+
validators_load = self.GetValidatorsLoad(start, end - 60, saveCompFiles=True)
2389+
voted_complaints = self.GetVotedComplaints(complaints)
2390+
voted_complaints_pseudohashes = [complaint['pseudohash'] for complaint in voted_complaints.values()]
2391+
result = {}
2392+
for complaint in complaints.values():
2393+
if complaint['pseudohash'] in voted_complaints_pseudohashes or complaint['pseudohash'] in result:
2394+
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
2395+
f"complaint with this pseudohash ({complaint['pseudohash']})"
2396+
f" has already been voted", "debug")
2397+
continue
2398+
# check that complaint is valid
24152399

2416-
exists = False
2417-
for item in data.values():
2418-
pubkey = item.get("pubkey")
2419-
if pubkey is None:
2400+
if complaint['electionId'] != start:
2401+
self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: "
2402+
f"election_id ({election_id}) doesn't match with "
2403+
f"start work time ({config32.get('startWorkTime')})", "info")
24202404
continue
2421-
pseudohash = pubkey + str(election_id)
2422-
if pseudohash == complaint['pseudohash']:
2423-
exists = True
2424-
break
24252405

2426-
if not exists:
2427-
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint info was not found", "info")
2428-
return False
2406+
exists = False
2407+
for item in validators_load.values():
2408+
if 'fileName' not in item:
2409+
continue
2410+
pubkey = item.get("pubkey")
2411+
if pubkey is None:
2412+
continue
2413+
pseudohash = pubkey + str(election_id)
2414+
if pseudohash == complaint['pseudohash']:
2415+
exists = True
2416+
break
24292417

2430-
# check complaint fine value
2431-
if complaint['suggestedFine'] != 101: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3708
2432-
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info")
2433-
return False
2434-
if complaint['suggestedFinePart'] != 0: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3709
2435-
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info")
2436-
return False
2418+
if not exists:
2419+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint info was not found, probably it's wrong", "info")
2420+
continue
24372421

2438-
return True
2422+
# check complaint fine value
2423+
if complaint['suggestedFine'] != 101: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3708
2424+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info")
2425+
continue
2426+
if complaint['suggestedFinePart'] != 0: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3709
2427+
self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info")
2428+
continue
2429+
2430+
result[complaint['pseudohash']] = complaint
2431+
return result
24392432

24402433
def GetOnlineValidators(self):
24412434
onlineValidators = list()
@@ -2449,7 +2442,7 @@ def GetOnlineValidators(self):
24492442
return onlineValidators
24502443
#end define
24512444

2452-
def GetValidatorsLoad(self, start, end, saveCompFiles=False):
2445+
def GetValidatorsLoad(self, start, end, saveCompFiles=False) -> dict:
24532446
# Get buffer
24542447
bname = f"validatorsLoad{start}{end}"
24552448
buff = self.GetFunctionBuffer(bname, timeout=60)
@@ -2574,6 +2567,7 @@ def CheckValidators(self, start, end):
25742567
self.local.add_log("start CheckValidators function", "debug")
25752568
electionId = start
25762569
complaints = self.GetComplaints(electionId)
2570+
valid_complaints = self.get_valid_complaints(complaints, electionId)
25772571
data = self.GetValidatorsLoad(start, end, saveCompFiles=True)
25782572
fullElectorAddr = self.GetFullElectorAddr()
25792573
wallet = self.GetValidatorWallet(mode="vote")
@@ -2592,7 +2586,7 @@ def CheckValidators(self, start, end):
25922586
var2 = item.get("var2")
25932587
pubkey = item.get("pubkey")
25942588
pseudohash = pubkey + str(electionId)
2595-
if pseudohash in complaints:
2589+
if pseudohash in valid_complaints:
25962590
continue
25972591
# Create complaint
25982592
fileName = self.remove_proofs_from_complaint(fileName)
@@ -2990,25 +2984,16 @@ def add_save_offer(self, offer):
29902984
self.local.save()
29912985
#end define
29922986

2993-
def GetVotedComplaints(self, election_id: int = None):
2994-
complaints = self.GetComplaints(election_id)
2987+
def GetVotedComplaints(self, complaints: dict):
29952988
result = {}
29962989
validator_index = self.GetValidatorIndex()
2997-
for pseudohash, complaint in complaints.items():
2998-
votedValidators = complaint.get("votedValidators")
2999-
if validator_index in votedValidators:
3000-
result[pseudohash] = complaint
2990+
for chash, complaint in complaints.items():
2991+
voted_validators = complaint.get("votedValidators")
2992+
if validator_index in voted_validators:
2993+
result[chash] = complaint
30012994
return result
30022995
#end define
30032996

3004-
def AddVotedComplaints(self, complaint):
3005-
pseudohash = complaint.get("pseudohash")
3006-
votedComplaints = self.GetVotedComplaints()
3007-
if pseudohash not in votedComplaints:
3008-
votedComplaints[pseudohash] = complaint
3009-
self.local.save()
3010-
#end define
3011-
30122997
def GetDestinationAddr(self, destination):
30132998
if self.IsAddrB64(destination):
30142999
pass

0 commit comments

Comments
 (0)