Skip to content

Commit

Permalink
Fixes for rpc request error handler, root list default empty values, …
Browse files Browse the repository at this point in the history
…prev delegate fetching (#175)

* Fixes issue from #131 where "rpc_request" was not changed to `payload_id` in the error handler.

* Correctly use default empty values to avoid None issues.

* Previous delegates fetching fix for non-archive nodes.
  • Loading branch information
thewhaleking authored Oct 10, 2024
1 parent c3fb1b8 commit f36deb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bittensor_cli/src/bittensor/async_substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ async def rpc_request(
result = await self._make_rpc_request(payloads, runtime=runtime)
if "error" in result[payload_id][0]:
raise SubstrateRequestException(
result["rpc_request"][0]["error"]["message"]
result[payload_id][0]["error"]["message"]
)
if "result" in result[payload_id][0]:
return result[payload_id][0]
Expand Down
25 changes: 17 additions & 8 deletions bittensor_cli/src/commands/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from rich.table import Column, Table
from rich.text import Text
from scalecodec import GenericCall, ScaleType
from substrateinterface.exceptions import SubstrateRequestException
import typer

from bittensor_cli.src import Constants, DelegatesDetails
from bittensor_cli.src import DelegatesDetails
from bittensor_cli.src.bittensor.balances import Balance
from bittensor_cli.src.bittensor.chain_data import (
DelegateInfo,
Expand Down Expand Up @@ -721,7 +722,7 @@ async def _get_list() -> tuple:

rn: list[NeuronInfoLite] = await subtensor.neurons_lite(netuid=0)
if not rn:
return None, None, None, None
return [], [], {}, {}

di: dict[str, DelegatesDetails] = await subtensor.get_delegate_identities()
ts: dict[str, ScaleType] = await subtensor.substrate.query_multiple(
Expand Down Expand Up @@ -1539,16 +1540,24 @@ async def list_delegates(subtensor: SubtensorInterface):
subtensor.get_delegates(block_hash=block_hash),
)

# TODO keep an eye on this, was not working at one point
print_verbose("Fetching previous delegates info from chain", status)
prev_block_hash = await subtensor.substrate.get_block_hash(
max(0, block_number - 1200)
)
prev_delegates = await subtensor.get_delegates(block_hash=prev_block_hash)

async def get_prev_delegates(fallback_offsets=(1200, 200)):
for offset in fallback_offsets:
try:
prev_block_hash = await subtensor.substrate.get_block_hash(
max(0, block_number - offset)
)
return await subtensor.get_delegates(block_hash=prev_block_hash)
except SubstrateRequestException:
continue
return None

prev_delegates = await get_prev_delegates()

if prev_delegates is None:
err_console.print(
":warning: [yellow]Could not fetch delegates history[/yellow]"
":warning: [yellow]Could not fetch delegates history. [/yellow]"
)

delegates.sort(key=lambda d: d.total_stake, reverse=True)
Expand Down

0 comments on commit f36deb0

Please sign in to comment.