Skip to content

Commit 4287907

Browse files
authored
Merge pull request #475 from yungwine/voting
Improve voting
2 parents 6116d41 + f594035 commit 4287907

File tree

6 files changed

+56
-32
lines changed

6 files changed

+56
-32
lines changed

modules/alert_bot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def init_alerts():
8484
"high",
8585
"Validator has been slashed in the previous validation round",
8686
"Validator has been slashed in previous round for {amount} TON",
87-
FREEZE_PERIOD
87+
VALIDATION_PERIOD
8888
),
8989
"stake_not_accepted": Alert(
9090
"high",
@@ -438,8 +438,9 @@ def check_voting(self):
438438
return
439439
need_to_vote = []
440440
offers = self.ton.GetOffers()
441+
saved_offers = self.ton.GetSaveOffers()
441442
for offer in offers:
442-
if not offer['isPassed'] and offer['approvedPercent'] >= 50 and validator_index not in offer['votedValidators']:
443+
if not offer['isPassed'] and offer['approvedPercent'] >= 50 and validator_index not in offer['votedValidators'] and offer['hash'] not in saved_offers:
443444
need_to_vote.append(offer['hash'])
444445
if need_to_vote:
445446
self.send_alert("voting", hashes=' '.join(need_to_vote))

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: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,17 @@ def SendFile(self, filePath, wallet=None, **kwargs):
12231223
self.liteClient.Run("sendfile " + filePath, useLocalLiteServer=False)
12241224
except: pass
12251225
if duplicateApi:
1226-
self.send_boc_toncenter(filePath)
1226+
try:
1227+
self.send_boc_toncenter(filePath)
1228+
except Exception as e:
1229+
self.local.add_log(f'Failed to send file {filePath} to toncenter: {e}', 'warning')
12271230
if timeout and wallet:
12281231
self.WaitTransaction(wallet, timeout)
1229-
if remove == True:
1230-
os.remove(filePath)
1232+
if remove:
1233+
try:
1234+
os.remove(filePath)
1235+
except Exception as e:
1236+
self.local.add_log(f'Failed to remove file {filePath}: {e}', 'warning')
12311237
#end define
12321238

12331239
def send_boc_toncenter(self, file_path: str):
@@ -2274,23 +2280,23 @@ def SignComplaintVoteRequestWithValidator(self, complaintHash, electionId, valid
22742280
return fileName
22752281
#end define
22762282

2277-
def VoteOffer(self, offerHash):
2283+
def VoteOffer(self, offer):
22782284
self.local.add_log("start VoteOffer function", "debug")
2279-
fullConfigAddr = self.GetFullConfigAddr()
2285+
full_config_addr = self.GetFullConfigAddr()
22802286
wallet = self.GetValidatorWallet(mode="vote")
2281-
validatorKey = self.GetValidatorKey()
2282-
validatorPubkey_b64 = self.GetPubKeyBase64(validatorKey)
2283-
validatorIndex = self.GetValidatorIndex()
2284-
offer = self.GetOffer(offerHash)
2285-
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"):
22862292
self.local.add_log("Proposal already has been voted", "debug")
22872293
return
22882294
self.add_save_offer(offer)
2289-
var1 = self.CreateConfigProposalRequest(offerHash, validatorIndex)
2290-
validatorSignature = self.GetValidatorSignature(validatorKey, var1)
2291-
resultFilePath = self.SignProposalVoteRequestWithValidator(offerHash, validatorIndex, validatorPubkey_b64, validatorSignature)
2292-
resultFilePath = self.SignBocWithWallet(wallet, resultFilePath, fullConfigAddr, 1.5)
2293-
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)
22942300
#end define
22952301

22962302
def VoteComplaint(self, electionId, complaintHash):
@@ -2598,11 +2604,12 @@ def CheckValidators(self, start, end):
25982604
self.local.add_log("var1: {}, var2: {}, pubkey: {}, election_id: {}".format(var1, var2, pubkey, electionId), "debug")
25992605
#end define
26002606

2601-
def GetOffer(self, offerHash):
2607+
def GetOffer(self, offer_hash: str, offers: list = None):
26022608
self.local.add_log("start GetOffer function", "debug")
2603-
offers = self.GetOffers()
2609+
if offers is None:
2610+
offers = self.GetOffers()
26042611
for offer in offers:
2605-
if offerHash == offer.get("hash"):
2612+
if offer_hash == offer.get("hash"):
26062613
return offer
26072614
raise Exception("GetOffer error: offer not found.")
26082615
#end define
@@ -2872,9 +2879,11 @@ def offers_gc(self, save_offers):
28722879
if offer_hash not in current_offers_hashes:
28732880
if isinstance(offer, list):
28742881
param_id = offer[1]
2875-
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:
28762884
# param has been changed so no need to keep anymore
28772885
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")
28782887
else: # old version of offer in db
28792888
save_offers.pop(offer_hash)
28802889
return save_offers
@@ -2885,7 +2894,6 @@ def GetSaveOffers(self):
28852894
if save_offers is None or isinstance(save_offers, list):
28862895
save_offers = dict()
28872896
self.local.db[bname] = save_offers
2888-
self.offers_gc(save_offers)
28892897
return save_offers
28902898
#end define
28912899

@@ -3208,12 +3216,15 @@ def set_initial_sync_off(self):
32083216
self.local.db.pop('initialSync', None)
32093217
self.local.save()
32103218

3211-
def Tlb2Json(self, text):
3219+
def Tlb2Json(self, text: str):
32123220
# Заменить скобки
32133221
start = 0
32143222
end = len(text)
32153223
if '=' in text:
32163224
start = text.find('=')+1
3225+
if text[start:].startswith(' x{'): # param has no tlb scheme, return cell value
3226+
end = text.rfind('}')+1
3227+
return {'_': text[start:end].strip()}
32173228
if "x{" in text:
32183229
end = text.find("x{")
32193230
text = text[start:end]

mytonctrl/mytonctrl.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ def PrintStatus(local, ton, args):
631631
startWorkTime = ton.GetActiveElectionId(fullElectorAddr)
632632
validator_index = ton.GetValidatorIndex()
633633

634-
offersNumber = ton.GetOffersNumber()
635-
complaintsNumber = ton.GetComplaintsNumber()
634+
offersNumber = local.try_function(ton.GetOffersNumber)
635+
complaintsNumber = local.try_function(ton.GetComplaintsNumber)
636636

637637
tpsAvg = ton.GetStatistics("tpsAvg", statistics)
638638

@@ -654,10 +654,10 @@ def PrintTonStatus(local, network_name, startWorkTime, totalValidators, onlineVa
654654
#tps5 = tpsAvg[1]
655655
#tps15 = tpsAvg[2]
656656
allValidators = totalValidators
657-
newOffers = offersNumber.get("new")
658-
allOffers = offersNumber.get("all")
659-
newComplaints = complaintsNumber.get("new")
660-
allComplaints = complaintsNumber.get("all")
657+
newOffers = offersNumber.get("new") if offersNumber else 'n/a'
658+
allOffers = offersNumber.get("all") if offersNumber else 'n/a'
659+
newComplaints = complaintsNumber.get("new") if complaintsNumber else 'n/a'
660+
allComplaints = complaintsNumber.get("all") if complaintsNumber else 'n/a'
661661
#tps1_text = bcolors.green_text(tps1)
662662
#tps5_text = bcolors.green_text(tps5)
663663
#tps15_text = bcolors.green_text(tps15)

0 commit comments

Comments
 (0)