Skip to content

Commit 4e2938a

Browse files
committed
WIP check-in
1 parent 51406fd commit 4e2938a

File tree

13 files changed

+95
-113
lines changed

13 files changed

+95
-113
lines changed

bittensor/core/async_subtensor.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,9 @@ async def sign_and_send_extrinsic(
26832683
wait_for_inclusion: bool = True,
26842684
wait_for_finalization: bool = False,
26852685
sign_with: str = "coldkey",
2686+
use_nonce: bool = False,
2687+
period: Optional[int] = None,
2688+
nonce_key: str = "hotkey"
26862689
) -> tuple[bool, str]:
26872690
"""
26882691
Helper method to sign and submit an extrinsic call to chain.
@@ -2697,13 +2700,25 @@ async def sign_and_send_extrinsic(
26972700
Returns:
26982701
(success, error message)
26992702
"""
2700-
if sign_with not in ("coldkey", "hotkey", "coldkeypub"):
2703+
possible_keys = ("coldkey", "hotkey", "coldkeypub")
2704+
if sign_with not in possible_keys:
27012705
raise AttributeError(
27022706
f"'sign_with' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{sign_with}'"
27032707
)
2708+
signing_keypair = getattr(wallet, sign_with)
2709+
extrinsic_data = {"call": call, "keypair": signing_keypair}
2710+
if use_nonce:
2711+
if nonce_key not in possible_keys:
2712+
raise AttributeError(
2713+
f"'nonce_key' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{nonce_key}'"
2714+
)
2715+
next_nonce = await self.substrate.get_account_next_index(getattr(wallet, nonce_key).ss58_address)
2716+
extrinsic_data["nonce"] = next_nonce
2717+
if period is not None:
2718+
extrinsic_data["era"] = {"period": period}
27042719

27052720
extrinsic = await self.substrate.create_signed_extrinsic(
2706-
call=call, keypair=getattr(wallet, sign_with)
2721+
**extrinsic_data
27072722
)
27082723
try:
27092724
response = await self.substrate.submit_extrinsic(

bittensor/core/extrinsics/asyncex/staking.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
from bittensor.utils.balance import Balance
77
from bittensor.utils.btlogging import logging
88
from bittensor.core.extrinsics.utils import get_old_stakes
9-
from bittensor.core.extrinsics.utils import (
10-
async_sign_and_send_with_nonce as sign_and_send_with_nonce,
11-
)
129

1310
if TYPE_CHECKING:
1411
from bittensor_wallet import Wallet
@@ -114,14 +111,14 @@ async def add_stake_extrinsic(
114111
"netuid": netuid,
115112
},
116113
)
117-
staking_response, err_msg = await sign_and_send_with_nonce(
118-
subtensor,
114+
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
119115
call,
120116
wallet,
121117
wait_for_inclusion,
122118
wait_for_finalization,
123119
nonce_key="coldkeypub",
124-
signing_key="coldkey",
120+
sign_with="coldkey",
121+
use_nonce=True
125122
)
126123
if staking_response is True: # If we successfully staked.
127124
# We only wait here if we expect finalization.
@@ -304,14 +301,14 @@ async def add_stake_multiple_extrinsic(
304301
"netuid": netuid,
305302
},
306303
)
307-
staking_response, err_msg = await sign_and_send_with_nonce(
308-
subtensor,
304+
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
309305
call,
310306
wallet,
311307
wait_for_inclusion,
312308
wait_for_finalization,
313309
nonce_key="coldkeypub",
314-
signing_key="coldkey",
310+
sign_with="coldkey",
311+
use_nonce=True
315312
)
316313

317314
if staking_response is True: # If we successfully staked.

bittensor/core/extrinsics/asyncex/unstaking.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
from typing import Optional, TYPE_CHECKING
33

