Skip to content

Commit 19dacf2

Browse files
authored
Merge pull request #192 from yungwine/mytonctrl2_dev
save offers db
2 parents b8b8e6e + 229adf1 commit 19dacf2

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

mytoncore/mytoncore.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,11 @@ def GetSaveElectionEntries(self, electionId):
20432043
return result
20442044
#end define
20452045

2046+
def calculate_offer_pseudohash(self, offer_hash: str, param_id: int):
2047+
config_val = self.GetConfig(param_id)
2048+
pseudohash_bytes = offer_hash.encode() + json.dumps(config_val, sort_keys=True).encode()
2049+
return hashlib.sha256(pseudohash_bytes).hexdigest()
2050+
20462051
def GetOffers(self):
20472052
self.local.add_log("start GetOffers function", "debug")
20482053
fullConfigAddr = self.GetFullConfigAddr()
@@ -2087,9 +2092,7 @@ def GetOffers(self):
20872092
item["approvedPercent"] = round(availableWeight / totalWeight * 100, 3)
20882093
item["isPassed"] = (weightRemaining < 0)
20892094
#item["pseudohash"] = hashlib.sha256(param_val.encode()).hexdigest()
2090-
config_val = self.GetConfig(param_id)
2091-
pseudohash_bytes = hash.encode() + json.dumps(config_val, sort_keys=True).encode()
2092-
item["pseudohash"] = hashlib.sha256(pseudohash_bytes).hexdigest()
2095+
item['pseudohash'] = self.calculate_offer_pseudohash(hash, param_id)
20932096
offers.append(item)
20942097
#end for
20952098
return offers
@@ -2345,7 +2348,7 @@ def VoteOffer(self, offerHash):
23452348
resultFilePath = self.SignProposalVoteRequestWithValidator(offerHash, validatorIndex, validatorPubkey_b64, validatorSignature)
23462349
resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullConfigAddr, 1.5)
23472350
self.SendFile(resultFilePath, wallet)
2348-
self.AddSaveOffer(offer)
2351+
self.add_save_offer(offer)
23492352
#end define
23502353

23512354
def VoteComplaint(self, electionId, complaintHash):
@@ -2968,9 +2971,15 @@ def WriteBookmarkData(self, bookmark):
29682971
def offers_gc(self, save_offers):
29692972
current_offers = self.GetOffers()
29702973
current_offers_hashes = [offer.get("hash") for offer in current_offers]
2971-
for offer in list(save_offers.keys()):
2972-
if offer not in current_offers_hashes:
2973-
save_offers.pop(offer)
2974+
for offer_hash, offer in list(save_offers.items()):
2975+
if offer_hash not in current_offers_hashes:
2976+
if isinstance(offer, list):
2977+
param_id = offer[1]
2978+
if param_id is not None and offer[0] != self.calculate_offer_pseudohash(offer_hash, param_id):
2979+
# param has been changed so no need to keep anymore
2980+
save_offers.pop(offer)
2981+
else: # old version of offer in db
2982+
save_offers.pop(offer)
29742983
return save_offers
29752984

29762985
def GetSaveOffers(self):
@@ -2983,12 +2992,12 @@ def GetSaveOffers(self):
29832992
return save_offers
29842993
#end define
29852994

2986-
def AddSaveOffer(self, offer):
2987-
offerHash = offer.get("hash")
2988-
offerPseudohash = offer.get("pseudohash")
2989-
saveOffers = self.GetSaveOffers()
2990-
if offerHash not in saveOffers:
2991-
saveOffers[offerHash] = offerPseudohash
2995+
def add_save_offer(self, offer):
2996+
offer_hash = offer.get("hash")
2997+
offer_pseudohash = offer.get("pseudohash")
2998+
save_offers = self.GetSaveOffers()
2999+
if offer_hash not in save_offers:
3000+
save_offers[offer_hash] = [offer_pseudohash, offer.get('config', {}).get("id")]
29923001
self.local.save()
29933002
#end define
29943003

0 commit comments

Comments
 (0)