Skip to content

Commit

Permalink
Randomise rpc request ID (#131)
Browse files Browse the repository at this point in the history
* Randomise rpc_request id to avoid possible dict duplicate keys.
  • Loading branch information
thewhaleking authored Oct 1, 2024
1 parent 9c333d8 commit edd3764
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,10 +1155,12 @@ def wallet_overview(
),
sort_by: Optional[str] = typer.Option(
None,
"--sort-by", "--sort_by",
help="Sort the hotkeys by the specified column title. For example: name, uid, axon.",
),
sort_order: Optional[str] = typer.Option(
None,
"--sort-order", "--sort_order",
help="Sort the hotkeys in the specified order (ascending/asc or descending/desc/reverse).",
),
include_hotkeys: str = typer.Option(
Expand Down
12 changes: 7 additions & 5 deletions bittensor_cli/src/bittensor/async_substrate_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import random
from collections import defaultdict
from dataclasses import dataclass
from hashlib import blake2b
Expand Down Expand Up @@ -1690,9 +1691,10 @@ async def rpc_request(
"""
block_hash = await self._get_current_block_hash(block_hash, reuse_block_hash)
params = params or []
payload_id = f"{method}{random.randint(0, 7000)}"
payloads = [
self.make_payload(
"rpc_request",
payload_id,
method,
params + [block_hash] if block_hash else params,
)
Expand All @@ -1704,14 +1706,14 @@ async def rpc_request(
self.type_registry,
)
result = await self._make_rpc_request(payloads, runtime=runtime)
if "error" in result["rpc_request"][0]:
if "error" in result[payload_id][0]:
raise SubstrateRequestException(
result["rpc_request"][0]["error"]["message"]
)
if "result" in result["rpc_request"][0]:
return result["rpc_request"][0]
if "result" in result[payload_id][0]:
return result[payload_id][0]
else:
raise SubstrateRequestException(result["rpc_request"][0])
raise SubstrateRequestException(result[payload_id][0])

async def get_block_hash(self, block_id: int) -> str:
return (await self.rpc_request("chain_getBlockHash", [block_id]))["result"]
Expand Down

0 comments on commit edd3764

Please sign in to comment.