def is_subscribed(self, wallet: 'bittensor.wallet.Wallet', ip: str, port: int, modality: int) -> bool: r""" Returns true if the bittensor endpoint is already subscribed with the wallet and metadata. Args: wallet (bittensor.wallet.Wallet): bittensor wallet object. ip (str): endpoint host port i.e. 192.122.31.4 port (int): endpoint port number i.e. 9221 modality (int): int encoded endpoint modality i.e 0 for TEXT coldkeypub (str): string encoded coldekey pub. """ uid = self.get_uid_for_pubkey(wallet.hotkey.public_key) if uid is None: return False neuron = self.get_neuron_for_uid(uid) if neuron['ip'] == net.ip_to_int(ip) and neuron['port'] == port: return True else: return False
async def async_subscribe( self, ip: str, port: int, modality: int, coldkeypub: str, wait_for_inclusion: bool = False, wait_for_finalization:bool = True, timeout: int = 3 * bittensor.__blocktime__, ) -> bool: r""" Subscribes an bittensor endpoint to the substensor chain. Args: ip (str): endpoint host port i.e. 192.122.31.4 port (int): endpoint port number i.e. 9221 modality (int): int encoded endpoint modality i.e 0 for TEXT coldkeypub (str): string encoded coldekey pub. wait_for_inclusion (bool): if set, waits for the extrinsic to enter a block before returning true, or returns false if the extrinsic fails to enter the block within the timeout. wait_for_finalization (bool): if set, waits for the extrinsic to be finalized on the chain before returning true, or returns false if the extrinsic fails to be finalized within the timeout. timeout (int): time that this call waits for either finalization of inclusion. Returns: success (bool): flag is true if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is true. """ if not await self.async_check_connection(): return False if await self.async_is_subscribed( ip, port, modality, coldkeypub ): print(colored('Already subscribed with [ip: {}, port: {}, modality: {}, coldkey: {}]'.format(ip, port, modality, coldkeypub), 'green')) return True ip_as_int = net.ip_to_int(ip) params = { 'ip': ip_as_int, 'port': port, 'ip_type': 4, 'modality': modality, 'coldkey': coldkeypub, } call = await self.substrate.compose_call( call_module='SubtensorModule', call_function='subscribe', call_params=params ) extrinsic = await self.substrate.create_signed_extrinsic(call=call, keypair=self.wallet.hotkey) result = await self._submit_and_check_extrinsic (extrinsic, wait_for_inclusion, wait_for_finalization, timeout) if result: print(colored('Successfully subscribed with [ip: {}, port: {}, modality: {}, coldkey: {}]'.format(ip, port, modality, coldkeypub), 'green')) else: print(colored('Failed to subscribe', 'red')) return result
async def subscribe(self, ip: str, port: int, modality: int, coldkey: str): ip_as_int = net.ip_to_int(ip) params = { 'ip': ip_as_int, 'port': port, 'ip_type': 4, 'modality': modality, 'coldkey': coldkey } call = await self.substrate.compose_call( call_module='SubtensorModule', call_function='subscribe', call_params=params ) extrinsic = await self.substrate.create_signed_extrinsic(call=call, keypair=self.__keypair) await self.substrate.submit_extrinsic(extrinsic, wait_for_inclusion=False) # Waiting for inclusion and other does not work
async def async_is_subscribed(self, ip: str, port: int, modality: int, coldkey: str) -> bool: r""" Returns true if the bittensor endpoint is already subscribed. Args: ip (str): endpoint host port i.e. 192.122.31.4 port (int): endpoint port number i.e. 9221 modality (int): int encoded endpoint modality i.e 0 for TEXT coldkeypub (str): string encoded coldekey pub. """ uid = await self.async_get_uid_for_pubkey(self.wallet.hotkey.public_key) if uid != None: neuron = await self.async_get_neuron_for_uid( uid ) if neuron['ip'] == net.ip_to_int(ip) and neuron['port'] == port: return True else: return False else: return False
def subscribe( self, wallet: 'bittensor.wallet.Wallet', ip: str, port: int, modality: int, wait_for_inclusion: bool = False, wait_for_finalization=True, timeout: int = 3 * bittensor.__blocktime__, ) -> bool: r""" Subscribes an bittensor endpoint to the substensor chain. Args: wallet (bittensor.wallet.Wallet): bittensor wallet object. ip (str): endpoint host port i.e. 192.122.31.4 port (int): endpoint port number i.e. 9221 modality (int): int encoded endpoint modality i.e 0 for TEXT wait_for_inclusion (bool): if set, waits for the extrinsic to enter a block before returning true, or returns false if the extrinsic fails to enter the block within the timeout. wait_for_finalization (bool): if set, waits for the extrinsic to be finalized on the chain before returning true, or returns false if the extrinsic fails to be finalized within the timeout. timeout (int): time that this call waits for either finalization of inclusion. Returns: success (bool): flag is true if extrinsic was finalized or uncluded in the block. If we did not wait for finalization / inclusion, the response is true. """ if self.is_subscribed(wallet, ip, port, modality): logger.success( "Already subscribed with:\n<cyan>[\n ip: {},\n port: {},\n modality: {},\n hotkey: {},\n coldkey: {}\n]</cyan>" .format(ip, port, modality, wallet.hotkey.public_key, wallet.coldkeypub)) return True ip_as_int = net.ip_to_int(ip) params = { 'ip': ip_as_int, 'port': port, 'ip_type': 4, 'modality': modality, 'coldkey': wallet.coldkeypub, } call = self.substrate.compose_call(call_module='SubtensorModule', call_function='subscribe', call_params=params) # TODO (const): hotkey should be an argument here not assumed. Either that or the coldkey pub should also be assumed. extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey) result = self.substrate.submit_extrinsic( extrinsic, wait_for_inclusion, wait_for_finalization).is_success if result: logger.success( "Successfully subscribed with:\n<cyan>[\n ip: {},\n port: {},\n modality: {},\n hotkey: {},\n coldkey: {}\n]</cyan>" .format(ip, port, modality, wallet.hotkey.public_key, wallet.coldkeypub)) else: logger.error("Failed to subscribe") return result