Skip to content

Commit e494c60

Browse files
committed
improve pool choosing
1 parent 09c9053 commit e494c60

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

mytoncore/mytoncore.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,7 @@ def GetStake(self, account, args=None):
13181318
stake = account.balance - 10
13191319
#end if
13201320

1321-
pool_version = self.GetVersionFromCodeHash(account.codeHash)
1322-
is_single_nominator = pool_version is not None and 'spool' in pool_version
1321+
is_single_nominator = self.is_account_single_nominator(account)
13231322

13241323
if stake is None and usePool and not is_single_nominator:
13251324
stake = account.balance - 20
@@ -1377,7 +1376,6 @@ def ElectionEntry(self, args=None):
13771376
addrB64 = wallet.addrB64
13781377
if wallet is None:
13791378
raise Exception("Validator wallet not found")
1380-
#end if
13811379

13821380
self.local.add_log("start ElectionEntry function", "debug")
13831381
# Check if validator is not synchronized
@@ -1386,7 +1384,6 @@ def ElectionEntry(self, args=None):
13861384
if validatorOutOfSync > 60:
13871385
self.local.add_log("Validator is not synchronized", "error")
13881386
return
1389-
#end if
13901387

13911388
# Get startWorkTime and endWorkTime
13921389
fullElectorAddr = self.GetFullElectorAddr()
@@ -1425,17 +1422,15 @@ def ElectionEntry(self, args=None):
14251422
if adnl_addr in entries:
14261423
self.local.add_log("Elections entry already completed", "info")
14271424
return
1428-
#end if
14291425

14301426
if usePool:
1431-
pool = self.GetPool(mode="stake")
1427+
pool = self.get_pool()
14321428
addrB64 = pool.addrB64
14331429
elif useController:
14341430
controllerAddr = self.GetController(mode="stake")
14351431
self.CheckController(controllerAddr)
14361432
self.CreateLoanRequest(controllerAddr)
14371433
addrB64 = controllerAddr
1438-
#end if
14391434

14401435
# Calculate stake
14411436
account = self.GetAccount(addrB64)
@@ -3547,38 +3542,42 @@ def GetPools(self):
35473542
pool = self.GetLocalPool(poolName)
35483543
pools.append(pool)
35493544
return pools
3550-
#end define
35513545

3552-
def GetPool(self, mode):
3546+
def get_pool(self):
35533547
pools = self.GetPools()
35543548
for pool in pools:
3555-
if mode == "stake" and self.IsPoolReadyToStake(pool.addrB64):
3556-
return pool
3557-
if mode == "vote" and self.IsPoolReadyToVote(pool.addrB64):
3549+
if self.is_pool_ready_to_stake(pool):
35583550
return pool
35593551
raise Exception("Validator pool not found or not ready")
3560-
#end define
35613552

3562-
def GetPoolLastSentStakeTime(self, addrB64):
3563-
poolData = self.GetPoolData(addrB64)
3564-
return poolData["stakeAt"]
3565-
#end define
3553+
def get_pool_last_sent_stake_time(self, addrB64):
3554+
pool_data = self.GetPoolData(addrB64)
3555+
return pool_data["stakeAt"]
35663556

3567-
def IsPoolReadyToStake(self, addrB64):
3557+
def is_pool_ready_to_stake(self, pool: Pool):
3558+
addr = pool.addrB64
3559+
account = self.GetAccount(addr)
3560+
is_single_nominator = self.is_account_single_nominator(account)
3561+
if self.using_single_nominator() and not is_single_nominator:
3562+
return False
3563+
try: # check that account balance is enough for stake
3564+
stake = self.GetStake(account)
3565+
if not stake:
3566+
raise Exception(f'Stake is {stake}')
3567+
except Exception as e:
3568+
self.local.add_log(f"Failed to get stake for pool {addr}: {e}", "debug")
3569+
return False
35683570
now = get_timestamp()
35693571
config15 = self.GetConfig15()
3570-
lastSentStakeTime = self.GetPoolLastSentStakeTime(addrB64)
3571-
stakeFreezeDelay = config15["validatorsElectedFor"] + config15["stakeHeldFor"]
3572-
result = lastSentStakeTime + stakeFreezeDelay < now
3573-
print(f"{addrB64}: {result}. {lastSentStakeTime}, {stakeFreezeDelay}, {now}")
3572+
last_sent_stake_time = self.get_pool_last_sent_stake_time(addr)
3573+
stake_freeze_delay = config15["validatorsElectedFor"] + config15["stakeHeldFor"]
3574+
result = last_sent_stake_time + stake_freeze_delay < now
3575+
print(f"{addr}: {result}. {last_sent_stake_time}, {stake_freeze_delay}, {now}")
35743576
return result
3575-
#end define
35763577

3577-
def IsPoolReadyToVote(self, addrB64):
3578-
vwl = self.GetValidatorsWalletsList()
3579-
result = addrB64 in vwl
3580-
return result
3581-
#end define
3578+
def is_account_single_nominator(self, account: Account):
3579+
account_version = self.GetVersionFromCodeHash(account.codeHash)
3580+
return account_version is not None and 'spool' in account_version
35823581

35833582
def GetPoolData(self, addrB64):
35843583
self.local.add_log("start GetPoolData function", "debug")

0 commit comments

Comments
 (0)