Skip to content

Commit 5d506c3

Browse files
feat: era param for set_weights and commit_weights
1 parent 07a62f3 commit 5d506c3

File tree

14 files changed

+135
-8
lines changed

14 files changed

+135
-8
lines changed

bittensor/core/async_subtensor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
set_weights_extrinsic,
7272
reveal_weights_extrinsic,
7373
)
74+
from bittensor.core.extrinsics.options import (
75+
DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA,
76+
ExtrinsicEra,
77+
ExtrinsicEraTypes,
78+
)
7479
from bittensor.core.metagraph import AsyncMetagraph
7580
from bittensor.core.settings import version_as_int, TYPE_REGISTRY
7681
from bittensor.core.types import ParamWithTypes, SubtensorMixin
@@ -3146,6 +3151,7 @@ async def commit_weights(
31463151
wait_for_inclusion: bool = False,
31473152
wait_for_finalization: bool = False,
31483153
max_retries: int = 5,
3154+
era: Optional[ExtrinsicEraTypes] = None,
31493155
) -> tuple[bool, str]:
31503156
"""
31513157
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
@@ -3163,6 +3169,7 @@ async def commit_weights(
31633169
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain. Default is
31643170
``False``.
31653171
max_retries (int): The number of maximum attempts to commit weights. Default is ``5``.
3172+
era (ExtrinsicEraTypes, optional): Blocks for which the transaction should be valid.
31663173
31673174
Returns:
31683175
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
@@ -3175,6 +3182,9 @@ async def commit_weights(
31753182
success = False
31763183
message = "No attempt made. Perhaps it is too soon to commit weights!"
31773184

3185+
if isinstance(era, dict):
3186+
era = ExtrinsicEra(**era)
3187+
31783188
logging.info(
31793189
f"Committing weights with params: netuid={netuid}, uids={uids}, weights={weights}, "
31803190
f"version_key={version_key}"
@@ -3199,6 +3209,7 @@ async def commit_weights(
31993209
commit_hash=commit_hash,
32003210
wait_for_inclusion=wait_for_inclusion,
32013211
wait_for_finalization=wait_for_finalization,
3212+
era=era,
32023213
)
32033214
if success:
32043215
break
@@ -3657,6 +3668,7 @@ async def set_weights(
36573668
wait_for_inclusion: bool = False,
36583669
wait_for_finalization: bool = False,
36593670
max_retries: int = 5,
3671+
era: Optional[ExtrinsicEraTypes] = DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA,
36603672
):
36613673
"""
36623674
Sets the inter-neuronal weights for the specified neuron. This process involves specifying the influence or
@@ -3685,6 +3697,9 @@ async def set_weights(
36853697
contribution are influenced by the weights it sets towards others【81†source】.
36863698
"""
36873699

3700+
if isinstance(era, dict):
3701+
era = ExtrinsicEra(**era)
3702+
36883703
async def _blocks_weight_limit() -> bool:
36893704
bslu, wrl = await asyncio.gather(
36903705
self.blocks_since_last_update(netuid, uid),
@@ -3724,6 +3739,7 @@ async def _blocks_weight_limit() -> bool:
37243739
version_key=version_key,
37253740
wait_for_inclusion=wait_for_inclusion,
37263741
wait_for_finalization=wait_for_finalization,
3742+
era=era if era is not DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA else None,
37273743
)
37283744
retries += 1
37293745
return success, message
@@ -3749,6 +3765,7 @@ async def _blocks_weight_limit() -> bool:
37493765
version_key=version_key,
37503766
wait_for_inclusion=wait_for_inclusion,
37513767
wait_for_finalization=wait_for_finalization,
3768+
era=era,
37523769
)
37533770
except Exception as e:
37543771
logging.error(f"Error setting weights: {e}")

bittensor/core/extrinsics/asyncex/weights.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from numpy.typing import NDArray
77

8+
from bittensor.core.extrinsics.options import ExtrinsicEra
89
import bittensor.utils.weight_utils as weight_utils
910
from bittensor.core.settings import version_as_int
1011
from bittensor.utils import format_error_message
@@ -23,6 +24,7 @@ async def _do_commit_weights(
2324
commit_hash: str,
2425
wait_for_inclusion: bool = False,
2526
wait_for_finalization: bool = False,
27+
era: Optional[ExtrinsicEra] = None,
2628
) -> tuple[bool, Optional[str]]:
2729
"""
2830
Internal method to send a transaction to the Bittensor blockchain, committing the hash of a neuron's weights.
@@ -36,6 +38,7 @@ async def _do_commit_weights(
3638
commit_hash (str): The hash of the neuron's weights to be committed.
3739
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
3840
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
41+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
3942
4043
Returns:
4144
tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
@@ -59,6 +62,7 @@ async def _do_commit_weights(
5962
use_nonce=True,
6063
nonce_key="hotkey",
6164
sign_with="hotkey",
65+
period=era.period if era else None,
6266
)
6367

6468

@@ -69,6 +73,7 @@ async def commit_weights_extrinsic(
6973
commit_hash: str,
7074
wait_for_inclusion: bool = False,
7175
wait_for_finalization: bool = False,
76+
era: Optional[ExtrinsicEra] = None,
7277
) -> tuple[bool, str]:
7378
"""
7479
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
@@ -82,6 +87,7 @@ async def commit_weights_extrinsic(
8287
commit_hash (str): The hash of the neuron's weights to be committed.
8388
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
8489
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
90+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
8591
8692
Returns:
8793
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
@@ -98,6 +104,7 @@ async def commit_weights_extrinsic(
98104
commit_hash=commit_hash,
99105
wait_for_inclusion=wait_for_inclusion,
100106
wait_for_finalization=wait_for_finalization,
107+
era=era,
101108
)
102109

103110
if success:
@@ -231,7 +238,9 @@ async def _do_set_weights(
231238
version_key: int = version_as_int,
232239
wait_for_inclusion: bool = False,
233240
wait_for_finalization: bool = False,
234-
period: int = 5,
241+
era: ExtrinsicEra = ExtrinsicEra(
242+
period=5,
243+
),
235244
) -> tuple[bool, Optional[str]]: # (success, error_message)
236245
"""
237246
Internal method to send a transaction to the Bittensor blockchain, setting weights
@@ -247,7 +256,7 @@ async def _do_set_weights(
247256
version_key (int, optional): Version key for compatibility with the network.
248257
wait_for_inclusion (bool, optional): Waits for the transaction to be included in a block.
249258
wait_for_finalization (bool, optional): Waits for the transaction to be finalized on the blockchain.
250-
period (int, optional): The period in seconds to wait for extrinsic inclusion or finalization. Defaults to 5.
259+
era (ExtrinsicEra, optional): The period in blocks to wait for extrinsic inclusion or finalization. Defaults to 5.
251260
252261
Returns:
253262
Tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
@@ -271,7 +280,7 @@ async def _do_set_weights(
271280
wallet,
272281
wait_for_inclusion,
273282
wait_for_finalization,
274-
period=period,
283+
period=era.period if era else None,
275284
use_nonce=True,
276285
nonce_key="hotkey",
277286
sign_with="hotkey",
@@ -287,6 +296,9 @@ async def set_weights_extrinsic(
287296
version_key: int = 0,
288297
wait_for_inclusion: bool = False,
289298
wait_for_finalization: bool = False,
299+
era: ExtrinsicEra = ExtrinsicEra(
300+
period=5,
301+
),
290302
) -> tuple[bool, str]:
291303
"""Sets the given weights and values on chain for wallet hotkey account.
292304
@@ -302,6 +314,7 @@ async def set_weights_extrinsic(
302314
returns ``False`` if the extrinsic fails to enter the block within the timeout.
303315
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
304316
``True``, or returns ``False`` if the extrinsic fails to be finalized within the timeout.
317+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
305318
306319
Returns:
307320
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. If we did not wait for
@@ -331,6 +344,7 @@ async def set_weights_extrinsic(
331344
version_key=version_key,
332345
wait_for_finalization=wait_for_finalization,
333346
wait_for_inclusion=wait_for_inclusion,
347+
era=era,
334348
)
335349

336350
if not wait_for_finalization and not wait_for_inclusion:

bittensor/core/extrinsics/commit_reveal.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
from numpy.typing import NDArray
88

9+
from bittensor.core.extrinsics.options import ExtrinsicEra
910
from bittensor.core.settings import version_as_int
1011
from bittensor.utils.btlogging import logging
1112
from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit
@@ -24,6 +25,7 @@ def _do_commit_reveal_v3(
2425
reveal_round: int,
2526
wait_for_inclusion: bool = False,
2627
wait_for_finalization: bool = False,
28+
era: Optional[ExtrinsicEra] = None,
2729
) -> tuple[bool, Optional[str]]:
2830
"""
2931
Executes the commit-reveal phase 3 for a given netuid and commit, and optionally waits for extrinsic inclusion or
@@ -57,7 +59,12 @@ def _do_commit_reveal_v3(
5759
},
5860
)
5961
return subtensor.sign_and_send_extrinsic(
60-
call, wallet, wait_for_inclusion, wait_for_finalization, sign_with="hotkey"
62+
call,
63+
wallet,
64+
wait_for_inclusion,
65+
wait_for_finalization,
66+
period=era.period if era else None,
67+
sign_with="hotkey",
6168
)
6269

6370

@@ -70,6 +77,7 @@ def commit_reveal_v3_extrinsic(
7077
version_key: int = version_as_int,
7178
wait_for_inclusion: bool = False,
7279
wait_for_finalization: bool = False,
80+
era: Optional[ExtrinsicEra] = None,
7381
) -> tuple[bool, str]:
7482
"""
7583
Commits and reveals weights for given subtensor and wallet with provided uids and weights.
@@ -124,6 +132,7 @@ def commit_reveal_v3_extrinsic(
124132
reveal_round=reveal_round,
125133
wait_for_inclusion=wait_for_inclusion,
126134
wait_for_finalization=wait_for_finalization,
135+
era=era,
127136
)
128137

129138
if success is not True:

bittensor/core/extrinsics/commit_weights.py

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

33
from typing import TYPE_CHECKING, Optional
44

5+
from bittensor.core.extrinsics.options import ExtrinsicEra
56
from bittensor.utils import format_error_message
67
from bittensor.utils.btlogging import logging
78

@@ -17,6 +18,7 @@ def _do_commit_weights(
1718
commit_hash: str,
1819
wait_for_inclusion: bool = False,
1920
wait_for_finalization: bool = False,
21+
era: Optional[ExtrinsicEra] = None,
2022
) -> tuple[bool, Optional[str]]:
2123
"""
2224
Internal method to send a transaction to the Bittensor blockchain, committing the hash of a neuron's weights.
@@ -29,6 +31,7 @@ def _do_commit_weights(
2931
commit_hash (str): The hash of the neuron's weights to be committed.
3032
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
3133
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
34+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
3235
3336
Returns:
3437
tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
@@ -52,6 +55,7 @@ def _do_commit_weights(
5255
use_nonce=True,
5356
sign_with="hotkey",
5457
nonce_key="hotkey",
58+
period=era.period if era else None,
5559
)
5660

5761

@@ -62,6 +66,7 @@ def commit_weights_extrinsic(
6266
commit_hash: str,
6367
wait_for_inclusion: bool = False,
6468
wait_for_finalization: bool = False,
69+
era: Optional[ExtrinsicEra] = None,
6570
) -> tuple[bool, str]:
6671
"""
6772
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
@@ -74,6 +79,7 @@ def commit_weights_extrinsic(
7479
commit_hash (str): The hash of the neuron's weights to be committed.
7580
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
7681
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
82+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
7783
7884
Returns:
7985
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
@@ -90,6 +96,7 @@ def commit_weights_extrinsic(
9096
commit_hash=commit_hash,
9197
wait_for_inclusion=wait_for_inclusion,
9298
wait_for_finalization=wait_for_finalization,
99+
era=era,
93100
)
94101

95102
if success:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import dataclasses
2+
from typing import Union
3+
4+
5+
@dataclasses.dataclass(frozen=True)
6+
class ExtrinsicEra:
7+
"""
8+
Defines blocks for which the transaction should be valid.
9+
10+
Attributes:
11+
period (int): Length (in blocks) for which the transaction should be valid.
12+
"""
13+
14+
period: int
15+
16+
17+
ExtrinsicEraTypes = Union[
18+
dict[str, int],
19+
ExtrinsicEra,
20+
]
21+
22+
23+
DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA = ExtrinsicEra(
24+
period=5,
25+
)

bittensor/core/extrinsics/set_weights.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Module sync setting weights extrinsic."""
22

3+
import dataclasses
34
from typing import Union, TYPE_CHECKING, Optional
45

56
import numpy as np
67
from numpy.typing import NDArray
78

9+
from bittensor.core.extrinsics.options import ExtrinsicEra
810
from bittensor.core.settings import version_as_int
911
from bittensor.utils import format_error_message, weight_utils
1012
from bittensor.utils.btlogging import logging
@@ -24,7 +26,9 @@ def _do_set_weights(
2426
version_key: int = version_as_int,
2527
wait_for_inclusion: bool = False,
2628
wait_for_finalization: bool = False,
27-
period: int = 5,
29+
era: Optional[ExtrinsicEra] = ExtrinsicEra(
30+
period=5,
31+
),
2832
) -> tuple[bool, Optional[str]]: # (success, error_message)
2933
"""
3034
Internal method to send a transaction to the Bittensor blockchain, setting weights
@@ -40,7 +44,7 @@ def _do_set_weights(
4044
version_key (int, optional): Version key for compatibility with the network.
4145
wait_for_inclusion (bool, optional): Waits for the transaction to be included in a block.
4246
wait_for_finalization (bool, optional): Waits for the transaction to be finalized on the blockchain.
43-
period (int, optional): The period in seconds to wait for extrinsic inclusion or finalization. Defaults to 5.
47+
era (ExtrinsicEra, optional): The period in blocks to wait for extrinsic inclusion or finalization. Defaults to 5.
4448
4549
Returns:
4650
Tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
@@ -64,7 +68,7 @@ def _do_set_weights(
6468
extrinsic = subtensor.substrate.create_signed_extrinsic(
6569
call=call,
6670
keypair=wallet.hotkey,
67-
era={"period": period},
71+
era=dataclasses.asdict(era) if era else None,
6872
nonce=next_nonce,
6973
)
7074
response = subtensor.substrate.submit_extrinsic(
@@ -91,6 +95,9 @@ def set_weights_extrinsic(
9195
version_key: int = 0,
9296
wait_for_inclusion: bool = False,
9397
wait_for_finalization: bool = False,
98+
era: Optional[ExtrinsicEra] = ExtrinsicEra(
99+
period=5,
100+
),
94101
) -> tuple[bool, str]:
95102
"""Sets the given weights and values on chain for wallet hotkey account.
96103
@@ -106,6 +113,7 @@ def set_weights_extrinsic(
106113
returns ``False`` if the extrinsic fails to enter the block within the timeout.
107114
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
108115
``True``, or returns ``False`` if the extrinsic fails to be finalized within the timeout.
116+
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.
109117
110118
Returns:
111119
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. If we did not wait for
@@ -135,6 +143,7 @@ def set_weights_extrinsic(
135143
version_key=version_key,
136144
wait_for_finalization=wait_for_finalization,
137145
wait_for_inclusion=wait_for_inclusion,
146+
era=era,
138147
)
139148

140149
if not wait_for_finalization and not wait_for_inclusion:

0 commit comments

Comments
 (0)