-
Notifications
You must be signed in to change notification settings - Fork 7
/
e2e_setup.py
205 lines (180 loc) · 5.82 KB
/
e2e_setup.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
from enum import Enum
from typing import List
import bittensor
from bittensor.cli import (
RegisterCommand,
RegisterSubnetworkCommand,
RootRegisterCommand,
StakeCommand,
TransferCommand,
WalletBalanceCommand,
)
from substrateinterface import Keypair
"""
- Uses special accounts (Alice, Bob, Charlie) as different roles
- Sets up wallets for each of the roles
- Creates a subnet with Alice as the owner
- Registers Bob as validator and Charlie as miner
- Register on root network so Bob can stake
"""
# NOTE @dev change to relevant subtensor network
subtensor_network = "ws://localhost:9946"
default_wallet_path = "~/.bittensor/wallets"
wallet_path = default_wallet_path
base_args = [
"--no_prompt",
"--wallet.path",
wallet_path,
"--subtensor.network",
subtensor_network,
]
transfer_to_subnet_owner_amt = 500
validator_stake_amt = 100
create_new_subnet = False
def get_coldkey_name(uri: str):
return "special-{}".format(uri.strip("/"))
def setup_wallet(uri: str, coldkey_name: str):
keypair = Keypair.create_from_uri(uri)
wallet = bittensor.wallet(path=wallet_path, name=coldkey_name)
print(wallet.name)
# don't encrypt just so that we can do stuff without prompts
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=True)
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=True)
wallet.set_hotkey(keypair=keypair, encrypt=False, overwrite=True)
seed_phrase = keypair.generate_mnemonic(words=24)
print(f"URI: {uri}, seed phrase: {seed_phrase}")
parser = bittensor.cli.__create_parser__()
def exec_command(command, extra_args: List[str]):
# Convert all arguments to strings to avoid any issues with argparse
extra_args = [str(arg) for arg in extra_args]
cli_instance = bittensor.cli(
bittensor.config(
parser=parser,
args=extra_args + base_args,
)
)
command.run(cli_instance)
return (keypair, wallet, exec_command)
class Roles(Enum):
SUBNET_OWNER = "//Alice"
SUBNET_VALI = "//Bob"
SUBNET_MINER = "//Charlie"
if __name__ == "__main__":
roles = {}
for r in Roles:
wallet_setup = setup_wallet(r.value, get_coldkey_name(r.name))
roles[r.name] = wallet_setup
print(f"{roles=}")
_, owner_wallet, _ = roles[Roles.SUBNET_OWNER.name]
if create_new_subnet:
for r in Roles:
# let alice be our subnet owner
if r == Roles.SUBNET_OWNER:
continue
_, _, exec = roles[r.name]
coldkey_name = get_coldkey_name(r.name)
exec(
TransferCommand,
[
"wallet",
"transfer",
"--dest",
str(owner_wallet.hotkey.ss58_address),
"--amount",
transfer_to_subnet_owner_amt,
"--wallet.name",
coldkey_name,
],
)
exec(
WalletBalanceCommand,
["wallet", "balance", "--wallet.name", coldkey_name],
)
_, _, owner_exec = roles[Roles.SUBNET_OWNER.name]
owner_exec(
RegisterSubnetworkCommand,
[
"subnet",
"create",
"--wallet.name",
get_coldkey_name(Roles.SUBNET_OWNER.name),
],
)
tmp_args = [
"subnet",
"list",
"--no_prompt",
"--subtensor.network",
subtensor_network,
]
tmp_config = bittensor.config(
parser=bittensor.cli.__create_parser__(), args=tmp_args
)
subtensor = bittensor.subtensor(config=tmp_config)
# register for each subnet
subnet_infos: List[bittensor.SubnetInfo] = subtensor.get_all_subnets_info()
for info in subnet_infos:
if info.owner_ss58 == owner_wallet.coldkey.ss58_address:
netuid = info.netuid
print(f"Owner owns subnet uid: {netuid}")
print(f"Sunet info: {info}")
print(f"Registering validator for netuid: {netuid}...")
_, _, vali_exec = roles[Roles.SUBNET_VALI.name]
vali_exec(
RegisterCommand,
[
"subnet",
"register",
"--netuid",
str(netuid),
"--wallet.name",
get_coldkey_name(Roles.SUBNET_VALI.name),
"--wallet.hotkey",
"default",
],
)
print(f"Registering miner for netuid: {netuid}...")
_, _, miner_exec = roles[Roles.SUBNET_MINER.name]
miner_exec(
RegisterCommand,
[
"subnet",
"register",
"--netuid",
str(netuid),
"--wallet.name",
get_coldkey_name(Roles.SUBNET_MINER.name),
"--wallet.hotkey",
"default",
],
)
# register on root network
print("Registering on root subnet...")
_, _, vali_exec = roles[Roles.SUBNET_VALI.name]
vali_exec(
RootRegisterCommand,
[
"root",
"register",
"--wallet.name",
get_coldkey_name(Roles.SUBNET_VALI.name),
"--wallet.hotkey",
"default",
]
+ base_args,
)
print("Adding stake to subnet validator")
vali_exec(
StakeCommand,
[
"stake",
"add",
"--wallet.name",
get_coldkey_name(Roles.SUBNET_VALI.name),
"--wallet.hotkey",
"default",
"--amount",
str(validator_stake_amt),
]
+ base_args,
)