Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions mytoncore/mytoncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,11 @@ def GetSaveElectionEntries(self, electionId):
return result
#end define

def calculate_offer_pseudohash(self, offer_hash: str, param_id: int):
config_val = self.GetConfig(param_id)
pseudohash_bytes = offer_hash.encode() + json.dumps(config_val, sort_keys=True).encode()
return hashlib.sha256(pseudohash_bytes).hexdigest()

def GetOffers(self):
self.local.add_log("start GetOffers function", "debug")
fullConfigAddr = self.GetFullConfigAddr()
Expand Down Expand Up @@ -2087,9 +2092,7 @@ def GetOffers(self):
item["approvedPercent"] = round(availableWeight / totalWeight * 100, 3)
item["isPassed"] = (weightRemaining < 0)
#item["pseudohash"] = hashlib.sha256(param_val.encode()).hexdigest()
config_val = self.GetConfig(param_id)
pseudohash_bytes = hash.encode() + json.dumps(config_val, sort_keys=True).encode()
item["pseudohash"] = hashlib.sha256(pseudohash_bytes).hexdigest()
item['pseudohash'] = self.calculate_offer_pseudohash(hash, param_id)
offers.append(item)
#end for
return offers
Expand Down Expand Up @@ -2345,7 +2348,7 @@ def VoteOffer(self, offerHash):
resultFilePath = self.SignProposalVoteRequestWithValidator(offerHash, validatorIndex, validatorPubkey_b64, validatorSignature)
resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullConfigAddr, 1.5)
self.SendFile(resultFilePath, wallet)
self.AddSaveOffer(offer)
self.add_save_offer(offer)
#end define

def VoteComplaint(self, electionId, complaintHash):
Expand Down Expand Up @@ -2968,9 +2971,15 @@ def WriteBookmarkData(self, bookmark):
def offers_gc(self, save_offers):
current_offers = self.GetOffers()
current_offers_hashes = [offer.get("hash") for offer in current_offers]
for offer in list(save_offers.keys()):
if offer not in current_offers_hashes:
save_offers.pop(offer)
for offer_hash, offer in list(save_offers.items()):
if offer_hash not in current_offers_hashes:
if isinstance(offer, list):
param_id = offer[1]
if param_id is not None and offer[0] != self.calculate_offer_pseudohash(offer_hash, param_id):
# param has been changed so no need to keep anymore
save_offers.pop(offer)
else: # old version of offer in db
save_offers.pop(offer)
return save_offers

def GetSaveOffers(self):
Expand All @@ -2983,12 +2992,12 @@ def GetSaveOffers(self):
return save_offers
#end define

def AddSaveOffer(self, offer):
offerHash = offer.get("hash")
offerPseudohash = offer.get("pseudohash")
saveOffers = self.GetSaveOffers()
if offerHash not in saveOffers:
saveOffers[offerHash] = offerPseudohash
def add_save_offer(self, offer):
offer_hash = offer.get("hash")
offer_pseudohash = offer.get("pseudohash")
save_offers = self.GetSaveOffers()
if offer_hash not in save_offers:
save_offers[offer_hash] = [offer_pseudohash, offer.get('config', {}).get("id")]
self.local.save()
#end define

Expand Down