Skip to content

Commit de3a3dd

Browse files
committed
improve voting
1 parent 7c7ccf3 commit de3a3dd

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

modules/utilities.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def get_offer_diff(self, offer_hash):
162162
offer = self.ton.GetOffer(offer_hash)
163163
config_id = offer["config"]["id"]
164164
config_value = offer["config"]["value"]
165+
if config_id < 0:
166+
color_print("{red}Offer config id is negative. Cannot get diff.{endc}")
167+
return
165168

166169
if '{' in config_value or '}' in config_value:
167170
start = config_value.find('{') + 1

modules/validator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ def vote_offer(self, args):
1616
if len(args) == 0:
1717
color_print("{red}Bad args. Usage:{endc} vo <offer-hash>")
1818
return
19-
for offerHash in args:
20-
self.ton.VoteOffer(offerHash)
19+
offers = self.ton.GetOffers()
20+
for offer_hash in args:
21+
offer = self.ton.GetOffer(offer_hash, offers)
22+
self.ton.add_save_offer(offer)
23+
for offer_hash in args:
24+
offer = self.ton.GetOffer(offer_hash, offers)
25+
self.ton.VoteOffer(offer)
2126
color_print("VoteOffer - {green}OK{endc}")
2227

2328
def vote_election_entry(self, args):

mytoncore/functions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ def GetBlockTimeAvg(local, timediff):
435435

436436
def Offers(local, ton):
437437
save_offers = ton.GetSaveOffers()
438+
if save_offers:
439+
ton.offers_gc(save_offers)
440+
else:
441+
return
438442
offers = ton.GetOffers()
439443
for offer in offers:
440444
offer_hash = offer.get("hash")
@@ -446,7 +450,7 @@ def Offers(local, ton):
446450
else: # old version of save offers {"hash": "pseudohash"}
447451
save_offer_pseudohash = save_offer
448452
if offer_pseudohash == save_offer_pseudohash and offer_pseudohash is not None:
449-
ton.VoteOffer(offer_hash)
453+
ton.VoteOffer(offer)
450454
# end define
451455

452456
def Telemetry(local, ton):

mytoncore/mytoncore.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,23 +2280,23 @@ def SignComplaintVoteRequestWithValidator(self, complaintHash, electionId, valid
22802280
return fileName
22812281
#end define
22822282

2283-
def VoteOffer(self, offerHash):
2283+
def VoteOffer(self, offer):
22842284
self.local.add_log("start VoteOffer function", "debug")
2285-
fullConfigAddr = self.GetFullConfigAddr()
2285+
full_config_addr = self.GetFullConfigAddr()
22862286
wallet = self.GetValidatorWallet(mode="vote")
2287-
validatorKey = self.GetValidatorKey()
2288-
validatorPubkey_b64 = self.GetPubKeyBase64(validatorKey)
2289-
validatorIndex = self.GetValidatorIndex()
2290-
offer = self.GetOffer(offerHash)
2291-
if validatorIndex in offer.get("votedValidators"):
2287+
validator_key = self.GetValidatorKey()
2288+
validator_pubkey_b64 = self.GetPubKeyBase64(validator_key)
2289+
validator_index = self.GetValidatorIndex()
2290+
offer_hash = offer.get("hash")
2291+
if validator_index in offer.get("votedValidators"):
22922292
self.local.add_log("Proposal already has been voted", "debug")
22932293
return
22942294
self.add_save_offer(offer)
2295-
var1 = self.CreateConfigProposalRequest(offerHash, validatorIndex)
2296-
validatorSignature = self.GetValidatorSignature(validatorKey, var1)
2297-
resultFilePath = self.SignProposalVoteRequestWithValidator(offerHash, validatorIndex, validatorPubkey_b64, validatorSignature)
2298-
resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullConfigAddr, 1.5)
2299-
self.SendFile(resultFilePath, wallet)
2295+
var1 = self.CreateConfigProposalRequest(offer_hash, validator_index)
2296+
validator_signature = self.GetValidatorSignature(validator_key, var1)
2297+
result_file_path = self.SignProposalVoteRequestWithValidator(offer_hash, validator_index, validator_pubkey_b64, validator_signature)
2298+
result_file_path = self.SignBocWithWallet(wallet, result_file_path, full_config_addr, 1.5)
2299+
self.SendFile(result_file_path, wallet, remove=False)
23002300
#end define
23012301

23022302
def VoteComplaint(self, electionId, complaintHash):
@@ -2604,11 +2604,12 @@ def CheckValidators(self, start, end):
26042604
self.local.add_log("var1: {}, var2: {}, pubkey: {}, election_id: {}".format(var1, var2, pubkey, electionId), "debug")
26052605
#end define
26062606

2607-
def GetOffer(self, offerHash):
2607+
def GetOffer(self, offer_hash: str, offers: list = None):
26082608
self.local.add_log("start GetOffer function", "debug")
2609-
offers = self.GetOffers()
2609+
if offers is None:
2610+
offers = self.GetOffers()
26102611
for offer in offers:
2611-
if offerHash == offer.get("hash"):
2612+
if offer_hash == offer.get("hash"):
26122613
return offer
26132614
raise Exception("GetOffer error: offer not found.")
26142615
#end define
@@ -2878,9 +2879,11 @@ def offers_gc(self, save_offers):
28782879
if offer_hash not in current_offers_hashes:
28792880
if isinstance(offer, list):
28802881
param_id = offer[1]
2881-
if param_id is not None and offer[0] != self.calculate_offer_pseudohash(offer_hash, param_id):
2882+
phash = self.calculate_offer_pseudohash(offer_hash, param_id)
2883+
if param_id is not None and offer[0] != phash:
28822884
# param has been changed so no need to keep anymore
28832885
save_offers.pop(offer_hash)
2886+
self.local.add_log(f"Removing offer {offer_hash} from save_offers. Saved phash: {offer[0]}, now phash: {phash}", "debug")
28842887
else: # old version of offer in db
28852888
save_offers.pop(offer_hash)
28862889
return save_offers
@@ -2891,7 +2894,6 @@ def GetSaveOffers(self):
28912894
if save_offers is None or isinstance(save_offers, list):
28922895
save_offers = dict()
28932896
self.local.db[bname] = save_offers
2894-
self.offers_gc(save_offers)
28952897
return save_offers
28962898
#end define
28972899

0 commit comments

Comments
 (0)