Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support SubnetInfo RPC #1049

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions bittensor/_subtensor/chain_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def from_json(cls, json: Dict) -> 'NeuronInfo':
uid = json['uid'],
netuid = json['netuid'],
active = int(json['active']), # 0 or 1
stake = Balance.from_rao(json['stake'][0][1]),
stake = Balance.from_rao(0 if len(json['stake']) == 0 else json['stake'][0][1]),
total_stake = Balance.from_rao(sum([stake for _, stake in json['stake']])),
rank = json['rank'] / U16_MAX,
emission = json['emission'] / RAOPERTAO,
Expand Down Expand Up @@ -201,8 +201,35 @@ class SubnetInfo:
max_n: int
blocks_since_epoch: int
tempo: int
blocks_per_epoch: int
modality: int
connection_requirements: Dict[str, int] # netuid -> connection requirements
emission_value: float

@classmethod
def from_json(cls, json: Dict) -> 'SubnetInfo':
r""" Returns a SubnetInfo object from a json dictionary.
"""
return SubnetInfo(
netuid = json['netuid'],
rho = json['rho'],
kappa = json['kappa'],
difficulty = json['difficulty'],
immunity_period = json['immunity_period'],
validator_batch_size = json['validator_batch_size'],
validator_sequence_length = json['validator_sequence_length'],
validator_epochs_per_reset = json['validator_epochs_per_reset'],
validator_epoch_length = json['validator_epoch_length'],
max_allowed_validators = json['max_allowed_validators'],
min_allowed_weights = json['min_allowed_weights'],
max_weight_limit = json['max_weights_limit'],
scaling_law_power = json['scaling_law_power'],
synergy_scaling_law_power= json['synergy_scaling_law_power'],
subnetwork_n = json['subnetwork_n'],
max_n = json['max_allowed_uids'],
blocks_since_epoch = json['blocks_since_last_step'],
tempo = json['tempo'],
modality = json['network_modality'],
connection_requirements= json['network_connect'],
emission_value= json['emission_values'],
)

65 changes: 38 additions & 27 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,35 +557,46 @@ def get_subnets( self, block: Optional[int] = None ) -> List[int]:
return []

def get_all_subnets_info( self, block: Optional[int] = None ) -> List[SubnetInfo]:
all_subnets = self.get_subnets( block )
return [ self.get_subnet_info( netuid ) for netuid in all_subnets]
@retry(delay=2, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
with self.substrate as substrate:
block_hash = None if block == None else substrate.get_block_hash( block )
params = []
if block_hash:
params = [block_hash] + params
return substrate.rpc_request(
method="subnetInfo_getSubnetsInfo", # custom rpc method
params=params
)

json_body = make_substrate_call_with_retry()
result = json_body['result']

if result == None:
return []

return [ SubnetInfo.from_json(subnet_info) for subnet_info in result ]

def get_subnet_info( self, netuid: int, block: Optional[int] = None ) -> Optional[SubnetInfo]:
if not self.subnet_exists( netuid ): return None
return SubnetInfo(
netuid=netuid,
blocks_per_epoch = 0,
rho = self.rho( netuid, block ),
kappa = self.kappa( netuid, block ),
difficulty = self.difficulty( netuid, block ),
immunity_period = self.immunity_period( netuid, block ),
validator_batch_size = self.validator_batch_size( netuid, block ),
validator_sequence_length = self.validator_sequence_length( netuid, block ),
validator_epochs_per_reset = self.validator_epochs_per_reset( netuid, block ),
validator_epoch_length = self.validator_epoch_length( netuid, block ),
min_allowed_weights = self.min_allowed_weights( netuid, block ),
max_weight_limit = self.max_weight_limit( netuid, block ),
scaling_law_power = self.scaling_law_power( netuid, block ),
synergy_scaling_law_power = self.synergy_scaling_law_power( netuid, block ),
subnetwork_n = self.subnetwork_n( netuid, block ),
max_n = self.max_n( netuid, block ),
blocks_since_epoch = self.blocks_since_epoch( netuid, block ),
max_allowed_validators = self.max_allowed_validators( netuid, block ),
emission_value = self.get_emission_value_by_subnet( netuid, block ),
tempo= self.tempo( netuid, block ),
modality= self.get_subnet_modality( netuid, block ),
connection_requirements = {},
)
@retry(delay=2, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
with self.substrate as substrate:
block_hash = None if block == None else substrate.get_block_hash( block )
params = [netuid]
if block_hash:
params = [block_hash] + params
return substrate.rpc_request(
method="subnetInfo_getSubnetInfo", # custom rpc method
params=params
)

json_body = make_substrate_call_with_retry()
result = json_body['result']

if result == None:
return None

return SubnetInfo.from_json(result)

####################
#### Nomination ####
Expand Down