From c34a459023c2510363926b1813a83980a58fd39c Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 6 Aug 2024 14:18:31 +0800 Subject: [PATCH 1/2] check validator wallet status before activating pool --- modules/__init__.py | 1 + modules/nominator_pool.py | 2 ++ modules/single_pool.py | 1 + mytoncore/mytoncore.py | 27 +++++++++++++++++++-------- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/__init__.py b/modules/__init__.py index 298302b0..625ecc79 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -34,6 +34,7 @@ class Setting: 'stake': Setting('validator', None, 'Stake amount'), 'stakePercent': Setting('validator', 99, 'Stake percent if `stake` is null'), 'isSlashing': Setting('validator', None, 'Create complaints to validators'), + 'walletName': Setting('validator', None, 'Wallet name'), 'maxFactor': Setting('validator', None, 'Param send to Elector. if null will be taken from 17 config param'), 'participateBeforeEnd': Setting('validator', None, 'Amount of seconds before start of round to participate'), 'liquid_pool_addr': Setting('liquid-staking', None, 'Liquid staking pool address'), diff --git a/modules/nominator_pool.py b/modules/nominator_pool.py index a5adcc4b..3ca8647b 100644 --- a/modules/nominator_pool.py +++ b/modules/nominator_pool.py @@ -62,6 +62,8 @@ def do_activate_pool(self, pool, ex=True): elif account.status == "active": self.local.add_log("do_activate_pool warning: account status is active", "warning") else: + validator_wallet = self.ton.GetValidatorWallet() + self.ton.check_account_status(validator_wallet.addrB64) self.ton.SendFile(pool.bocFilePath, pool, timeout=False, remove=False) #end define diff --git a/modules/single_pool.py b/modules/single_pool.py index d2721c5a..7e33a260 100644 --- a/modules/single_pool.py +++ b/modules/single_pool.py @@ -51,6 +51,7 @@ def do_activate_single_pool(self, pool): self.local.add_log("start activate_single_pool function", "debug") boc_mode = "--with-init" validator_wallet = self.ton.GetValidatorWallet() + self.ton.check_account_status(validator_wallet.addrB64) result_file_path = self.ton.SignBocWithWallet(validator_wallet, pool.bocFilePath, pool.addrB64_init, 1, boc_mode=boc_mode) self.ton.SendFile(result_file_path, validator_wallet) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 098d6ec9..fffede66 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1180,9 +1180,7 @@ def SignBocWithWallet(self, wallet, boc_path, dest, coins, **kwargs): # Balance checking account = self.GetAccount(wallet.addrB64) - if account.balance < coins + 0.1: - raise Exception("Wallet balance is less than requested coins") - #end if + self.check_account_balance(account, coins + 0.1) # Bounceable checking destAccount = self.GetAccount(dest) @@ -1864,6 +1862,22 @@ def GetWalletId(self, wallet): return subwallet #end define + def check_account_balance(self, account, coins): + if not isinstance(account, Account): + account = self.GetAccount(account) + if account.balance < coins: + raise Exception(f"Wallet {account.addrB64} balance is less than requested coins. Balance: {account.balance}, requested amount: {coins} (need {coins - account.balance} more)") + # end if + # end define + + def check_account_status(self, account): + if not isinstance(account, Account): + account = self.GetAccount(account) + if account.status != "active": + raise Exception(f"Wallet {account.addrB64} account is uninitialized") + # end if + # end define + def MoveCoins(self, wallet, dest, coins, **kwargs): self.local.add_log("start MoveCoins function", "debug") flags = kwargs.get("flags", list()) @@ -1884,11 +1898,8 @@ def MoveCoins(self, wallet, dest, coins, **kwargs): # Balance checking account = self.GetAccount(wallet.addrB64) - if account.balance < coins + 0.1: - raise Exception("Wallet balance is less than requested coins") - if account.status != "active": - raise Exception("Wallet account is uninitialized") - #end if + self.check_account_balance(account, coins + 0.1) + self.check_account_status(account) # Bounceable checking destAccount = self.GetAccount(dest) From 968437a4b598974a5c72d78c7343cf0fd45d0f54 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 6 Aug 2024 14:20:32 +0800 Subject: [PATCH 2/2] fixes fix exceptions rename method fix name --- modules/__init__.py | 2 +- modules/nominator_pool.py | 2 +- modules/single_pool.py | 2 +- mytoncore/mytoncore.py | 11 +++++++---- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/__init__.py b/modules/__init__.py index 625ecc79..8b92c260 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -34,7 +34,7 @@ class Setting: 'stake': Setting('validator', None, 'Stake amount'), 'stakePercent': Setting('validator', 99, 'Stake percent if `stake` is null'), 'isSlashing': Setting('validator', None, 'Create complaints to validators'), - 'walletName': Setting('validator', None, 'Wallet name'), + 'validatorWalletName': Setting('validator', 'wallet_001', 'Validator\'s wallet name'), 'maxFactor': Setting('validator', None, 'Param send to Elector. if null will be taken from 17 config param'), 'participateBeforeEnd': Setting('validator', None, 'Amount of seconds before start of round to participate'), 'liquid_pool_addr': Setting('liquid-staking', None, 'Liquid staking pool address'), diff --git a/modules/nominator_pool.py b/modules/nominator_pool.py index 3ca8647b..14f4fbdd 100644 --- a/modules/nominator_pool.py +++ b/modules/nominator_pool.py @@ -63,7 +63,7 @@ def do_activate_pool(self, pool, ex=True): self.local.add_log("do_activate_pool warning: account status is active", "warning") else: validator_wallet = self.ton.GetValidatorWallet() - self.ton.check_account_status(validator_wallet.addrB64) + self.ton.check_account_active(validator_wallet.addrB64) self.ton.SendFile(pool.bocFilePath, pool, timeout=False, remove=False) #end define diff --git a/modules/single_pool.py b/modules/single_pool.py index 7e33a260..5b5c6e2b 100644 --- a/modules/single_pool.py +++ b/modules/single_pool.py @@ -51,7 +51,7 @@ def do_activate_single_pool(self, pool): self.local.add_log("start activate_single_pool function", "debug") boc_mode = "--with-init" validator_wallet = self.ton.GetValidatorWallet() - self.ton.check_account_status(validator_wallet.addrB64) + self.ton.check_account_active(validator_wallet.addrB64) result_file_path = self.ton.SignBocWithWallet(validator_wallet, pool.bocFilePath, pool.addrB64_init, 1, boc_mode=boc_mode) self.ton.SendFile(result_file_path, validator_wallet) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index fffede66..ed643083 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1866,15 +1866,18 @@ def check_account_balance(self, account, coins): if not isinstance(account, Account): account = self.GetAccount(account) if account.balance < coins: - raise Exception(f"Wallet {account.addrB64} balance is less than requested coins. Balance: {account.balance}, requested amount: {coins} (need {coins - account.balance} more)") + raise Exception(f"Account {account.addrB64} balance is less than requested coins. Balance: {account.balance}, requested amount: {coins} (need {coins - account.balance} more)") # end if # end define - def check_account_status(self, account): + def check_account_active(self, account): if not isinstance(account, Account): + address = account account = self.GetAccount(account) + else: + address = account.addrB64 if account.status != "active": - raise Exception(f"Wallet {account.addrB64} account is uninitialized") + raise Exception(f"Account {address} account is uninitialized") # end if # end define @@ -1899,7 +1902,7 @@ def MoveCoins(self, wallet, dest, coins, **kwargs): # Balance checking account = self.GetAccount(wallet.addrB64) self.check_account_balance(account, coins + 0.1) - self.check_account_status(account) + self.check_account_active(account) # Bounceable checking destAccount = self.GetAccount(dest)