From f54ded076af11c5d584a3ddecbb8cdf4ff028510 Mon Sep 17 00:00:00 2001 From: yungwine Date: Fri, 16 Feb 2024 14:07:27 +0700 Subject: [PATCH 1/2] refactor complaints --- mytoncore/mytoncore.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index f49a4eba..eaa1268c 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -2185,6 +2185,7 @@ def GetComplaints(self, electionId=None, past=False): buff = subdata[0] # *complaint* item["electionId"] = electionId item["hash"] = chash + item["hash_hex"] = dec2hex(chash) pubkey = Dec2HexAddr(buff[0]) # *validator_pubkey* adnl = self.GetAdnlFromPubkey(pubkey) item["pubkey"] = pubkey @@ -2324,18 +2325,11 @@ def VoteComplaint(self, electionId, complaintHash): validatorKey = self.GetValidatorKey() validatorPubkey_b64 = self.GetPubKeyBase64(validatorKey) validatorIndex = self.GetValidatorIndex() - complaint = self.GetComplaint(electionId, complaintHash) - votedValidators = complaint.get("votedValidators") - pubkey = complaint.get("pubkey") - if validatorIndex in votedValidators: - self.local.add_log("Complaint already has been voted", "info") - return var1 = self.CreateComplaintRequest(electionId, complaintHash, validatorIndex) validatorSignature = self.GetValidatorSignature(validatorKey, var1) resultFilePath = self.SignComplaintVoteRequestWithValidator(complaintHash, electionId, validatorIndex, validatorPubkey_b64, validatorSignature) resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullElectorAddr, 1.5) self.SendFile(resultFilePath, wallet) - self.AddVotedComplaints(complaint) #end define def SaveComplaints(self, electionId): @@ -2377,19 +2371,19 @@ def CheckComplaint(self, file_path: str): def complaint_is_valid(self, complaint: dict): self.local.add_log("start complaint_is_valid function", "debug") - voted_complaints = self.GetVotedComplaints() + election_id = complaint['electionId'] + voted_complaints = self.GetVotedComplaints(election_id) if complaint['pseudohash'] in voted_complaints: - self.local.add_log(f"skip checking complaint {complaint['hash']}: " + self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: " f"complaint with this pseudohash ({complaint['pseudohash']})" f" has already been voted", "debug") return False # check that complaint is valid - election_id = complaint['electionId'] config32 = self.GetConfig32() start = config32.get("startWorkTime") if election_id != start: - self.local.add_log(f"skip checking complaint {complaint['hash']}: " + self.local.add_log(f"skip checking complaint {complaint['hash_hex']}: " f"election_id ({election_id}) doesn't match with " f"start work time ({config32.get('startWorkTime')})", "info") return False @@ -2407,15 +2401,15 @@ def complaint_is_valid(self, complaint: dict): break if not exists: - self.local.add_log(f"complaint {complaint['hash']} declined: complaint info was not found", "info") + self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint info was not found", "info") return False # check complaint fine value if complaint['suggestedFine'] != 101: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3708 - self.local.add_log(f"complaint {complaint['hash']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info") + self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine value is {complaint['suggestedFine']} ton", "info") return False if complaint['suggestedFinePart'] != 0: # https://github.com/ton-blockchain/ton/blob/5847897b3758bc9ea85af38e7be8fc867e4c133a/lite-client/lite-client.cpp#L3709 - self.local.add_log(f"complaint {complaint['hash']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info") + self.local.add_log(f"complaint {complaint['hash_hex']} declined: complaint fine part value is {complaint['suggestedFinePart']} ton", "info") return False return True @@ -2958,13 +2952,17 @@ def AddSaveOffer(self, offer): self.local.save() #end define - def GetVotedComplaints(self): - bname = "votedComplaints" - votedComplaints = self.local.db.get(bname) - if votedComplaints is None: - votedComplaints = dict() - self.local.db[bname] = votedComplaints - return votedComplaints + def GetVotedComplaints(self, election_id: int = None): + complaints = self.GetComplaints(election_id) + result = {} + validator_index = self.GetValidatorIndex() + for complaint in complaints.values(): + votedValidators = complaint.get("votedValidators") + if validator_index in votedValidators: + pubkey = complaint.get("pubkey") + election_id = complaint.get("electionId") + result[pubkey + str(election_id)] = complaint + return result #end define def AddVotedComplaints(self, complaint): From f9353b7121b10c80209271b0c31e9ae4266bc873 Mon Sep 17 00:00:00 2001 From: yungwine Date: Fri, 16 Feb 2024 14:10:46 +0700 Subject: [PATCH 2/2] improves --- mytoncore/mytoncore.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index eaa1268c..b515d01b 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -2956,12 +2956,10 @@ def GetVotedComplaints(self, election_id: int = None): complaints = self.GetComplaints(election_id) result = {} validator_index = self.GetValidatorIndex() - for complaint in complaints.values(): + for pseudohash, complaint in complaints.items(): votedValidators = complaint.get("votedValidators") if validator_index in votedValidators: - pubkey = complaint.get("pubkey") - election_id = complaint.get("electionId") - result[pubkey + str(election_id)] = complaint + result[pseudohash] = complaint return result #end define