-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathchain_interactions.py
232 lines (199 loc) · 7.33 KB
/
chain_interactions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
This module provides functions interacting with the chain for end-to-end testing;
these are not present in btsdk but are required for e2e tests
"""
import asyncio
from typing import Union, Optional, TYPE_CHECKING
from bittensor.utils.btlogging import logging
# for typing purposes
if TYPE_CHECKING:
from bittensor import Wallet
from bittensor.core.subtensor import Subtensor
from bittensor.utils.balance import Balance
from substrateinterface import SubstrateInterface
def sudo_set_hyperparameter_bool(
substrate: "SubstrateInterface",
wallet: "Wallet",
call_function: str,
value: bool,
netuid: int,
) -> bool:
"""
Sets boolean hyperparameter value through AdminUtils. Mimics setting hyperparams
"""
call = substrate.compose_call(
call_module="AdminUtils",
call_function=call_function,
call_params={"netuid": netuid, "enabled": value},
)
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey)
response = substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=True,
wait_for_finalization=True,
)
response.process_events()
return response.is_success
def sudo_set_hyperparameter_values(
substrate: "SubstrateInterface",
wallet: "Wallet",
call_function: str,
call_params: dict,
return_error_message: bool = False,
) -> Union[bool, tuple[bool, Optional[str]]]:
"""
Sets liquid alpha values using AdminUtils. Mimics setting hyperparams
"""
call = substrate.compose_call(
call_module="AdminUtils",
call_function=call_function,
call_params=call_params,
)
extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey)
response = substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=True,
wait_for_finalization=True,
)
response.process_events()
if return_error_message:
return response.is_success, response.error_message
return response.is_success
def add_stake(
substrate: "SubstrateInterface", wallet: "Wallet", amount: "Balance", netuid: int
) -> bool:
"""
Adds stake to a hotkey using SubtensorModule. Mimics command of adding stake
"""
stake_call = substrate.compose_call(
call_module="SubtensorModule",
call_function="add_stake",
call_params={"hotkey": wallet.hotkey.ss58_address, "amount_staked": amount.rao, "netuid": netuid},
)
extrinsic = substrate.create_signed_extrinsic(
call=stake_call, keypair=wallet.coldkey
)
response = substrate.submit_extrinsic(
extrinsic, wait_for_finalization=True, wait_for_inclusion=True
)
response.process_events()
return response.is_success
def register_subnet(substrate: "SubstrateInterface", wallet: "Wallet") -> bool:
"""
Registers a subnet on the chain using wallet. Mimics register subnet command.
"""
register_call = substrate.compose_call(
call_module="SubtensorModule",
call_function="register_network",
call_params={
"mechid": 1,
"hotkey": wallet.hotkey.ss58_address
},
)
extrinsic = substrate.create_signed_extrinsic(
call=register_call, keypair=wallet.coldkey
)
response = substrate.submit_extrinsic(
extrinsic, wait_for_finalization=True, wait_for_inclusion=True
)
response.process_events()
return response.is_success
async def wait_epoch(subtensor: "Subtensor", netuid: int = 1):
"""
Waits for the next epoch to start on a specific subnet.
Queries the tempo value from the Subtensor module and calculates the
interval based on the tempo. Then waits for the next epoch to start
by monitoring the current block number.
Raises:
Exception: If the tempo cannot be determined from the chain.
"""
q_tempo = [
v.value
for [k, v] in subtensor.query_map_subtensor("Tempo")
if k.value == netuid
]
if len(q_tempo) == 0:
raise Exception("could not determine tempo")
tempo = q_tempo[0]
logging.info(f"tempo = {tempo}")
await wait_interval(tempo, subtensor, netuid)
def next_tempo(current_block: int, tempo: int, netuid: int) -> int:
"""
Calculates the next tempo block for a specific subnet.
Args:
current_block (int): The current block number.
tempo (int): The tempo value for the subnet.
netuid (int): The unique identifier of the subnet.
Returns:
int: The next tempo block number.
"""
interval = tempo + 1
last_epoch = current_block - 1 - (current_block + netuid + 1) % interval
next_tempo = last_epoch + interval
return next_tempo
async def wait_interval(
tempo: int, subtensor: "Subtensor", netuid: int = 1, reporting_interval: int = 10
):
"""
Waits until the next tempo interval starts for a specific subnet.
Calculates the next tempo block start based on the current block number
and the provided tempo, then enters a loop where it periodically checks
the current block number until the next tempo interval starts.
"""
current_block = subtensor.get_current_block()
next_tempo_block_start = next_tempo(current_block, tempo, netuid)
last_reported = None
while current_block < next_tempo_block_start:
await asyncio.sleep(
1
) # Wait for 1 second before checking the block number again
current_block = subtensor.get_current_block()
if last_reported is None or current_block - last_reported >= reporting_interval:
last_reported = current_block
print(
f"Current Block: {current_block} Next tempo for netuid {netuid} at: {next_tempo_block_start}"
)
logging.info(
f"Current Block: {current_block} Next tempo for netuid {netuid} at: {next_tempo_block_start}"
)
# Helper to execute sudo wrapped calls on the chain
def sudo_set_admin_utils(
substrate: "SubstrateInterface",
wallet: "Wallet",
call_function: str,
call_params: dict,
return_error_message: bool = False,
) -> Union[bool, tuple[bool, Optional[str]]]:
"""
Wraps the call in sudo to set hyperparameter values using AdminUtils.
Args:
substrate (SubstrateInterface): Substrate connection.
wallet (Wallet): Wallet object with the keypair for signing.
call_function (str): The AdminUtils function to call.
call_params (dict): Parameters for the AdminUtils function.
return_error_message (bool): If True, returns the error message along with the success status.
Returns:
Union[bool, tuple[bool, Optional[str]]]: Success status or (success status, error message).
"""
inner_call = substrate.compose_call(
call_module="AdminUtils",
call_function=call_function,
call_params=call_params,
)
sudo_call = substrate.compose_call(
call_module="Sudo",
call_function="sudo",
call_params={"call": inner_call},
)
extrinsic = substrate.create_signed_extrinsic(
call=sudo_call, keypair=wallet.coldkey
)
response = substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=True,
wait_for_finalization=True,
)
response.process_events()
if return_error_message:
return response.is_success, response.error_message
return response.is_success