Skip to content

State-Safe RNG #97

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

Merged
merged 1 commit into from
Apr 15, 2025
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
8 changes: 6 additions & 2 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import asyncio
import inspect
import logging
import random
import ssl
import time
from hashlib import blake2b
Expand Down Expand Up @@ -48,7 +47,12 @@
SubstrateMixin,
Preprocessed,
)
from async_substrate_interface.utils import hex_to_bytes, json, get_next_id
from async_substrate_interface.utils import (
hex_to_bytes,
json,
get_next_id,
rng as random,
)
from async_substrate_interface.utils.cache import async_sql_lru_cache
from async_substrate_interface.utils.decoding import (
_determine_if_old_runtime_call,
Expand Down
8 changes: 6 additions & 2 deletions async_substrate_interface/sync_substrate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import functools
import logging
import random
from hashlib import blake2b
from typing import Optional, Union, Callable, Any

Expand Down Expand Up @@ -30,7 +29,12 @@
Preprocessed,
ScaleObj,
)
from async_substrate_interface.utils import hex_to_bytes, json, get_next_id
from async_substrate_interface.utils import (
hex_to_bytes,
json,
get_next_id,
rng as random,
)
from async_substrate_interface.utils.decoding import (
_determine_if_old_runtime_call,
_bt_decode_to_dict_or_list,
Expand Down
4 changes: 3 additions & 1 deletion async_substrate_interface/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

id_cycle = cycle(range(1, 999))

rng = random.Random()


def get_next_id() -> str:
"""
Generates a pseudo-random ID by returning the next int of a range from 1-998 prepended with
two random ascii characters.
"""
random_letters = "".join(random.choices(string.ascii_letters, k=2))
random_letters = "".join(rng.choices(string.ascii_letters, k=2))
return f"{random_letters}{next(id_cycle)}"


Expand Down
Loading