44
from bittensor.core.errors import StakeError, NotRegisteredError
5-
from bittensor.core.extrinsics.utils import (
6-
async_sign_and_send_with_nonce as sign_and_send_with_nonce,
7-
)
85
from bittensor.utils import unlock_key
96
from bittensor.utils.balance import Balance
107
from bittensor.utils.btlogging import logging
@@ -100,14 +97,9 @@ async def unstake_extrinsic(
10097
"netuid": netuid,
10198
},
10299
)
103-
staking_response, err_msg = await sign_and_send_with_nonce(
104-
subtensor,
105-
call,
106-
wallet,
107-
wait_for_inclusion,
108-
wait_for_finalization,
109-
nonce_key="coldkeypub",
110-
signing_key="coldkey",
100+
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
101+
call, wallet, wait_for_inclusion, wait_for_finalization,
102+
nonce_key="coldkeypub", sign_with="coldkey", use_nonce=True
111103
)
112104

113105
if staking_response is True: # If we successfully unstaked.
@@ -271,14 +263,14 @@ async def unstake_multiple_extrinsic(
271263
},
272264
)
273265

274-
staking_response, err_msg = await sign_and_send_with_nonce(
275-
subtensor,
266+
staking_response, err_msg = await subtensor.sign_and_send_extrinsic(
276267
call,
277268
wallet,
278269
wait_for_inclusion,
279270
wait_for_finalization,
280271
nonce_key="coldkeypub",
281-
signing_key="coldkey",
272+
sign_with="coldkey",
273+
use_nonce=True
282274
)
283275

284276
if staking_response is True: # If we successfully unstaked.

bittensor/core/extrinsics/asyncex/weights.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
from bittensor.core.settings import version_as_int
1010
from bittensor.utils import format_error_message
1111
from bittensor.utils.btlogging import logging
12-
from bittensor.core.extrinsics.utils import (
13-
async_sign_and_send_with_nonce as sign_and_send_with_nonce,
14-
)
1512

1613
if TYPE_CHECKING:
1714
from bittensor_wallet import Wallet
@@ -54,8 +51,9 @@ async def _do_commit_weights(
5451
"commit_hash": commit_hash,
5552
},
5653
)
57-
return await sign_and_send_with_nonce(
58-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
54+
return await subtensor.sign_and_send_extrinsic(
55+
call, wallet, wait_for_inclusion, wait_for_finalization, use_nonce=True,
56+
nonce_key="hotkey", sign_with="hotkey"
5957
)
6058

6159

@@ -151,8 +149,9 @@ async def _do_reveal_weights(
151149
"version_key": version_key,
152150
},
153151
)
154-
return await sign_and_send_with_nonce(
155-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
152+
return await subtensor.sign_and_send_extrinsic(
153+
call, wallet, wait_for_inclusion, wait_for_finalization,
154+
sign_with="hotkey", nonce_key="hotkey", use_nonce=True,
156155
)
157156

158157

@@ -257,8 +256,9 @@ async def _do_set_weights(
257256
"version_key": version_key,
258257
},
259258
)
260-
return await sign_and_send_with_nonce(
261-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization, period
259+
return await subtensor.sign_and_send_extrinsic(
260+
call, wallet, wait_for_inclusion, wait_for_finalization, period=period,
261+
use_nonce=True, nonce_key="hotkey", sign_with="hotkey"
262262
)
263263

264264

bittensor/core/extrinsics/commit_weights.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from typing import TYPE_CHECKING, Optional
44

5-
from bittensor.core.extrinsics.utils import sign_and_send_with_nonce
65
from bittensor.utils import format_error_message
76
from bittensor.utils.btlogging import logging
87

@@ -45,9 +44,9 @@ def _do_commit_weights(
4544
"commit_hash": commit_hash,
4645
},
4746
)
48-
49-
return sign_and_send_with_nonce(
50-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
47+
return subtensor.sign_and_send_extrinsic(
48+
call, wallet, wait_for_inclusion, wait_for_finalization,
49+
use_nonce=True, sign_with="hotkey", nonce_key="hotkey"
5150
)
5251

5352

@@ -141,8 +140,9 @@ def _do_reveal_weights(
141140
"version_key": version_key,
142141
},
143142
)
144-
return sign_and_send_with_nonce(
145-
subtensor, call, wallet, wait_for_inclusion, wait_for_finalization
143+
return subtensor.sign_and_send_extrinsic(
144+
call, wallet, wait_for_inclusion, wait_for_finalization,
145+
use_nonce=True, sign_with="hotkey", nonce_key="hotkey"
146146
)
147147

148148

bittensor/core/extrinsics/staking.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional, TYPE_CHECKING, Sequence
33

44
from bittensor.core.errors import StakeError, NotRegisteredError
5-
from bittensor.core.extrinsics.utils import get_old_stakes, sign_and_send_with_nonce
5+
from bittensor.core.extrinsics.utils import get_old_stakes
66
from bittensor.utils import unlock_key
77
from bittensor.utils.balance import Balance
88
from bittensor.utils.btlogging import logging
@@ -106,14 +106,9 @@ def add_stake_extrinsic(
106106
"netuid": netuid,
107107
},
108108
)
109-
staking_response, err_msg = sign_and_send_with_nonce(
110-
subtensor,
111-
call,
112-
wallet,
113-
wait_for_inclusion,
114-
wait_for_finalization,
115-
nonce_key="coldkeypub",
116-
signing_key="coldkey",
109+
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
110+
call, wallet, wait_for_inclusion, wait_for_finalization,
111+
use_nonce=True, sign_with="coldkey", nonce_key="coldkeypub"
117112
)
118113
if staking_response is True: # If we successfully staked.
119114
# We only wait here if we expect finalization.
@@ -285,14 +280,9 @@ def add_stake_multiple_extrinsic(
285280
"netuid": netuid,
286281
},
287282
)
288-
staking_response, err_msg = sign_and_send_with_nonce(
289-
subtensor,
290-
call,
291-
wallet,
292-
wait_for_inclusion,
293-
wait_for_finalization,
294-
nonce_key="coldkeypub",
295-
signing_key="coldkey",
283+
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
284+
call, wallet, wait_for_inclusion, wait_for_finalization,
285+
use_nonce=True, nonce_key="coldkeypub", sign_with="coldkey"
296286
)
297287

