Skip to content

Commit

Permalink
(un)Staking multiple avoid tx limit (#1244)
Browse files Browse the repository at this point in the history
* add tx rate limit

* wait for tx limit if not done multi stake/unstake

* dont "decrypt" hotkey
  • Loading branch information
camfairchild authored Mar 29, 2023
1 parent 9443c66 commit f3cca53
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 11 additions & 3 deletions bittensor/_subtensor/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import bittensor

from rich.prompt import Confirm
from time import sleep
from typing import List, Dict, Union, Optional
from bittensor.utils.balance import Balance
from ..errors import *
Expand Down Expand Up @@ -240,7 +241,7 @@ def add_stake_multiple_extrinsic (
amounts = [Balance.from_tao(amount.tao * percent_reduction) for amount in amounts]

successful_stakes = 0
for hotkey_ss58, amount, old_stake in zip(hotkey_ss58s, amounts, old_stakes):
for idx, (hotkey_ss58, amount, old_stake) in enumerate(zip(hotkey_ss58s, amounts, old_stakes)):
staking_all = False
# Convert to bittensor.Balance
if amount == None:
Expand Down Expand Up @@ -271,9 +272,17 @@ def add_stake_multiple_extrinsic (
wait_for_inclusion = wait_for_inclusion,
wait_for_finalization = wait_for_finalization,
)

if staking_response: # If we successfully staked.
# We only wait here if we expect finalization.

if idx < len(hotkey_ss58s) - 1:
# Wait for tx rate limit.
tx_rate_limit_blocks = subtensor.tx_rate_limit()
if tx_rate_limit_blocks > 0:
bittensor.__console__.print(":hourglass: [yellow]Waiting for tx rate limit: [white]{}[/white] blocks[/yellow]".format(tx_rate_limit_blocks))
sleep( tx_rate_limit_blocks * 12 ) # 12 seconds per block

if not wait_for_finalization and not wait_for_inclusion:
bittensor.__console__.print(":white_heavy_check_mark: [green]Sent[/green]")
old_balance -= staking_balance
Expand Down Expand Up @@ -356,7 +365,6 @@ def __do_add_stake_single(
"""
# Decrypt keys,
wallet.coldkey
wallet.hotkey

hotkey_owner = subtensor.get_hotkey_owner( hotkey_ss58 )
own_hotkey = (wallet.coldkeypub.ss58_address == hotkey_owner)
Expand Down
11 changes: 10 additions & 1 deletion bittensor/_subtensor/extrinsics/unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import bittensor

from rich.prompt import Confirm
from time import sleep
from typing import List, Dict, Union, Optional
from bittensor.utils.balance import Balance
from ..errors import *
Expand Down Expand Up @@ -245,7 +246,7 @@ def unstake_multiple_extrinsic (
old_stakes.append(old_stake) # None if not registered.

successful_unstakes = 0
for hotkey_ss58, amount, old_stake in zip(hotkey_ss58s, amounts, old_stakes):
for idx, (hotkey_ss58, amount, old_stake) in enumerate(zip(hotkey_ss58s, amounts, old_stakes)):
# Covert to bittensor.Balance
if amount == None:
# Unstake it all.
Expand Down Expand Up @@ -279,6 +280,14 @@ def unstake_multiple_extrinsic (

if staking_response: # If we successfully unstaked.
# We only wait here if we expect finalization.

if idx < len(hotkey_ss58s) - 1:
# Wait for tx rate limit.
tx_rate_limit_blocks = subtensor.tx_rate_limit()
if tx_rate_limit_blocks > 0:
bittensor.__console__.print(":hourglass: [yellow]Waiting for tx rate limit: [white]{}[/white] blocks[/yellow]".format(tx_rate_limit_blocks))
sleep( tx_rate_limit_blocks * 12 ) # 12 seconds per block

if not wait_for_finalization and not wait_for_inclusion:
bittensor.__console__.print(":white_heavy_check_mark: [green]Sent[/green]")
successful_unstakes += 1
Expand Down
3 changes: 3 additions & 0 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ def total_stake (self,block: Optional[int] = None ) -> 'bittensor.Balance':

def serving_rate_limit (self, block: Optional[int] = None ) -> Optional[int]:
return self.query_subtensor( "ServingRateLimit", block ).value

def tx_rate_limit (self, block: Optional[int] = None ) -> Optional[int]:
return self.query_subtensor( "TxRateLimit", block ).value

#####################################
#### Network Parameters ####
Expand Down

0 comments on commit f3cca53

Please sign in to comment.