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

Fast block improvements #245

Merged
merged 1 commit into from
Nov 25, 2024
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
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 bittensor_wallet.errors import KeyFileError
from Crypto.Hash import keccak
Expand Down Expand Up @@ -687,7 +686,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 @@ -737,8 +736,12 @@ async def run_faucet_extrinsic(
# Attempt rolling registration.
attempts = 1
successes = 1
pow_result: Optional[POWSolution]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you add this if we have pow_result=None in line 745?
line 745 should be more than enough since pow_result variable is using just within while loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so mypy wouldn't yell at me on line 746 😅

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 @@ -747,7 +750,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 @@ -760,7 +763,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 @@ -780,7 +783,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 @@ -1247,8 +1250,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, bytes]:
Expand All @@ -1263,10 +1264,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
Loading