Skip to content

Commit

Permalink
Speed up faucet (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking authored Nov 25, 2024
1 parent e07b330 commit 63917ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
7 changes: 2 additions & 5 deletions bittensor_cli/src/bittensor/async_substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()}",
Expand All @@ -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):
Expand Down
22 changes: 11 additions & 11 deletions bittensor_cli/src/bittensor/extrinsics/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand Down

0 comments on commit 63917ab

Please sign in to comment.