From 63917abe375af14095b75bcf424212f62cc86f03 Mon Sep 17 00:00:00 2001 From: Benjamin Himes <37844818+thewhaleking@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:27:18 +0200 Subject: [PATCH] Speed up faucet (#245) --- .../bittensor/async_substrate_interface.py | 7 ++---- .../src/bittensor/extrinsics/registration.py | 22 +++++++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/bittensor_cli/src/bittensor/async_substrate_interface.py b/bittensor_cli/src/bittensor/async_substrate_interface.py index d9ea3765..af54b531 100644 --- a/bittensor_cli/src/bittensor/async_substrate_interface.py +++ b/bittensor_cli/src/bittensor/async_substrate_interface.py @@ -1769,7 +1769,6 @@ async def compose_call( call_params = {} await self.init_runtime(block_hash=block_hash) - call = self.runtime_config.create_scale_object( type_string="Call", metadata=self.metadata ) @@ -2087,7 +2086,8 @@ async def create_signed_extrinsic( :return: The signed Extrinsic """ - await self.init_runtime() + if not self.metadata: + await self.init_runtime() # Check requirements if not isinstance(call, GenericCall): @@ -2140,7 +2140,6 @@ async def create_signed_extrinsic( extrinsic = self.runtime_config.create_scale_object( type_string="Extrinsic", metadata=self.metadata ) - value = { "account_id": f"0x{keypair.public_key.hex()}", "signature": f"0x{signature.hex()}", @@ -2158,9 +2157,7 @@ async def create_signed_extrinsic( signature_cls = self.runtime_config.get_decoder_class("ExtrinsicSignature") if issubclass(signature_cls, self.runtime_config.get_decoder_class("Enum")): value["signature_version"] = signature_version - extrinsic.encode(value) - return extrinsic async def get_chain_finalised_head(self): diff --git a/bittensor_cli/src/bittensor/extrinsics/registration.py b/bittensor_cli/src/bittensor/extrinsics/registration.py index 33c9c156..84f761a8 100644 --- a/bittensor_cli/src/bittensor/extrinsics/registration.py +++ b/bittensor_cli/src/bittensor/extrinsics/registration.py @@ -18,7 +18,6 @@ from typing import Optional import subprocess -import backoff from bittensor_wallet import Wallet from Crypto.Hash import keccak import numpy as np @@ -688,7 +687,7 @@ async def run_faucet_extrinsic( tpb: int = 256, num_processes: Optional[int] = None, update_interval: Optional[int] = None, - log_verbose: bool = False, + log_verbose: bool = True, max_successes: int = 3, ) -> tuple[bool, str]: r"""Runs a continual POW to get a faucet of TAO on the test net. @@ -736,8 +735,12 @@ async def run_faucet_extrinsic( # Attempt rolling registration. attempts = 1 successes = 1 + pow_result: Optional[POWSolution] while True: try: + account_nonce = await subtensor.substrate.get_account_nonce( + wallet.coldkey.ss58_address + ) pow_result = None while pow_result is None or await pow_result.is_stale(subtensor=subtensor): # Solve latest POW. @@ -746,7 +749,7 @@ async def run_faucet_extrinsic( if prompt: err_console.print("CUDA is not available.") return False, "CUDA is not available." - pow_result: Optional[POWSolution] = await create_pow( + pow_result = await create_pow( subtensor, wallet, -1, @@ -759,7 +762,7 @@ async def run_faucet_extrinsic( log_verbose=log_verbose, ) else: - pow_result: Optional[POWSolution] = await create_pow( + pow_result = await create_pow( subtensor, wallet, -1, @@ -779,7 +782,7 @@ async def run_faucet_extrinsic( }, ) extrinsic = await subtensor.substrate.create_signed_extrinsic( - call=call, keypair=wallet.coldkey + call=call, keypair=wallet.coldkey, nonce=account_nonce ) response = await subtensor.substrate.submit_extrinsic( extrinsic, @@ -1246,8 +1249,6 @@ def _terminate_workers_and_wait_for_exit( worker.terminate() -# TODO verify this works with async -@backoff.on_exception(backoff.constant, Exception, interval=1, max_tries=3) async def _get_block_with_retry( subtensor: "SubtensorInterface", netuid: int ) -> tuple[int, int, str]: @@ -1262,10 +1263,9 @@ async def _get_block_with_retry( :raises Exception: If the block hash is None. :raises ValueError: If the difficulty is None. """ - block_number = await subtensor.substrate.get_block_number(None) - block_hash = await subtensor.substrate.get_block_hash( - block_number - ) # TODO check if I need to do all this + block = await subtensor.substrate.get_block() + block_hash = block["header"]["hash"] + block_number = block["header"]["number"] try: difficulty = ( 1_000_000