298288
if staking_response is True: # If we successfully staked.

bittensor/core/extrinsics/unstaking.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from bittensor.utils import unlock_key
77
from bittensor.utils.balance import Balance
88
from bittensor.utils.btlogging import logging
9-
from bittensor.core.extrinsics.utils import sign_and_send_with_nonce
109

1110
if TYPE_CHECKING:
1211
from bittensor_wallet import Wallet
@@ -93,14 +92,14 @@ def unstake_extrinsic(
9392
"netuid": netuid,
9493
},
9594
)
96-
staking_response, err_msg = sign_and_send_with_nonce(
97-
subtensor,
95+
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
9896
call,
9997
wallet,
10098
wait_for_inclusion,
10199
wait_for_finalization,
102100
nonce_key="coldkeypub",
103-
signing_key="coldkey",
101+
sign_with="coldkey",
102+
use_nonce=True
104103
)
105104

106105
if staking_response is True: # If we successfully unstaked.
@@ -254,14 +253,14 @@ def unstake_multiple_extrinsic(
254253
"netuid": netuid,
255254
},
256255
)
257-
staking_response, err_msg = sign_and_send_with_nonce(
258-
subtensor,
256+
staking_response, err_msg = subtensor.sign_and_send_extrinsic(
259257
call,
260258
wallet,
261259
wait_for_inclusion,
262260
wait_for_finalization,
263261
nonce_key="coldkeypub",
264-
signing_key="coldkey",
262+
sign_with="coldkey",
263+
use_nonce=True
265264
)
266265

267266
if staking_response is True: # If we successfully unstaked.

bittensor/core/extrinsics/utils.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,11 @@
2020
from scalecodec.types import GenericExtrinsic, GenericCall
2121

2222

23-
async def async_sign_and_send_with_nonce(
24-
subtensor: "AsyncSubtensor",
23+
def sign_and_send_with_nonce(
24+
subtensor: "Subtensor",
2525
call: "GenericCall",
2626
wallet: "Wallet",
2727
wait_for_inclusion: bool,
28-
wait_for_finalization: bool,
29-
period: Optional[int] = None,
30-
nonce_key: str = "hotkey",
31-
signing_key: str = "hotkey",
32-
):
33-
"""
34-
Signs an extrinsic call with the wallet keypair (default hotkey), adding an optional era for period
35-
"""
36-
keypair = getattr(wallet, nonce_key)
37-
next_nonce = await subtensor.substrate.get_account_next_index(keypair.ss58_address)
38-
signing_keypair = getattr(wallet, signing_key)
39-
extrinsic_data = {"call": call, "keypair": signing_keypair, "nonce": next_nonce}
40-
if period is not None:
41-
extrinsic_data["era"] = {"period": period}
42-
43-
extrinsic = await subtensor.substrate.create_signed_extrinsic(**extrinsic_data)
44-
45-
try:
46-
response = await subtensor.substrate.submit_extrinsic(
47-
extrinsic=extrinsic,
48-
wait_for_inclusion=wait_for_inclusion,
49-
wait_for_finalization=wait_for_finalization,
50-
)
51-
52-
if not wait_for_finalization and not wait_for_inclusion:
53-
return True, None
54-
55-
if await response.is_success:
56-
return True, None
57-
58-
return False, format_error_message(await response.error_message)
59-
except SubstrateRequestException as e:
60-
return False, format_error_message(e)
61-
62-
63-
def sign_and_send_with_nonce(
64-
subtensor: "Subtensor",
65-
call,
66-
wallet,
67-
wait_for_inclusion,
6828
wait_for_finalization,
6929
nonce_key: str = "hotkey",
7030
signing_key: str = "hotkey",

bittensor/core/subtensor.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,9 @@ def sign_and_send_extrinsic(
20292029
wait_for_inclusion: bool = True,
20302030
wait_for_finalization: bool = False,
20312031
sign_with: str = "coldkey",
2032+
use_nonce: bool = False,
2033+
period: Optional[int] = None,
2034+
nonce_key: str = "hotkey"
20322035
) -> tuple[bool, str]:
20332036
"""
20342037
Helper method to sign and submit an extrinsic call to chain.
@@ -2043,13 +2046,26 @@ def sign_and_send_extrinsic(
20432046
Returns:
20442047
(success, error message)
20452048
"""
2046-
if sign_with not in ("coldkey", "hotkey", "coldkeypub"):
2049+
possible_keys = ("coldkey", "hotkey", "coldkeypub")
2050+
if sign_with not in possible_keys:
20472051
raise AttributeError(
20482052
f"'sign_with' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{sign_with}'"
20492053
)
20502054

2055+
signing_keypair = getattr(wallet, sign_with)
2056+
extrinsic_data = {"call": call, "keypair": signing_keypair}
2057+
if use_nonce:
2058+
if nonce_key not in possible_keys:
2059+
raise AttributeError(
2060+
f"'nonce_key' must be either 'coldkey', 'hotkey' or 'coldkeypub', not '{nonce_key}'"
2061+
)
2062+
next_nonce = self.substrate.get_account_next_index(getattr(wallet, nonce_key).ss58_address)
2063+
extrinsic_data["nonce"] = next_nonce
2064+
if period is not None:
2065+
extrinsic_data["era"] = {"period": period}
2066+
20512067
extrinsic = self.substrate.create_signed_extrinsic(
2052-
call=call, keypair=getattr(wallet, sign_with)
2068+
**extrinsic_data
20532069
)
20542070
try:
20552071
response = self.substrate.submit_extrinsic(

0 commit comments

Comments
 (0)