Skip to content

Commit 742888c

Browse files
committed
Allows for using new account ID encoding with flag.
1 parent 70ad1a5 commit 742888c

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ def __init__(
673673
max_retries: int = 5,
674674
retry_timeout: float = 60.0,
675675
_mock: bool = False,
676+
legacy_account_id_decode: bool = True,
676677
):
677678
"""
678679
The asyncio-compatible version of the subtensor interface commands we use in bittensor. It is important to
@@ -690,6 +691,10 @@ def __init__(
690691
max_retries: number of times to retry RPC requests before giving up
691692
retry_timeout: how to long wait since the last ping to retry the RPC request
692693
_mock: whether to use mock version of the subtensor interface
694+
legacy_account_id_decode: determines whether AccountIds will be encoded as str
695+
i.e. `False` => '5CPpYrrPXVWBpFVPwucyaVz8yHmgEx2gSYsqKxjkpcS6XRTC'
696+
or left as a tuple of ints, i.e. `True` => `((14, 148, 49, 116, 204, 181, 15, 159, 227, 104, 234, 66,
697+
22, 124, 11, 249, 247, 114, 82, 128, 38, 255, 65, 253, 173, 84, 57, 90, 168, 143, 203, 96),)`
693698
694699
"""
695700
self.max_retries = max_retries
@@ -726,6 +731,7 @@ def __init__(
726731
self._initializing = False
727732
self.registry_type_map = {}
728733
self.type_id_to_name = {}
734+
self.legacy_account_id_decode = legacy_account_id_decode
729735

730736
async def __aenter__(self):
731737
await self.initialize()
@@ -901,7 +907,12 @@ async def decode_scale(
901907
return ss58_encode(scale_bytes, SS58_FORMAT)
902908
else:
903909
await self._wait_for_registry(_attempt, _retries)
904-
obj = decode_by_type_string(type_string, self.runtime.registry, scale_bytes)
910+
obj = decode_by_type_string(
911+
type_string,
912+
self.runtime.registry,
913+
scale_bytes,
914+
self.legacy_account_id_decode,
915+
)
905916
if return_scale_obj:
906917
return ScaleObj(obj)
907918
else:
@@ -3243,6 +3254,7 @@ async def query_map(
32433254
value_type,
32443255
key_hashers,
32453256
ignore_decoding_errors,
3257+
self.legacy_account_id_decode,
32463258
)
32473259
return AsyncQueryMapResult(
32483260
records=result,

async_substrate_interface/sync_substrate.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ def __init__(
483483
max_retries: int = 5,
484484
retry_timeout: float = 60.0,
485485
_mock: bool = False,
486+
legacy_account_id_decode: bool = True,
486487
):
487488
"""
488489
The sync compatible version of the subtensor interface commands we use in bittensor. Use this instance only
@@ -499,6 +500,10 @@ def __init__(
499500
max_retries: number of times to retry RPC requests before giving up
500501
retry_timeout: how to long wait since the last ping to retry the RPC request
501502
_mock: whether to use mock version of the subtensor interface
503+
legacy_account_id_decode: determines whether AccountIds will be encoded as str
504+
i.e. `False` => '5CPpYrrPXVWBpFVPwucyaVz8yHmgEx2gSYsqKxjkpcS6XRTC'
505+
or left as a tuple of ints, i.e. `True` => `((14, 148, 49, 116, 204, 181, 15, 159, 227, 104, 234, 66,
506+
22, 124, 11, 249, 247, 114, 82, 128, 38, 255, 65, 253, 173, 84, 57, 90, 168, 143, 203, 96),)`
502507
503508
"""
504509
self.max_retries = max_retries
@@ -525,6 +530,7 @@ def __init__(
525530
self.ws = self.connect(init=True)
526531
self.registry_type_map = {}
527532
self.type_id_to_name = {}
533+
self.legacy_account_id_decode = legacy_account_id_decode
528534
if not _mock:
529535
self.initialize()
530536

@@ -668,7 +674,12 @@ def decode_scale(
668674
# Decode AccountId bytes to SS58 address
669675
return ss58_encode(scale_bytes, SS58_FORMAT)
670676
else:
671-
obj = decode_by_type_string(type_string, self.runtime.registry, scale_bytes)
677+
obj = decode_by_type_string(
678+
type_string,
679+
self.runtime.registry,
680+
scale_bytes,
681+
self.legacy_account_id_decode,
682+
)
672683
if return_scale_obj:
673684
return ScaleObj(obj)
674685
else:
@@ -2873,7 +2884,6 @@ def query_map(
28732884
Returns:
28742885
QueryMapResult object
28752886
"""
2876-
hex_to_bytes_ = hex_to_bytes
28772887
params = params or []
28782888
block_hash = self._get_current_block_hash(block_hash, reuse_block_hash)
28792889
if block_hash:
@@ -2955,6 +2965,7 @@ def query_map(
29552965
value_type,
29562966
key_hashers,
29572967
ignore_decoding_errors,
2968+
self.legacy_account_id_decode,
29582969
)
29592970
return QueryMapResult(
29602971
records=result,

async_substrate_interface/utils/decoding.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from typing import Union, TYPE_CHECKING
22

33
from bt_decode import AxonInfo, PrometheusInfo, decode_list
4-
from scalecodec import ss58_encode
5-
from bittensor_wallet.utils import SS58_FORMAT
64

75
from async_substrate_interface.utils import hex_to_bytes
86
from async_substrate_interface.types import ScaleObj
@@ -59,8 +57,11 @@ def _decode_scale_list_with_runtime(
5957
scale_bytes_list: list[bytes],
6058
runtime_registry,
6159
return_scale_obj: bool = False,
60+
legacy_account_id: bool = True,
6261
):
63-
obj = decode_list(type_strings, runtime_registry, scale_bytes_list)
62+
obj = decode_list(
63+
type_strings, runtime_registry, scale_bytes_list, legacy_account_id
64+
)
6465
if return_scale_obj:
6566
return [ScaleObj(x) for x in obj]
6667
else:
@@ -76,6 +77,7 @@ def decode_query_map(
7677
value_type,
7778
key_hashers,
7879
ignore_decoding_errors,
80+
legacy_account_id: bool = True,
7981
):
8082
def concat_hash_len(key_hasher: str) -> int:
8183
"""
@@ -112,6 +114,7 @@ def concat_hash_len(key_hasher: str) -> int:
112114
pre_decoded_key_types + pre_decoded_value_types,
113115
pre_decoded_keys + pre_decoded_values,
114116
runtime.registry,
117+
legacy_account_id,
115118
)
116119
middl_index = len(all_decoded) // 2
117120
decoded_keys = all_decoded[:middl_index]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ keywords = ["substrate", "development", "bittensor"]
99
dependencies = [
1010
"wheel",
1111
"asyncstdlib~=3.13.0",
12-
"bt-decode==v0.6.0",
12+
"bt-decode==0.7.0",
1313
"scalecodec~=1.2.11",
1414
"websockets>=14.1",
1515
"xxhash"

0 commit comments

Comments
 (0)